All Distros, Git

Git: two awesome quick commands

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!

Advertisements
All Distros, Git

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)]

 

Cheers!

All Distros, Git

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

Git

Creating Git Aliases from Command Line

Creating git aliases is as easy as it is useful to speed your production time in development. The hard way — not so hard, actually — is as simple as adding a string in your /home/$USER/.gitconfig file. Like so:

[alias]
    co = checkout
    st = status
    unstage = reset HEAD --
    wc = whatchanged

Well, if you do not have any aliases configured yet, create an [alias] block there and start appending your aliases.

However, the easiest way is to issue a command line in your terminal, and the alias will be added for you and will be ready to be used. E.g.:

git config --global alias.st status

or

git config --global alias.wc whatchanged

Keep adding them as you wish.

To use them, just issue git <your-alias> [args]  from the command line.

Go ahead and share your most used aliases in the comments.

Thanks for reading.

All Distros, Git, MacOS, Packages

How to enable git-completion on PS1 variable

A nicer bash prompt I use includes a git completion script that indicates the branch of a repository I may be into. Like this:

git_completion

Green indicates “master” is clean and no modified files are present. Red indicates there is something new in that branch.

In order to configure that, I had to include some instructions on my .bashrc file. It looks like this:

# git completion – Linux
source /usr/share/git-core/contrib/completion/git-prompt.sh

# git completion – Mac OS
#source /usr/local/git/contrib/completion/git-prompt.sh

(…)

PS1='\[\e[01;30m\]\t`if [ $? = 0 ]; then echo "\[\e[32m\] ✔ "; else echo "\[\e[31m\] ✘ "; fi`\[\e[00;40;37m\]\u\[\e[01;37m\]:`[[ $(git status 2> /dev/null | head -n2 | tail -n1) != "# Changes to be committed:" ]] && echo "\[\e[31m\]" || echo "\[\e[33m\]"[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit, working directory clean" ]] || echo "\[\e[32m\]"`$(__git_ps1 "(%s)\[\e[00m\]")\[\e[01;34m\]\w\[\e[00m\]\$ '

Now, sometimes (in a new environment mostly), we may configure the .bashrc just the way we want it and then:

git_2

Boom! You see a big "__git_ps1: command not found"  message.

That is because you don’t have the git-prompt.sh file you included in the directory loaded at the top of you .bashrc file (the /usr/share/git-core/contrib/completion/git-prompt.sh)

So in order to get rid of that, we need to include it. The file is available in contrib/completion/git-prompt.bash from the git repository git://git.kernel.org/pub/scm/git/git.git

So after you clone it, you should simply copy the missing files to your system file system. In my case, I preferred to copy the whole “completion” dir to my file systems, as I did not have it. So:

$ sudo cp -r completion/ /usr/share/git-core/contrib/

NOTE: If you are using MacOS, the path should be /usr/local/git/contrib/. For more MacOS, please consider visiting this github page.

Now I have my git-completion.bash file in the correct place. Let’s see if that worked when running a new bash session:

git_3

Great! Worked like a charm.

Let me know if you have a different solution for it or if this did not solve your problem.

Thanks for reading.

Cheers!

Technically Speaking was used as a reference for this post.