简体   繁体   中英

How can I push a local Git branch to a remote with a different name easily?

I've been wondering if there's an easy way to push and pull a local branch with a remote branch with a different name without always specifying both names.

For example:

$ git clone myrepo.git
$ git checkout -b newb
$ ...
$ git commit -m "Some change"
$ git push origin newb:remote_branch_name

Now if someone updates remote_branch_name, I can:

$ git pull

And everything is merged / fast-forwarded. However, if I make changes in my local "newb", I can't:

$ git push

Instead, I have to:

% git push origin newb:remote_branch_name

Seems a little silly. If git-pull uses git-config branch.newb.merge to determine where to pull from, why couldn't git-push have a similar config option? Is there a nice shortcut for this or should I just continue the long way?

When you do the initial push add the -u parameter :

git push -u origin my_branch:remote_branch

Subsequent pushes will go where you want.

EDIT:

As per the comment, that only sets up pull.

git branch --set-upstream

should do it.

Sure. Just set your push.default to upstream to push branches to their upstreams (which is the same that pull will pull from, defined by branch.newb.merge ), rather than pushing branches to ones matching in name (which is the default setting for push.default , matching ).

git config push.default upstream

Note that this used to be called tracking not upstream before Git 1.7.4.2, so if you're using an older version of Git, use tracking instead. The push.default option was added in Git 1.6.4, so if you're on an older version than that, you won't have this option at all and will need to explicitly specify the branch to push to.

The command by Adam is now deprecated. You can use:

git branch --set-upstream-to origin/my_remote_branch my_local_branch

to set the upstream branch of my_local_branch to origin/my_remote_branch .

Here's the process that has worked for me.

git clone original-repo-url
git remote rename origin upstream
git remote add origin new-repo-url

Now your new repo will be 'origin' and the original repo is 'upstream'. Confirm it by running git remote -v. (Side note: Upstream is used to fetch from the original repo - in order to keep your local copy in sync with the project you want to contribute to - and origin is used to pull and push since you can contribute to your own repo).

git push origin master

Now your new remote repo's master (on Github) will be in-sync with the original master, but it won't have any of the feature branches.

git rebase upstream/branch-name
git push origin master

Rebase is a smart merge. Then push to master again and you'll see the selected feature branch as master on the new repo.

Optional:

git remote rm upstream
git remote add upstream new-repo-url

I have been running into the same issue for quite sometime now. I finally have a set of statements so I don't have to do git push origin local:remote every time. I followed these:

git branch --set-upstream-to origin/remote_branch_name
git config push.default upstream
git push

After setting upstream to a remote branch with different name (1st line) and then making that upstream as default (2nd line), 3rd line will now obey these rules and push to the set upstream.

How to push to a branch of a different name on Git

You will usually push your local branch to a remote branch of the same name—but not always.

To push to a branch of a different name, you just need to specify the branch you want to push and the name of the branch you want to push to separated by a colon (:).

For example, if you want to push a branch called some-branch to my-feature :

(some-branch)$ git push origin some-branch:my-feature
Total 0 (delta 0), reused 0 (delta 0)
To github.com:johnmosesman/burner-repo.git
 + 728f0df...8bf04ea some-branch -> my-feature

How to push all local branches to the remote

You won't need to push all branches from your local very often, but if you do you can add the --all flag:

(main)$ git branch
* main
  my-feature

(main)$ git push --all
...
To github.com:johnmosesman/burner-repo.git
   b7f661f..6e36148  main -> main
 * [new branch]      my-feature -> my-feature

Push and create a temporary remote branch

If you want to:

  • Push the current branch to remote under a new name, but:
  • Don't change the remote tracking branch of current branch, and:
  • Don't create a local branch under the new name,

Then it's as simple as this:

git push origin HEAD:temp-branch-name

Note: You can replace HEAD with any other branch or commit ID to push that instead.

How can I push a local Git branch to a remote with a different name easily?

TLDR;

Here is a short summary of just the key commands you need in general:

# push to a remote branch which has a different name
git push -u origin local_branch:remote_branch
# pull from a remote branch which has a different name
git pull origin remote_branch

# Set your upstream to something new in case you want to change it
git branch -u origin/remote_branch
# Unset your upstream
git branch --unset-upstream

# See what your upstream is currently set to
git branch -vv

DETAILS: Pushing to another branch, pulling from another branch, setting and unsetting an upstream branch to track

There are too many incomplete and partial answers here which leave me with a lot of questions and a lot to be desired. So, here is my attempt at providing a complete solution.

1. Pushing from your local branch to a remote branch with a different name

