<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>git Archives - Info Spot</title>
	<atom:link href="https://info-spot.net/tag/git/feed/" rel="self" type="application/rss+xml" />
	<link>https://info-spot.net/tag/git/</link>
	<description>Info Spot blog</description>
	<lastBuildDate>Fri, 29 Nov 2024 18:05:17 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.2</generator>

<image>
	<url>https://info-spot.net/wp-content/uploads/2024/11/cropped-icon-32x32.png</url>
	<title>git Archives - Info Spot</title>
	<link>https://info-spot.net/tag/git/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Git: Undo the most recent local Git commits</title>
		<link>https://info-spot.net/git-undo-the-most-recent-local-git-commits/</link>
					<comments>https://info-spot.net/git-undo-the-most-recent-local-git-commits/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 29 Nov 2024 07:37:53 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[git reflog]]></category>
		<category><![CDATA[git reset]]></category>
		<category><![CDATA[git undo changes]]></category>
		<guid isPermaLink="false">https://info-spot.net/?p=634</guid>

					<description><![CDATA[<p>The current branch will be restored to a specified prior commit using the git reset command. By default,&#8230;</p>
<p>The post <a href="https://info-spot.net/git-undo-the-most-recent-local-git-commits/">Git: Undo the most recent local Git commits</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>The current branch will be restored to a specified prior commit using the <code>git reset</code> command. By default, this command will preserve the files in the working tree while deleting commits from the history of the current branch. By doing this, you can undo one or more commits without erasing any of your work.</p>



<p>You must indicate the commit you want to reset to when you call <code>git reset</code>. You can use the <code>tilde (~) </code>suffix to identify an ancestor of <code>HEAD</code>, the current commit, or you can use the <code>git log</code> to obtain the hash of the desired commit. The most recent commit can be undone and redone using the commands below:</p>



<pre class="wp-block-code"><code>git add .
git commit -m "This commit is a mistake"
git reset HEAD~
git add main.py <em># need to re-add files after reset</em>
git commit -m "This commit corrects the mistake"</code></pre>



<p>To undo the last two commits, use the commands:</p>



<pre class="wp-block-code"><code>git add .
git commit -m "This commit is a mistake"
<em># make changes</em>
git add .
git commit -m "This commit is another mistake"
git reset HEAD~2
git add .
git commit -m "this commit corrects both mistakes"</code></pre>



<p>If you don’t want to have to re-stage your files after a reset, you can use the&nbsp;<code>--soft</code>&nbsp;flag:</p>



<pre class="wp-block-code"><code>git add .
git commit -m "This commit is a mistake"
git reset --soft HEAD~
<em># no need to git add, as files are already staged</em>
git commit -m "This commit corrects the mistake"</code></pre>



<p>The <code>--hard</code> flag can be used to restore the working tree and Git history to the state of a prior commit. Keep in mind that doing this would undo all tracked file modifications, even those that haven&#8217;t been committed yet.</p>



<pre class="wp-block-code"><code>git add .
git commit -m ""
git reset --hard HEAD~</code></pre>



<p>Use of <code>git reset --hard</code> should be done carefully. Nevertheless, up to 90 days after they were erased, you can still use <code>git reflog</code> to recover any deleted changes. <code>Git reflog</code> will display a list of prior commits on the tips of branches when it is executed. You can select the partial hash of the commit (such as <code>5c8f5a7</code>) from this list in order to restore it and make a new branch for it:</p>



<pre class="wp-block-code"><code>git checkout -b restored-commit-branch 5c8f5a7</code></pre>



<p><code>Git revert</code> can be used to produce new commits that do the opposite of current commits, i.e., removing lines and files that were added and adding lines and files that were removed, if you want to keep your repository&#8217;s history intact but restore the contents to a former state:</p>



<pre class="wp-block-code"><code>git add .
git commit -m "This commit is a mistake"
git revert HEAD <em># will create a new commit doing the opposite of the one above</em></code></pre>
<p>The post <a href="https://info-spot.net/git-undo-the-most-recent-local-git-commits/">Git: Undo the most recent local Git commits</a> appeared first on <a href="https://info-spot.net">Info Spot</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://info-spot.net/git-undo-the-most-recent-local-git-commits/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
