Create a new branch from an existing remote branch<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Create a new branch from an existing remote branch

Posted on: July 3, 2015

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.

1mkdir MyNewFeature
2 cd MyNewFeature
3 git init
4 git clone -b teamBranch https://yourgit.git
5 git checkout -b users/mrdesjardins/MyNewFeature
6 git branch -u origin/teamBranch
7 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.