开发者

Creating a 'push prefix' for git

开发者 https://www.devze.com 2022-12-27 16:21 出处:网络
In this post, I found out that it was possible to create individual mappings for branch names so that pushing back to a central repository can be permanently mapped to a subtree, like:

In this post, I found out that it was possible to create individual mappings for branch names so that pushing back to a central repository can be permanently mapped to a subtree, like:

  • git master branch name: foo
  • remote branch name: foo
  • git push origin foo on slave1 does the equivalent of git push origin foo:refs/slave1/foo

But the post implied that it's not possible to do this on an automatic basis, e.g. specifying a wildcard map from refs/heads/* on the remote to refs/slave1/* on the master.

Well, is it possible?

I clone my repository in a ton of places, make changes on my devbox, and fetch those changes to the copy on the development server for testing. I end up making changes on the dev server and wanting to push them back, but I can't push it with the 开发者_StackOverflowsame branch name because I have that branch checked out on the master. It would be nice to just configure the clone once to be smart about it.

If it's not possible, this is a feature request and I'm pasting this URL to the git mailing list :)


I found the answer myself.

git config --add remote.origin.push +refs/heads/*:refs/MACHINENAME/*


The command in the accepted answer will force push every branch in the local repo to the remote, which is not quite what I wanted when I found this post. Here's an adaptation that worked for me.

Prefixed Push

To push only the current branch (without forcing) while adding a prefix, I made a "prefixed push" alias:

git config --global --add alias.ppush '!git push -u origin "HEAD:refs/heads/PREFIX/$(git rev-parse --abbrev-ref HEAD)"'

where I replace PREFIX with my desired prefix string. Now I can say git ppush when I want to push the current branch while adding the prefix.

I use this when I want to prefix all my branches with my username, so that I can tell it apart from other people's branches while working in a shared repository. In situations where I don't want to add a prefix, I can still use the regular git push.

Mixing push and ppush

If I start out with git ppush and later switch to git push, Git's default behavior is to complain because the local and remote branches will not have the same name (one has a prefix and the other does not). To avoid this, I ended up also configuring push.default:

git config --global --add push.default upstream

With this configured, I can use ppush when pushing my branch for the first time, and then switch to the regular push for subsequent updates.

0

精彩评论

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