Quick Guide to branching & merging in GIT
Branching confused me for a while and I shyed away from it. Now it’s my bestest friend in the world, we laugh and play together all day. You didn’t come here to read about that though, because you’re busy and important and you want to get stuff done. So let’s crack on.
I’ve made a rather basic project that you can clone if your interested in doing that, but all you need to know is that we have a project that is about cats. Our client is going to change their mind and decide that they want the project to feature dogs as well. This is where we’re going to branch, and we’ll call that branch “plusdogs”. We’ll add the changes that incorporate dogs into the plusdogs branch and then merge them back into the master branch, which is where you’ll find the github project at
Cats & Kittens
As you can see, the original project is all about Cats & Kittens, but our client is about to change their mind and decide that they want dogs in on the project as well. Let’s get busy and bust some “mad branch skillz”
First of all let’s list the available branches
git branch -l
Here’s what it came back with, which makes sense because at this point in time there is only the master.
>> * master
Let’s create a the “plusdogs” branch.
git branch plusdogs
You may think nothing happened because git didn’t feedback to you, but it did. Run the list branch command again
git branch -l
Now you can see the two branches. The Asterix indicates which one you’re currently working on.
>> * master plusdogs
We want to work on the plusdogs branch. To do that we have to check it out
git checkout plusdogs
Git will feedback to you telling you that you’ve switched to the plusdogs branch
>> Switched to branch 'plusdogs'
Do The Dog Stuff
So now let’s assume that we’ve been able to complete the work and that our project now looks like this.
We need to add the new files and commit our changes.
git add -A git commit -a -m "Added in the changes for the dog amends" >> [plusdogs 36cafab] Added in the changes for the dog amends >> 4 files changed, 295 insertions(+), 867 deletions(-) >> create mode 100644 img/dog.jpg >> rewrite img/kitten.jpg (98%)
Looks good and now finally push back to the master (on github ) because we want a permanent record of this branch.
git push origin plusdogs >> Counting objects: 14, done. >> Delta compression using up to 4 threads. >> Compressing objects: 100% (7/7), done. >> Writing objects: 100% (8/8), 176.42 KiB, done. >> Total 8 (delta 2), reused 0 (delta 0) >> To firstname.lastname@example.org:jamiecurle/branchbasics.git >> * [new branch] plusdogs -> plusdogs
All is good, we’ve done our changes, but we still need to merge them into the master branch.
Now all we have to do is merge the changes from our plusdog branch into the master. This bit is easy, but there is something you have to bear in mind and might help to express it as a sentence
We want to merge the plusdogs branch into the master branch
To do this we have to first switch over to the master branch because we want to merge the changes from plusdogs branch into the master branch.
git checkout master >>Switched to branch 'master'
You can see where on the master branch now, but let’s double check for that asterix
git branch -l >>* master >> plusdogs
Looks good, let’s merge. The command is very straightforward
git merge plusdogs
Depending on the size of the merge it’ll spit back quite a bit and may even throw a few conflicts at you, but that’s outside the scope of this post. Heres what our merge came back with
>>Updating df6df09..36cafab >>Fast-forward >> css/global.css | 9 ---++
And that ladies and gentlemen wraps up a a basic introduction to branching and merging in git.