5 Unpopular yet useful git commands

Mohith Aakash G
featurepreneur
Published in
4 min readJan 15, 2022

Git is the most popular and widely used Version Control System used to manage our source code. Although the purpose of Git is to ease version controlling and team collaboration, there may be situations where Git might give you a hard time.

There are tons of confusing commands and most people only learn the absolute basics of Git such as adding and committing files. This is only a small portion of what you can do with Git, and in this article let us have a look at some other useful commands.

1. git cherry-pick

Cherry picking is the act of picking an arbitrary commit from a branch and applying it to another. git cherry-pick can be useful for undoing changes. For example, say a commit is accidentally made to the wrong branch. You can switch to the correct branch and cherry-pick the commit to where it should belong.

cherry-pick visualisation

When executing this command, you should first retrieve the reference to the commit you want to pick using logs. Then, check out the branch you wish to apply the picked commit.

git checkout feature-1

Now, you can apply the commit with:

git cherry-pick <commit-ref>

2. git rebase

A rebase is what you do when you combine a commit or series of commits to a new commit. It is similar to merging in that it moves changes from one branch to another.

Rebasing allows you to rewrite the history of a Git repository. When you run a rebase operation, it merges the entire history of two branches into one. This will create brand new commits for each commit in another branch inside the branch you are rebasing.

rebase visualisation

Syntax:

$git rebase <branch name>

If there are some conflicts in the branch, resolve them, and perform below commands to continue changes:

$ git status

It is used to check the status,

$git rebase — continue

The above command is used to continue with the changes you made. If you want to skip the change, you can skip as follows:

$ git rebase — skip

When the rebasing is completed. Push the repository to the origin.

3. git stash

git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on. Stashing is handy if you need to quickly switch context and work on something else, but you're mid-way through a code change and aren't quite ready to commit.

Syntax:

git stash

This simple command will stash all your code changes, but does not actually commit them. Instead it stores them locally on your computer inside a stash which can be accessed later.

git stash pop

This command will take all the changes from the stash and apply them to your current branch and also remove the code from the stash.

4. git bisect

The bisect command in Git is incredible for finding which commits caused certain bugs. It is very common for a repository to have thousands of commits from hundreds of developers so when a bug report comes in it can be tricky to track down which changes caused this issue. With bisect, though, this problem is trivial.

git bisect start
git bisect bad
git bisect good 48c86d6

To start a bisect you need to run three commands. The first command starts the bisect. The second command tells Git which commit is the bad commit with the bug. If you leave this blank, as we have, Git will just use the latest commit. The final command tells Git which commit is known to not have this bug. In our example we assume that in commit 48c86d6 there is no bug.

Now after you run these three commands Git will choose the commit in the middle of these two commits and grab all the code from that commit. You can then test to see if the bug is in this commit or not. If the bug is present you just type git bisect bad and it will select the commit that is halfway between this bad commit and the last good commit.

If the bug is not present then you can type git bisect good and Git will select the commit that is halfway between this good commit and the last bad commit. You keep repeating this process of typing either good or bad until eventually you are able to narrow it down to the exact commit that caused the bug.

5. git logs — pretty logs

Another useful logging command in Git is the log command. This command combined with some special flags gives you the ability to print out a pretty log of your commits/branches.

git log --graph --decorate --oneline

--

--