如何轻松地将本地 Git 分支推送到具有不同名称的远程?

发布于 2024-11-02 18:23:23 字数 620 浏览 1 评论 0原文

我一直想知道是否有一种简单的方法可以使用具有不同名称的远程分支来推送和拉取本地分支,而无需始终指定两个名称。

例如:

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

现在,如果有人更新了remote_branch_name,我可以:

$ git pull

并且所有内容都会被合并/快进。但是,如果我在本地“新手”中进行更改,我不能:

$ git push

相反,我必须:

% git push origin newb:remote_branch_name

看起来有点傻。如果git-pull使用git-configbranch.newb.merge来确定从哪里拉取,为什么git-push不能有一个类似的配置选项?有没有一个好的捷径,或者我应该继续走很长的路?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

知足的幸福 2024-11-09 18:23:23

当您执行初始推送时,添加 -u 参数< /a>:

git push -u origin my_branch:remote_branch

后续推送将到达您想要的位置。

编辑:

根据评论,这只设置了拉力。

git branch --set-upstream

应该这样做。

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.

挽心 2024-11-09 18:23:23

当然。只需将 push.default 设置为 upstream 即可将分支推送到上游(与 pull 从中拉取的内容相同,由 定义) >branch.newb.merge),而不是将分支推送到名称匹配的分支(这是 push.defaultmatching 的默认设置)。

git config push.default upstream

请注意,在 Git 1.7.4.2 之前,这被称为 tracking 而不是 upstream,因此,如果您使用的是旧版本的 Git,请使用 tracking > 相反。 push.default 选项是在 Git 1.6.4 中添加的,因此如果您使用的是比该版本更旧的版本,则根本没有此选项,并且需要显式指定分支推至。

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.

烟雨扶苏 2024-11-09 18:23:23

Adam 的命令现已弃用。您可以使用:

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

my_local_branch 的上游分支设置为 origin/my_remote_branch

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.

避讳 2024-11-09 18:23:23

如何轻松地将本地 Git 分支推送到具有不同名称的远程?

摘要:

这里是您通常需要的关键命令的简短摘要:

# push from your local `branch2` to a remote `branch1` (push to a branch with
# a different name) on the remote named `origin`
git push -u origin branch2:branch1
# pull from a remote branch `branch1` into your currently-checked-out branch
# (which could have a different name--ex: `branch2`)
git pull origin branch1

# Set your upstream to something new in case you want to change it; ex: set your
# currently-checked-out branch (perhaps `branch2`) to track `branch1` on the 
# remote named `origin`
git branch -u origin/branch1
# Unset your upstream
git branch --unset-upstream

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

详细信息:

下面按顺序介绍以下部分:

  1. 推送到另一个分支
  2. 从另一个分支拉取
  3. 设置和取消设置要跟踪的上游分支

太多这里的不完整和部分答案给我留下了很多问题和很多不足之处。因此,经过大量的努力、研究和实验,我尝试提供一个完整的解决方案。

1.从本地分支推送到具有不同名称的远程分支

从本地branch2推送到远程branch1,您必须同时指定像这样的分支:

# Push from local `branch2` to remote `branch1`
git push origin branch2:branch1

# General form: push from local `from_branch` to remote `to_branch`. 
# - Watch out!: see also the additional explanations and NB note below!
git push <remote> <from_branch>[:to_branch]

但是请注意,我以上面的一般形式编写的方括号表示 :to_branch 部分是可选。我的意思是,要从具有一个名称的本地分支推送到具有不同名称的远程分支,该部分不是可选的,但是,作为一般 git 命令,如果不包含 ,该命令将运行:to_branch 部分,这意味着它在这个意义上是可选的。但是,却可能会产生意想不到的结果!看一下这个命令,例如:

# (push to a remote branch with the **same name** as the local branch)

# Reduced **and confusing** form: this pushes from local `branch2` (even if you
# don't currently have it checked-out!) to remote `branch2`.
git checkout branch3 
git push origin branch2          # Push from local branch2 to remote branch2

您当前可能已签出本地 branch3,并认为 git push originbranch2 将推送您的本地 branch3< /code> 到远程 branch2,因为您当前在系统上签出了 branch3,但这不会发生!相反,git push originbranch2 会将本地 branch2 推送到远程 branch2,即使您没有>branch2 当前已签出! git push originbranch2 因此是一个等效的简写形式:

