« Back to News

Git Tips & Tricks – Part III (Additional useful commands)

In the previous two posts, we discussed Git Tips & Tricks (File related), and Git Tips & Tricks (Commits related).  This post delves into additional useful commands for Git.

  1. Git bisect

This command uses a binary search algorithm to find which commit in the project’s history introduced a bug. We use it by first saying it’s a “bad” commit that is known to contain the bug, and a “good” commit that is known to be before the bug was introduced. Then git bisect picks a commit between those two endpoints and asks us whether the selected commit is “good” or “bad.” It continues narrowing down the range until it finds the exact commit that introduced the change.

In fact, git bisect can be used to find the commit that changed any property of our project; e.g., the commit that fixed a bug.

The actual commands we need to follow step-by-step to execute the full git bisect are:

  • git bisect start – let git know to start bisecting.
  • git bisect good {{some-commit-hash}}let git know about a known good commit (i.e. last commit where there was no bug).
  • git bisect bad {{some-commit-hash}}let git know about a known bad commit (i.e. the HEAD of the master branch).

At this point git would check out a middle commit, and let us know to run tests.

  • git bisect badlet git know that the feature does not work in the currently checked out commit.  Or
  •  git bisect goodlet git know that the feature does work in currently checked out commit.

When the first bad commit is found, git would let us know. At this point git bisect is done.

  • git bisect resetreturns to the initial starting point of git bisect process, (i.e., the HEAD of the master branch).
  • git bisect loglog the last git bisect that completed successfully.
  1. How to see changes only for a specific branch

Suppose we have a local branch tracking the remote/master branch. After running git-pull and git-log, the log will show all commits in the remote tracking branch as well as the current branch. If there are many changes made to the remote branch, then git log will show large number of commits.

If we are interested in seeing only commits made to the current local branch (i.e., the commits which are not yet merged into master), then the below command would do the required things:

git log –no-merges master..

  1. How to change Author Name in a commit

Suppose we need to change the author for the latest commit. Such a situation might arise if multiple people are working on a feature where there is a requirement to keep the single review link.

Below are the steps to be followed in such case:

Step 1. Add the author name, i.e. your name while amending

Example: git commit –amend –author “Abc Xyz <[email protected]>”

Step 2. git push origin branchName:refs/for/master

  1. Git Auto-completion

Git packages for some operating systems (like Ubuntu) come with git auto-completion enabled by default.

If your operating system did not come with one (Mac doesn’t), you can easily enable it by following the steps below.

Step 1. First get the git-completion.bash script and put it in your home directory using:

curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash

Step 2. Then add the following lines to your .bash_profile. This tells bash to execute the git autocomplete script if it exists.

function parse_git_branch () {

git branch 2> /dev/null | sed -e ‘/^[^*]/d’ -e ‘s/* \(.*\)/[\1]/’

}

export PS1=”\w:\[\033[30m\]\[\033[31m\]\$(parse_git_branch)\[\033[30m\]\[\033[0m\]$”

if [ -f ~/.git-completion.bash ]; then

. ~/.git-completion.bash

Fi

Step 3. Then execute Source ~/.bash_profile

Now you can see it will show branch name, and git will auto-complete command on pressing Tab.

  1. Changing default editor for Git

The “vi” editor is the default editor for git. Hence when we run “git var -l” in git repository, it will show GIT_EDITOR=vi

As we know, editing in vi editor is not simple and everyone is not comfortable with it as there is a chance of spelling mistakes or deletions of text without user notice.

We can change this default editor to some other editor installed in our system using:

git config –add core.editor /path/to/your/editor

Example: git config –add core.editor ‘/home/SublimeText/sublime_text’

(setting sublime text as my default editor for git)

References:

https://www.atlassian.com

https://git-scm.com/

https://ariejan.net/

http://www.alexkras.com/

Comments are closed.

About the Writer

  • Tushar Bende
    Senior Software Developer at Synerzip

How can Synerzip Help You?

By partnering with Synerzip, clients rapidly scale their engineering team, decrease time to market and save at least 50 percent with our Agile development teams in India.