<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Git on Rishav's Digital Garden</title><link>https://blog.rishavs.in/tags/git/</link><description>Recent content in Git on Rishav's Digital Garden</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Mon, 03 Feb 2025 16:37:00 +0000</lastBuildDate><atom:link href="https://blog.rishavs.in/tags/git/index.xml" rel="self" type="application/rss+xml"/><item><title>GitFlow</title><link>https://blog.rishavs.in/posts/gitflow/</link><pubDate>Mon, 03 Feb 2025 16:37:00 +0000</pubDate><guid>https://blog.rishavs.in/posts/gitflow/</guid><description>&lt;figure>
&lt;img loading="lazy" src="https://blog.rishavs.in/images/Pasted%20image%2020250203182747.png"
alt="Pasted image 20250203182747"/>
&lt;/figure>
&lt;h2 id="reference">Reference&lt;/h2>
&lt;ul>
&lt;li>GirFlow Explained: &lt;a href="https://youtu.be/Aa8RpP0sf-Y">https://youtu.be/Aa8RpP0sf-Y&lt;/a>&lt;/li>
&lt;/ul></description></item><item><title>Git Undo Mistakes</title><link>https://blog.rishavs.in/posts/git-undo-mistakes/</link><pubDate>Wed, 24 Mar 2021 23:12:00 +0000</pubDate><guid>https://blog.rishavs.in/posts/git-undo-mistakes/</guid><description>&lt;h2 id="undoing-mistakes-in-git">Undoing Mistakes in Git&lt;/h2>
&lt;h3 id="1-discarding-uncommitted-changes">1. Discarding Uncommitted Changes&lt;/h3>
&lt;h4 id="a-git-restore-file">a) &lt;code>git restore &amp;lt;file&amp;gt;&lt;/code>&lt;/h4>
&lt;ul>
&lt;li>Discards uncommitted modifications in the specified file.&lt;/li>
&lt;li>&lt;strong>Irreversible&lt;/strong>: once applied, changes cannot be recovered.&lt;/li>
&lt;/ul>
&lt;h4 id="b-git-restore--p-file">b) &lt;code>git restore -p &amp;lt;file&amp;gt;&lt;/code>&lt;/h4>
&lt;ul>
&lt;li>The &lt;code>-p&lt;/code> (patch) option lets you interactively choose hunks to discard.&lt;/li>
&lt;li>Useful for selectively reverting parts of a file.&lt;/li>
&lt;/ul>
&lt;h4 id="c-git-restore-">c) &lt;code>git restore .&lt;/code>&lt;/h4>
&lt;ul>
&lt;li>Discards all uncommitted changes in the working directory since the last commit.&lt;/li>
&lt;/ul>
&lt;h3 id="2-amending-the-last-commit">2. Amending the Last Commit&lt;/h3>
&lt;h4 id="git-commit---amend--m-new-commit-message">&lt;code>git commit --amend -m &amp;quot;New Commit Message&amp;quot;&lt;/code>&lt;/h4>
&lt;ul>
&lt;li>Updates the message (and/or contents) of the last commit.&lt;/li>
&lt;li>&lt;strong>Do not amend&lt;/strong> commits that have already been shared with others.&lt;/li>
&lt;/ul>
&lt;h3 id="3-reverting-a-specific-commit">3. Reverting a Specific Commit&lt;/h3>
&lt;h4 id="git-revert-sha">&lt;code>git revert &amp;lt;SHA&amp;gt;&lt;/code>&lt;/h4>
&lt;ul>
&lt;li>Creates a new commit that inverts the changes of the specified commit.&lt;/li>
&lt;li>Safe way to &amp;ldquo;undo&amp;rdquo; a change without rewriting history.&lt;/li>
&lt;li>Obtain the SHA from &lt;code>git log&lt;/code>, e.g. &lt;code>74e3b2b&lt;/code>.&lt;/li>
&lt;/ul>
&lt;h3 id="4-resetting-to-an-earlier-commit">4. Resetting to an Earlier Commit&lt;/h3>
&lt;h4 id="a-git-reset---hard-sha">a) &lt;code>git reset --hard &amp;lt;SHA&amp;gt;&lt;/code>&lt;/h4>
&lt;ul>
&lt;li>Moves &lt;code>HEAD&lt;/code> and current branch to the given commit, discarding all subsequent commits and uncommitted changes.&lt;/li>
&lt;li>&lt;strong>Warning&lt;/strong>: unpushed commits are permanently lost.&lt;/li>
&lt;/ul>
&lt;h4 id="b-git-reset---mixed-sha-default">b) &lt;code>git reset --mixed &amp;lt;SHA&amp;gt;&lt;/code> (default)&lt;/h4>
&lt;ul>
&lt;li>Similar to &lt;code>--hard&lt;/code>, but preserves changes from discarded commits as unstaged modifications.&lt;/li>
&lt;/ul>
&lt;h3 id="5-restoring-a-file-from-a-past-commit">5. Restoring a File from a Past Commit&lt;/h3>
&lt;h4 id="git-restore---source-sha----file">&lt;code>git restore --source &amp;lt;SHA&amp;gt; -- &amp;lt;file&amp;gt;&lt;/code>&lt;/h4>
&lt;ul>
&lt;li>Replaces the working copy of &lt;code>&amp;lt;file&amp;gt;&lt;/code> with its state at the given commit.&lt;/li>
&lt;li>Only affects the specified file.&lt;/li>
&lt;/ul>
&lt;h3 id="6-using-the-reflog-to-recover-lost-commits">6. Using the Reflog to Recover Lost Commits&lt;/h3>
&lt;h4 id="git-reflog">&lt;code>git reflog&lt;/code>&lt;/h4>
&lt;ul>
&lt;li>Records updates to &lt;code>HEAD&lt;/code> (including resets) for a limited time.&lt;/li>
&lt;li>Find the SHA of a lost state and then create a branch or reset to it.&lt;/li>
&lt;/ul>
&lt;h5 id="restoring-after-a-hard-reset">Restoring After a Hard Reset&lt;/h5>
&lt;ul>
&lt;li>Locate the prior &lt;code>HEAD&lt;/code> reference in the reflog.&lt;/li>
&lt;li>Use &lt;code>git branch &amp;lt;new-branch&amp;gt; &amp;lt;reflog-SHA&amp;gt;&lt;/code> to recover.&lt;/li>
&lt;/ul>
&lt;h5 id="recovering-a-deleted-branch">Recovering a Deleted Branch&lt;/h5>
&lt;ol>
&lt;li>Identify the branch tip SHA via &lt;code>git reflog&lt;/code>.&lt;/li>
&lt;li>Recreate the branch: &lt;code>git branch &amp;lt;branch-name&amp;gt; &amp;lt;SHA&amp;gt;&lt;/code>.&lt;/li>
&lt;/ol>
&lt;h3 id="7-moving-commits-to-another-branch">7. Moving Commits to Another Branch&lt;/h3>
&lt;h4 id="a-to-a-new-branch">a) To a New Branch&lt;/h4>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>git branch &amp;lt;new-branch&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git reset --hard HEAD~1
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>Creates &lt;code>&amp;lt;new-branch&amp;gt;&lt;/code> at the current &lt;code>HEAD&lt;/code>, then removes the last commit from the original branch.&lt;/li>
&lt;/ul>
&lt;h4 id="b-to-an-existing-branch">b) To an Existing Branch&lt;/h4>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>git checkout &amp;lt;target-branch&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git cherry-pick &amp;lt;SHA&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git checkout &amp;lt;original-branch&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git reset --hard HEAD~1
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>Cherry-picks the commit into &lt;code>&amp;lt;target-branch&amp;gt;&lt;/code>, then removes it from the source branch.&lt;/li>
&lt;/ul>
&lt;h3 id="8-interactive-rebase-for-history-rewriting">8. Interactive Rebase for History Rewriting&lt;/h3>
&lt;p>Use with caution: Interactive rebase rewrites commit history, which can break shared branches.&lt;/p></description></item></channel></rss>