<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Git - Category -</title><link>https://amir.goodarzi.net/categories/git/</link><description>Git - Category -</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.</copyright><lastBuildDate>Wed, 16 Oct 2024 13:25:00 +0200</lastBuildDate><atom:link href="https://amir.goodarzi.net/categories/git/" rel="self" type="application/rss+xml"/><item><title>TIL: Using git restore to Revert File Changes and Sync with Another Branch</title><link>https://amir.goodarzi.net/til-16-10-2024-git-restore/</link><pubDate>Wed, 16 Oct 2024 13:25:00 +0200</pubDate><author>Amir</author><guid>https://amir.goodarzi.net/til-16-10-2024-git-restore/</guid><description><![CDATA[<blockquote>
<p><strong>Today I learned</strong> that if you need to revert a specific file to its original
state, even after commits, or if you want to sync a file with another branch,
you can use the <code>git restore</code> command.</p>
</blockquote>
<p>The <code>git restore</code> command allows you to revert a file to its previous state or sync it with a different branch. This can be particularly useful when you need to discard changes and align a file with its version from another branch, such as main.</p>
<p>In this example, I want to restore and discard all committed changes in my current branch for a specific file to match the version from the main branch:</p>
<div class="code-block code-line-numbers open" style="counter-reset: code-block 0">
    <div class="code-header language-bash">
        <span class="code-title"><i class="arrow fas fa-angle-right" aria-hidden="true"></i></span>
        <span class="ellipses"><i class="fas fa-ellipsis-h" aria-hidden="true"></i></span>
        <span class="copy" title="Copy to clipboard"><i class="far fa-copy" aria-hidden="true"></i></span>
    </div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">git restore --source<span class="o">=</span>main -- main.tf</span></span></code></pre></div></div>
<ul>
<li>git-restore <a href="https://git-scm.com/docs/git-restore" target="_blank" rel="noopener noreffer ">documentation</a></li>
<li>Cover image is licensed with <a href="https://creativecommons.org/licenses/by/3.0/deed.en" target="_blank" rel="noopener noreffer "> Creative Commons Attribution 3.0
Unported</a>
and <a href="https://commons.wikimedia.org/wiki/File:Git-logo.svg" target="_blank" rel="noopener noreffer ">source</a></li>
</ul>
]]></description></item></channel></rss>