Can I map local branches to remote branches with different prefixes in git?
Asked Answered
K

4

15

We're working with a semi-centralized git repository here where I work. Each developer has their own subtree in the central git repository, so it looks something like this:

master
alice/branch1
alice/branch2
bob/branch1
michael/feature
release/1.0
release/1.1

Working locally in my tree I have topic/feature, which corresponds to michael/feature in the central tree.

I've been using

git push origin topic/feature:michael/feature

to push my changes to the remote tree. However, this is cumbersome and prone to mistakes (e.g. omitting the developer name, misspelling the feature name, etc.).

I'm looking for a cleaner way to do this. For instance, "git push". I suspect that setting a different remote with a modified fetch refspec will do it, but I'm not sure how exactly to do it. I'm also not sure how to modify my current branch definitions to use the different remote.

My current .git/config looks something like:

[remote "origin"]
    url = git://central/git/project
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "topic/feature"]
    remote = origin
    merge = refs/heads/michael/project

Edit: I'd also like to apply this to pulls/fetches. But does the branch.<name>.merge take care of that?

I'll continue to research this and post here if I find something, but I'm hoping to get some other good ideas.

Edit 2: I've decided I'll keep local and remote branch names the same. It appears it will be the least work and least prone to future problems.

Kanarese answered 3/10, 2008 at 16:19 Comment(2)
When you modify the .git/config, don't forget to back it up first. If you screw something up, you only need to restore the known good version of the file to come back to the corresponding state.Ashling
This actually comes up a lot when doing git deployment to host services like Heroku, Nodester and AppFog.Kopans
A
3

If you can, I suggest you use the same branch names locally & remotely. Then git push will push all of your local branches to corresponding branches in the central repository.

To use different prefixes in local and remote repos, you need to add a mapping to your config file each time you create a new feature branch. The command to set up the mapping for topic/BRANCH_NAME is

 git config remote.origin.push refs/heads/topic/BRANCH_NAME:michael/BRANCH_NAME
Absolve answered 4/10, 2008 at 5:9 Comment(0)
A
3

In your [remote "origin"] section, add one line per mapping. Including master to master.

push = refs/heads/master:master
push = refs/heads/topic/feature:michael/feature

I'm not sure how to do it with the git-config command.

Be aware that from now on, all branches are pushed at the same when you do a straight git push (with no params).

Would you care to explain why you don't keep the same branch names locally and remotely?

Ashling answered 3/10, 2008 at 22:19 Comment(1)
Good point about keeping the branches matching... I may switch to that. In lieu of that, is it possible to include wildcards in the push mappings, similar to the generic one in the question?Kanarese
A
3

If you can, I suggest you use the same branch names locally & remotely. Then git push will push all of your local branches to corresponding branches in the central repository.

To use different prefixes in local and remote repos, you need to add a mapping to your config file each time you create a new feature branch. The command to set up the mapping for topic/BRANCH_NAME is

 git config remote.origin.push refs/heads/topic/BRANCH_NAME:michael/BRANCH_NAME
Absolve answered 4/10, 2008 at 5:9 Comment(0)
C
2

Use

git branch --set-upstream-to=origin/remoteBranch localBranch

where origin/remoteBranch is the remote name, and localBranch is the local branch name.

Cyperaceous answered 23/10, 2020 at 14:30 Comment(1)
Please add some explanation so that the person who posted, can also learn about how this works.Doctor
K
1

You can map your branch to different tracking branch on the remote with something like this:

git remote add heroku [email protected]:YOURAPPNAME.git
git checkout -b heroku -t heroku/master

Your config ends up similar to what @Paul suggests (a tad "simpler" actually).

See this gist (with tweaks by me) for usage steps that work well for me https://gist.github.com/2002048.

Kopans answered 13/3, 2012 at 23:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.