In the first part of this series we took our first steps with Git and Github. This tutorial is off to set you on the pro route. Let’s wind up this exciting journey to mastery of Git and GitHub.
Branches
The master branch is the default branch when you create a repository. A branch has a separate history from the master. With a team working on one huge project, branches enable you to avoid unnecessary conflicts. Once a team member obtains a copy of the GitHub repository (this is known as forking), they can then clone it to have a local repository and finally create a new branch to begin making new changes without affecting the original project. To create a new branch run the following command:
git checkout -b branch-name
This immediately switches you to the new branch.

If you need to get back to the master or any other branch you have created, just run the following command:
git checkout branch-name
The git checkout command enables you to switch from branch to another. Sometimes you no longer need a branch. Maybe you worked on it and discovered that it is a feature that the user no longer requires. To delete a branch, run the following command:
git branch -d branch-name
This is a permanent action and therefore be careful. Git will not allow you to delete the branch that you are currently on. You need to first check out to another branch then run the delete command for the branch that you want to get rid of. Use -D
to force delete.
To see all the branches you have in your project, run the following command:
git branch
Your new branch can only be available to others if you push the changes to the remote repository. This is done by following command:
git push origin branch-name
Upstream, Update, and Merge
You have learned what forking is. The original GitHub repository you forked is the upstream and the copy of it in your account is the origin. From your project, check to see if you have set your remote by running:
git remote -v
origin https://github.com/wachiya/Project-Algae.git (fetch)
origin https://github.com/wachiya/Project-Algae.git (push)
To add the upstream, run:
git remote add upstream https://github.com/Team/Project-Algae.git
Now if you check again the remote, you will find both the origin and upstream present:
git remote -v
origin https://github.com/wachiya/Project-Algae.git (fetch)
origin https://github.com/wachiya/Project-Algae.git (push)
upstreamĀ https://github.com/Team/Project-Algae.git (fetch)
upstreamĀ https://github.com/Team/Project-Algae.git (push)
Excellent!
To get your repository up to date with the current changes merged to the upstream master between the time you started working on your new feature and the time you want to push back your own changes to GitHub, execute the following command from the local master branch:
git pull upstream master
To merge another branch into your currently active branch, run:
git merge branch-name
Bonus
For review, one commit is enough since in most cases they have similar changes with only the latest commit having all the changes required. Squashing commits comes in handy to avoid conflicts and confusion. This means you are going to squash all the other commits so that you are only left with one. First, you need to study the history of your commits. This is done by:
git log or git log –oneline for a nice precise view of all the commits on one line as the command implies and q to quit the view and get back to the normal terminal. You can squash your commits comfortably since you can now tell how many you have.
git rebase -i HEAD~2
In Git, HEAD is a reference to the last commit in the current check-out branch. The integer at the end of the command is the number of all commits that you wish to squash into one. This command will bring you to an interactive terminal below. Click i (for insert) to get you to edit mode. The commit will have a pick at the beginning. Replace the second pick with s (this is short for squash) then click escape to get out edit mode. To save the changes, type :wq (for write quit) as shown in the image below.

Clicking enter will bring you to another interactive terminal. This requires you to comment the squashed commit by adding a # sign followed by a space before the second commit as shown below following the above instructions to edit and save.

Clicking enter will notify you of the successful squashing.

Force push the final squashed commit by adding the -f at the end of the push command.
You are a Git ninja now. I won’t kid you though, there’s so much to Git that you will learn along the way, but you have all you need to get started like a pro. Happy learning mate!