How to merge code in git in 7 easy steps

We have all been there, working on a project probably for school or early on at work when suddenly whoever is in charge says that we have to use this Git thing to share our code with the rest of the team and at first it all seems like complicated command line based Dropbox for programmers. 

Then you see a little bit of the light in this system(after reading articles of people telling you how amazing this thing is) and you finally start to understand what it’s all for.

You start to commit your code and it works like a charm! Your code is safely stored somewhere, you can see all the previous iterations of it, you can create branches to work on different features(just like the tutorial told us 👍 ) and it’s all going great… until in one evening, while you sleep, one of your teammates decides to make changes to the file that you were working on.

You wake up the next morning and get back to work on the same file, on the same branch and since this has been going so well so far, you simply forgot to do a git pull.  You finish what you were doing and decide to push that code but then Git hits you with one of these:

Git push error
Oh my Git! Not this again :'(

I don’t know what was your strategy here but mine is very well explained in the xkcd comic below.

Git by XKCD

I knew that this wasn’t the right way to do it, but I was in school and everyone was doing it so I never felt the urge to learn more about Git in order to stop pulling this amateur move… until I went on my second internship.

During my first week at this company, one experienced full-stack developer gave me this easy 7 step process for merging code from different branches that I felt I should have known already. These are literally the most important 7 steps I have learned about git and by the end of this article you’ll be able merge different branches with ease(no more continuous cloning 😉).

Let’s dive right into step 1.

NOTE: We’ll be merging branch feature_merge into master. Of course these branches can be named anything we want.

1 – git checkout master

Before you run this command, make sure that you have pushed the latest code in your current branch(feature_merge in this example) otherwise Git won’t let you checkout the master branch.

All this line of code does is to get the code from the master branch into your computer.

Step 1 – git checkout master

I will be using Visual Studio Code’s built-in terminal and conflict resolution tool, both of which are amazing and time saving. Here’s a link to and article I wrote that will help you setup all the tools you need for this tutorial and web development in general.

2 – git pull

This command will make sure that you have the latest version of the repository in your computer, so all the new code in master and reference to any new branches that might have been created recently.

Step 2 – git pull

3 – git checkout feature_merge

Now we can get back to the branch that we want to merge into master by running the command above.

Step 3 – git checkout feature_merge

Now our computer knows about the latest master version and the latest feature_merge version, which means that we can finally run the merge command

4 – git merge master

This command tells git to merge whichever branch we are currently in(feature_merge) with master.

Step 4  – git merge master

As we can see, git tried to auto-merge but it wasn’t able to do so which made it alert us of a merge conflict, which means that now we get to use the secret tool…🎁, Visual Studio Code’s conflict resolution tool.

If you have been using VSCode for this, you can probably see something like the picture below being displayed.

VSCode conflict resolution tool

You can now click on whichever option you prefer, the tool shows whether you’d like to accept the changes from master and ignore yours, keep yours and ignore master‘s or keep both.

I decided to keep both and it’ll look like this after I pick  Accept Both Changes .

Merge conflict resolved

5 – git add .

After saving the file we have just fixed, it’s time to glide over to a wonderful merge.

So now we run git add . to let git know which files we want to commit next.

Step 5 – git add.

6 – git commit -m "My first smooth merge"

We can now commit the code with a relevant message just like the one on the title here 👌.

Step 6 – git commit -m “My first smooth merge”

This is really all for this step, we have done all the hard work already and as you can see, these steps make things much much better and easier to deal with. Only one last command to run and we are done!

7 – git push origin feature_merge

Finally, we push the code. We’ll push the code to our own branch, but since we already merged with the code on master we can now make a Pull Request and our code will integrate into the master branch without a problem.

Step 7 – git push origin feature_merge

There we go, our code was safely and easily merged and pushed.

This is how easy things can be. This 7 step has saved me so much confusion, I have these commands written down on a note on my computer and I advise you to do the same and whenever you need to merge your code, just take a look at it and merge effortlessly.

I strongly advise you to use Visual Studio Code for this or any other tools that have a similar interface for solving conflicts.

If you found this article helpful and you think that it’ll help others, share it with them or just tell them what you learned and if anything is unclear, feel free to ask me in the comment section, via email or twitter. There are terms that I used here that you might have not heard before, so don’t be afraid to simply ask what it means and how does it work, I would hate it if this article became just another one full of needless jargon.

Thanks for reading.😀

Leave a Reply

Your email address will not be published.