Git branching and rebasing
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-branching-and-merging/
In this blog post, we will discuss branching and rebasing in git.
Git branching and rebasing:
What is git rebase?
In Git, there are two main ways to integrate changes from one branch into another, merge and the rebase. We have already covered git merge in a previous blog post and in this blog post we will cover git rebase. In case of git merge the entire history of the feature branch will be contained in the master branch. Now suppose you don’t want all the commit message of a feature branch into your master branch. You can use git rebase in this case.
Demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
##-------------------------------- ## Git Branching And Reabsing ## ##-------------------------------- ##################### ## Git Branching ## ##################### ## 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 #################### ## Git Rebasing ## #################### ## 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