Category Archives: All Distros

Create patches with git format-patch

Git has a wonderful tool that allows you to create patches of the commits in the tree of your project: git format-patch.

Usage:

 $ git format-patch [<options>] [<since> | <revision-range>]

More information can be seen via man page:

$ git format-patch -h

or

$ man git format-patch

For example:

$ git format-patch -N

This command generates one .patch file for each of the N commits on the top of your tree. Example:

$ git format-patch -1

Generates a .patch file for the commit on the top of your tree. Increase the number to get one file per commit (for the top 3 commits, use ‘-3’).

To apply the patch, use

$ git am [your-patch].patch

Cheers!

Advertisements

Add bash_aliases file to zsh

This one is super simple. It’s on how to add your ~/.bash_aliases file to zsh, in case you are making such transition.

The magic is about adding this line

source $HOME/.bash_aliases

to your ~/.zshrc file, simple as:

bash_aliases

And that would be all. All your aliases are now added to your zsh.

Cheers!

Git: two awesome quick commands: –contains and –no-merged

Find branches the commit is on

git branch --contains <commit #>

Sample output:

Screen Shot 2017-10-17 at 10.18.41 PM10-17-17

fetch_weather, implement_axios and master all have the commit # 3dea5b6

Ref.: https://stackoverflow.com/questions/2706797/finding-what-branch-a-git-commit-came-from

Show all branches with unmerged commits to <branch>

git branch -a --no-merged <branch>

Sample output:

Screen Shot 2017-10-17 at 10.22.39 PM10-17-17

remotes/origin/error_handler has not yet been merged into master

Ref.: https://stackoverflow.com/questions/3600728/git-list-all-unmerged-changes-in-git

Happy git work!

Cheers!

Git history (log) of a line

This is the first answer of this question on Stack Overflow, about how to see the history of a line of code on git.

Since git 1.8.4, there is a more direct way to answer your question.

Assuming that line 110 is the line saying var identifier = "SOME_IDENTIFIER";, then do this:

git log -L110,110:/lib/client.js

This will return every commit which touched that line of code.

[Git Documentation (see the “-L” command line paramenter)]

You might also like to read Git: Show history of a file

Cheers!

Git: find what commit inserted such file

I was in need of finding the commit that added a specific file in my project and “I got lucky” on Google:

Say we need the commit that introduced foo.js in the project, the command would be:

$ git log --diff-filter=A -- foo.js

And a super nice alias would be:

$ git config --global alias.whatadded 'log --diff-filter=A --'

and to use it:

$ git whatadded filename

That’s it.

Reference: https://stackoverflow.com/questions/11533199/find-commit-where-file-was-added

Nice examples: Find

This link has a super cool list of find examples for everyday use. In this post, I collected a few that I found more interesting. I left the example number from the list for my (maybe your) reference.

4. Executing Commands on the Files Found by the Find Command.

In the example below, the find command calculates the md5sum of all the files with the name MyCProgram.c (ignoring case). {} is replaced by the current file name.

# find -iname "MyCProgram.c" -exec md5sum {} \;
d41d8cd98f00b204e9800998ecf8427e  ./mycprogram.c
d41d8cd98f00b204e9800998ecf8427e  ./backup/mycprogram.c
d41d8cd98f00b204e9800998ecf8427e  ./backup/MyCProgram.c
d41d8cd98f00b204e9800998ecf8427e  ./MyCProgram.c

 Update: Move Files by Creation Date Using Find

Files located at ~/vertiv/bugs/images

ls_lt_images

No files last modified in the last 48 hours ‘-mtime 2’ (2 periods of 24 hours) ago, but 7 files were created less than 0 blocks of 24 hours.

find_mtime_mv

Move all those files to the ./images directory with find /home/dkreling/vertiv/bugs/images -type f -mtime 0 -exec mv {} ./images \;

find_by_date_and_movels_la_images

9. Finding the Top 5 Big Files

The following command will display the top 5 largest file in the current directory and its subdirectories. This may take a while to execute depending on the total number of files the command it has to process.

# find . -type f -exec ls -s {} \; | sort -n -r | head -5

11. Find files based on file-type using option -type

Find all directories

# find . -type d

Find only the normal files

# find . -type f

12. Find files by comparing with the modification time of other file.

Show files which are modified after the specified file. The following find command displays all the files that are created/modified after ordinary_file.

# ls -lrt
total 0
-rw-r----- 1 root root 0 2009-02-19 20:27 others_can_also_read
----r----- 1 root root 0 2009-02-19 20:27 others_can_only_read
-rw------- 1 root root 0 2009-02-19 20:29 ordinary_file
-rw-r--r-- 1 root root 0 2009-02-19 20:30 everybody_read
-rwxrwxrwx 1 root root 0 2009-02-19 20:31 all_for_all
---------- 1 root root 0 2009-02-19 20:31 no_for_all

# find -newer ordinary_file
.
./everybody_read
./all_for_all
./no_for_all

Reference: http://www.thegeekstuff.com/2009/03/15-practical-linux-find-command-examples/