<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Prakarsh Parashar on Medium]]></title>
        <description><![CDATA[Stories by Prakarsh Parashar on Medium]]></description>
        <link>https://medium.com/@prakarshparashar?source=rss-3217848e87ef------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*peTTzwjN2jSVrwIStZvQQg.jpeg</url>
            <title>Stories by Prakarsh Parashar on Medium</title>
            <link>https://medium.com/@prakarshparashar?source=rss-3217848e87ef------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 10:32:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@prakarshparashar/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Git Branching Made Easy]]></title>
            <link>https://medium.com/geekculture/git-branching-made-easy-1cc894b9fd03?source=rss-3217848e87ef------2</link>
            <guid isPermaLink="false">https://medium.com/p/1cc894b9fd03</guid>
            <category><![CDATA[git-branch]]></category>
            <category><![CDATA[git-branching]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[git-internals]]></category>
            <category><![CDATA[git-branching-model]]></category>
            <dc:creator><![CDATA[Prakarsh Parashar]]></dc:creator>
            <pubDate>Mon, 19 Apr 2021 15:20:11 GMT</pubDate>
            <atom:updated>2021-04-21T03:13:16.874Z</atom:updated>
            <content:encoded><![CDATA[<p>This is the second article in the series of articles on Introduction to Git. The first article can be found <a href="https://medium.com/geekculture/a-beginners-guide-to-git-and-git-internals-1e7dcd89d65e">here</a>. Note that the first article is a prerequisite for this article.</p><p>When working on a big project, several features of the project are being developed simultaneously. Git branching helps serve this purpose in an extremely efficient way. Let us try to understand it using an example. Suppose there are two developers — DevA and DevB. Both are working on a project together. This is how the commit history of their Git repository currently looks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/673/1*0geoL_NfASFFkQKTciHOoQ.jpeg" /></figure><p>After some time they feel that it would be better if DevA start working on featureA and DevB starts working on featureB. In a world without branching both the developers would commit to the same line of development. DevA would see the commits and changes made by DevB and vice-versa. Won’t things become confusing? It would become extremely difficult to isolate the work of DevA from the work of DevB.</p><p>Git Branching provides an easy mechanism to develop several features of your project simultaneously and then later merge those features in the main project. It helps to isolate your work completely from that of other members of the team. I think we would better understand the power of Git Branching once we begin working with it. So let’s jump right into it.</p><p>In a nutshell, this is how Git branching works. Whenever you want to develop a certain feature in your project, you branch into another line of development. And once you finish implementing the feature, the feature branch can be merged into the original line of development (also called the master branch).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/924/1*fXE4dcsWMV9Z7K1fJFObnQ.jpeg" /><figcaption>Orange color depicts the branch in which featureA is implemented, while the Cyan color depicts the branch in which featureB is implemented. Branching helps maintain isolation in the development environment for both the developers.</figcaption></figure><h3>What is a Git Branch?</h3><p>All this time (in the previous article) we were developing in a linear line, without any branches. Well, that was a lie! We were still on a branch, the master branch. This is the default branch that Git creates when you do a git init . Remember, when you did a git log , you would see a master written on your latest commit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*9xwNWZO1UQnwENA51t50UQ.jpeg" /><figcaption>Every branch has a name. Stickers denote the name of the branches. The Master branch is the default branch created when we do a git init.</figcaption></figure><p>Generally, the master branch is considered the main line of development. For implementing a specific feature you branch into another feature branch and then later merge the feature branch into the master branch. Thus the master branch is meant to be permanent while other branches die after the purpose for which they were created has been fulfilled (after merging them into another branch).</p><h3>How Git Branching works internally? How to create a Git Branch?</h3><p>Currently, this is how our repository looks like -</p><pre>&gt;&gt;&gt; git log<br>commit e097d8da357fe97c36c7178f0b770ae44b1f1f3c (HEAD -&gt; master)<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Thu May 21 10:24:55 2020 +0530</pre><pre>changed file2</pre><pre>commit 6642a0de776b52bbe993555a4bf14aed060afea2<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Sun May 3 22:14:12 2020 +0530</pre><pre>added another line in file1</pre><pre>commit 155891ca19d94f67159a992c77923818b57d74a5<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Sun May 3 10:41:06 2020 +0530</pre><pre>created file1, created subDir1 and created file2 in it</pre><p>Notice master written on the latest commit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*8hS5O_YFZKtpwMCGdgCwWg.jpeg" /></figure><p>To create a new branch feature1 , execute the following command</p><pre>&gt;&gt;&gt; git branch feature1<br>&gt;&gt;&gt; git log<br>commit e097d8da357fe97c36c7178f0b770ae44b1f1f3c (HEAD -&gt; master, feature1)<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Thu May 21 10:24:55 2020 +0530</pre><pre>changed file2</pre><pre>commit 6642a0de776b52bbe993555a4bf14aed060afea2<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Sun May 3 22:14:12 2020 +0530</pre><pre>added another line in file1</pre><pre>commit 155891ca19d94f67159a992c77923818b57d74a5<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Sun May 3 10:41:06 2020 +0530</pre><pre>created file1, created subDir1 and created file2 in it</pre><p>You will now notice master, feature1 written on the latest commit. Thus a new branch feature1 has been created which points to the same commit pointed by the master branch.</p><p>To see all git branches execute the command below.</p><pre>&gt;&gt;&gt; git branch<br>  feature1<br>* master</pre><p>Notice the asterisk on the master. This denotes that the master is our current working branch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/1*bpl9KcHwVeF9J1gYgnkbHQ.jpeg" /><figcaption>feature1 branch originates from Commit-3. (Currently, no commits have been created in feature1 branch)</figcaption></figure><p>So, to conclude:</p><ol><li>Create a new branch using git branch branchname .</li><li>When a new branch is created, say, branch2 created from branch1, then branch2 points to the latest commit in branch1 as we just saw.</li><li>git branch to see all working branches in your repository</li></ol><h3>Diving Deep into Git References</h3><p>Let us digress from the topic for a moment and talk about Git References. A good understanding of Git References helps a lot in the understanding of how Git Branches work internally.</p><p>So, What are Git References? And how are they useful?</p><p>References in Git provide a mechanism to mark important objects so that they can be easily referred to later. Suppose for a certain section of your project you would like to refer to the code in commit-2 frequently. This would require that we know the commit hash for the commit object. But, Instead of executing a git log and then searching for the commit hash repeatedly, we can use the reference that we have created to refer to that commit object. We will see how to create references in a while.</p><p>There are two types of references:<br>1. heads<br>2. tags</p><p>Execute the following command to see how these references are stored in the .git directory.</p><pre>&gt;&gt;&gt; tree .git/refs</pre><pre>.git/refs<br>├── heads<br>│   ├── feature1<br>│   └── master<br>└── tags</pre><p>Let us take a look into both <em>heads</em> and <em>tags</em> one by one -</p><h4>heads</h4><p>Remember when we executed git log earlier, we also saw HEAD -&gt; master . What is this HEAD? The HEAD is nothing but a pointer to the branch we are currently working on. An exception to this is the detached HEAD state (Don&#39;t worry! We will talk about this in a while). If we are working in the master branch we will see HEAD -&gt; master .</p><pre>&gt;&gt;&gt; git log<br>commit e097d8da357fe97c36c7178f0b770ae44b1f1f3c (HEAD -&gt; master, feature1)<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Thu May 21 10:24:55 2020 +0530</pre><pre>changed file2</pre><pre>commit 6642a0de776b52bbe993555a4bf14aed060afea2<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Sun May 3 22:14:12 2020 +0530</pre><pre>added another line in file1</pre><pre>commit 155891ca19d94f67159a992c77923818b57d74a5<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Sun May 3 10:41:06 2020 +0530</pre><pre>created file1, created subDir1 and created file2 in it</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*_QZuksfc5Iun-ZB91Oh88g.jpeg" /><figcaption>HEAD points to the current working branch, master branch.</figcaption></figure><p>Suppose if we switch to feature1 branch. (We were currently on the master branch. We had created the feature1 branch but we were still on the master branch). To switch to feature1 branch execute the following command.</p><pre>&gt;&gt;&gt; git checkout feature1<br>Switched to branch &#39;feature1&#39;</pre><pre>&gt;&gt;&gt; git branch<br>* feature1<br>  master</pre><p>We have changed our current working branch to feature1 .</p><pre>&gt;&gt;&gt; git log<br>commit e097d8da357fe97c36c7178f0b770ae44b1f1f3c (HEAD -&gt; feature1, master)<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Thu May 21 10:24:55 2020 +0530</pre><pre>changed file2</pre><pre>commit 6642a0de776b52bbe993555a4bf14aed060afea2<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Sun May 3 22:14:12 2020 +0530</pre><pre>added another line in file1</pre><pre>commit 155891ca19d94f67159a992c77923818b57d74a5<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Sun May 3 10:41:06 2020 +0530</pre><pre>created file1, created subDir1 and created file2 in it</pre><p>Notice that, instead of HEAD -&gt; master , we now have HEAD -&gt; feature1 on our latest snapshot. Since we switched to feature1 branch, our HEAD now points to feature1 . Thus HEAD is nothing but a pointer to the current branch in our git repository.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/573/1*Nx5EqQhySznAnNl3kZ-y_g.jpeg" /><figcaption>After switching to the feature1 branch, HEAD now points to feature1.</figcaption></figure><p>Now once again, a moment of truth! The branch we have been talking about all this while is also nothing but a <em>reference object</em>. It is a pointer to the latest commit in the branch. When a new commit object is created in a branch, the branch pointer is simply updated to point to the new commit object.</p><p>So, How is Git able to identify the current commit object that we are working on, it fetches the HEAD reference object, which provides the branch reference object (for the current working branch) and then the branch reference object provides the commit object. Hence the denotation HEAD -&gt; branch .</p><p>In our git repository, currently, references — HEAD-&gt;feature1 , feature1 and master point to the same commit object. While the HEAD reference object points to the feature1 reference object.</p><p>To conclude:<br>1. <em>branch </em>reference objects keep track of our latest commit on the branch. The <em>branch </em>reference object is updated only when a new commit object is created in the branch.<br>2. <em>HEAD </em>reference object keeps track of our current working branch. The <em>HEAD</em> reference object is updated whenever the current working branch is switched to another branch.</p><h4>tags</h4><p>Tags are mainly used to mark important points (commits) in your Git commit history. While heads are inbuilt references, tags are required to be created.</p><p>Suppose commit-2 is an important commit and would be required to be referred to frequently. We can create a tag that points to commit-2. Whenever it would be required to refer to the commit-2 object, we can use this tag.</p><p>Since it is not required to know tags for understanding Git Branching. We will talk about it in another article. The link to the article would be available <a href="http://nothing/">here</a>.</p><h3>Continuing with Git Branching</h3><p>So, we looked at how to create branches, how to switch between branches, and list all branches. We also looked at what is a HEAD reference object and that the <em>branch</em> is nothing but a pointer (reference object) to the latest commit of the branch.<br>Now, let us make some changes and create some more commits in our new feature1 branch. Note that we are currently on our feature1 branch. Execute the following commands to create two new commits in the feature1 branch.</p><pre>&gt;&gt;&gt; echo &quot;I have been created in feature1 branch.&quot; &gt; file3<br>&gt;&gt;&gt; git add file3<br>&gt;&gt;&gt; git commit -m &quot;added file3&quot;<br>[feature1 693f09b] added file3<br> 1 file changed, 1 insertion(+)</pre><pre>&gt;&gt;&gt; echo &quot;Another commit in file3&quot; &gt;&gt; file3<br>&gt;&gt;&gt; git add file3<br>&gt;&gt;&gt; git commit -m &quot;change in file3&quot;<br>[feature1 ef154d8] change in file3<br> 1 file changed, 1 insertion(+)</pre><pre>&gt;&gt;&gt; git log<br>commit ef154d8ae3bac0e2bdcd8102381e49a3a5c480ee (HEAD -&gt; feature1)<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Sat Jul 18 12:23:49 2020 +0530</pre><pre>change in file3</pre><pre>commit 693f09b8c388a684156f9654d99ad5c4b8f2133c<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Sat Jul 18 12:18:07 2020 +0530</pre><pre>added file3</pre><pre>commit e097d8da357fe97c36c7178f0b770ae44b1f1f3c (master)<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Thu May 21 10:24:55 2020 +0530</pre><pre>changed file2</pre><pre>commit 6642a0de776b52bbe993555a4bf14aed060afea2<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Sun May 3 22:14:12 2020 +0530</pre><pre>added another line in file1</pre><pre>commit 155891ca19d94f67159a992c77923818b57d74a5<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Sun May 3 10:41:06 2020 +0530</pre><pre>created file1, created subDir1 and created file2 in it</pre><p>We can see that the feature1 branch is 2 commits ahead of the master branch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/763/1*az1WJscOXHvPazRtuQKhAA.jpeg" /><figcaption>Commit history of our current Git repository. feature1 pointer moves to the latest commit, while HEAD points to the working branch pointer (feature1)</figcaption></figure><p>Now we would like to merge our feature1 branch into the master branch. So, How can we do this?</p><h3>Merging Branches Overview:</h3><p>When we finish implementing a particular feature, we would want to merge it into the branch it originated from. When we want to merge branch2 into branch1, we have two possible scenarios:</p><h4>Fast Forward Merge:</h4><p>A Fast Forward merge is performed when the Lowest common Ancestor of branch1 (reference object) and branch2 (reference object) is branch1 (reference object).</p><p>Note that when we are talking about branch1/branch2 (reference object), we are talking about the latest commit on that branch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/939/1*7OugJcoghWka7p_eYhm9vw.jpeg" /><figcaption>Lowest Common Ancestor of branch1 and branch2 is the latest commit of branch1</figcaption></figure><p>Note that the HEAD points to branch1. Thus if want to merge branch2 into branch1, our current working branch must be branch1.</p><p>Fast Forward merge is simple. During a Fast-Forward merge, the branch1 pointer is simply moved to the same commit where the branch2 pointer points.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/938/1*F6ZDuCzykQtHhoMOqnUnog.jpeg" /><figcaption>In a Fast-Forward merge, the branch1 pointer simply moves to the position of branch2 pointer. The HEAD pointer follows the branch1 pointer.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*udoG9WCW_99J_EDxG4flzg.jpeg" /><figcaption>Redrawing the previous image</figcaption></figure><h4>3-way Merge:</h4><p>The 3-way merge is performed when the Lowest Common Ancestor of branch1 (reference object) and branch2 (reference object) is some other commit object, not branch1 (reference object) or branch2 (reference object). <br>This generally happens when some new commits have been added to the original branch after the new branch has been created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/935/1*nlo7al4cgDvx-8AePbNV4g.jpeg" /><figcaption>Lowest Common Ancestor of branch1 and branch2 is neither pointed by branch1 pointer nor by branch2 pointer. Commit-3, Commit-5, and Commit-7 will be involved in a 3-way merge</figcaption></figure><p>Instead of moving the branch pointer forward. A new commit is created — <em>merge commit. </em>This commit has two parents — one from branch1 and one from branch2. Changes in both branch1 and branch2 are merged and combined into this new <em>merge commit.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/949/1*jl_nC3UGSpfttSx9NRG6OA.jpeg" /><figcaption>On merging, a new merge commit, commit-8 is created in branch1, which incorporates the changes made in both branch1 and branch2.</figcaption></figure><p>So, now we have a good idea about the two possible scenarios when merging branch2 into branch1. Let us now try out the fast-forward merge and the 3-way merge in our git repository.</p><h3>Merging Branches Implementation:</h3><p>Simply doing a git log may not be the best way when dealing with branches. A better command for visualizing git branches is shown below.</p><pre>&gt;&gt;&gt; git log --all --color --graph --pretty=format:&#39;%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)&lt;%an&gt;%Creset&#39; --abbrev-commit</pre><pre>* ef154d8 - (HEAD -&gt; feature1) change in file3 (5 days ago) &lt;pprakarsh&gt;<br>* 693f09b - added file3 (5 days ago) &lt;pprakarsh&gt;<br>* e097d8d - (master) changed file2 (9 weeks ago) &lt;pprakarsh&gt;<br>* 6642a0d - added another line in file1 (3 months ago) &lt;pprakarsh&gt;<br>* 155891c - created file1, created subDir1 and created file2 in it (3 months ago) &lt;pprakarsh&gt;</pre><p>The output becomes colorful and neat. You need not write this gigantic command repeatedly, you can set an alias for this command (to say git lg).</p><pre>&gt;&gt;&gt; git config --global alias.lg &quot;log --all --color --graph --pretty=format:&#39;%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)&lt;%an&gt;%Creset&#39; --abbrev-commit&quot;</pre><p>Now executing git lg will give us the same pretty output.</p><pre>&gt;&gt;&gt; git lg<br>* ef154d8 - (HEAD -&gt; feature1) change in file3 (5 days ago) &lt;pprakarsh&gt;<br>* 693f09b - added file3 (5 days ago) &lt;pprakarsh&gt;<br>* e097d8d - (master) changed file2 (9 weeks ago) &lt;pprakarsh&gt;<br>* 6642a0d - added another line in file1 (3 months ago) &lt;pprakarsh&gt;<br>* 155891c - created file1, created subDir1 and created file2 in it (3 months ago) &lt;pprakarsh&gt;</pre><p>We see that the feature1 branch is two commits ahead of the master branch. The Lowest Common Ancestor of the feature1 branch and the master branch is the commit pointed by the master. Thus, this is the case of a Fast-Forward merge. We want to merge the feature1 branch into the master branch. Thus HEAD must point to master. So, the first step is to checkout to the master branch (the branch we want to merge into).</p><pre>&gt;&gt;&gt; git checkout master<br>Switched to branch &#39;master&#39;<br>&gt;&gt;&gt; git branch<br>  feature1<br>* master<br>&gt;&gt;&gt; git lg<br>* ef154d8 - (feature1) change in file3 (5 days ago) &lt;pprakarsh&gt;<br>* 693f09b - added file3 (5 days ago) &lt;pprakarsh&gt;<br>* e097d8d - (HEAD -&gt; master) changed file2 (9 weeks ago) &lt;pprakarsh&gt;<br>* 6642a0d - added another line in file1 (3 months ago) &lt;pprakarsh&gt;<br>* 155891c - created file1, created subDir1 and created file2 in it (3 months ago) &lt;pprakarsh&gt;</pre><p>To merge the feature1 branch into the master branch, execute the following command.</p><pre>&gt;&gt;&gt; git merge feature1<br>Updating e097d8d..ef154d8<br>Fast-forward<br> file3 | 2 ++<br> 1 file changed, 2 insertions(+)<br> create mode 100644 file3<br>&gt;&gt;&gt; git lg<br>* ef154d8 - (HEAD -&gt; master, feature1) change in file3 (5 days ago) &lt;pprakarsh&gt;<br>* 693f09b - added file3 (5 days ago) &lt;pprakarsh&gt;<br>* e097d8d - changed file2 (9 weeks ago) &lt;pprakarsh&gt;<br>* 6642a0d - added another line in file1 (3 months ago) &lt;pprakarsh&gt;<br>* 155891c - created file1, created subDir1 and created file2 in it (3 months ago) &lt;pprakarsh&gt;</pre><p>Observe the output of the git merge feature1 command. The output shows Fast-forward merge. On executing git lg we see that the master now points to the same commit object as feature1 and HEAD points to the master.</p><p>The feature1 branch pointer is now useless, so we can delete the feature1 branch.</p><pre>&gt;&gt;&gt; git branch -d feature1<br>Deleted branch feature1 (was ef154d8).<br>&gt;&gt;&gt; git branch<br>* master<br>&gt;&gt;&gt; git lg<br>* ef154d8 - (HEAD -&gt; master) change in file3 (5 days ago) &lt;pprakarsh&gt;<br>* 693f09b - added file3 (5 days ago) &lt;pprakarsh&gt;<br>* e097d8d - changed file2 (9 weeks ago) &lt;pprakarsh&gt;<br>* 6642a0d - added another line in file1 (3 months ago) &lt;pprakarsh&gt;<br>* 155891c - created file1, created subDir1 and created file2 in it (3 months ago) &lt;pprakarsh&gt;</pre><p>Notice that there is no feature1 branch anymore.</p><p>Let us now create a new branch branch1 and try doing a 3-way merge. For this, we would need to create some new commits as well. Execute the following commands to create a new branch branch1 .</p><pre>&gt;&gt;&gt; git branch branch1<br>&gt;&gt;&gt; git branch<br>  branch1<br>* master<br>&gt;&gt;&gt; git lg<br>* ef154d8 - (HEAD -&gt; master, branch1) change in file3 (5 days ago) &lt;pprakarsh&gt;<br>* 693f09b - added file3 (5 days ago) &lt;pprakarsh&gt;<br>* e097d8d - changed file2 (9 weeks ago) &lt;pprakarsh&gt;<br>* 6642a0d - added another line in file1 (3 months ago) &lt;pprakarsh&gt;<br>* 155891c - created file1, created subDir1 and created file2 in it (3 months ago) &lt;pprakarsh&gt;</pre><p>We are currently in the master branch. We will create a new commit object in the master branch. Currently, master and branch1 both point to the same commit object.</p><pre>&gt;&gt;&gt; echo &quot;Another commit in master&quot; &gt;&gt; file3<br>&gt;&gt;&gt; git add file3<br>&gt;&gt;&gt; git commit -m &quot;file3 changes&quot;<br>[master f614763] file3 changes<br> 1 file changed, 1 insertion(+)<br>&gt;&gt;&gt; git lg<br>* f614763 - (HEAD -&gt; master) file3 changes (10 seconds ago) &lt;pprakarsh&gt;<br>* ef154d8 - (branch1) change in file3 (5 days ago) &lt;pprakarsh&gt;<br>* 693f09b - added file3 (5 days ago) &lt;pprakarsh&gt;<br>* e097d8d - changed file2 (9 weeks ago) &lt;pprakarsh&gt;<br>* 6642a0d - added another line in file1 (3 months ago) &lt;pprakarsh&gt;<br>* 155891c - created file1, created subDir1 and created file2 in it (3 months ago) &lt;pprakarsh&gt;</pre><p>Notice that the master now points to the new commit object. We will now switch to branch1 . The HEAD would now point to branch1.</p><pre>&gt;&gt;&gt; git checkout branch1<br>Switched to branch &#39;branch1&#39;<br>&gt;&gt;&gt; git branch<br>* branch1<br>  master<br>&gt;&gt;&gt; git lg<br>* f614763 - (master) file3 changes (8 minutes ago) &lt;pprakarsh&gt;<br>* ef154d8 - (HEAD -&gt; branch1) change in file3 (5 days ago) &lt;pprakarsh&gt;<br>* 693f09b - added file3 (5 days ago) &lt;pprakarsh&gt;<br>* e097d8d - changed file2 (9 weeks ago) &lt;pprakarsh&gt;<br>* 6642a0d - added another line in file1 (3 months ago) &lt;pprakarsh&gt;<br>* 155891c - created file1, created subDir1 and created file2 in it (3 months ago) &lt;pprakarsh&gt;</pre><p>We will now create a new commit in branch1 .</p><pre>&gt;&gt;&gt; echo &quot;creating file4&quot; &gt; file4<br>&gt;&gt;&gt; git add file4<br>&gt;&gt;&gt; git commit -m &quot;created file4&quot;<br>[branch1 9e2566a] created file4<br> 1 file changed, 1 insertion(+)<br> create mode 100644 file4<br>&gt;&gt;&gt; git lg<br>* 9e2566a - (HEAD -&gt; branch1) created file4 (3 seconds ago) &lt;pprakarsh&gt;<br>| * f614763 - (master) file3 changes (10 minutes ago) &lt;pprakarsh&gt;<br>|/  <br>* ef154d8 - change in file3 (5 days ago) &lt;pprakarsh&gt;<br>* 693f09b - added file3 (5 days ago) &lt;pprakarsh&gt;<br>* e097d8d - changed file2 (9 weeks ago) &lt;pprakarsh&gt;<br>* 6642a0d - added another line in file1 (3 months ago) &lt;pprakarsh&gt;<br>* 155891c - created file1, created subDir1 and created file2 in it (3 months ago) &lt;pprakarsh&gt;</pre><p>Notice the divergence at the commit object ef154d8 . Both commit objects f614763 (belonging to the master branch) and 9e2566a (belonging to the branch1 branch) contains a reference to the same parent commit object ef154d8.</p><p>Creating another commit object in branch1 .</p><pre>&gt;&gt;&gt; echo &quot;another line in file4&quot; &gt;&gt; file4<br>&gt;&gt;&gt; git add file4<br>&gt;&gt;&gt; git commit -m &quot;file4 changes&quot;<br>[branch1 0b17834] file4 changes<br> 1 file changed, 1 insertion(+)<br>&gt;&gt;&gt; git lg<br>* 0b17834 - (HEAD -&gt; branch1) file4 changes (7 seconds ago) &lt;pprakarsh&gt;<br>* 9e2566a - created file4 (14 minutes ago) &lt;pprakarsh&gt;<br>| * f614763 - (master) file3 changes (24 minutes ago) &lt;pprakarsh&gt;<br>|/  <br>* ef154d8 - change in file3 (5 days ago) &lt;pprakarsh&gt;<br>* 693f09b - added file3 (5 days ago) &lt;pprakarsh&gt;<br>* e097d8d - changed file2 (9 weeks ago) &lt;pprakarsh&gt;<br>* 6642a0d - added another line in file1 (3 months ago) &lt;pprakarsh&gt;<br>* 155891c - created file1, created subDir1 and created file2 in it (3 months ago) &lt;pprakarsh&gt;</pre><p>We can see that new commit object 0b17834 has been created in branch1.</p><p>Let us now merge branch1 into the master branch. Notice that the <em>Lowest Common Ancestor</em> of the branch1 reference object (0b17834) and the master reference object (f614763) is a different commit object (ef154d8). Therefore, it is a 3-way merge. <br>But, first, we need to switch to the master branch.</p><pre>&gt;&gt;&gt; git checkout master<br>Switched to branch &#39;master&#39;<br>&gt;&gt;&gt; ls<br>subDir1  file1  file3<br>&gt;&gt;&gt; git lg<br>* 0b17834 - (branch1) file4 changes (11 minutes ago) &lt;pprakarsh&gt;<br>* 9e2566a - created file4 (24 minutes ago) &lt;pprakarsh&gt;<br>| * f614763 - (HEAD -&gt; master) file3 changes (34 minutes ago) &lt;pprakarsh&gt;<br>|/  <br>* ef154d8 - change in file3 (5 days ago) &lt;pprakarsh&gt;<br>* 693f09b - added file3 (5 days ago) &lt;pprakarsh&gt;<br>* e097d8d - changed file2 (9 weeks ago) &lt;pprakarsh&gt;<br>* 6642a0d - added another line in file1 (3 months ago) &lt;pprakarsh&gt;<br>* 155891c - created file1, created subDir1 and created file2 in it (3 months ago) &lt;pprakarsh&gt;</pre><p>Notice that the content of the repository does not show file4 , because file4 blob object cannot be accessed via the current commit object f614763 .</p><p>Execute the following command to merge branch1 into the master branch.</p><pre>&gt;&gt;&gt; git merge branch1<br>Merge made by the &#39;recursive&#39; strategy.<br> file4 | 2 ++<br> 1 file changed, 2 insertions(+)<br> create mode 100644 file4</pre><pre>&gt;&gt;&gt; git lg<br>*   15357ad - (HEAD -&gt; master) Merge branch &#39;branch1&#39; (15 seconds ago) &lt;pprakarsh&gt;<br>|\  <br>| * 0b17834 - (branch1) file4 changes (15 minutes ago) &lt;pprakarsh&gt;<br>| * 9e2566a - created file4 (29 minutes ago) &lt;pprakarsh&gt;<br>* | f614763 - file3 changes (39 minutes ago) &lt;pprakarsh&gt;<br>|/  <br>* ef154d8 - change in file3 (5 days ago) &lt;pprakarsh&gt;<br>* 693f09b - added file3 (5 days ago) &lt;pprakarsh&gt;<br>* e097d8d - changed file2 (9 weeks ago) &lt;pprakarsh&gt;<br>* 6642a0d - added another line in file1 (3 months ago) &lt;pprakarsh&gt;<br>* 155891c - created file1, created subDir1 and created file2 in it (3 months ago) &lt;pprakarsh&gt;</pre><p>The merge can be seen clearly by executing the git lg command. A new merge commit object (15357ad ) has been created, which contains changes in both the master branch and branch1. The master now points to this commit object.</p><p>If we are certain that branch1 servers no further purpose, we can delete branch1 by executing the git branch -d branch1 command. I choose not to delete it.</p><h3>The case of Merge Conflicts</h3><p>Now, we understand how a <em>Git merge</em> works and we also know how to do a <em>Git merge</em>. If we are merging two branches, changes in both the branches are incorporated in the merge. But, what if we change in the same part of the same file in the two branches. Clearly, both changes cannot be incorporated in the final merge. This results in a merge conflict. How do we resolve these merge conflicts?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/907/1*BKULSvkVPh7hp7H_RRvS1g.jpeg" /><figcaption>In file2.txt, changes in the same part of the file lead to a merge conflict. Which change should be incorporated in the merge? — Tuesday or Wednesday</figcaption></figure><p>An important point to consider here is that merge conflicts can happen only in the case of a 3-way Merge. In the case of a Fast-Forward merge, changes are made in only one branch, thus merge conflict is not possible.</p><p>Let us see how to resolve a merge conflict. <br>Firstly, we will create a new branch branch2 in our git repository.</p><pre>&gt;&gt;&gt; git branch branch2<br>&gt;&gt;&gt; git branch<br>  branch1<br>  branch2<br>* master<br>&gt;&gt;&gt; git lg<br>*   15357ad - (HEAD -&gt; master, branch2) Merge branch &#39;branch1&#39; (3 hours ago) &lt;pprakarsh&gt;<br>|\  <br>| * 0b17834 - (branch1) file4 changes (3 hours ago) &lt;pprakarsh&gt;<br>| * 9e2566a - created file4 (3 hours ago) &lt;pprakarsh&gt;<br>* | f614763 - file3 changes (3 hours ago) &lt;pprakarsh&gt;<br>|/  <br>* ef154d8 - change in file3 (5 days ago) &lt;pprakarsh&gt;<br>* 693f09b - added file3 (5 days ago) &lt;pprakarsh&gt;<br>* e097d8d - changed file2 (9 weeks ago) &lt;pprakarsh&gt;<br>* 6642a0d - added another line in file1 (3 months ago) &lt;pprakarsh&gt;<br>* 155891c - created file1, created subDir1 and created file2 in it (3 months ago) &lt;pprakarsh&gt;</pre><p>Notice that a new branch branch2 has been created which points to the same commit object as master (15357ad). Also, note that we are currently in the master branch. We will create another commit object in the master branch.</p><pre>&gt;&gt;&gt; echo &quot;Today is Tuesday&quot; &gt; file5<br>&gt;&gt;&gt; git add file5<br>&gt;&gt;&gt; git commit -m &quot;file5 added, Tuesday&quot;<br>[master 2a4af24] file5 added, Tuesday<br> 1 file changed, 1 insertion(+)<br> create mode 100644 file5</pre><pre>&gt;&gt;&gt; git lg<br>* 2a4af24 - (HEAD -&gt; master) file5 added, Tuesday (11 minutes ago) &lt;pprakarsh&gt;<br>*   15357ad - (branch2) Merge branch &#39;branch1&#39; (3 hours ago) &lt;pprakarsh&gt;<br>|\  <br>| * 0b17834 - (branch1) file4 changes (3 hours ago) &lt;pprakarsh&gt;<br>| * 9e2566a - created file4 (4 hours ago) &lt;pprakarsh&gt;<br>* | f614763 - file3 changes (4 hours ago) &lt;pprakarsh&gt;<br>|/  <br>* ef154d8 - change in file3 (5 days ago) &lt;pprakarsh&gt;<br>* 693f09b - added file3 (5 days ago) &lt;pprakarsh&gt;<br>* e097d8d - changed file2 (9 weeks ago) &lt;pprakarsh&gt;<br>* 6642a0d - added another line in file1 (3 months ago) &lt;pprakarsh&gt;<br>* 155891c - created file1, created subDir1 and created file2 in it (3 months ago) &lt;pprakarsh&gt;</pre><p>We can see that a new commit object has been created in the master branch. We will switch to branch2 now and create a new commit in branch2 . On switching to branch2 , we will see that there is no file5 in the working directory. This is because we are currently on a different commit object (15357ad).</p><pre>&gt;&gt;&gt; git checkout branch2<br>Switched to branch &#39;branch2&#39;<br>&gt;&gt;&gt; git branch<br>  branch1<br>* branch2<br>  master</pre><pre>&gt;&gt;&gt; git lg<br>* 2a4af24 - (master) file5 added, Tuesday (16 minutes ago) &lt;pprakarsh&gt;<br>*   15357ad - (HEAD -&gt; branch2) Merge branch &#39;branch1&#39; (3 hours ago) &lt;pprakarsh&gt;<br>|\  <br>| * 0b17834 - (branch1) file4 changes (3 hours ago) &lt;pprakarsh&gt;<br>| * 9e2566a - created file4 (4 hours ago) &lt;pprakarsh&gt;<br>* | f614763 - file3 changes (4 hours ago) &lt;pprakarsh&gt;<br>|/  <br>* ef154d8 - change in file3 (5 days ago) &lt;pprakarsh&gt;<br>* 693f09b - added file3 (5 days ago) &lt;pprakarsh&gt;<br>* e097d8d - changed file2 (9 weeks ago) &lt;pprakarsh&gt;<br>* 6642a0d - added another line in file1 (3 months ago) &lt;pprakarsh&gt;<br>* 155891c - created file1, created subDir1 and created file2 in it (3 months ago) &lt;pprakarsh&gt;</pre><pre>&gt;&gt;&gt; ls<br>subDir1  file1  file3  file4</pre><p>We will now create a new commit object in branch2 . We will create file5 in branch2 as well, with the same content as in the master branch but the name of the day.</p><pre>&gt;&gt;&gt; echo &quot;Today is Wednesday&quot; &gt; file5<br>&gt;&gt;&gt; git add file5<br>&gt;&gt;&gt; git commit -m &quot;file5 added, Wednesday&quot;<br>[branch2 206760e] file5 added, Wednesday<br> 1 file changed, 1 insertion(+)<br> create mode 100644 file5</pre><pre>&gt;&gt;&gt; git lg<br>* 206760e - (HEAD -&gt; branch2) file5 added, Wednesday (31 seconds ago) &lt;pprakarsh&gt;<br>| * 2a4af24 - (master) file5 added, Tuesday (18 minutes ago) &lt;pprakarsh&gt;<br>|/  <br>*   15357ad - Merge branch &#39;branch1&#39; (3 hours ago) &lt;pprakarsh&gt;<br>|\  <br>| * 0b17834 - (branch1) file4 changes (3 hours ago) &lt;pprakarsh&gt;<br>| * 9e2566a - created file4 (4 hours ago) &lt;pprakarsh&gt;<br>* | f614763 - file3 changes (4 hours ago) &lt;pprakarsh&gt;<br>|/  <br>* ef154d8 - change in file3 (5 days ago) &lt;pprakarsh&gt;<br>* 693f09b - added file3 (5 days ago) &lt;pprakarsh&gt;<br>* e097d8d - changed file2 (9 weeks ago) &lt;pprakarsh&gt;<br>* 6642a0d - added another line in file1 (3 months ago) &lt;pprakarsh&gt;<br>* 155891c - created file1, created subDir1 and created file2 in it (3 months ago) &lt;pprakarsh&gt;</pre><p>A new commit object 206760e has been created in the branch2 branch. We can see a divergence at commit 15357ad . This commit object 15357ad serves as a parent for commit objects — 2a4af24 (in themaster branch) and 206760e (in the branch2 branch). Now let us switch back to the master branch and try to merge branch2 into the the master branch.</p><pre>&gt;&gt;&gt; git checkout master<br>Switched to branch &#39;master&#39;</pre><pre>&gt;&gt;&gt; git merge branch2<br>Auto-merging file5<br>CONFLICT (add/add): Merge conflict in file5<br>Automatic merge failed; fix conflicts and then commit the result.</pre><p>On executing the git merge command, we will see that our merge has failed. This is because git cannot resolve the conflict on its own. We will have to manually resolve the conflict.</p><p>Run git status , this will provide the name of the files which have conflicts.</p><pre>&gt;&gt;&gt; git status<br>On branch master<br>You have unmerged paths.<br>  (fix conflicts and run &quot;git commit&quot;)<br>  (use &quot;git merge --abort&quot; to abort the merge)</pre><pre>Unmerged paths:<br>  (use &quot;git add &lt;file&gt;...&quot; to mark resolution)</pre><pre>both added:      file5</pre><pre>no changes added to commit (use &quot;git add&quot; and/or &quot;git commit -a&quot;)</pre><p>We will see that file5 is causing a merge conflict. Now open file5 using the editor of your choice. You will see something like this. Everything below &lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD and above ======= belongs to the latest commit in the current working branch (master in this case) while everything between ======= and &gt;&gt;&gt;&gt;&gt;&gt;&gt; branch2 belongs to the latest commit of the other branch (branch2 in this case).</p><pre>&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD<br>Today is Tuesday<br>=======<br>Today is Wednesday<br>&gt;&gt;&gt;&gt;&gt;&gt;&gt; branch2</pre><p>Edit the file and resolve the conflict. Now your file would look something like this.</p><pre>Today is Wednesday</pre><p>Then execute git add filename to mark the file as resolved and finally execute git commit to commit the changes and complete the branch merging process.</p><pre>&gt;&gt;&gt; git add file5<br>&gt;&gt;&gt; git commit<br>[master 4cf897d] Merge branch &#39;branch2&#39;<br>&gt;&gt;&gt; git lg<br>*   4cf897d - (HEAD -&gt; master) Merge branch &#39;branch2&#39; (2 minutes ago) &lt;pprakarsh&gt;<br>|\  <br>| * 206760e - (branch2) file5 added, Wednesday (32 minutes ago) &lt;pprakarsh&gt;<br>* | 2a4af24 - file5 added, Tuesday (49 minutes ago) &lt;pprakarsh&gt;<br>|/  <br>*   15357ad - Merge branch &#39;branch1&#39; (4 hours ago) &lt;pprakarsh&gt;<br>|\  <br>| * 0b17834 - (branch1) file4 changes (4 hours ago) &lt;pprakarsh&gt;<br>| * 9e2566a - created file4 (4 hours ago) &lt;pprakarsh&gt;<br>* | f614763 - file3 changes (4 hours ago) &lt;pprakarsh&gt;<br>|/  <br>* ef154d8 - change in file3 (5 days ago) &lt;pprakarsh&gt;<br>* 693f09b - added file3 (5 days ago) &lt;pprakarsh&gt;<br>* e097d8d - changed file2 (9 weeks ago) &lt;pprakarsh&gt;<br>* 6642a0d - added another line in file1 (3 months ago) &lt;pprakarsh&gt;<br>* 155891c - created file1, created subDir1 and created file2 in it (3 months ago) &lt;pprakarsh&gt;</pre><p>On running git lg we can clearly see that the branch2 has been successfully merged into the master branch. A new commit object has been created (4cf897d) which incorporates the changes of both the branches after the merge conflict resolution.</p><p>This completes our discussion on Git Branching. An important point to consider is that although we have discussed only two branches at a time, however in practice there can be nesting of multiple branches. The idea is that we consider only two branches at a time, merge them into one and then pick another two branches and so on.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*91VkCEoq0cNwmiG-lBc4dA.jpeg" /><figcaption>Notice the nesting of maser branch, Branch-A, Branch-B, and Branch-C</figcaption></figure><h4>Detached HEAD state</h4><p>From our earlier discussion in the beginning of the article about Git references, we understand, branch is nothing but a reference (pointer) to the latest commit in the branch. We also talked about HEAD being a pointer to the current branch (HEAD-&gt;&lt;branchname&gt;). <br>In a detached HEAD state, the HEAD points to some other commit (not the branch). This enables developers to move the HEAD to a different commit object and re-create the state of working directory same as that of another commit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*bz7qMX0XFXTlxeu64nTL6Q.png" /><figcaption>Detached HEAD state, HEAD pointing to Commit-2, instead of branch1 (HEAD-&gt;branch1).</figcaption></figure><p>You can execute this command to move HEAD to another commit object.</p><p>git checkout &lt;commit-hash&gt;</p><p>You can further make changes to your repository in detached HEAD state by branching to a new branch, allowing you to play around with the commit snapshots that you have saved.</p><p>This concludes our discussion of Git branching. Hope you found this article helpful! Let me know your thoughts in the comments below. Next article, we will learn in depth about tracking changes in Git repository.</p><p>The first article in this series (on Git Internals) can be found <a href="https://medium.com/geekculture/a-beginners-guide-to-git-and-git-internals-1e7dcd89d65e">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1cc894b9fd03" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/git-branching-made-easy-1cc894b9fd03">Git Branching Made Easy</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Beginner’s Guide to Git and Git Internals]]></title>
            <link>https://medium.com/geekculture/a-beginners-guide-to-git-and-git-internals-1e7dcd89d65e?source=rss-3217848e87ef------2</link>
            <guid isPermaLink="false">https://medium.com/p/1e7dcd89d65e</guid>
            <category><![CDATA[git-internals]]></category>
            <category><![CDATA[git-deep]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[git]]></category>
            <dc:creator><![CDATA[Prakarsh Parashar]]></dc:creator>
            <pubDate>Sun, 04 Apr 2021 09:22:24 GMT</pubDate>
            <atom:updated>2021-04-21T03:14:03.739Z</atom:updated>
            <content:encoded><![CDATA[<p>I have struggled a lot with Git! 8 out of 10 times when I would push my code, the project would break. Then I would spend the entire night trying to make my code work again but to no avail. In the morning, half-asleep, Finally, I would seek help from my friend, who is a Git God. To my amazement, he would fix the Project in just a couple of minutes. He would always say Git is simple once you understand its underlying internals. <br>Frustrated with my struggles with Git, one weekend, I decided that I would spend the entire weekend studying Git Internals. Since then Git has never seemed so simple yet beautiful. Now, every problem in Git, which would earlier be a nightmare to solve, seems so obvious and trivial.</p><p>Frankly speaking, for beginners Git is not an easy tool to use. Although it has a beautiful design on the inside, for a newbie understanding even basic functionalities of Git is a tough nut to crack. Through this article, I want to unwrap the Git black box and dive into the Git internals. In this series of articles, we’ll learn about Git’s Internal Structure. <em>This is the first article in a series of 4 articles. (second article on Git branching has been published. You can find it </em><a href="https://medium.com/geekculture/git-branching-made-easy-1cc894b9fd03"><em>here</em></a><em>)</em></p><p>The article is written for complete beginners. However, it would also be useful to those who have some familiarity with Git but are not confident with their Git skills or those who want to demystify the underlying internals of Git. The only prerequisite is some familiarity with commands like cd, ls, cat, etc.</p><p>Before beginning our Introduction of Git, let us first understand, What a Version Control System is?</p><h4>What is a Version Control System?</h4><p>Simply speaking, a Version Control System (VCS) is a tool that helps to record and manage changes to our Project.</p><p>For a better understanding, let us consider an example.</p><p>Imagine a world without Version Control Systems. Suppose you are working on a project. You are saving your project files in google drive (or your local computer). You start your project on Day-1 and you have completed a part of your project (say 50%) by Day-7. After 2 days (Day-9) you realize that the project has now become more complicated and you want to go back to the state of the project on Day-7. Sadly! There is no way you can revert to the previous version of your project because all project files on your computer have been overwritten by the changes you had made. The only way could have been, had you saved a copy of your project (as on Day-7) somewhere. But saving a copy of your project every time you complete a part of the project is cumbersome.</p><p>To avoid such problems we use Version Control systems. Version control systems save a snapshot of your Project repository (Project folder) on your local computer from time to time. Every time you complete a part of your project you issue a command to your VCS, saying — <em>Take a snapshot of my current Project Repository (snapshot of all files and folders in your current Project repository or Project directory) and save it in your history. </em>Now if you want to come back to a previous state (snapshot which you had saved earlier) of your project, you can easily do so by issuing another command to your VCS, saying — <em>Revert the state of my project to this particular snapshot</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/1*LtsSZ8Mil8ES5zsxtmJJsg.png" /><figcaption>You can revert to any snapshot in your Project’s VCS’s history</figcaption></figure><p>The name is Version Control because it saves <em>versions</em> (snapshots) of your Project. The importance of Version Control systems increases as the size and complexity of the project increases. As we’ll see, there are many additional features a VCS provides. They help you manage your projects in a much better way. <br>Git is a Version Control system. It is free and open-source and also one of the most popular VCS. Some other Version Control Systems are Mercurial, SVN, etc.</p><h4>Git — Introduction</h4><p>So now we understand what Git is. But before beginning with Git’s Data model, I would like to address another point of confusion for complete beginners. Apart from Git and Version Control Systems, many must have also heard about GitHub. So, a natural question is — What is GitHub? How is it different from Git? (Those who’ve not heard about GitHub, you can choose to ignore this part of the article) You can think of GitHub as a Google Drive for Projects. It stores your projects on the cloud. Not only your project but also all the project snapshots that Git has been saving along the way. It is helpful if you want to access your project on a different machine, or you accidentally lose all the data on your computer. And, GitHub is not only meant to serve as a backup for your Projects, but the USP of GitHub is also that it makes collaboration on a project so much easier. <br>A few beginners, just about to begin learning Git have a misconception that GitHub and Git are intermingled, that for using Git, you need to use GitHub as well. This is not true. If you don’t want to save your project on a cloud and your project does not require collaboration with others, GitHub is not required. Git will manage your project on your local computer without the need for GitHub. Also note that apart from GitHub there are other code hosting platforms as well.<br>Now let’s begin with Git’s Data Model.</p><h4>Git — Data Model</h4><p>So, time to get started with Git’s internal design. Internally git stores the content of snapshots in different types of objects. Let us take a closer look at these objects.</p><p><strong>Commit object<br></strong>We talked about a snapshot of our Project repository. A commit object denotes a snapshot of our Project. It helps us view a particular snapshot of our Project repository (present in the snapshot history that our VCS has saved). A commit object contains a pointer (reference) to its parent commit object (i.e. the snapshot saved just before). This forms a linked list of commit objects (A Linked list is a list of objects, an object is linked to the next object via a pointer. The next object, in turn, points to another object and so on). The only exception is the initial commit which does not have a parent.<br><em>NOTE</em>: In the article on branches, we’ll discover that a commit object can also have 2 or more parents and a parent commit object can also have 2 or more children commit objects. <br>A commit object also contains other useful information like the author of the commit, the commit message, etc. We’ll cover those later in the article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/747/1*Y4wWaizcN3d5NiMzh8jxTQ.jpeg" /><figcaption>Commit Object-3 is the latest commit while Commit object-1 is the oldest commit ( which does not have a parent)</figcaption></figure><p><em>So how does this commit object help us view a snapshot of our Project? <br></em>Ans — It contains a reference to a tree object.</p><p><strong>Tree object<br></strong>A tree object is used to represent directories (folders). If the Project is contained in a directory named — <em>MyProject</em>, then the commit object contains a reference to the tree object representing the directory <em>MyProject</em>. We’ll call this Project directory as the root directory. Our root Project directory is also stored as a tree object. The commit object contains a reference to this tree object. A Tree object contains information about the directory that it represents, i.e. all files and sub-directories present in the directory. Sub-directories will also be represented as Tree objects. So a Tree object can contain a reference to another Tree object.</p><p>Files pointed by Tree objects are called blob objects in git.</p><p><strong>Blob object</strong></p><p>A blob object represents a file in git. If a file does not change from commit-1 to commit-2 (say) then the blob object also does not change. <br>A Blob object contains the content of the file.</p><p>Now let us take a closer look at these objects. We’ll get our hands dirty and start executing some commands. First, install git on your system. You can refer to this <a href="https://git-scm.com/book/en/v2/Getting-Started-Installing-Git">article</a> for installation steps. I would advise you to follow along by executing the same commands in your system.</p><p>NOTE: To avoid any confusion, &gt;&gt;&gt; is my command prompt. 😅</p><p>Firstly, create an empty directory gitDemo which will be our root Project directory.</p><pre>&gt;&gt;&gt; mkdir gitDemo<br>&gt;&gt;&gt; cd gitDemo</pre><p>Next, we’ll initialize this repository as a <em>git repository. </em>git init initializes the repository as a <em>git repository </em>and creates a .git folder in our root project directory.</p><pre>&gt;&gt;&gt; git init<br>Initialized empty Git repository in /home/prakarsh/MediumGitArticle1/gitDemo/.git/</pre><p>Listing the content of our <em>gitDemo</em> directory, we will notice a <em>.git </em>directory. The <em>.git</em> directory is responsible for storing all the important information to manage our project using Git.</p><pre>&gt;&gt;&gt; ls -a<br>.  ..  .git</pre><p>Let‘s look at the structure of the <em>.git </em>directory using tree command.</p><pre>&gt;&gt;&gt; tree .git<br>.git<br>├── branches<br>├── config<br>├── description<br>├── HEAD<br>├── hooks<br>│   ├── applypatch-msg.sample<br>│   ├── commit-msg.sample<br>│   ├── fsmonitor-watchman.sample<br>│   ├── post-update.sample<br>│   ├── pre-applypatch.sample<br>│   ├── pre-commit.sample<br>│   ├── prepare-commit-msg.sample<br>│   ├── pre-push.sample<br>│   ├── pre-rebase.sample<br>│   ├── pre-receive.sample<br>│   └── update.sample<br>├── info<br>│   └── exclude<br>├── objects<br>│   ├── info<br>│   └── pack<br>└── refs<br>    ├── heads<br>    └── tags</pre><p>We see that there are many sub-directories like branches, config, HEAD, info, refs, objects, etc. In this article, we’ll focus on the <em>objects</em> directory.</p><pre>&gt;&gt;&gt; tree .git/objects<br>.git/objects<br>├── info<br>└── pack</pre><p>We can see that there are only two items in the objects directory. This is because as of now, we’ve not created an object. First, let us create some objects. We’ll create some files and folders in our <em>gitDemo</em> directory and save our first snapshot.</p><pre>&gt;&gt;&gt; echo &quot;I am file1&quot; &gt; file1<br>&gt;&gt;&gt; ls<br>file1<br>&gt;&gt;&gt; mkdir subDir1<br>&gt;&gt;&gt; cd subDir1<br>&gt;&gt;&gt; echo &quot;I am file2&quot; &gt; file2<br>&gt;&gt;&gt; ls<br>file2<br>&gt;&gt;&gt; cd ..</pre><p>Now, execute the command <em>tree</em> in your <em>gitDemo </em>directory to see the structure of your repository.</p><pre>&gt;&gt;&gt; tree<br>.<br>├── file1<br>└── subDir1<br>    └── file2</pre><pre>1 directory, 2 files</pre><p>We’ve created some files and folders in our project repository. Time to save the first snapshot of our repository 😎. Execute the following commands to save the first snapshot of your repository.</p><pre>&gt;&gt;&gt; git add file1 subDir1/<br>&gt;&gt;&gt; git commit -m &quot;created file1, created subDir1 and created file2 in it&quot;<br>[master (root-commit) 155891c] created file1, created subDir1 and created file2 in it<br> 2 files changed, 2 insertions(+)<br> create mode 100644 file1<br> create mode 100644 subDir1/file2</pre><p>Hurray! We’ve saved the first snapshot of our repository. The first commit object has been created. But you must be wondering, what does this <em>git add</em> and <em>git commit</em> commands do?</p><p><strong>git add<br></strong>git add filename1 filename2 filename3 is used to stage the changes in your repository for commit (snapshot). It is like a preliminary step before the snapshot. Note that, if you’ve made changes to some file but you’ve not staged the file using <em>git add, </em>the file will not be considered for commit when you run <em>git commit.</em> i.e. the file will not be included in the snapshot. I am intentionally omitting a few details about the staging part and staging area to keep the focus on the Git Data Model. We’ll cover those details in another article.</p><p><strong>git commit<br></strong>git commit -m “commit message” saves the snapshot of the current state of your repository in your commit history. The commit message is used to describe particular changes in this snapshot of the repository. It is important to provide a good commit message. If you want to look at a previous state of your Project, which has thousands of commits, it is a lot easier to find the commit when you have a good commit message.</p><p>Let us look at all the objects created after the commit.</p><p>git log shows us the information about all commits in our Project history. Since we have only one commit now, it will only output the information about a single commit. Execute the command git logand observe the output. We can see that the first line contains a 40 characters long alphanumeric string. It is the SHA-1 hash of the content of the commit object. If you are not familiar with SHA-1 hash, do not worry, just think of this alphanumeric string as a unique identifier for the commit object. All types of objects — commit, tree, blob are uniquely identified by this SHA-1 hash. <br>The second line provides information about the author of the commit (i.e. the one who made the changes in the repository). When you are collaborating with others on a project, sometimes it can be useful to know the author of the commit. The third line is the date and time of the commit. And the last line is the commit message which was added by the author at the time of commit.</p><pre>&gt;&gt;&gt; git log<br>commit 155891ca19d94f67159a992c77923818b57d74a5 (HEAD -&gt; master)<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Sun May 3 10:41:06 2020 +0530</pre><pre>created file1, created subDir1 and created file2 in it</pre><p>Execute this command git cat-file commit &lt;commit-hash&gt; to see the content of the commit object. You can copy the commit hash from the output of the git log command. In the command argument, you need not write the entire commit hash, even a few characters are enough to identify the commit almost all the time. Execute the command and observe the output. Notice that the first line in the output contains the SHA-1 hash of a tree object. This tree object represents the root Project directory.</p><blockquote>Commit hash is a function of the content of the commit object. In this case author, time, date etc are different. So the commit hash will vary even when the content of the snapshot of the repository is the same. (You will notice that my commit hash and your commit hash are different even though the content of the repository is exactly same)</blockquote><pre>&gt;&gt;&gt; git cat-file commit 155891c<br>tree 4587af994fe34fed465b64b6ce8b8adb07a17aaf<br>author pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt; 1588482666 +0530<br>committer pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt; 1588482666 +0530</pre><pre>created file1, created subDir1 and created file2 in it</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/231/1*Aan5ldgWP5e-jhw72P0foQ.jpeg" /><figcaption>Commit object points to the Tree object corresponding to the root Project directory</figcaption></figure><p>Execute the command git ls-tree &lt;tree object hash&gt; to take a peek at this tree object. Again copy the tree object hash from the output of the git cat-file commit &lt;commit-hash&gt; command executed above. We observe that the tree object contains a reference to a blob object (file1) and a reference to another tree object (subDir1).</p><pre>&gt;&gt;&gt; git ls-tree 4587af994fe34fed465b64b6ce8b8adb07a17aaF<br>100644 blob a83c85bfc5f4f056b0932eb2e5cf767f167a67f2 file1<br>040000 tree 8993f258581e2772c7a4c27ad07498caed668c57 subDir1</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/438/1*cBkeiMOwIeF7OWl_rjpcHg.jpeg" /></figure><p>Now, let’s take a look at the blob object(file1) and the tree object referenced by the root Tree object(subDir1). Executing the command git cat-file blob &lt;blob-hash&gt; provides us the content of the blob object i.e. the content of <em>file1</em>.<br>And executing the command git ls-tree &lt;tree hash&gt; shows that the Tree object (hash 8993f2 in the picture) contains a reference to another blob object (file2).</p><pre>&gt;&gt;&gt; git cat-file blob a83c85bfc5f4f056b0932eb2e5cf767f167a67f2<br>I am file1<br>&gt;&gt;&gt; git ls-tree 8993f258581e2772c7a4c27ad07498caed668c57<br>100644 blob 4e01f27ee757413022834dcb1bbf4c66fb01f05c file2</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/444/1*9msUZF8KKjhCRu799b4TUg.jpeg" /></figure><p>Finally, before moving on to the second commit, we’ll have a look at the blob object (hash 4e01f27 in the picture) referenced by the tree object (which represents subDir). Execute the command git cat-file blob &lt;blob-hash&gt; . The output is the content of <em>subDir/file2.</em></p><pre>&gt;&gt;&gt; git cat-file blob 4e01f27ee757413022834dcb1bbf4c66fb01f05c<br>I am file2</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/448/1*IbnVy2zd-8u_7U5wGj6xhw.jpeg" /><figcaption>Pictorial representation of all objects after the first commit</figcaption></figure><p>Let us go back and look at our <em>.git/objects </em>directory again. We see that we have some new items listed. These denote the objects which have been created — the commit objects, the tree objects, the blob objects. Match the object hash in the picture above with the output below. Content of all objects is stored here, in <em>.git/objects.</em></p><pre>&gt;&gt;&gt; tree .git/objects<br>.git/objects<br>├── 15<br>│   └── 5891ca19d94f67159a992c77923818b57d74a5<br>├── 45<br>│   └── 87af994fe34fed465b64b6ce8b8adb07a17aaf<br>├── 4e<br>│   └── 01f27ee757413022834dcb1bbf4c66fb01f05c<br>├── 89<br>│   └── 93f258581e2772c7a4c27ad07498caed668c57<br>├── a8<br>│   └── 3c85bfc5f4f056b0932eb2e5cf767f167a67f2<br>├── info<br>└── pack</pre><p>Now let us make some changes in our repository and commit again. Execute the commands below to make some changes in file1 and commit those changes.</p><pre>&gt;&gt;&gt; echo &quot;Adding another line in file1&quot; &gt;&gt; file1<br>&gt;&gt;&gt; git add file1<br>&gt;&gt;&gt; git commit -m &quot;added another line in file1&quot;<br>[master 6642a0d] added another line in file1<br> 1 file changed, 1 insertion(+)</pre><p>Execute git log to see the new commit added in the log. The latest commit is at the top.</p><pre>&gt;&gt;&gt; git log<br>commit 6642a0de776b52bbe993555a4bf14aed060afea2 (HEAD -&gt; master)<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Sun May 3 22:14:12 2020 +0530</pre><pre>added another line in file1</pre><pre>commit 155891ca19d94f67159a992c77923818b57d74a5<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Sun May 3 10:41:06 2020 +0530</pre><p><em>*Ignore </em>HEAD -&gt; master <em>in the output above. We’ll cover this in the branching article.</em></p><p>Execute git cat-file commit &lt;commit-hash&gt; to see the content of the newly created commit object. We will observe that in the first line, the tree object it points to has a different hash than the previous commit’s hash, due to the changes in <em>file1</em>.<em> </em>Thus commit-2 points to a different tree object. <br>Remember at the beginning of the article we talked about <a href="#59f8">the parent of a commit object</a>. We can see that a <em>parent</em> data member has also been added to the newly created commit object. The hash corresponding to this parent data member is equal to that of the previous commit object’s hash, thus it verifies that this <em>parent</em> data member is a reference to the previous commit object.</p><pre>&gt;&gt;&gt; git cat-file commit 6642a0d<br>tree 60f96fcee961146cdcf8c4cfd0faf5a0e821cebf<br>parent 155891ca19d94f67159a992c77923818b57d74a5<br>author pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt; 1588524252 +0530<br>committer pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt; 1588524252 +0530</pre><pre>added another line in file1</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/713/1*6B0UrRQGEQT1fEgbGNn-iw.jpeg" /><figcaption>Commit-2 has been created. Notice the parent data member. It points to its parent (i.e. commit-1). Also, the new commit (i.e. commit-2) points to a different Tree object.</figcaption></figure><p>Now, we’ll explore the tree object (60f96fce). The newly created commit object contains a pointer to this tree object. Execute the command git ls-tree &lt;tree hash&gt;<br>The tree object (60f96fce) contains a pointer to a blob object and a pointer to another tree object (hash 8993f2585) corresponding to <em>subDir</em>. Since the <em>subDir </em>did not change between the two commits, the content of this tree object (8993f2585) also did not change. Since <em>file1</em> was changed, we have a new blob object corresponding to <em>file1. </em>We can look at the content of the new blob object using this command git cat-file blob &lt;blob hash&gt; . <em>blob hash </em>will be available to us from the output of the git ls-tree &lt;tree hash&gt; command executed just before.</p><pre>&gt;&gt;&gt; git ls-tree 60f96fcee961146cdcf8c4cfd0faf5a0e821cebf<br>100644 blob 668be5e5d8709844c9e14efb34565cdbc475b57f file1<br>040000 tree 8993f258581e2772c7a4c27ad07498caed668c57 subDir1</pre><pre>&gt;&gt;&gt; git cat-file blob 668be5e5d8709844c9e14efb34565cdbc475b57f<br>I am file1<br>Adding another line in file1</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/673/1*g2RcGESmjRofi3MaxiB-9w.jpeg" /><figcaption>Tree object corresponding to subDir1 remains the same, blob object for file1 changes.</figcaption></figure><p>Time for commit-3. We’ll make some changes in <em>subDir/file2. </em>Execute the following commands to make changes in <em>subDir/file2</em> and commit those changes.</p><pre>&gt;&gt;&gt; echo &quot;I have been changed&quot; &gt;&gt; subDir1/file2<br>&gt;&gt;&gt; git add subDir1/file2<br>&gt;&gt;&gt; git commit -m &quot;changed file2&quot;<br>[master e097d8d] changed file2<br> 1 file changed, 1 insertion(+)</pre><p>To see the new commit. Execute git log. The new commit object can be found at the top.</p><pre>&gt;&gt;&gt; git log<br>commit e097d8da357fe97c36c7178f0b770ae44b1f1f3c (HEAD -&gt; master)<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Thu May 21 10:24:55 2020 +0530</pre><pre>changed file2</pre><pre>commit 6642a0de776b52bbe993555a4bf14aed060afea2<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Sun May 3 22:14:12 2020 +0530</pre><pre>added another line in file1</pre><pre>commit 155891ca19d94f67159a992c77923818b57d74a5<br>Author: pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt;<br>Date:   Sun May 3 10:41:06 2020 +0530</pre><pre>created file1, created subDir1 and created file2 in it</pre><p>To see the content of the newly created commit object. Execute the command git cat-file &lt;commit-hash&gt; .</p><pre>&gt;&gt;&gt; git cat-file commit e097d8d<br>tree e1564dac72800b9ee123f05e882543c4b7db56d1<br>parent 6642a0de776b52bbe993555a4bf14aed060afea2<br>author pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt; 1590036895 +0530<br>committer pprakarsh &lt;<a href="/service/mailto:prakarshparashar@gmail.com">prakarshparashar@gmail.com</a>&gt; 1590036895 +0530</pre><pre>changed file2</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*plapuDLB-uyRbhx1GZKLjA.jpeg" /><figcaption>We see that commit-3 points to a different tree object (e1564dac)</figcaption></figure><p>Now, we’ll look into the newly created tree object (e1564dac) and other objects pointed by this tree object (e1564dac). This tree object points to the blob object (668be5e) and a new tree object (ba6b84ec) corresponding to <em>subDir. </em>Why a new<em> </em>tree object for <em>subDir?</em> Because the content of <em>subDir </em>has changed. The blob object (668be5e) corresponding to <em>file1</em> does not change, because <em>file1 </em>does not change.</p><p>Execute the following commands to see the contents of these newly created objects.</p><pre>&gt;&gt;&gt; git ls-tree e1564dac72800b9ee123f05e882543c4b7db56d1<br>100644 blob 668be5e5d8709844c9e14efb34565cdbc475b57f file1<br>040000 tree ba6b84ec235a7cc5eeefba17dd744d270e2b258d subDir1</pre><pre>&gt;&gt;&gt; git ls-tree ba6b84ec235a7cc5eeefba17dd744d270e2b258d<br>100644 blob 38ec3b70732a6b5e219fea0edbf48b26c7bd2234 file2</pre><pre>&gt;&gt;&gt; git cat-file blob 38ec3b70732a6b5e219fea0edbf48b26c7bd2234<br>I am file2<br>I have been changed</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*zIzvYjKwz40LJ9hjMyi_Mw.jpeg" /><figcaption>Flow chart representation of git objects after Commit-3</figcaption></figure><p>I hope that now we have a clear understanding of Git’s Data model. We understand how Git stores different types of objects. In the next article, we’ll learn about one of the most powerful features of Git - branching. The second article can be found <a href="https://medium.com/geekculture/git-branching-made-easy-1cc894b9fd03">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1e7dcd89d65e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/a-beginners-guide-to-git-and-git-internals-1e7dcd89d65e">A Beginner’s Guide to Git and Git Internals</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>