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 origin3. Then you can see that the branch is created.
git branch -r
This 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 pullCleaning 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 -r
to 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:
-
Pragmatic Version Control Using Git (Pragmatic Starter Kit)
-
Version Control with Git: Powerful tools and techniques for collaborative software development
-
Pro Git
This is a enhanced copy of an old post on my blog (spanish)
http://nilclass.blogspot.com/2009/08/branches-remotos-en-git.html
Even the push command has copied wrong
Is:
git push origin HEAD:refs/heads/new_feature_branch
Not:
git push origin origin:refs/heads/new_feature_branch
However, the more practical method to create a branch from current commit is:
git checkout -b new_feature_branch
git push -u origin new_feature_branch
@Dario, If you notice the date on mine is from 2008 and more than a year older than your post. So please don’t claim that I copied your work because you happened to write something similar more than a year after I did.
Pingback: Git: Links, News, Resources (1) « Angel “Java” Lopez on Blog
Many thanks! This was a useful post.
@GeoffLane You may have used a Time Machine or something like that
Seriously, both you and me have copied this commands from a dubious source… I dont known what origin means as commit specificator. is the current commit? is equivalent to origin/master?, is cleaner to put it as git push origin HEAD:refs/heads/new_feature
Surely, git push origin origin:refs/heads/new_feature works (I’ve used it), but the meaning of origin to specify the commit is not clear
Nice blog, just watch your sources to avoid “strange” information
Pingback: » Push branch to server / remote location Emil's tech stuff
Thanks Geoff – that really helped me.
@Dario: Copyrigth does not apply to knowledge, which is the only thing the two articles have in common. I don’t see a copy in terms of the creative process I don’t see any copyright infringement.
@GeoffLane Moral of story, break the spirit of sharing and keep all knowledge to yourself. You just cant help some people! Rest assured Geoff it helped me and countless others that didn’t comment.
Pingback: How to add and delete remote branches in git
Pingback: Git branches | WP Cookbook
I am wondering what is the difference between these two lines:
git push origin new_feature_name
git push origin origin:refs/heads/new_feature_name
If I have already used the first line to create a remote branch, should I delete that remote branch and create it again using the second line?
Can I also use the –track option to checkout a branch created with the first line?
Would be nice if you could clarify that. Also great tutorial thanks very much for sharing.
Cheers
Great tip, helped a lot.