Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Create a new branch from an existing remote branch

Posted on: 2015-07-03

If you want to quickly works on a new feature without affecting the team's branch or the master branch, you may want to create a specific branch for your work. This is also known as a topic branch. Here are the commands that I use.

 mkdir MyNewFeature 
 cd MyNewFeature 
 git init 
 git clone -b teamBranch https://yourgit.git 
 git checkout -b users/mrdesjardins/MyNewFeature 
 git branch -u origin/teamBranch 
 git push origin users/mrdesjardins/MyNewFeature 

The first two lines create a new folder and move your position into it. This has nothing to do with Git. The next one create a new local branch with the specified existing remote branch of the remote server. At that moment, you have the same branch. You need to create a new local branch from that branch. This is done with checkout. Something interesting but not required is to set the upstream. Setting the upstream allow to know if the branch you branched from has changed. This way you can synchronize (pull). The last line is pushing your new local branch to the server. In that example, the same name so I do not need semi-colon. Otherwise you need to specify local:remove. For example, I could create the local without having the users/mrdesjardins/ and just using MyNewFeature local branch into the remote location users/mrdesjardins/MyNewFeature.