# These 2 commands are **exactly identical**! The 1st cmd is the short form
# of the 2nd. 
git push origin branch2          # Push from local branch2 to remote branch2
git push origin branch2:branch2  # Push from local branch2 to remote branch2

如果您使用上面的 cmd 的简写形式,则会产生非常令人困惑的行为认为它会从您当前签出的分支推送。这是一个 Nota bene 注释,总结了上述行为:

注意:采用一般形式git push <远程>[:to_branch],如果您没有使用 :to_branch 指定远程 TO 分支,则假定它与本地 FROM 分支同名,from_branch,在远程!这意味着如果您仅输入 git push originbranch2 而不是 git push origin some_other_branch:branch2 ,它会从本地 branch2 推送到远程副本branch2,即使您在运行命令时没有在本地签出 branch2!如果您认为输入git push originbranch2只是告诉您当前签出的分支some_other_branch推送到< code>branch2 位于远程,而本地 branch2 被推送到远程 branch2

通用形式的文档 (branch2 >git push[:to_branch]) 很难找到,但实际上可以在 顶部附近的 man git push 页面中找到“...”部分:

参数的格式是可选的加上 +,后跟源对象 ,最后是冒号 :,后跟目标引用

然后后来:

: 部分可以省略 - 这样的推送将更新 通常更新的引用,而无需任何 在命令行上。

我认为这个文档是不直观的,并且非常难以理解,但是,没有一些示例和我上面的解释。

[git push的更好形式]您还可以在推送的同时设置上游分支

# Push from local `branch2` to the remote `branch1`, while also at the same time
# setting `branch2` to track `origin/branch1` as the upstream
git push -u origin branch2:branch1
# OR (same thing)
git push --set-upstream origin branch2:branch1
# General form
git push -u <remote> <from_branch>[:to_branch]

作为命令输出的一部分上面,你应该看到:

分支“branch2”设置为从“origin”跟踪远程分支“branch1”。

为了清楚地表明那里发生了什么,请知道上面的两个命令中的任何一个都相当于这两个单独的命令:

git push origin branch2:branch1
git branch -u origin/branch1

现在,查看您的分支的上游分支当前设置为< /strong>,运行双详细 (-vv) git分支 cmd:

git branch -vv

示例输出:
这里可以看到上游分支是 origin/master,这意味着名为 origin 的远程上的 master 分支:

* master b2f0466 [origin/master] c/array_filter_and_remove_element.c:添加 O(n) 就地解决方案

注意:

  1. 上面的 -vv 表示“双重详细”。这意味着它不仅会详细地打印 gitbranch,而且会双重详细地打印,或者更加详细地打印。现在打印的“额外详细”内容包括方括号中的上游分支,如上所示:[origin/matser]
  2. 您可以使用 git remote -v 查看所有遥控器。 origin 是上面示例中显示的遥控器。

2. 从与本地分支名称不同的远程分支中拉取

[如果您已在本地签出分支 branch2,则推荐使用!]从 < code>branch1 在名为 origin 的远程服务器上,TO branch2,您必须指定要从中提取的远程分支,如下所示:

# THIS ASSUMES YOU ARE ALREADY CHECKED-OUT ON BRANCH `branch2`!

git pull origin branch1
# General form
git pull <remote> [from_branch]

您还可以指定两个分支,但我不完全确定在这种情况下有什么区别

git pull origin branch1:branch2

# The general form seems to be:
git pull <remote> <from_branch>[:to_branch]

以下命令仅在远程和本地分支具有相同名称的情况下才有效! em> (因此它不回答这个堆栈溢出问题)。如果您尚未签出分支 some_branch,建议使用此命令!

# 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.
git fetch origin some_branch:some_branch
# General form
git fetch <remote> <from_branch>:<to_branch>

# The above is a special form of `git fetch`, and (I believe) requires that 
# `from_branch` and `to_branch` are **the same branch name**. It is roughly 
# equivalent to the following *several* commands:
git checkout any_other_branch
# this `git fetch` cmd updates the **locally-stored**, hidden, remote-tracking
# branch named `origin/some_branch` with the latest changes from the branch
# by this name stored on the remote server named `origin`
git fetch origin some_branch 
git checkout some_branch
git merge origin/some_branch  # merge `origin/some_branch` into `some_branch`
git checkout any_other_branch # go back to the branch we started on

