Notes on git pushing

Published on December 27, 2009 by Jesse Storimer

Pushing all local branches


git push

will update all local branches that are tracking remote branches. For example, if you have set up your local master branch to track the remote origin master branch, then executing git push will update that branch. More on tracking remote branches here

Pushing the current local branch

Say you are working in your local master branch and want to push your changes, but don’t want to update all branches. You can run the git push command, passing in the remote and the name of the branch you want to update:

git push origin master

Pushing to a named remote branch

So, what if have changes in your master branch that you want to push to the remote experimental branch? You could checkout your experimental branch, cherry pick the changes and then use the method above to push those changes, but that’s no fun.

You can actually specify that you want to push your changes to some branch with a different name than the one you are working on with something like this:

git push <remote> <from>:<to>

In our example we would fill in the blanks like so:

git push origin master:experimental

This tells git to push the changes from your local master branch to the remote experimental branch. And we didn’t even need to write /refs/heads anywhere ;)

Deleting remote branches

I discovered a while ago the way to delete remote branches. For example, if we wanted to delete our remote experimental branch we would issue this command:

git push origin :experimental

At first I was thinking ‘Wow! That looks really dumb and makes no sense.’ But, if you look at the above section it makes perfect sense. Think of it this way:

git push origin <nothing>:experimental

It’s as if we are pushing nothing into the experimental branch. The same way we pushed the master branch into the experimental branch above.