{"id":2903,"date":"2024-09-08T14:57:22","date_gmt":"2024-09-08T14:57:22","guid":{"rendered":"https:\/\/sp2hari.com\/?p=2903"},"modified":"2024-09-08T15:15:51","modified_gmt":"2024-09-08T15:15:51","slug":"advanced-git-commands","status":"publish","type":"post","link":"https:\/\/sp2hari.com\/index.php\/2024\/09\/08\/advanced-git-commands\/","title":{"rendered":"Advanced Git Commands"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Git is a powerful tool and I&#8217;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. <\/p>\n\n\n<div class=\"wp-block-image is-style-rounded\">\n<figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/sp2hari.com\/wp-content\/uploads\/2024\/09\/git-user.jpeg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/sp2hari.com\/wp-content\/uploads\/2024\/09\/git-user.jpeg\" alt=\"\" class=\"wp-image-2920\" style=\"width:450px\" srcset=\"https:\/\/sp2hari.com\/wp-content\/uploads\/2024\/09\/git-user.jpeg 1024w, https:\/\/sp2hari.com\/wp-content\/uploads\/2024\/09\/git-user-300x300.jpeg 300w, https:\/\/sp2hari.com\/wp-content\/uploads\/2024\/09\/git-user-150x150.jpeg 150w, https:\/\/sp2hari.com\/wp-content\/uploads\/2024\/09\/git-user-768x768.jpeg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">git bisect<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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\u2019s 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\u2019s broken. git bisect will then help you automate the process of checking commits in between, narrowing down the problematic commit.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/sp2hari\/learn-git-bisect-example\">https:\/\/github.com\/sp2hari\/learn-git-bisect-example<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Clone the repo and you can try the bisect command by running the following commands<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">$ git clone git@github.com:sp2hari\/learn-git-bisect-example.git\n\n#Start the bisect by providing the bad commit id and good commit id\n#Bad commit = current commit. Good commit = first commit of the repo.\n$ git bisect start origin\/main d613b3f\nBisecting: 5 revisions left to test after this (roughly 3 steps)\n[a43319812e06febe1491739d2921d72cc28a0aae] Make text bold\n\n#Load the webpage and check if everything works as expected (it does)\n$ git bisect good\nBisecting: 2 revisions left to test after this (roughly 2 steps)\n[e4ea05da47c825016d4de2bd4b6cb6ce4b5df7e2] Add comments\n\n#Load the webpage and check if everything works as expected (it doesn't)\n$ git bisect bad\nBisecting: 0 revisions left to test after this (roughly 1 step)\n[fcc3b7e7ee805bc37624ef90795775dbf7168772] Update the text in the form\n\n#Load the webpage and check if everything works as expected (it doesn't)\n$ git bisect bad\nBisecting: 0 revisions left to test after this (roughly 0 steps)\n[fa574427d1b5ccc3fb2ce756277f3f819089f6d1] Change the rules of universe\n\n#Load the webpage and check if everything works as expected (it doesn't)\n$ git bisect bad\nfa574427d1b5ccc3fb2ce756277f3f819089f6d1 is the first bad commit\ncommit fa574427d1b5ccc3fb2ce756277f3f819089f6d1\nAuthor: Harishankaran K &lt;hari@hackerrank.com>\nDate:   Sun Sep 8 19:16:07 2024 +0530\n\n    Change the rules of universe\n\n index.html | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\n$ git bisect reset<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Why it\u2019s a power move:<\/strong> 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">git reflog<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes you make mistakes, and sometimes those mistakes make you think you\u2019ve 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).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Clone this repo <a href=\"https:\/\/github.com\/sp2hari\/learn-git-reflog-example\">https:\/\/github.com\/sp2hari\/learn-git-reflog-example<\/a><\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">$ git clone git@github.com:sp2hari\/learn-git-reflog-example.git\n\n#Make a change \n$ echo \"HackerRank Labs Team\" >> index.html\n$ git commit -am \"Useful commit\"\n\n#Make mistakes to lose the changes. \n$ git reset --hard origin\/main\n\n$ git reflog\n77bd8ba (HEAD -> main, origin\/main, origin\/HEAD) HEAD@{0}: reset: moving to origin\/main\n180ef43 HEAD@{1}: commit: Useful commit\n77bd8ba (HEAD -> main, origin\/main, origin\/HEAD) HEAD@{2}: clone: from github.com:sp2hari\/learn-git-reflog-example.git\n\n#You can recover the id of the \"Useful commit\" (180ef43 in this case)\n\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Why it\u2019s a power move:<\/strong> git reflog is like a safety net, giving you the ability to recover commits that don\u2019t 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">git cherry-pick<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">$ git cherry-pick &lt;commit id><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Why it\u2019s a power move:<\/strong> 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\u2019s ideal for bug fixes that need to be deployed quickly but are isolated from ongoing development.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">git rebase<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">git rebase is one of the most misunderstood commands, but once you master it, you\u2019ll 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/sp2hari\/learn-git-rebase-example\">https:\/\/github.com\/sp2hari\/learn-git-rebase-example<\/a><\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">$ git clone git@github.com:sp2hari\/learn-git-rebase-example.git\n$ git config --global core.editor \"code --wait\"\n$ git checkout feature-multiply\n$ git rebase -i main\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Why it\u2019s a power move:<\/strong> 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\u2019re merged into the main branch, creating a more streamlined history that\u2019s easier to navigate.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><br>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Git is a powerful tool and I&#8217;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. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-2903","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/posts\/2903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/comments?post=2903"}],"version-history":[{"count":19,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/posts\/2903\/revisions"}],"predecessor-version":[{"id":2924,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/posts\/2903\/revisions\/2924"}],"wp:attachment":[{"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/media?parent=2903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/categories?post=2903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/tags?post=2903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}