To push FROM local_branch TO remote_branch , you must specify both branches like this:

git push origin local_branch:remote_branch
# General form
# NB: if you don't specify the `:remote_branch`, it is assumed to be the same 
# name as `local_branch` on the `remote`! This means it pushes FROM local_branch
# TO local_branch, EVEN IF YOU DIDNT HAVE local_branch checked-out at the time!
# This can be incredibly confusing if you *thought* you had just told 
# your currently-checked out branch to push to some branch on the remote
# and instead, the local copy of that some branch gets pushed to the remote.
git push <remote> <local_branch>[:remote_branch]

The documentation for this is hard to find, but it's actually found in the man git push pages near the top under the "<refspec>... " section:

The format of a <refspec> parameter is an optional plus + , followed by the source object <src> , followed by a colon : , followed by the destination ref <dst> .

And then later:

:<dst> part can be omitted—such a push will update a ref that <src> normally updates without any <refspec> on the command line.

This documentation is non-intuitive and not easy to understand at all without an example.

[BETTER] You can also set the upstream branch at the same time as pushing :

git push -u origin local_branch:remote_branch
# OR (same thing)
git push --set-upstream origin local_branch:remote_branch
# General form
git push -u <remote> <local_branch>[:remote_branch]

As part of the output of the command above, you should see:

 Branch 'local_branch' set up to track remote branch 'remote_branch' from 'origin'.

To make it obvious what is happening there, know that either of the two commands just above are equivalent to these two separate commands:

git push origin local_branch:remote_branch
git branch -u origin/remote_branch

Now, to view what your branch's upstream branch is currently set to , run the double-verbose ( -vv ) git branch cmd:

git branch -vv

Sample output:
Here you can see that the upstream branch is origin/master , which means the master branch on the remote named origin :

 * master b2f0466 [origin/master] c/array_filter_and_remove_element.c: add O(n) in-place solution

Notes:

  1. -vv above means "double verbose". This means it will print git branch not just verbosely, but double verbosely, or extra verbosely. The "extra verbose" part includes the upstream branch in square brackets, as shown above: [origin/matser] .
  2. You can view all your remotes with git remote -v . origin is the remote shown in the examples above.

2. Pulling from a remote branch with a different name to your local branch

[Recommended if you already have branch local_branch checked-out!] To pull FROM remote_branch TO local_branch , you must specify the remote branch to pull from, like this:

# THIS ASSUMES YOU ARE ALREADY CHECKED-OUT ON branch `local_branch`!

git pull origin remote_branch
# General form
git pull <remote> [remote_branch]

You can also specify both branches, but I'm not entirely sure what the difference is in this case (someone help me out here please if you know):

git pull origin remote_branch:local_branch

[The following command only works if the remote and local branches have the same name! (therefore it does NOT answer this Stack Overflow question), and is recommended if you do NOT already have branch local_branch checked-out!] [I use this command frequently too] To pull FROM a remote branch named some_branch TO a local branch named some_branch , while you do NOT have some_branch locally checked-out , do this special form of git fetch :

git fetch origin some_branch:some_branch

Notes:

  1. Unlike git push , git pull does NOT have a -u option.
  2. See also another of my answers: How to change the owner of a PR on GitHub / How to commandeer an open GitHub PR

3. Configuring your local branch to track or untrack a remote branch

You can set your local branch named local_branch to track an upstream branch named remote_branch at the same time as pushing by using the git push -u cmd shown above.

You can also set your local branch named local_branch to track an upstream branch named remote_branch like this:

# Set local_branch to track origin/remote_branch (`remote_branch` on remote
# `origin`)
git branch --set-upstream-to=origin/remote_branch local_branch
# OR (same thing as just above)
git branch -u origin/remote_branch local_branch
# General form
git branch -u <remote>/<remote_branch> [local_branch]

# OR, same as above if the currently-checked-out branch is `local_branch`
git branch --set-upstream-to=origin/remote_branch
# OR (same thing as just above)
git branch -u origin/remote_branch
# General form
git branch -u <remote>/<remote_branch>

To UNset your upstream branch for local_branch , so it no longer tracks the previously-set upstream branch, run this:

git branch --unset-upstream local_branch
# OR, same as above if the currently-checked-out branch is `local_branch`
git branch --unset-upstream

And again, as already shown above, to view what your branch's upstream branch is currently set to , run the double-verbose ( -vv ) git branch cmd:

git branch -vv

References:

  1. Where I first learned the git push -u origin local_FROM_branch:remote_TO_branch syntax: @Adam Dymitruk's answer
  2. https://devconnected.com/how-to-set-upstream-branch-on-git/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM