Tuesday, 21 February 2012

GIT - How to creates and merge branches

When there are many people working on the same project, it is a good idea create one specific branch for a specific function/problem. Then, once the function has been implemented or once the problem has been solved, the coder can push the code tot he main repository. In this way the master branch will contains only the working code.

1. Create a new branch
To create a new branch, the command branch could be used:

git branch newBranchName

2. Show all the branches
Once it is created, all the branches could be shown by the command branch:

git branch

The branch that the user is working will be shown as green and the name will starts with the symbol *.

3. Change between branches
To change between branches, the command checkout could be used

git checkout branchName

*Tip: The user can create a new branch and switch to it by one single command:

git checkout -b newBranchName

4. Merge Branches
To merge from different branches, the command merge is used

git merge --squash myOtherBranch

This command will merge the content of the actual branch and the myOtherBranch. The option --squash is used to compact all the commit messages from the other branch into one single message.

Once the branches are merged, the changes should be committed and later, pushed to the repository:

git commit -am "New branch merged"

git push

5. Remove a branch
If you want to remove an existence branch, you can use the option -D of the command branch:

git branch -D branchToBeRemovedName

Notice that you cannot remove the branch that you are currently on.

1 comment:

  1. Thank you, very useful. This blog would be great with the new blogger style. I recommend the upgrade to you!!