<?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 </title>
    <link>https://albertogalvez.com/categories/git/</link>
    <description>Recent content in Git on </description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <lastBuildDate>Sun, 13 Apr 2025 00:00:00 +0000</lastBuildDate><atom:link href="https://albertogalvez.com/categories/git/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Lazygit: Git for Lazy People</title>
      <link>https://albertogalvez.com/posts/git/git-lazygit/</link>
      <pubDate>Sun, 13 Apr 2025 00:00:00 +0000</pubDate>
      
      <guid>https://albertogalvez.com/posts/git/git-lazygit/</guid>
      <description>&lt;h1 id=&#34;lazygit&#34;&gt;Lazygit&lt;/h1&gt;
&lt;h2 id=&#34;how-to-cherry-pick-with-lazygit&#34;&gt;How to cherry pick with LazyGit&lt;/h2&gt;
&lt;p&gt;From the &amp;ldquo;Commit&amp;rdquo; panel (or the &amp;ldquo;Branches&amp;rdquo; panel) focus on a commit an press &lt;code&gt;Shift + c&lt;/code&gt;, you can select more than one
Go to the &amp;ldquo;Commit&amp;rdquo; panel (in the &amp;ldquo;Branches&amp;rdquo; panel it doesn&amp;rsquo;t work) and press &lt;code&gt;Shift + v&lt;/code&gt;, will ask for confirmation.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<h1 id="lazygit">Lazygit</h1>
<h2 id="how-to-cherry-pick-with-lazygit">How to cherry pick with LazyGit</h2>
<p>From the &ldquo;Commit&rdquo; panel (or the &ldquo;Branches&rdquo; panel) focus on a commit an press <code>Shift + c</code>, you can select more than one
Go to the &ldquo;Commit&rdquo; panel (in the &ldquo;Branches&rdquo; panel it doesn&rsquo;t work) and press <code>Shift + v</code>, will ask for confirmation.</p>
]]></content:encoded>
    </item>
    
    <item>
      <title>Git Fuck Up</title>
      <link>https://albertogalvez.com/posts/git/git_fuck_up/</link>
      <pubDate>Sun, 19 Jan 2025 00:00:00 +0000</pubDate>
      
      <guid>https://albertogalvez.com/posts/git/git_fuck_up/</guid>
      <description>Git terror moments</description>
      <content:encoded><![CDATA[<h1 id="i-committed-and-immediately-realized-i-need-to-make-one-small-change">I committed and immediately realized I need to make one small change!</h1>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="c1"># make your change</span>
</span></span><span class="line"><span class="cl">git add . <span class="c1"># or add individual files</span>
</span></span><span class="line"><span class="cl">git commit --amend --no-edit
</span></span><span class="line"><span class="cl"><span class="c1"># now your last commit contains that change!</span>
</span></span><span class="line"><span class="cl"><span class="c1"># WARNING: never amend public commits</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>You could also make the change as a new commit and then do rebase -i in order to squash them both together, but this is about a million times faster.</p>
<p><strong>Warning</strong>: You should never amend commits that have been pushed up to a public/shared branch! Only amend commits that only exist in your local copy or you&rsquo;re gonna have a bad time.</p>
<h1 id="i-accidentally-committed-something-to-master-that-should-have-been-on-a-brand-new-branch">I accidentally committed something to master that should have been on a brand new branch!</h1>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="c1"># create a new branch from the current state of master</span>
</span></span><span class="line"><span class="cl">git branch some-new-branch-name
</span></span><span class="line"><span class="cl"><span class="c1"># remove the last commit from the master branch</span>
</span></span><span class="line"><span class="cl">git reset HEAD~ --hard
</span></span><span class="line"><span class="cl">git checkout some-new-branch-name
</span></span><span class="line"><span class="cl"><span class="c1"># your commit lives in this branch now </span>
</span></span></code></pre></td></tr></table>
</div>
</div><p><strong>Note</strong>: this doesn&rsquo;t work if you&rsquo;ve already pushed the commit to a public/shared branch, and if you tried other things first, you might need to git reset HEAD@{number-of-commits-back} instead of HEAD~.</p>
<h1 id="i-accidentally-committed-to-the-wrong-branch">I accidentally committed to the wrong branch!</h1>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="c1"># undo the last commit, but leave the changes available</span>
</span></span><span class="line"><span class="cl">git reset HEAD~ --soft
</span></span><span class="line"><span class="cl">git stash
</span></span><span class="line"><span class="cl"><span class="c1"># move to the correct branch</span>
</span></span><span class="line"><span class="cl">git checkout name-of-the-correct-branch
</span></span><span class="line"><span class="cl">git stash pop
</span></span><span class="line"><span class="cl">git add . <span class="c1"># or add individual files</span>
</span></span><span class="line"><span class="cl">git commit -m <span class="s2">&#34;your message here&#34;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="c1"># now your changes are on the correct branch</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>A lot of people have suggested using cherry-pick for this situation too, so take your pick on whatever one makes the most sense to you!</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">git checkout name-of-the-correct-branch
</span></span><span class="line"><span class="cl"><span class="c1"># grab the last commit to master</span>
</span></span><span class="line"><span class="cl">git cherry-pick master
</span></span><span class="line"><span class="cl"><span class="c1"># delete it from master</span>
</span></span><span class="line"><span class="cl">git checkout master
</span></span><span class="line"><span class="cl">git reset HEAD~ --hard
</span></span></code></pre></td></tr></table>
</div>
</div><h1 id="i-tried-to-run-a-diff-but-nothing-happened">I tried to run a diff but nothing happened?!</h1>
<p>If you know that you made changes to files, but diff is empty, you probably add-ed your files to staging and you need to use a special flag.</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">git diff --staged
</span></span></code></pre></td></tr></table>
</div>
</div><h1 id="i-need-to-undo-my-changes-to-a-file">I need to undo my changes to a file!</h1>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="c1"># find a hash for a commit before the file was changed</span>
</span></span><span class="line"><span class="cl">git log
</span></span><span class="line"><span class="cl"><span class="c1"># use the arrow keys to scroll up and down in history</span>
</span></span><span class="line"><span class="cl"><span class="c1"># once you&#39;ve found your commit, save the hash</span>
</span></span><span class="line"><span class="cl">git checkout <span class="o">[</span>saved hash<span class="o">]</span> -- path/to/file
</span></span><span class="line"><span class="cl"><span class="c1"># the old version of the file will be in your index</span>
</span></span><span class="line"><span class="cl">git commit -m <span class="s2">&#34;Wow, you don&#39;t have to copy-paste to undo&#34;</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>For real though, if your branch is sooo borked that you need to reset the state of your repo to be the same as the remote repo in a &ldquo;git-approved&rdquo; way.</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="c1"># get the lastest state of origin</span>
</span></span><span class="line"><span class="cl">git fetch origin
</span></span><span class="line"><span class="cl">git checkout master
</span></span><span class="line"><span class="cl">git reset --hard origin/master
</span></span><span class="line"><span class="cl"><span class="c1"># delete untracked files and directories</span>
</span></span><span class="line"><span class="cl">git clean -d --force
</span></span><span class="line"><span class="cl"><span class="c1"># repeat checkout/reset/clean for each borked branch</span>
</span></span></code></pre></td></tr></table>
</div>
</div>]]></content:encoded>
    </item>
    
    <item>
      <title>Git Undo Changes</title>
      <link>https://albertogalvez.com/posts/git/git-undo/</link>
      <pubDate>Sat, 16 Nov 2024 00:00:00 +0000</pubDate>
      
      <guid>https://albertogalvez.com/posts/git/git-undo/</guid>
      <description>Git Undo Changes</description>
      <content:encoded><![CDATA[<h1 id="git">Git</h1>
<h2 id="discard-all-local-changes-in-the-working-directory">Discard All Local Changes in the Working Directory</h2>
<p>If you want to discard all uncommitted changes (modifications and untracked files) in your working directory:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">git reset --hard
</span></span></code></pre></td></tr></table>
</div>
</div><p>If you also have untracked files and want to delete them:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">git clean -fd
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li><code>-f</code> forces the cleaning.</li>
<li><code>-d</code> removes untracked directories.</li>
</ul>
<h2 id="discard-changes-in-a-specific-file">Discard Changes in a Specific File</h2>
<p>If you only need to discard changes in a particular file:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">git checkout -- filename
</span></span></code></pre></td></tr></table>
</div>
</div><p>Or, in recent versions of Git:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">git restore filename
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="discard-all-changes-in-tracked-files">Discard All Changes in Tracked Files</h2>
<p>To discard all changes in tracked files without affecting untracked files:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">git restore .
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="revert-changes-in-the-staging-area">Revert Changes in the Staging Area</h2>
<p>If you&rsquo;ve already added files to the staging area (<code>git add</code>) but haven&rsquo;t committed yet and want to remove them from there:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">git restore --staged filename
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="delete-the-last-commit">Delete the Last Commit</h2>
<p>If you&rsquo;ve made a commit but haven&rsquo;t pushed it yet:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">git reset --soft HEAD~1
</span></span></code></pre></td></tr></table>
</div>
</div><p>This undoes the last commit while keeping the changes in your working directory.</p>
<p>If you want to undo the commit and delete the changes from the working directory:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">git reset --hard HEAD~1
</span></span></code></pre></td></tr></table>
</div>
</div>]]></content:encoded>
    </item>
    
    <item>
      <title>Git Submodules</title>
      <link>https://albertogalvez.com/posts/git/git-submodules/</link>
      <pubDate>Sat, 19 Oct 2024 00:00:00 +0000</pubDate>
      
      <guid>https://albertogalvez.com/posts/git/git-submodules/</guid>
      <description>Git Submodules</description>
      <content:encoded><![CDATA[<h3 id="using-git-repositories-inside-other-git-repositories">Using Git repositories inside other Git repositories</h3>
<p>Git allows you to include other Git repositories called submodules into a repository. This allows you to track changes in several repositories via a central one. Submodules are Git repositories nested inside a parent Git repository at a specific path in the parent repository’s working directory. A submodule can be located anywhere in a parent Git repository’s working directory and is configured via a .gitmodules file located at the root of the parent repository. This file contains which paths are submodules and what URL should be used when cloning and fetching for that submodule. Submodule support includes support for adding, updating, synchronizing, and cloning submodules.</p>
<p>Git allows you to commit, pull and push to these repositories independently.</p>
<p>Submodules allow you to keep projects in separate repositories but still be able to reference them as folders in the working directory of other repositories.</p>
<h3 id="working-with-repositories-that-contain-submodules">Working with repositories that contain submodules</h3>
<ol>
<li>Cloning a repository that contains submodules</li>
</ol>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">git clone --recursive <span class="o">[</span>URL to Git repo<span class="o">]</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>If you already have cloned a repository and now want to load it’s submodules you have to use submodule update.</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">git submodule update --init
</span></span><span class="line"><span class="cl"><span class="c1"># if there are nested submodules:</span>
</span></span><span class="line"><span class="cl">git submodule update --init --recursive
</span></span></code></pre></td></tr></table>
</div>
</div><ol start="2">
<li>Downloading multiple submodules at once</li>
</ol>
<p>Since a repository can include many submodules, downloading them all sequentially can take much time. For this reason clone and submodule update command support the &ndash;jobs parameter to fetch multiple submodules at the same time.</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="c1"># download up to 8 submodules at once</span>
</span></span><span class="line"><span class="cl">git submodule update --init --recursive --jobs <span class="m">8</span>
</span></span><span class="line"><span class="cl">git clone --recursive --jobs <span class="m">8</span> <span class="o">[</span>URL to Git repo<span class="o">]</span>
</span></span><span class="line"><span class="cl"><span class="c1"># short version</span>
</span></span><span class="line"><span class="cl">git submodule update --init --recursive -j <span class="m">8</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ol start="3">
<li>Pulling with submodules</li>
</ol>
<p>Once you have set up the submodules you can update the repository with fetch/pull like you would normally do. To pull everything including the submodules, use the &ndash;recurse-submodules and the &ndash;remote parameter in the git pull command.</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="c1"># pull all changes in the repo including changes in the submodules</span>
</span></span><span class="line"><span class="cl">git pull --recurse-submodules
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># pull all changes for the submodules</span>
</span></span><span class="line"><span class="cl">git submodule update --remote
</span></span></code></pre></td></tr></table>
</div>
</div><ol start="4">
<li>Executing a command on every submodule</li>
</ol>
<p>Git provides a command that lets us execute an arbitrary shell command on every submodule. To allow execution in nested subprojects the &ndash;recursive parameter is supported. For our example we assume that we want to reset all submodules.</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">git submodule foreach <span class="s1">&#39;git reset --hard&#39;</span>
</span></span><span class="line"><span class="cl"><span class="c1"># including nested submodules</span>
</span></span><span class="line"><span class="cl">git submodule foreach --recursive <span class="s1">&#39;git reset --hard&#39;</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="creating-repositories-with-submodules">Creating repositories with submodules</h3>
<ol>
<li>Adding a submodule to a Git repository and tracking a branch</li>
</ol>
<p>If you add a submodule, you can specify which branch should be tracked via the -b parameter of the submodule add command. The git submodule init command creates the local configuration file for the submodules, if this configuration does not exist.</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="c1"># add submodule and define the master branch as the one you want to track</span>
</span></span><span class="line"><span class="cl">git submodule add -b master <span class="o">[</span>URL to Git repo<span class="o">]</span>
</span></span><span class="line"><span class="cl">git submodule init
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li>adds a new submodule to an existing Git repository and defines that the master branch should be tracked</li>
<li>initialize submodule configuration</li>
</ul>
<p>If you track branches in your submodules, you can update them via the &ndash;remote parameter of the git submodule update command. This pulls in new commits into the main repository and its submodules. It also changes the working directories of the submodules to the commit of the tracked branch.</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="c1"># update your submodule --remote fetches new commits in the submodules</span>
</span></span><span class="line"><span class="cl"><span class="c1"># and updates the working tree to the commit described by the branch</span>
</span></span><span class="line"><span class="cl">git submodule update --remote
</span></span></code></pre></td></tr></table>
</div>
</div><ol start="2">
<li>Adding a submodule and tracking commits
Alternatively to the tracking of a branch, you can also control which commit of the submodule should be used. In this case the Git parent repository tracks the commit that should be checked out in each configured submodule. Performing a submodule update checks out that specific revision in the submodule’s Git repository. You commonly perform this task after you pull a change in the parent repository that updates the revision checked out in the submodule. You would then fetch the latest changes in the submodule’s Git repository and perform a submodule update to check out the current revision referenced in the parent repository. Performing a submodule update is also useful when you want to restore your submodule’s repository to the current commit tracked by the parent repository. This is common when you are experimenting with different checked out branches or tags in the submodule and you want to restore it back to the commit tracked by the parent repository. You can also change the commit that is checked out in each submodule by performing a checkout in the submodule repository and then committing the change in the parent repository.</li>
</ol>
<p>You add a submodule to a Git repository via the git submodule add command.</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">git submodule add <span class="o">[</span>URL to Git repo<span class="o">]</span>
</span></span><span class="line"><span class="cl">git submodule init
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li>adds a submodule to the existing Git repository</li>
<li>initialize submodule configuration</li>
</ul>
<ol start="3">
<li>Updating which commit you are tracking</li>
</ol>
<p>The relevant state for the submodules are defined by the main repository. If you commit in your main repository, the state of the submodule is also defined by this commit.</p>
<p>The git submodule update command sets the Git repository of the submodule to that particular commit. The submodule repository tracks its own content which is nested into the main repository. The main repository refers to a commit of the nested submodule repository.</p>
<p>Use the git submodule update command to set the submodules to the commit specified by the main repository. This means that if you pull in new changes into the submodules, you need to create a new commit in your main repository in order to track the updates of the nested submodules.</p>
<p>The following example shows how to update a submodule to its latest commit in its master branch.</p>
<p>Another developer can get the update by pulling in the changes and running the submodules update command.</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="c1"># another developer wants to get the changes</span>
</span></span><span class="line"><span class="cl">git pull
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># this updates the submodule to the latest</span>
</span></span><span class="line"><span class="cl"><span class="c1"># commit in master as set in the last example</span>
</span></span><span class="line"><span class="cl">git submodule update
</span></span></code></pre></td></tr></table>
</div>
</div><p>With this setup you need to create a new commit in the master repository, to use a new state in the submodule. You need to repeat this procedure every time you want to use another state in one of the submodules.</p>
<h3 id="delete-a-submodule-from-a-repository">Delete a submodule from a repository</h3>
<p>To remove a submodule you need to:</p>
<ul>
<li>Delete the relevant section from the .gitmodules file.</li>
<li>Stage the .gitmodules changes:</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">git add .gitmodules
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li>Delete the relevant section from .git/config.</li>
<li>Remove the submodule files from the working tree and index:</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">git rm --cached path_to_submodule <span class="o">(</span>no trailing slash<span class="o">)</span>.
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li>Remove the submodule&rsquo;s .git directory:</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">rm -rf .git/modules/path_to_submodule
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li>Commit the changes:</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">git commit -m <span class="s2">&#34;Removed submodule &lt;name&gt;&#34;</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li>Delete the now untracked submodule files:</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">rm -rf path_to_submodule
</span></span></code></pre></td></tr></table>
</div>
</div>]]></content:encoded>
    </item>
    
  </channel>
</rss>
