{"id":9221,"date":"2024-06-18T13:23:28","date_gmt":"2024-06-18T17:23:28","guid":{"rendered":"https:\/\/blogs.swarthmore.edu\/its\/?p=9221"},"modified":"2024-07-18T16:51:37","modified_gmt":"2024-07-18T20:51:37","slug":"%f0%9f%94%93-unlocking-the-power-of-git-tips-and-tricks","status":"publish","type":"post","link":"https:\/\/blogs.swarthmore.edu\/its\/2024\/06\/18\/%f0%9f%94%93-unlocking-the-power-of-git-tips-and-tricks\/","title":{"rendered":"Unlocking the Power of Git: Tips and Tricks"},"content":{"rendered":"\n<p>Git is an indispensable tool in the modern developer&#8217;s toolkit. Its powerful version control system and ability to make code more collaborative have made it an industry standard, yet, how much do you really know about git?<\/p>\n\n\n\n<p>Here are some of the more interesting git commands that I&#8217;ve accumulated over the years. (Last updated on 6\/18\/2024)<\/p>\n\n\n\n<div class=\"wp-block-jetpack-markdown\"><h3>Undoing Changes<\/h3>\n<h4>Undo the last commit and keep the changes<\/h4>\n<pre><code class=\"language-sh\">git reset --soft HEAD~1\n<\/code><\/pre>\n<h4>Undo the last commit and discard the changes<\/h4>\n<pre><code class=\"language-sh\">git reset --hard HEAD~1\n<\/code><\/pre>\n<h3>Checkouts<\/h3>\n<h4>Checkout a certain commit (without detaching HEAD)<\/h4>\n<pre><code class=\"language-sh\">git checkout -b &lt;tmp-branch&gt; &lt;commit-hash&gt;\n<\/code><\/pre>\n<h4>Checkout a specific file form a repo<\/h4>\n<pre><code class=\"language-sh\">git checkout &lt;commit-hash&gt; -- &lt;file-path&gt;\n<\/code><\/pre>\n<h3>Commits<\/h3>\n<h4>Amend the last commit message<\/h4>\n<pre><code class=\"language-sh\">git commit --amend\n<\/code><\/pre>\n<h3>Branches<\/h3>\n<h4>Delete a local branch<\/h4>\n<pre><code class=\"language-sh\">git branch -d &lt;branch-name&gt;\n<\/code><\/pre>\n<h4>Delete a remote branch<\/h4>\n<pre><code class=\"language-sh\">git branch &lt;remote-name&gt; -d &lt;branch-name&gt;\n<\/code><\/pre>\n<h3>Logs<\/h3>\n<h4>Show the the commmit log<\/h4>\n<pre><code class=\"language-sh\">git log\n<\/code><\/pre>\n<h4>Show the commit log in a pretty graph<\/h4>\n<pre><code class=\"language-sh\">git log --graph --online --all\n<\/code><\/pre>\n<h3>Stashes<\/h3>\n<h4>Stash your changes<\/h4>\n<pre><code class=\"language-sh\">git stash\n<\/code><\/pre>\n<h4>Apply your stashed changes<\/h4>\n<pre><code class=\"language-sh\">git stash apply\n<\/code><\/pre>\n<h3>Submodules<\/h3>\n<h4>Clone a repo with submodules<\/h4>\n<pre><code class=\"language-sh\">git clone --recurse-submodules &lt;path-to-repo&gt;\n<\/code><\/pre>\n<h4>Add a submodule<\/h4>\n<pre><code class=\"language-sh\">git submodule add &lt;path-to-submodule-repo&gt;\n<\/code><\/pre>\n<h4>Remove a submodule<\/h4>\n<pre><code class=\"language-sh\">git rm &lt;path-to-submodule&gt;\n<\/code><\/pre>\n<h3>Bonus: Bisects<\/h3>\n<h4>Using bisect to find bugs<\/h4>\n<pre><code class=\"language-sh\"># This starts your bisect session\ngit bisect start    \n\n# Mark the current commit as bad\ngit bisect bad      \n\n# Specify the commit where the bug was not present.\ngit bisect good &lt;commit-hash&gt;\n\n# Git will checkout a commit halfway between the 2 specified. It is now up to the user to check that commit.\n\n# IF the commit has the bug, it gets marked as bad\ngit bisect bad\n\n# If the commit DOES NOT have the bug, it gets marked as good\ngit bisect good\n\n# Repeat until the bug is found, then end the session\ngit bisect reset\n<\/code><\/pre>\n<\/div>\n\n\n\n<p>As a reminder all staff and faculty have access to Swarthmore&#8217;s GitHub instance at <a href=\"https:\/\/github.swarthmore.edu\">github.swarthmore.edu<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Git is an indispensable tool in the modern developer&#8217;s toolkit. Its powerful version control system and ability to make code more collaborative have made it an industry standard, yet, how much do you really know about git? Here are some &hellip; <a href=\"https:\/\/blogs.swarthmore.edu\/its\/2024\/06\/18\/%f0%9f%94%93-unlocking-the-power-of-git-tips-and-tricks\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Unlocking the Power of Git: Tips and Tricks<\/span><\/a><\/p>\n","protected":false},"author":70,"featured_media":9249,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2,472],"tags":[90,325,274],"class_list":{"0":"post-9221","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","6":"hentry","7":"category-academic-technology","8":"category-github","9":"tag-featured","10":"tag-git","11":"tag-github","13":"fallback-thumbnail"},"jetpack_featured_media_url":"https:\/\/blogs.swarthmore.edu\/its\/wp-content\/uploads\/2024\/06\/totally-not-ai-generated.png","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/ph2nPL-2oJ","_links":{"self":[{"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/posts\/9221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/users\/70"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/comments?post=9221"}],"version-history":[{"count":22,"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/posts\/9221\/revisions"}],"predecessor-version":[{"id":9272,"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/posts\/9221\/revisions\/9272"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/media\/9249"}],"wp:attachment":[{"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/media?parent=9221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/categories?post=9221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.swarthmore.edu\/its\/wp-json\/wp\/v2\/tags?post=9221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}