注意:

  1. git Push 不同,git pull 确实如此没有 -u 选项。
  2. 另请参阅我的另一个答案:如何更改 GitHub 上 PR 的所有者/如何征用开放的 GitHub PR
  3. git fetch origin some_branch:some_branch 命令使用在命令的两个位置中使用两次的相同 some_branch 名称来完成。区别在于,git fetch origin some_branch 仅使用最新版本更新名为 origin/some_branch本地存储、隐藏的远程跟踪分支。存储在名为 origin 的远程服务器上的按此名称进行的分支的更改,而 git fetch origin some_branch:some_branch 则可以更新本地存储的可见 some_branch 也进行了这些更改。
    1. 如果您对此感到困惑,您需要了解,对于您认为拥有的每 1 个 some_branch,您实际上拥有最多3 个分支:1) 本地分支 some_branch,2) 名为 origin 的远程服务器上的远程分支 some_branch和 3) 以及名为 origin/some_branch 的本地存储、隐藏、远程跟踪分支。阅读此处了解更多信息。我第一次了解到每个分支3个分支的概念:如何本地和远程删除 Git 分支?。另请参阅我的评论,在该答案下。

3. 配置本地分支以跟踪或取消跟踪远程分支

您可以设置名为 branch2 的本地分支来跟踪名为 的上游分支 >branch1 与推送同时,使用上面显示的 git push -u cmd。

您还可以设置名为 branch2 的本地分支来跟踪名为 branch1 的上游分支,如下所示:

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

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

取消设置您的上游分支 < code>branch2,因此它不再跟踪先前设置的上游分支(在上面的示例中是 origin/branch1),运行以下命令

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

:如上所示,要查看分支的上游分支当前设置为,请运行double-verbose (-vv) git 分支 cmd:

git branch -vv

参考资料:

  1. 我第一次学习 git push -u origin local_FROM_branch:remote_TO_branch 语法的地方: @Adam Dymitruk 的回答
  2. https://devconnected.com/如何设置上游分支-on-git/
  3. 如何在本地和远程删除 Git 分支?

我写过的相关 git 主题:

  1. 初学者:
    1. 从另一个分支在 Git 中创建分支< /a>


  2. 中间的:
    1. 如何挑选多个提交
  3. 高级:
    1. 如何仅获取一个文件另一个分支?
    2. 谁是“我们”根据 Git 的说法,“他们”是谁?

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

Summary:

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

# push from your local `branch2` to a remote `branch1` (push to a branch with
# a different name) on the remote named `origin`
git push -u origin branch2:branch1
# pull from a remote branch `branch1` into your currently-checked-out branch
# (which could have a different name--ex: `branch2`)
git pull origin branch1

# Set your upstream to something new in case you want to change it; ex: set your
# currently-checked-out branch (perhaps `branch2`) to track `branch1` on the 
# remote named `origin`
git branch -u origin/branch1
# Unset your upstream
git branch --unset-upstream

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

Details:

The following sections are covered below, in this order:

  1. Pushing to another branch
  2. Pulling from another branch
  3. 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, after a bunch of effort and research and experimenting, 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 your local branch2 TO remote branch1, you must specify both branches like this:

# Push from local `branch2` to remote `branch1`
git push origin branch2:branch1

# General form: push from local `from_branch` to remote `to_branch`. 
# - Watch out!: see also the additional explanations and NB note below!
git push <remote> <from_branch>[:to_branch]

Notice, however, that the square brackets I have written in the general form above indicate the :to_branch part is optional. What I mean is that to push from a local branch with one name to a remote branch with a different name, that part is NOT optional, but, as a general git command, the command will run if you do not include the :to_branch part, meaning it is optional in that sense. But, it may produce unexpected results! Take a look at this command, for example:

# (push to a remote branch with the **same name** as the local branch)

# Reduced **and confusing** form: this pushes from local `branch2` (even if you
# don't currently have it checked-out!) to remote `branch2`.
git checkout branch3 
git push origin branch2          # Push from local branch2 to remote branch2

You might have local branch3 currently checked-out, and think that git push origin branch2 will push your local branch3 to the remote branch2, since you have branch3 currently checked-out on your system, but this is NOT what will happen! Rather, git push origin branch2 will push your local branch2 to your remote branch2, again, even if you do NOT have branch2 currently checked-out! git push origin branch2 is therefore an equivalent short-hand of this:

# These 2 commands are **exactly identical**! The 1st cmd is the short form
# of the 2nd. 
git push origin branch2          # Push from local branch2 to remote branch2
git push origin branch2:branch2  # Push from local branch2 to remote branch2

The short form of the cmd just above produces very confusing behavior if you think it will push from your currently-checked-out branch instead. Here is a Nota bene note summarizing the behavior described above:

NB: In the general form git push <remote> <from_branch>[:to_branch], if you don't specify the remote TO branch with :to_branch, it is assumed to be the same name as your local FROM branch, from_branch, on the remote! This means if you type only git push origin branch2 instead of git push origin some_other_branch:branch2, it pushes FROM your local branch2 TO the remote copy of branch2, EVEN IF YOU DIDNT HAVE branch2 locally checked-out at the time of running the command! This can be VERY CONFUSING if you thought typing git push origin branch2 had just told your currently-checked out branch, some_other_branch, to push to branch2 on the remote and instead, the local branch2 got pushed to the remote branch2.

The documentation for the general form (git push <remote> <from_branch>[:to_branch]) 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.

I think this documentation is non-intuitive and very difficult to understand, however, without some examples and my explanation above.

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

# Push from local `branch2` to the remote `branch1`, while also at the same time
# setting `branch2` to track `origin/branch1` as the upstream
git push -u origin branch2:branch1
# OR (same thing)
git push --set-upstream origin branch2:branch1
# General form
git push -u <remote> <from_branch>[:to_branch]

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

Branch 'branch2' set up to track remote branch 'branch1' 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 branch2:branch1
git branch -u origin/branch1

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" content now printed 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 branch2 checked-out locally!] To pull FROM branch1 on the remote named origin, TO branch2, you must specify the remote branch to pull from, like this:

# THIS ASSUMES YOU ARE ALREADY CHECKED-OUT ON BRANCH `branch2`!

git pull origin branch1
# General form
git pull <remote> [from_branch]

You can also specify both branches, but I'm not entirely sure what the difference is in this case:

git pull origin branch1:branch2

# The general form seems to be:
git pull <remote> <from_branch>[:to_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). This command is recommended if you do NOT already have branch some_branch checked-out!

# 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.
git fetch origin some_branch:some_branch
# General form
git fetch <remote> <from_branch>:<to_branch>

# The above is a special form of `git fetch`, and (I believe) requires that 
# `from_branch` and `to_branch` are **the same branch name**. It is roughly 
# equivalent to the following *several* commands:
git checkout any_other_branch
# this `git fetch` cmd updates the **locally-stored**, hidden, remote-tracking
# branch named `origin/some_branch` with the latest changes from the branch
# by this name stored on the remote server named `origin`
git fetch origin some_branch 
git checkout some_branch
git merge origin/some_branch  # merge `origin/some_branch` into `some_branch`
git checkout any_other_branch # go back to the branch we started on

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. The git fetch origin some_branch:some_branch command is done with the same some_branch name used twice--in both locations in the command. The difference is simply that git fetch origin some_branch only updates the locally-stored, hidden, remote-tracking branch named origin/some_branch with the latest changes from the branch by this name stored on the remote server named origin, whereas git fetch origin some_branch:some_branch does that PLUS also updates the locally-stored visible some_branch with those changes too.
    1. If you feel confused about this, you need to learn that for every 1 some_branch you think you have, you actually have up to 3 branches: 1) a local branch some_branch, 2) a remote branch some_branch on a remote server named origin, and 3) and locally-stored, hidden, remote-tracking branch named origin/some_branch. Read here for more info. and for where I first learned this concept of 3 branches per branch: How do I delete a Git branch locally and remotely?. See also my comment here, under that answer.

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

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

You can also set your local branch named branch2 to track an upstream branch named branch1 like this:

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

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

To UNset your upstream branch for branch2, so it no longer tracks the previously-set upstream branch (which was origin/branch1 in the examples above), run this:

git branch --unset-upstream branch2
# OR, same as above if the currently-checked-out branch is `branch2`
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/
  3. How do I delete a Git branch locally and remotely?

Related git topics I've written about:

  1. BEGINNER:
    1. Create a branch in Git from another branch
  2. INTERMEDIATE:
    1. How to cherry-pick multiple commits
  3. ADVANCED:
    1. How to get just one file from another branch?
    2. Who is "us" and who is "them" according to Git?
念﹏祤嫣 2024-11-09 18:23:23

如何推送到 Git 上不同名称的分支

您通常会将本地分支推送到同名的远程分支,但并非总是如此。

要推送到不同名称的分支,您只需指定要推送的分支以及要推送到的分支的名称,并用冒号 (:) 分隔。

例如,如果您想将名为 some-branch 的分支推送到 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

如何将所有本地分支推送到远程

不需要经常从本地推送所有分支,但如果需要,可以添加 --all 标志:

(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

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
森末i 2024-11-09 18:23:23

是的,有一个配置选项可以让 git 默认推送到上游分支。

使用以下一组语句,这样您就不必每次都执行 git push origin local:remote

# Set remote branch with a different name as an upstream branch 
# for local branch currently checked out
git branch --set-upstream-to origin/remote_branch_name

# Change the default behavior for git-push (see manpage for git-config)
git config push.default upstream

# Now git will push to the upstream branch by default
git push

将当前分支的上游设置为具有不同名称的远程分支后,您需要更改git Pushupstream 的默认行为(请参阅 git-config 的联机帮助页)。
现在,git push将遵守这些规则并推送到上游集合。

Yes, there is a config option to make git push to upstream branches by default.

Use the following set of statements so you don't have to do git push origin local:remote every time:

# Set remote branch with a different name as an upstream branch 
# for local branch currently checked out
git branch --set-upstream-to origin/remote_branch_name

# Change the default behavior for git-push (see manpage for git-config)
git config push.default upstream

# Now git will push to the upstream branch by default
git push

After setting the current branch's upstream to a remote branch with a different name, you need to change the default behavior of git push to upstream (see manpage for git-config).
Now, git push will obey these rules and push to the set upstream.

揽清风入怀 2024-11-09 18:23:23

推送并创建临时远程分支

如果您想:

  • 以新名称将当前分支推送到远程,但是:
  • 不要更改当前分支的远程跟踪分支,并且:
  • 不要以新名称创建本地分支,

那么就这么简单:

git push origin HEAD:temp-branch-name

注意:您可以将 HEAD 替换为任何其他分支或提交 ID 来推送它。

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.

疾风者 2024-11-09 18:23:23

两个选项:

  1. push 所有存储库的分支到通过 push.default=upstream
  2. 使用 远程。<名称>.push< /code>

要让所有存储库的分支推送到它们从中提取的分支,请使用:

git config --local push.default upstream

注意:这也会影响其他分支。


要设置单个分支的远程推送对(例如:本地 mainorigin/master):

  1. 设置分支的远程

     git 分支 --set-upstream-to origin
    
  2. 设置远程的推送映射

    git config --local remote.origin.push main:master
    

Two options:

  1. push all the repo's branches to where they get pulled from via push.default = upstream
  2. Change a single branch with remote.<name>.push

To have all your repo's branches push to the branch that they pull from, use:

git config --local push.default upstream

Note: this will affect other branches, also.


To setup a single branch's remote push pair (Example: local main to origin/master):

  1. Set the branch's remote

     git branch --set-upstream-to origin
    
  2. Set the remote's push mapping

    git config --local remote.origin.push main:master
    
戈亓 2024-11-09 18:23:23

这是对我有用的过程。

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

现在,您的新存储库将是“原始”,原始存储库将是“上游”。通过运行 git Remote -v 进行确认。 (旁注:Upstream 用于从原始存储库中获取 - 以便使本地副本与您想要贡献的项目保持同步 - 而 origin 用于拉取和推送,因为您可以为自己的存储库做出贡献)。

git push origin master

现在,您的新远程存储库的 master(在 Github 上)将与原始 master 同步,但它不会有任何功能分支。

git rebase upstream/branch-name
git push origin master

Rebase 是一种智能合并。然后再次推送到 master,您将在新存储库上看到所选功能分支作为 master。

选修的:

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

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
晌融 2024-11-09 18:23:23

如果您的本地分支中有一堆文件想要保存到不同的分支,您可以做的一件很酷的事情是执行 git stash 来保存所有修改过的文件(这是存储库) Wide),然后执行 git checkout remoteBranchYouWant ,然后执行 git stash pop ,现在所有修改的文件都已在新签出的分支中准备就绪,您可以提交并根据需要将它们推送到新分支。

One cool thing you can do if you have a bunch of files in your local branch that you want to save to a different branch, is to do a git stash to save all your modified files (this is repository wide), then you do a git checkout remoteBranchYouWant then do a git stash pop and now all your modified files are ready in your newly checked-out branch and you can commit and push these up to the new branch as you wish.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文