Git Local Repository Branch And Merge

Git local repository branch and merge

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed different rollback scenarios in a git repository through a demo.

https://cloudaffaire.com/git-local-repository-rollback/

In this blog post, we will discuss how to branch and merge in git repository.

Git local repository branch and merge:

Demo:

#create new directory and initialize git and get inside the directory
git init mygit && cd mygit

#create a file file1
echo "hello from branch master" > file1

#before commit add your user name and email to git config
git config --local user.name "Debjeet" && \
git config --local user.email "debjeet@cloudaffaire.com"

#commit file1
git add file1 && \
git commit -m "commit: A"

#list git branches > we are in default master branch
git branch

#get the logs
git log --all --decorate --oneline --graph

#create a alias for above command
alias graph="git log --all --decorate --oneline --graph"

# A (HEAD -> master)

#edit the file1 again under master branch
echo "hello again! from branch master" >> file1

#commit file1
git add file1 && \
git commit -m "commit: B"

#check graph
graph

# A------>B (HEAD -> master)

#create a branch named slave1
git branch slave1

#create another branch named slave2
git branch slave2

#check all the branch for this git repo (HEAD pointer is pointing to master branch)
git branch

#check the graph (HEAD pointer is pointing to master branch)
graph

# A------>B (HEAD -> master, slave1, slave2)

#switch to slave1 branch
git checkout slave1

#check the graph (HEAD pointer is pointing to slave1 branch)
graph

# A------>B (HEAD -> slave1, slave2, master)

#check the branch (we are now in slave1 branch)
git branch

#check git status
git status

#edit file1 under slave1 branch
echo "hello from branch slave1" >> file1

#commit file1 under slave1 branch
git add file1 && \
git commit -m "commit: C"

#check the graph (HEAD pointer is pointing to slave1 branch)
graph

# A------>B (slave2, master)
#          \
#           C (HEAD -> slave1)

#switch to slave2 branch
git checkout slave2

#check content of file1 ("hello from branch slave1" not present in slave2 branch)
cat file1

#edit file1 and add fav slave2
echo "hello from branch slave2" >> file1

#commit file1 under slave2 branch (note we are both staging and commiting in a single command using -a switch)
git commit -am "commit: D"

#check the graph (HEAD pointer is pointing to slave2 branch)
graph

#           D (HEAD -> slave2)
#          /
# A------>B (master)
#          \
#           C (slave1)

##fast forward mearge (will merge slave2 with master)##
#check what will be merged
git diff master..slave2

#from the master branch we will merge slave2 (note Fast-forward)
git checkout master && \
git merge slave2

#check the graph (master has catched up with slave2 branch)
graph

# A------>B------> D (HEAD -> master, slave2)
#          \
#           C (slave1)

#get file1 content (due to merge with slave2, master branch has slave2 information)
cat file1

#check with branch are merged with which one (slave2 is merged with master)
git branch --merged

#now branch slave2 is merged with branch master it can be deleted
git branch -d slave2

#check the graph (slave2 branch has been deleted)
graph

# A------>B------> D (HEAD -> master)
#          \
#           C (slave1)

#if we try to delete slave1 branch, it will not get deleted since its not yet merged with master
git branch -d slave1

##three way merge (will merge slave1 with master)##
#try to merge slave1 with master (conflict will be detected)
git merge slave1

#check the conflict
cat file1

#check git status > You have un-merged paths
git status

#you can abort merge using "git merge --abort" command

#check the conflict
cat file1

#resolve conflict (remove <<<<<<<, =======, >>>>>>> from the file and commit)
vi file1
----------------
hello from branch master
hello again! from branch master
hello from branch slave2
hello from branch slave1
----------------
:wq

#add and commit the changes
git commit -am "commit: E"

#check the graph (HEAD pointer is pointing to master branch and has slave1 branch changes)
graph

# A------>B------>D------>E (HEAD -> master)
#          \             /
#           C (slave1)---

#delete slave1 branch
git branch -d slave1

#check graph (slave1 branch no longer present)
graph

# A------>B------>D------>E (HEAD -> master)
#          \             /
#           C------------

#cleanup
cd .. && rm -rf mygit/

Hope you have enjoyed this article. In the next blog post, we will discuss git rebase.

To get more details on git, please refer below git documentation

https://git-scm.com/doc

 

Leave a Reply

Close Menu