DEV Community

Adeel Hussain
Adeel Hussain

Posted on

Notes on Git

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 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

Now let's run a git status

git status

Git Status

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

Checking the git log to check the commit

git log --decorate --oneline

Git Log

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

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

Now checking the git log for this branch

Git log

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

First commit to queue

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
Stack after changes
queue after changes

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
git merge stack
git merge queue

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

Alt Text

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

Top comments (0)