Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 827
Michael Johnson Avatar asked Oct 03 '08 16:10

Michael Johnson


People also ask

How do I diff a local branch to a remote?

You can git branch -a to list all branches (local and remote) and then choose the branch name from the list (just remove remotes/ from the remote branch name. Example: git diff main origin/main (where "main" is the local main branch and "origin/main" is a remote, namely the origin and main branch.)

How do you create a new branch that tracks remote branches?

To create a new local branch based on a remote branch, use the "-track" option in the branch command. You can also do this by using the "checkout" command. If you want your local branch to have the same name as the remote branch, you only need to specify the name of the remote branch.

What is the difference between local branch and remote branch in git?

A local branch is a branch that only you (the local user) can see. It exists only on your local machine. A remote branch is a branch on a remote location (in most cases origin ). You can push the newly created local branch myNewBranch to origin .

How do you synchronize branches?

In GitHub Desktop, click Current Branch. Click Choose a branch to merge into BRANCH. Click the branch you want to merge into the current branch, then click Merge BRANCH into BRANCH. Note: If there are merge conflicts, GitHub Desktop will warn you above the Merge BRANCH into BRANCH button.


1 Answers

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
like image 64
Paul Avatar answered Nov 15 '22 15:11

Paul