Notes on Git
Tags: git, beginners
Published: 2019-12-20 11:48:25

So in this post I will try and discuss the basics of git branch and git merge.
 

Since my development environment is ubuntu so setup is in ubuntu but for windows and mac i think you can install a [desktop client](https://git-scm.com/downloads) that also installs it for cli. 

But for ubuntu you can start by installing git

**sudo apt-get install git**

So let's start by making a repo

make a directory

**mkdir pydatastruct && cd "$_"**

Now initializing the git repo with

**git init**

![Initializing a repo](https://thepracticaldev.s3.amazonaws.com/i/jjyx7lhp18lxx82mppd3.png)

Now let's run a git status 

**git status**

![Git Status](https://thepracticaldev.s3.amazonaws.com/i/b2pohx8elh83nzqf2d0j.png)

We are on the master branch and right now we have no files so let's add a file and make a first commit to the master branch to the master branch

**git add .**
**git commit -m "First Commit"**

![First Commit](https://thepracticaldev.s3.amazonaws.com/i/a1uonk79l84576myg2p4.png)

Checking the git log to check the commit

**git log --decorate --oneline**

![Git Log](https://thepracticaldev.s3.amazonaws.com/i/ag4ybuhl1s3h6a32wi3z.png)

Now let's make branches. Since I am going to add a linkedlist, a stack and a queue I will make three branches. The most important thing about making a branch is that the branch points to the commit it is made at so all three branches will point at the master branch right now.

**git branch linkedlist**

**git branch stack**

**git branch queue**

![Git Branch](https://thepracticaldev.s3.amazonaws.com/i/p4rj3o0gy9c943yx04tj.png)

Now let's checkout to the branch add a file make a commit for all the three branches. 

**git checkout linkedlist**
**touch linkedlist.py**
**git add .**
**git commit -m "Added linkedlist**

![Added Linkedlist](https://thepracticaldev.s3.amazonaws.com/i/upix2bxt2twnmbxil7ni.png)

Now checking the git log for this branch

![Git log](https://thepracticaldev.s3.amazonaws.com/i/c87i5xk2ymphln6g3ycn.png)

As you can see branches master stack and queue are at the same commit whereas linkedlist is not at a different commit. 

Similarly for branches stack and queue and you will have three branches

![First commit to stack](https://thepracticaldev.s3.amazonaws.com/i/kzqodkq9743ti2lzlh5t.png)

![First commit to queue](https://thepracticaldev.s3.amazonaws.com/i/mftsso1q3eo1rdrte1t9.png)

Now you can make individual changes on the branches and implement linkedlist, stack and queue through the same process of making commits. 

![Linkedlist after changes](https://thepracticaldev.s3.amazonaws.com/i/p3eqeo1ymqnjwmia8asr.png)
![Stack after changes](https://thepracticaldev.s3.amazonaws.com/i/yblbq6uov0e09aj40lxd.png)
![queue after changes](https://thepracticaldev.s3.amazonaws.com/i/f4d8083gg66or13tbh3i.png)


As all the branches are two commits ahead of master it's time to merge them to master. Right now since I haven't edited main.py there is no merge conflict. So just check out and run git merge

**git checkout master**
**git merge linkedlist**
**git merge stack**
**git merge queue**

![git merge linkedlist](https://thepracticaldev.s3.amazonaws.com/i/nwgonc7xmwkuhl74go2d.png)
![git merge stack](https://thepracticaldev.s3.amazonaws.com/i/ieptzgg6bltdvtnrpufe.png)
![git merge queue](https://thepracticaldev.s3.amazonaws.com/i/17c8kqek64t01ve2ruz3.png)


Now when you check the git log on master branch you can see all the commits at one place. 

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/i4en56yx4afg3kuc9p12.png)

Next time I will look into the details of recursive merge and add a remote repository.