tags:

views:

56

answers:

1

What is the all-in-one (or at least easier) way to accomplish this? Basically I want to create a branch but have it tracked so I can push changes, as a backup, to a central repo.

git branch BranchName
git push origin BranchName
git -d BranchName 
git branch --track BranchName origin/BranchName

I've done a bunch of google & SO searches but I'm confused by the normal descriptions of setting up remote branches and tracking them.

+2  A: 

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
Jefromi
Hi again @Jefromi, thanks for taking the time to help me with git. Someone has to push to a remote branch in order for me to pull it, right? I have two reasons to do this a) backup b) share with colleagues.
Hans
If I change the push default to 'current' then did 'git checkout -b branchname' made some edits then did 'git push' it would automatically put that branch on the remote origin? I'll give it a shot.
Hans
Yup, thanks. 'current' was exactly what I needed.
Hans
@Hans - yes, assuming you don't have network access to each other's computers, you need to push to some central repo to share work. That doesn't really mean you have to set `push.default` to current, though. With workflows with a lot of branches, you usually want it set to matching, so that you can push all your branches at once. `current` really is not that useful; once you've pushed it once, it'll obviously be included in `matching`, so you can push it the first time with `git push origin foo`, then just use `git push` from then on.
Jefromi
Hmm... I'm a little on the fence but I guess I don't want to little the repo with all my little branches. I'm just worried I'll do some important work and then forget to "git push origin new-branch" but I'll play with the setting as "current" and "matching" to see which works better. My understanding is *much* better now and it turns out my frustrations probably were because I saw something about setting it to 'tracking', did so, and then git's behavior wasn't what I wanted. Cheers.
Hans
also, by "works better" I mean "works in a way that is the best for how I work" - thanks
Hans
@Hans: Ah, I see. And do note that with the default setting, you don't have to push all your local "clutter" branches. It'll only push the things that exist both places.
Jefromi
@Jefromi, yes. This is turning out to be the best setting. I read a lot about git and I guess I saw some power user's cheatsheet and did a few things I didn't understand and it didn't work for me as I would have liked. I see the defaults are pretty intuitive. Git has seriously changed so much about how I work. With svn I hardly bothered writing a decent log message because it was so much trouble to investigate logs. Now I use git log and gitk like crazy.
Hans