Git Branching And Merging
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/undo-changes-in-git-local-repository/
In this blog post, we will discuss how to branch and merge in git repository.
Git Branching And Merging:
Branching: Nearly every VCS has some form of branching support. Branching means you diverge from the main line of development and continue to do work without messing with that main line. In many VCS tools, this is a somewhat expensive process, often requiring you to create a new copy of your source code directory, which can take a long time for large projects.
Git supports branching and git branches is incredibly lightweight, making branching operations nearly instantaneous, and switching back and forth between branches generally just as fast. Unlike many other VCSs, Git encourages workflows that branch and merge often, even multiple times in a day. Understanding and mastering this feature gives you a powerful and unique tool and can entirely change the way that you develop.
Merging: Merging is the reverse of branching and in merging, branches are merged with the main line. Merging can occur in two way’s first forward merge and three way merge. Fast forward merging happens when two branches are connected directly and if there is no liner path between the two branches, three way merge occurs. Data needs to be consistent in both branches in order to merge happen and if there is any inconsistency, merge conflict will appear.
Next, we are going to demonstrate git branching and merging with a demo.
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
##------------------------------- ## Git branching and merging ## ##------------------------------- ## 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 ## ########################### ## Check what will be merged (will merge slave2 with master) 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
Hello Debjeet ,
Nice article to understand GITHUB from the scratch.