After learning how to use the basics of git like adding, committing, and pushing, I often found myself wondering how all of this would be helpful in the future. Sure, having some saves of my old work is nice, but do I really need it? After all, I do have my finished product here. While this mindset is somewhat understandable for small personal projects, having these different saves are necessary when working in a large code base with multiple people. Got it, but how do I keep track of my changes without messing up the whole codebase with my commits? Great question! This is where git branches come into play. A git branch points to your changes in a new location, where it does not directly change the content of the main files[1]. Think of this as making a new copy of a Word document. Here, you can make changes to the code, add new commits, add/delete files, etc., and it will not affect the main code. Once you are satisfied with these changes, you can combine them into back your original code, but more on this later.
How to create a branch
To get started, all you need to do is type this one line into your terminal:
git checkout -b insert_branch_name_here
This single line will create your new branch, and also move you into it! This is a condensed version of:
git branch insert_branch_name_here
git checkout insert_branch_name_here
If you would like to check that you are in the new branch, or see all of your active branches, use the command git branch
. Congrats! You are now in your new branch. In here, you can update your code as you see fit. If you need to go back to your main branch, all you need to do is: git checkout main
.
Git Merging
Now that we have our new branch with all of our updates, we need to move these changes back into the main branch. This is where merging comes into play. There are two ways to tackle branch merging: through your terminal, and Github. This post will cover both options, so let’s jump into the former. First things first, add and commit all of your changes onto your new branch (make sure you are in your new branch, and then git add and git commit). Then, you will want to run git checkout main
. Again, to make sure you are in the correct location, run git branch
, and look for the star next to the ‘main’ branch. From here, type git merge insert_branch_name_here
and that’s it! All of the changes have been merged to the main branch, nice job!
Let’s now go through this the Github way. After you make all the changes you want on your new branch, make sure to commit these changes. Now, push this branch up to your Github repo. To do so, run your git push, but instead of main/master, you will put your branch name. Git push origin insert_branch_name_here
. From here, open the Github repo that corresponds to your current project. At the top of the page, Github should inform you that a branch has recent pushes, and asks if you would like to Compare & Pull requests. Click this green button.
This will take you to the ‘Open a pull request’ page, and again click on the green ‘Create pull request’ button.
Finally, You will now be on a new page that has a green button titled ‘Merge pull request’, go ahead and click on the said button.
Congrats, now you have learned how to merge branches through Github!
Merge Conflicts
Sadly, sometimes git merges will not be that easy. When you merge your branches, you may receive this error:
CONFLICT (content): Merge conflict in file_name
Automatic merge failed; fix conflicts and then commit the result.
No worries! This is an error called a ‘Merge Conflict’, and it occurs while git is merging all of the changes. When merging, git may come across different changes on the same part of the same file that it is merging [1]. To fix this, you will need to open the specified file and look for:
<<<<<<< HEAD: file_name
*Main branch code here*=======
*New branch code here*>>>>>>> branch_name: file_name
This is git showing you where the conflict is happening, and letting you decide what code to keep. The top half is the changes that are in the main branch, while the bottom half is the changes coming in from the new branch. It is your decision on which half to keep, and which to get rid of. Go ahead and delete the half that you would not like to save (this includes the operator signs as well). Then, to save these changes, run git add file_name
and then git commit -m “Message"
.
To use Github’s merge conflict editor, go through the process of making a pull request again until you cannot click the final green button. From there, click the grey ‘Resolve conflicts’ button, and delete the half of the conflict that you would not like to save.
From there, click the grey ‘Mark as resolved’ button, and go ahead and finish the pull-request.
Phew. Crisis averted!
Tips
If finding merge conflicts has proven to be a challenge, I would recommend using a code editor like VS Code or Atom. Both of these editors have built-in tools that can help you find the merge conflicts and easily choose which code to keep.
Conclusion
You survived! After following this post, you should be able to create new git branches and merge them into your main branch. I highly recommend that you start implementing these ideas into your personal projects. This may feel like a chore, but practice makes perfect! Keep making and merging branches until you become a git pro!
Sources:
https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging [1]