MFH Lab : Git Branching & Merging

Branching can help make your life easier as you work on projects both big and small. Maybe you started making a bug fix on Master but realized it wasn’t so quick and you need to do something else on Master. You can create a branch to commit all of the existing changes that will help you to save your work and you can come back to it later.

If you are on master and have some changes, that could be files you modified or files you added, you can create a branch called Bug Fix by using the command

git branch bugfix

and move to that branch

git checkout bugfix

Next, you’ll need to stage the changes for commit and actually commit them in the bugfix branch

git add --allgit commit -m "Start of changes for bugfix"

Now when you switch back to master by typing git checkout master you will not see any of the new changes you made. Those are safely tucked away in the bugfix Branch for you to get back to when you were ready.

You may think of something like the picture below when you hear jumping around branches or moving from branch to branch

Git branch is very lightweight so using branches is quite easy. Let’s review the basic syntax

  • To create a new branch git branch <branch name>
  • To see the list your branches and show you which one you are currently working on, simply run git branch
  • You can create and switch to a new branch same time by running the command git branch -b <new branch name>
  • When you want to switch to a different branch, run git checkout <branch name>
  • If you don’t like the name of a branch no problem you can rename it to rename a branch, use git branch -m <old name> <new name>

If you’re not careful you could find yourself surrounded by branches on your workstation and you may not like to get surrounded by lots of branches. Sometimes it is required to delete branches. Deleting branches is simple and painless and does not require you to climb a tree or use a chainsaw, you’ll use

git checkout -d <branch name>

Suppose you try to delete a branch that has committed and has not been merged to another branch, you’ll get an error message. This is a way that Git branching helps you avoid accidentally deleting code

If you’re still sure you want to delete the branch anyway you can pass the D flag to force the branch to delete

git branch -D <branch name>

This is how Git makes it easy for you to switch from task to task. Using branches all of your work is encapsulated in each branch and you won’t get things mixed up between different tasks that you’re working on. The file system is automatically updated so while it may appear like things are disappearing, that’s just Git doing its job.

Now, Let’s learn about merging which helps all of the work you do and branches come together.

Merging in Git is easy. If you figured out a solution on a branch and ask yourself how do I get that back into Master, the answer is merging. Git uses the term merging to combine branch data and history together. If the word merging makes you think of something like this, don’t worry

There are two different algorithms in Git to perform a merge, one is called fast-forward and the other called 3way merge.

If there is no additional work that has occurred on Master then Git can perform a fast-forward merge. Suppose you want to merge one branch into another branch. First switch to the target branch by using git checkout <target branch> then by running the command git merge <source branch> you will merge the source branch to the target branch. For example, you want to merge the bugfix branch with master, you will use git checkout master . Next, to merge the bugfix branch with the master you’ll use git merge bugfix

You may be thinking of seeing the difference between the two branches. You can do it following the way below

git diff <branch1> <branch2>
git diff master bugfix

Git performers a 3-way merge, when there is not a linear path to the source branch git has no choice but to combine. This kind of merge occurs when you create a new branch, work on that branch and at the same time, someone else makes commits on the master branch before you merge the branch.

You can merge branches here using the same command git merge bugfix you used above until the merge conflict comes. Sometimes multiple developers may try to add the same content, for example, while one is adding something another one is editing the same portion of code then the conflict may occur.

Resolve merge conflicts

Suppose, you created a file on the master branch named with the code below

name = "Mike"print("My Name is ", name)

*After every change made, you need to add and commit them

Create a branch and checkout to that branch git branch -b bugfix and edit the file with

age = 25
name = "Mike"print("My Name is ", name, " and I am ", age, " years old")

Checkout to the master branch git checkout master , edit the same code with

age = 23
name = "Mike"print("My Name is ", name, " and I am ", age, " years old")

Now, its time to merge the bugfix branch with the master. So, you will checkout to the master branch and simply run git merge bugfix to merge bugfix with master

If you open the file, git will show you where is the conflict area between two branches using the <<<<< and >>>>> indication sign

<<<<<<< HEAD
age = 23
age = 25
>>>>>>> bugfix
name = "Mike"print("My Name is ", name, " and I am ", age, " years old")

You can keep both changes by just removing the indication sign or you need to choose which one to keep because git can not decide itself, it needs your help. After changes made, commit chose changes.

If you are messing around and want to avoid the merging conflicts or want to undo merge, simply use

git merge --abort