Git Branches and Remote Repositories 5 September 2009
Posted by manniwood in Uncategorized.trackback
I’ll be honest: it took me a while to wrap my head around Git, and how it really worked, and how use it effectively.
I’m going to put in a high recommendation for Scott Chacon’s excellent Git Internals, published by PeepCode. It’s a US$9.00 pdf, and it’s better than any other book or online resource I’ve ever read about Git.
Chacon does a better job than anybody else of showing you how Git works, so that by the time he gets into every day tasks, why you are doing what you are doing makes perfect sense, and you’re just learning the commands and syntax to leverage Git’s capabilities.
When it comes to sharing Git branches between repositories, there’s still not a perfectly good, clear resource out there, so I’m going to share with you what I scribbled on my copy of the tear-out “Git Command Quick Reference” from Travis Swicegood’s Pragmatic Version Control Using Git. Hopefully, what I had to scribble on my quick reference card will appear in a second edition.
First off, let me describe what I want Git to do for me.
I want to have a clone of my repository on another geographically remote server. (For purposes of this discussion, I will assume that it has already been created, and is called myremote.)
I want to create a new branch in my local repository on my development machine at my desktop, and at the end of the work day, I want to push this branch out to my remote repository—perhaps for sharing, perhaps just for easy backup purposes.
Git does not push new local branches out by default, and I (and maybe it’s just me) find the documentation very uninformative on how to manage remote branches.
Here’s how.
Let’s say I’m at my desktop computer and I’m using my local Git repository.
Let’s say I make a new branch based on master:
git branch my-new-branch master
Now let’s say it’s the end of the work day, and my work in the branch my-new-branch is not complete. I don’t want to merge my-new-branch back into master, but I do want to push my-new-branch out to my remote repository for backup purposes.
Here’s how:
git push myremote my-new-branch
From now on, while working locally in my-new-branch, doing a
git push myremote
should do the right thing and push out changes I make in my-new-branch.
Now let’s say it’s a day or two later, and I’ve merged my-new-branch back into master. I know I no longer need my-new-branch, so I delete it locally:
git -d my-new-branch
Done.
Of course, my-new-branch still exists in my remote repository; I’ve only deleted it in my local repository.
I delete my remote copy of my-new-branch like so:
git push myremote :my-new-branch
Yes, that’s right: putting a colon in front of a branch and pushing it will delete it on the remote repository. Not very obvious, is it?
I may go into detail about pushing local tags out to remote repositories in another post.
Happy Gitting in the meantime.
Hey, glad you’ve forked my tear out card. :-)
The “:branch-name” syntax is a little odd at first glance, but does make sense in a round about way. The full syntax for pushing a branch is “git push :”. Essentially what you’re doing with the colon is telling Git that you want to push an empty branch to the remote repository.
The command could definitely be made more intuitive so it’s more apparent what’s happening, though.
Hello, Travis Swicegood!
Yes, your tear-out card is a permanent fixture on my desktop, and it has a few scribblings on it. Will your second edition have more into on remote git repositories? I find managing remote repositories to be one of the least-clearly documented parts of Git, and yet it is the most useful in day-to-day programming!