February 23, 2017

Introduction to Git and GitHub - Part II

A lot of times it is difficult to coordinate changes in a project while using Version Control especially when there are more than one contributors in a particular repository! At such times it is important to use a particular feature of Git called branch.

What are branches and how to use them?

git branch commands

Branches are a feature of Git used when multiple collaborators are working on the same project, but want to work on different parts of the project. This is where the concept of branches in Git is particularly useful. A branch is like a copy of the original code which runs parlallely with other branches in the code repository! The default branch in a GitHub repository is called master. To create a new branch you need to execute the following command:

git checkout -b <branch_name>
Switched to a new branch 'salman'

In case you are looking to switch back to your master branch on your local repository, type in the following commands to get back again:

git checkout master
git checkout <branch_name>

If you are looking to delete a branch first make sure you aren’t currently on that branch! Use git checkout to move to another branch and then use the following commands to delete the branch.

git branch -d <branch_name>

In case your branch is not being deleted because you have certain unmerged changes and you really want to delete them without merging your changes, you can use the following instructions!

git branch -D <branch_name>
How to sync your branch with the Master branch

If you are looking to sync your branch with your master, as in update the online changes that someone else has made to the master branch to your branch, type in the following commands:

git pull origin master

P.S. This can be extended to all branches and isn’t restricted to the master branch alone!

git pull origin <branch_name>
How to push branches on GitHub

Create GitHub Account

Once you have your local changes made and committed, it is essential for you to push them on GitHub so that ohers can review them as well! To do this you can simply push your branch on to GitHub just like you would push the master branch to GitHub! After you’re done working on them use the following commands to push your new branch on to GitHub.

git add --all
git commit -m "<your_message>"
git push origin <branch_name>

Check out the next tutorial to see how to resolve Merge Conflicts as and when you encounter them using Git.

Git GitHub Version Control