I see, learn and rediscover… everyday!
 
Advanced Git Commands

Advanced Git Commands

Git is a powerful tool and I’ve been using it for years. But there are few commands that make me feel like a power user. I use the basic commands like git add, git commit and git push everyday, there are some advanced commands that can significantly workflow and troubleshooting process.

git bisect

Have you ever faced a bug in your code but had no idea when it was introduced? This is where git bisect comes to the rescue. It’s essentially a binary search for your Git history. You start by specifying a commit where the code was known to work, and the commit where it’s broken. git bisect will then help you automate the process of checking commits in between, narrowing down the problematic commit.

While it is easy to imagine how git bisect works, the true power comes in using it in the real world. Here is a GitHub repo where you can try this out and a bug was introduced in one of the earlier commits.

https://github.com/sp2hari/learn-git-bisect-example

Clone the repo and you can try the bisect command by running the following commands

$ git clone git@github.com:sp2hari/learn-git-bisect-example.git

#Start the bisect by providing the bad commit id and good commit id
#Bad commit = current commit. Good commit = first commit of the repo.
$ git bisect start origin/main d613b3f
Bisecting: 5 revisions left to test after this (roughly 3 steps)
[a43319812e06febe1491739d2921d72cc28a0aae] Make text bold

#Load the webpage and check if everything works as expected (it does)
$ git bisect good
Bisecting: 2 revisions left to test after this (roughly 2 steps)
[e4ea05da47c825016d4de2bd4b6cb6ce4b5df7e2] Add comments

#Load the webpage and check if everything works as expected (it doesn't)
$ git bisect bad
Bisecting: 0 revisions left to test after this (roughly 1 step)
[fcc3b7e7ee805bc37624ef90795775dbf7168772] Update the text in the form

#Load the webpage and check if everything works as expected (it doesn't)
$ git bisect bad
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[fa574427d1b5ccc3fb2ce756277f3f819089f6d1] Change the rules of universe

#Load the webpage and check if everything works as expected (it doesn't)
$ git bisect bad
fa574427d1b5ccc3fb2ce756277f3f819089f6d1 is the first bad commit
commit fa574427d1b5ccc3fb2ce756277f3f819089f6d1
Author: Harishankaran K <hari@hackerrank.com>
Date:   Sun Sep 8 19:16:07 2024 +0530

    Change the rules of universe

 index.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git bisect reset

Why it’s a power move: Instead of manually hunting through your Git history or rolling back changes aimlessly, git bisect turns what could be hours of debugging into a methodical and efficient process.

git reflog

Sometimes you make mistakes, and sometimes those mistakes make you think you’ve lost work for good. Enter git reflog. While the normal Git history shows the sequence of commits, git reflog shows every action that has altered the state of your Git repository, even ones you thought were lost (e.g., after a reset or rebase).

Clone this repo https://github.com/sp2hari/learn-git-reflog-example

$ git clone git@github.com:sp2hari/learn-git-reflog-example.git

#Make a change 
$ echo "HackerRank Labs Team" >> index.html
$ git commit -am "Useful commit"

#Make mistakes to lose the changes. 
$ git reset --hard origin/main

$ git reflog
77bd8ba (HEAD -> main, origin/main, origin/HEAD) HEAD@{0}: reset: moving to origin/main
180ef43 HEAD@{1}: commit: Useful commit
77bd8ba (HEAD -> main, origin/main, origin/HEAD) HEAD@{2}: clone: from github.com:sp2hari/learn-git-reflog-example.git

#You can recover the id of the "Useful commit" (180ef43 in this case)


Why it’s a power move: git reflog is like a safety net, giving you the ability to recover commits that don’t appear in your normal history. Whether you accidentally reset your branch or lose a commit during a rebase, git reflog lets you trace everything back.

git cherry-pick

Need to apply a commit from one branch to another without merging the entire branch? git cherry-pick is your friend. This command allows you to apply individual commits from one branch into another, which is particularly helpful when you want to introduce fixes or small features selectively.

$ git cherry-pick <commit id>

Why it’s a power move: Rather than merging two branches and potentially introducing unintended changes, git cherry-pick gives you precise control over which commits you pull into your current work. It’s ideal for bug fixes that need to be deployed quickly but are isolated from ongoing development.

git rebase

git rebase is one of the most misunderstood commands, but once you master it, you’ll have cleaner, more linear commit histories. Instead of creating merge commits, git rebase rewrites history by placing your changes on top of the latest state of a branch.

https://github.com/sp2hari/learn-git-rebase-example

$ git clone git@github.com:sp2hari/learn-git-rebase-example.git
$ git config --global core.editor "code --wait"
$ git checkout feature-multiply
$ git rebase -i main

Why it’s a power move: It keeps your commit history cleaner and more readable by avoiding unnecessary merge commits. It also enables you to better organize your changes before they’re merged into the main branch, creating a more streamlined history that’s easier to navigate.


In conclusion, using these advanced Git commands allows you to troubleshoot more efficiently, control your history with precision, and recover from even the trickiest situations. Mastering commands like git bisect, git reflog, git cherry-pick, and git rebase can elevate your Git game to the next level.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.