Branching is very useful part of project management and software development life cycle. Branches are generally useful when we start work on new modules of any project. It also uses when two development teams works on same project but from different location. They can create separate branch for each team and merge them on task completed.
This tutorial will help you will simple steps to how to create Git branches and switching between branches.
Create New Branch:
Use -b switch to create new branch with ‘git checkout’ command’. As per below command will create new branch and switch to new branch automatically.
$ git checkout -b stage1 Switched to a new branch 'stage1'
This will list all branches used in current working git repository. The branch name start with “ * ” shows your current active branch, in which you are working on.
$ git branch master *
Switch to Other Branch:
Use the following command to switch to any other branch. Change ‘master‘ with your new branch name in below command to switch.
$ git checkout master Switched to branch 'master' Your branch is up-to-date with 'origin/master'.
Now again list branches, you will get that master will be your active branch.
$ git branch *
Push New Branch:
Now push your newly created branch ‘stage1‘ to remote git repository. To push new branch first switch to that branch.
$ git checkout stage1 Switched to branch 'stage1'
Now use following command to push your branch ‘stage1’ to remote git repository.
$ git push origin stage1 Username for 'https://github.com': your_git_user_name Password for 'https:[email protected]': Total 0 (delta 0), reused 0 (delta 0) To https://github.com/tecadmin/firstrepo.git * [new branch] stage1 -> stage1