Mahesh's blog
Published on

git gems to find commit

Git became the default version control for both professional and personal projects across the world. There are numerous articles to get started on git and understand different branch strategies. Some of them are:

As more people work on same project, there will be frequent number of changes and merges happening to the repository. In such a fast-changing distributed environment, there will be times where developers have to hunt for individual commits either to find the bug or copy changes from one branch to another. Combination of git log and grep can do the job. But there are some hidden jewels in git to help developer life to find the commits much faster with less time and effort.

Here are some I have used to speed up commit hunt:

  • git cherry
  • git bisect
  • git reflog

git cherry

There are scenarios where you want to find differences between branches in commits without merged commits. git cherry comes in handy to do the same. Let's look at the example:

For commits which are present in develop branch but not in master branch.


git cherry develop master

You might have guessed that this name is close to another popular command cherry-pick.

We can pick commits using cherry and apply them using cherry-pick.

For picking all commits between 2 commits as a range,

git cherry-pick abcd..efgh #abcd and eggh are commit hashes

git bisect

Root-cause analysis for resolving bugs plays an important role for any project sustainability. Finding commit in git history that caused the bug can be tedious process. When its related to production failures, time is luxury and reverting commit has to happen quickly. During those times, git bisect can be life saver.

For example, consider you know current state of branch is bad and there was earlier version where you know it was in good state.

git bisect bad
git bisect good v20200901
git bisect log # lists all commits between good and bad commits
git bisect next # move through history one commit at a time
git bisect good # marking a commit as good or bad
git bisect reset # anytime to stop bisecting

As you go through history, you can run the app to find the bug. Once you zero in on commit, its matter time to fix or revert.

git reflog

There are scenarios when remote repository loses your changes or local branches get deleted by mistake. It can be stressful to recover or redo all the changes. git reflog can be savior command.

It works similar to git log but focus is on local machine changes to git repository.You can pick any commits and cherry-pick to get back those lost changes.

git reflog

To conclude, these commands were useful in different situations occasionally. But with these in your repertoire, you can save lot of time and effort in the times where you needed them the most.

References