If you are just getting into coding and are working on projects, ether solo or in a group, you probably use GitHub, a platform to house your projects. Or you use it at your job and are always doing adds and commits, waiting for you superior to approve the merge. If you already know how all of that works, then you most likely won't need to read this post. I am writing this as more of a quick reference guide for us newbies... and there is a lot of additional good documentation from GitHub and lots of extra resources on the web.
There are 2 major ways to update a main/master branch in GitHub: the first is the good old git merge
, and the second is git rebase
. Both of these commands accomplish the same thing: they update the main/master branch with your newly written code.
This is coming from the view point that you are working on a branch that is not the main. Make sure you have the most up-to-date version of the main repository by running git pull
. To make your own branch, you can run
git branch
#this will display all of the branches in the current repo
git branch example_branch
#this will make a branch off the main if you are currently on the main branch
Now we need to get into the new branch we just made. We can do this with
git checkout example_branch
So you have been working on a feature for the main code file; let's talk about when you are ready to have your code reviewed and potentially merged into the main branch. First we check the status to make sure we only edited the files we were supposed to.
git status
Then we add all of our changes to the staging area to be committed by running one of the following commands:
git add .
git add -A
git add 'file name'
Now your code is ready for a commit. This is where we are able to leave a detailed message about what we did with our code. Here are some commit commands:
git commit -m "message"
git commit -am "message"
#This command will add your changes and commit them in one go
Then once you have your code committed, you can push your code to the origin branch with
git push
Finally it is ready to be reviewed and sent back for editing, or it can be merged into the main. If there is no more code for you to add, when your branch and the main branch are merged, you have the option to delete your branch. If you will need to use it again soon, then you can just leave it be.
To merge to 2 branches, we will want to go into the main/master branch, then run git pull
, just in case there have been any changes (it will let you know if you are up to date, or it will automatically load the changes). Our commands can look like this:
#currently not on main branch
git checkout main
#now we are on the main branch
git pull
# just to double check for any changes
git merge example_branch
#now the branch has been merged into main
Hopefully there are no merge conflicts, and if not, you are done! That is the easy way to do a merge and its called a "fast forward merge."
If you do get some merge conflicts you will need to resolve those conflicts before you can move on.
A merge is pretty self explanatory, you are combining the contents of 2 items into 1. There are a few ways that a merge can happen, 1) you can do it strait in your terminal, 2) you can use the GitHub webpage, 3) Github also has an app for OSX that works pretty well.
When there are commits on the main branch that are added after you made your branch, it will prompt what is called a "merge commit," once the merge is initiated. You can leave a message and move on to write more code.
A merge commit will have a lot of "~" symbols and you need to leave a message to move forward in the commit, it would look like so:
~
~
~
~
~
~
~
~
You can hit the escape key, then type : w q which will save the commit and get you through the merge commit. Then you should double check with git log
to make sure all the commit history looks correct to you.
"Rebase" is a git command that will get us a similar result of adding our code to the main branch, but it works very differently. Rather than having branches come together into the main, you are basically adding your 'example_branch' onto the end of the master branch. To do this you follow all of the same steps above, but when it comes time to merge, you will instead run
#you will want to be in your "example_branch" when you run this command
git rebase main/master
The links below are some good resources that I found, and they go into greater detail of whats going on behind the scenes.
Resources
GITHUB PULL REQUEST, Branching, Merging & Team Workflow
Git Rebase Vs. Merge
Git Cheat Sheet
Top comments (5)
So the "thing" with all the tilde's is vim. You can of course have git use any editor of your choosing by setting core.editor (though why you'd not want to use vim is beyond me!)
In a git article targeted at new users, I'd perhaps not mention 'git rebase' or at least enclose it in WARNINGs.
Also, lets not encourage
git commit -m ""
but rather actually writing proper commit messages.
Thanks, I will right proper commit messages from now on.
Thx for this! This is really what I wanted. Helped A LOT.
Can I translate in Korean this article? If you don't mind, I wanna share this awesome information in Korean. Surely, There will be a link directing to this original one.
Sure, translate it into any language you need. I'm glad this post was able to help you.
I love the rebase interactive tool ^^