Git Branching
Git is a free and open source distributed version control system designed to handle from small to very large projects with efficiency. Git is really easy to use and learn. In this article we are going to cover one important aspect of Git which is called Branching.
Git Branches are separate, independent line of development in a collaborative project. The first branch in every project is called 'master-branch'. Branching is very useful in a research and development project as many developers might work in many features of the final product. So, the developers can finally merge there independent work in to the 'Master' branch and develop a fully functional product without knowing what the other developer has done.
Listing Branches
To list the branches and tags in the current project please use the following command.
git branch
Creating a Branch
Just use the following command with an appropriate branch name to 'branch-name'.
Then we have to remove the old-branch from remote repository and add the new-branch.
Then remove it from the remote repository.
I hope that this will be helpful when learning git branching. You can have more details from 'git' manual pages or git home page.
git branch <branch-name>After creating the branch use following command to push the changes in the newly created branch into the remote-git-repository.
git push --set-upstream origin <branch-name>
Renaming Branches
To rename the branches we have first rename the local branch and push it to the remote repository. To rename the local branch we can use two methods.
If in the branch which need to be renamed we can use :
git branch -m <new-name>Otherwise you have to specify the old branch name also :
git branch -m <old-name> <new-name>(-m = option is stand to 'move')
Then we have to remove the old-branch from remote repository and add the new-branch.
git push origin: <old-name> <new-name>Then finally we have to set the upstream to the new-branch.
git push origin -u <new-name>
Deleting Branches
To delete a branch you have to first remove the branch from the local repository and finally remove it from the remote repository. The latter step might need some privileges from the system-administrator unless you are the owner of the repository.
First remove the branch from the local repository.
git branch -d <branch-name>(-d = stands for 'delete')
Then remove it from the remote repository.
git push --delete <branch-name>
Changing the Branch
To change your current branch you can use 'checkout' command.
git checkout <branch-name>
Merging Branches
Merging meaning connecting components in two git branches into one. Following command will merge the content in 'another-branch' into the current branch.
git merge <another-branch>