开发者

Git Remote Branch tracking

开发者 https://www.devze.com 2023-01-13 15:45 出处:网络
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.

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.


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


I used this website http://git-wt-commit.rubyforge.org/git-publish-branch for my own use (after a lot of searching on stack).

The process came down to:

git branch *branchname*
git push origin *branchname*
git config branch.*branchname*.remote origin
git config branch.*branchname*.merge *branchname*

My case was pretty basic, but if you dig through the code in the link I'm sure it'll be helpful for the more complicated cases.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号