Start a New Branch on your Remote Git Repository
Git is a distributed version control system so it allows you to create branches locally and commit against them. It also supports a more centralized repository model. When using a centralized repository you can push changes to it so that others can pull them more easily. I have a tendency to work on multiple computers. Because of this, I like to use a centralized repository to track the branches as I work on them. That way no matter what machine I’m on, I can still get at my branches.
The Workflow
My workflow is generally something like this:
- Create a remote branch
- Create a local branch that tracks it
- Work, Test, Commit (repeat) – this is all local
- Push (pushes commits to the remote repository)
Git commands can be a bit esoteric at times and I can’t always seem to remember how to create a remote git branch and then start working on new code. There also seems to be multiple ways of doing it. I’m documenting the way that seem to work for me so that I can remember it. Maybe it will help someone else too.
Creating a Remote Branch
1. Create the remote branch
git push origin origin:refs/heads/new_feature_name
2. Make sure everything is up-to-date
git fetch origin
3. Then you can see that the branch is created.
git branch -rThis should show ‘origin/new_feature_name’
4. Start tracking the new branch
git checkout --track -b new_feature_name origin/new_feature_name
This means that when you do pulls that it will get the latest from that branch as well.
5. Make sure everything is up-to-date
git pull
Cleaning up Mistakes
If you make a mistake you can always delete the remote branch
git push origin :heads/new_feature_name(Ok Git’ers – that has to be the least intuitive command ever.)
Use the Branch from Another Location
When you get to another computer or clone the git repository to a new computer, then you just need to start tracking the new branch again.
git branch -rto show all the remote branches
git checkout --track -b new_branch origin/new_feature_name
to start tracking the new branch
Automate it A Bit
That’s a pretty easy thing to automate with a small shell script luckily
#!/bin/sh # git-create-branch <branch_name> if [ $# -ne 1 ]; then echo 1>&2 Usage: $0 branch_name exit 127 fi set branch_name = $1 git push origin origin:refs/heads/${branch_name} git fetch origin git checkout --track -b ${branch_name} origin/${branch_name} git pull
For further help, you might want to check out:
Trackbacks & Pingbacks
- Working with remote branches « Aslak Johansen and Jan Chu @ DIKU
- Vertical Visions :: A technical Blog about Scripting, Monitoring and so on...
- Tims Blog » Blog Archive » Time to use vacation
- Creating remote branches from local ones in Git | Question Defense
- Working with remote GIT branches « Aslak Johansen and Jan Chu @ DIKU
- djwonk › Tracking Remote Git Branches
- When in doubt… › Creating (and removing) remote Git branches
- Entangled discussions » Gecko’s Guide to GIT
- Marcyes / Working with remote branches « Aslak Johansen and Jan Chu @ DIKU
- Creating remote git branch « Jonny Zone : Copyleft – all rights reversed
Thanks very much for this; this is exactly what I was looking for.
i think it is easier to first create the branch locally and then push it to the remote repo:
git checkout -b new_feature_name
git push origin new_feature_name
Pietro,
does this really setup tracking correctly? I had problems with that way when I modified that branch on a second computer. After I returned to the first computer where I initially setup the branch I tried to push some commited work and it told me, that the remote branch is somehow not compatible. But trying to merge the new commits into my local repo told me that there is no such remote head to sync with. Very strange. I had to rename my branch, merge the other branch into it and rebase it to commit it.
Not sure if I did everything right because I am still pretty new to git and originally came from subversion. Git is so much nicer in many ways but the workflow is completly different if you want to use it as its full potential. So I may sometimes still to thing “intuitively wrong”.
(take from http://cheat.errtheblog.com/s/git):
git config branch.autosetupmerge true
Tells git-branch and git-checkout to setup new branches so that git-pull(1)
will appropriately merge from that remote branch. Recommended. Without this,
you will have to add –track to your branch command or manually merge remote
tracking branches with “fetch” and then “merge”.
Quite helpful and concise. Thanks.
If someone happens to have a problem with pushing updates, at this command:
git checkout –track -b new_branch origin/new_feature_name
Instead of “new_branch” use the same name of the “new_feature_name”. If you don’t git won’t be able to figure out where to push your changes.
You can also delete a remote branch with:
git branch -r -d origin/new_feature_name
@kai & @pietro (3 & 4): (I know I’m late, but… just saw this)
No, this doesn’t automatically add tracking (even with Nikos’ hint). Just tried it. What you *can* do, is to edit the .git/config file and add the following (after doing the push):
[branch "new_feature"]
remote = origin
merge = refs/heads/new_feature
You can also do the same using git config:
git config branch.new_feature.remote origin
git config branch.new_feature.merge refs/heads/new_feature
I tend to do the edit, though.
I’ve just added a simple alias to my ~/.gitconfig file that lets me “publish” an existing branch like:
git publish branchname
So I can create my branch and publish it later. The alias is:
[alias]
publish = !sh -c ‘git push origin \”$0\” && git config \”branch.$0.remote\” origin && \
git config \”branch.$0.merge\” \”refs/heads/$0\”‘
I’d like to modify it so it will publish the current branch, but for now this will work, I believe.
In the command “git push origin origin:refs/heads/new_feature_name” to create the remote branch, is the second “origin” correct? This is the <src> in the <src>:<dst> refspec, so I’d expect it to be a branch name such as “master” or a revision name such as “HEAD”. This would then be the point at which the new branch came off its parent, which is what would make sense to me.
I stared at http://www.kernel.org/pub/software/scm/git/docs/git-push.html for a long time to understand what you’re doing here.
Other than that, it’s a great post and answered the question of how to create a branch in a remote respoitory and then use it, tracked, locally.
Thanks so much for this post and the comments. I read it and others and tried to synthesize what people have already said:
http://djwonk.com/blog/2009/04/18/tracking-remote-git-branches/
My goal was to zero in on the simplest way of starting with a local branch, pushing to a remote branch, and getting automatic tracking to work.
Thanks for this bro.
Bash Friendly Script:
@Mauricio. Thanks for the advice. I got it to work now.
Windows CMD friendly script
Hi,
great article,
anyways i was wondering, if there is a way to create an empty remote tracking branches.
Is there a way to recover a branch that is deleted from both local and remote ?
Superb !!!!!!!!!!!!!
remote branch creation is better than local branch when multiple persons working on same modules at random in different branches.
Thanks lots
Just want to say that your post is listed in a private wiki for the company I work for and I reference it every now and then – thanks for the lasting contribution :)
John!
You should know a patch id from your removed branches. When you remove a branch, your patches are not removed immediately, you can remove them by using the garbage collector of git (git-gc). So you need to find one commit on the removed branch, follow the commit tree, and create a branch that refers to the last commit…
I use a remote work branch to create snapshots before I change workstation (from notebook to desktop). Therefore local branches are not enough, because I’d like to sync to that “snapshot” branch from both machine…
Thanks the article!
Great article, nice to keep in bookmarks and quickly check if you need to create a new branch, thanks!
Another useful way of creating a remote branch is by naming the topic branch you want to push to origin
git push origin master:new_remote_branch
or
git push origin topic_branch:new_remote_branch