The normal use for tracking branches is so that you can fetch from them. If you want to push to a remote for backup, you probably want all of your branches there, yes? So you could do this:
git config remote.backup.mirror true
Then whenever you run git push backup
, it'll default to the behavior of git push --mirror backup
, which pushes all refs (not just all branches - all tags, all your other remote branches, everything).
If you don't want to go that far, you could still do:
git push --all backup
That'll push all branches, but not the rest of your refs.
Finally, if you really do just want to push one branch... well, essentially you should always just do this:
git push origin backup-branch
The only way you could possibly make it shorter is to just make it the default operation carried out by git push
, if you really want to. There are four options for the behavior of git push
with no arguments:
- nothing do not push anything.
- matching push all matching branches. All branches having the same name in both ends are considered to be matching. This is the default.
- tracking push the current branch to its upstream branch.
- current push the current branch to a branch of the same name.
You can set your preferred one with git config push.default <value>
. If you change it to tracking
, configuring your backup branch as a tracking branch would let you push it with no arguments, but only if it's already checked out. And it would keep you from using git push
to push many branches at once (the default behavior, which is really pretty nice). So, if you really want to do it that way, you would indeed need to set up your branch as a tracking branch. You can shorten the way you did it a little:
git branch backup-branch
git push origin backup-branch
# most elegant way:
git branch --set-upstream backup-branch origin/backup-branch
# or this:
# git branch -f backup-branch origin/backup-branch
# or this:
# git config branch.backup-branch.remote origin
# git config branch.backup-branch.merge refs/heads/backup-branch