Git Local Repository Branch And Rebase

Git local repository branch and rebase

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed how to branch and merge in git repository.

https://cloudaffaire.com/git-local-repository-branch-and-merge/

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

Git local repository branch and rebase:

Demo:

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

#add your user name and email to git config
git config --local user.name "Debjeet" && \
git config --local user.email "debjeettoni@gmail.com"

#create a alias for visual presentation of logs
alias graph="git log --all --decorate --oneline --graph"

#create a file file1 and commit
echo "hello master" > file1
git add file1 && git commit -m "commit A | file1 added"

#create a file file2 and commit
echo "hello master" >> file2
git add file2 && git commit -m "commit B | file2 added"

#create a branch
git branch slave

#create a file file3 and commit
echo "hello master" > file3
git add file3 && git commit -m "commit C | file3 added"

#switch to branch slave
git checkout slave

#create a file file4 and commit

echo "hello slave" > file4
git add file4 && git commit -m "commit D | file4 added"

#create a file file5 and commit
echo "hello slave" > file5
git add file5 && git commit -m "commit E | file5 added"

#check graph
graph

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

#merge master and slave
git checkout master && git merge slave

#delete slave branch
git branch -d slave

#check graph
graph

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

#cleanup
cd .. && rm -rf mygit

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

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

#create a file file1 and commit
echo "hello master" > file1
git add file1 && git commit -m "commit A | file1 added"

#create a file file2 and commit
echo "hello master" > file2
git add file2 && git commit -m "commit B | file2 added"

#create a branch
git branch slave

#create a file file3 and commit
echo "hello master" > file3
git add file3 && git commit -m "commit C | file3 added"

#switch to branch slave
git checkout slave

#create a file file4 and commit
echo "hello slave" > file4
git add file4 && git commit -m "commit D | file4 added"

#create a file file5 and commit
echo "hello slave" > file5
git add file5 && git commit -m "commit E | file5 added"

#check graph
graph

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

#rebase slave to master (Never rebase a public branch)
git rebase master

#check graph (observe commit D and E hash has been changed)
graph

# A------>B------>C (master)
#         \
#          D*------>E* (slave)

#merge slave to master
git checkout master && git merge slave

#delete slave branch
git branch -d slave

#check graph
graph

# A------>B------>C------>D------>E (master)

#cleanup
cd .. && rm -rf mygit

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

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

https://git-scm.com/doc

 

Leave a Reply

Close Menu