Command-Line Interface vs Git Hub, which is better to use when merging? That is the question. Atlassian defines merging as “ Merging is Git’s way of putting a forked history back together again. The git merge
command lets you take the independent lines of development created by git branch
and integrate them into a single branch.”
What does this look like? What are the actual steps that go into this? Let’s take a look. At the beginning of your project, you’ll create a new branch to work on.
git checkout (or co for short) -b “branch name”
This would look like this:
git co -b first_branch_added_new_feature
Afterward, the first step to merging should be to locate the branch you’re working on by typing:
git branch
This will show you all the branches you have, the one that’s prefaced with an asterisk is the branch that you are currently on.
* first_branch_added_new_feature
second_branch_changed_schema
master
Once you get your current branch, you’re going to want to update and commit the files that you’re eventually going to push. You do this with the ‘add’ and ‘commit’ command:
git add .
git commit -m 'some message about what you're committing'
So for our example, it would look like this:
git add .
git commit -m 'added the likes feature'
Once you do these two steps, you are ready to push up your changes to GitHub. You do this by pushing the branch that you created at the very beginning.
git push origin “branch name”git push origin first_branch_added_new_feature
From here you switch to the branch that you want to merge with.
git co “name of branch”
If we wanted to switch to the master branch we would write:
git co master
If we were to ‘git branch’ we would see that we are now on the master branch.
git branchfirst_branch_added_new_feature
second_branch_changed_schema
* master
From here we want to pull the changes to our local directory.
git pull
Next, it’s time to finally merge!
git merge branch_name_you_want_to_merge
Since we want to merge our branch with the master. Ours would look like this:
git merge first_branch_added_new_feature
Our ‘first_branch_added_new_feature’ has now been merged with the master branch. We can check to see if they are synced and everything went okay by typing
git status
If you get a message saying everything is up to date, congrats you have merged successfully. If not, and you see a ‘your branch is ahead by so & so amount of commits’ no worries. Just ‘git push’ again to sync them up.
Now unto the GitHub, my personal version. The steps are the same until you get to the ‘git pull’ part. After we ‘git push origin first_branch_added_new_feature’, we will go to GitHub’s website. Since I don’t have an actual example to walk through the process with, I will reference another photo that explains it. As soon as you go to the repo you should already see a green pull request button, but if not you can just click on ‘New pull request’
After you click on the ‘pull request tab’ you get taken to a new screen.
GitHub will tell you right away if there any conflicts that will prevent you from merging or if you have the go-ahead. It’s straight to the point and colorful, one of the main reasons I like this way better. Once you have the green light, you are good to continue. Click on the ‘Create pull request’.
This will take you to a new page, from here you can add little comments on what you are merging, what you did, etc. Once that’s done, click on ‘create pull request’ again, letting GitHub know you really want to do this.
This takes you to this page where you can finalize merging. Since you have no conflicts, go ahead and click on ‘merge pull request’. And voila! If everything went alright if you go back to the original repo, you should be able to see the changes you made.
With everything successfully merged and your branches synced you can now safely delete your previous branch to help with clutter.
Back in your terminal to get your changes on your local directory you just have to switch to the branch you just merged with, git pull, and now your version and GitHub’s version are the same. In our original example, we merged first_branch_added_new_feature with the master branch, so we would switch to master and pull from there so we can get a snapshot of all the new changes.
git co master
git pull origin master
At the start of this post, I asked which version is better to use when merging, using Command-Line Interface or the GitHub website, the answer; whichever you like the most. Some developers like myself like the ‘easy to follow’ steps on GitHub’s website, green for ‘yes’, red for ‘issues’. Simple and direct. Others like to not have to deviate from the terminal, they like the simplicity of using one platform without having to switch back and forth.
Resources: