将上游分支设置为与本地不同名称的远程分支

21

我在github上使用下拉菜单从现有PR中创建了一个新分支...输入一个新的分支名称,以便基于我查看的PR创建一个新分支。

然后我在本地执行了git checkout -b myBranch

如何将我的分支与远程同步?我没有将本地分支命名为与远程相同的名称。

git push -u origin my_branch - 我假设这是在已经同步且本地和远程分支名称完全相同时可以使用的命令。

那么对于我的情况呢?

我尝试过这个但是出现了错误

▶ git branch --set-upstream-to=origin/feature/WA-3 WA-3 error: the requested upstream branch 'origin/feature/WA-3' does not exist

更多信息以帮助解决问题

远程分支的名称是feature/WA-3,而我的本地分支名称为W3

▶ git remote show origin

* remote origin
  Fetch URL: https://github.com/xxxx.git
  Push  URL: https://github.com/xxxx.git
  HEAD branch: develop
  Remote branches:
    develop                         tracked
    feature/WA-3                    new (next fetch will store in remotes/origin)
    master                          tracked
    refs/remotes/origin/w9-homepage stale (use 'git remote prune' to remove)
    w1-log-in              tracked
    wa-9                  tracked
  Local branches configured for 'git pull':
    develop            merges with remote develop
    w1-log-in merges with remote w1-user-can-log-in
    w9-homepage        merges with remote wa-9-homepage
  Local refs configured for 'git push':
    develop            pushes to develop            (up to date)
    w1-log-in pushes to w1-log-in (up to date)

在这里输入图片描述


“new (next fetch will store in remotes/origin)”听起来很奇怪。你尝试过fetch吗? - choroba
这个回答解决了你的问题吗?[如何轻松将本地 Git 分支推送到具有不同名称的远程分支?] (https://dev59.com/CW025IYBdhLWcg3w4qIx) - pkamb
2个回答

36
使用冒号表示法:
git push -u origin local_branch:remote_branch

▶ git branch -u feature/WA-3:WA-3 错误:请求的上游分支 'feature/WA-3:WA-3' 不存在。 - PositiveGuy
我在 Github 上给它取的名字不好吗?为什么找不到它? - PositiveGuy
我把远程分支直接命名为feature/WA-3 - PositiveGuy
有点奇怪,我在GitHub上看到那个分支了,但是执行这个命令却没有列出那个特定的分支:" ▶ git branch -r origin/HEAD -> origin/develop origin/develop origin/master origin/w1-log-in origin/w9 origin/wa-9-homepage - PositiveGuy
我真的不敢相信这个。我删除了远程分支并重新创建了一个名为W3的新分支,但是当我执行git branch -r时,它甚至看不到它。 - PositiveGuy
好的,你的代码可行。▶ git push -u origin WA-3:WA-3 分支WA-3已设置为跟踪来自origin的远程分支WA-3。 - PositiveGuy

0

我不确定是否有什么遗漏,每当涉及到这个问题时,我总是回到初学者的状态。

你可以轻松地重命名你的分支,例如使用lazygit,这只需要一个按钮步骤,或者从给定的分支检出一个具有正确名称的新分支。

首先尝试使用push

$ git push
fatal: The upstream branch of your current branch does not match
the name of your current branch.  To push to the upstream branch
on the remote, use

    git push origin HEAD:master

To push to the branch of the same name on the remote, use

    git push origin HEAD

To choose either option permanently, see push.default in 'git help config'.

你在这里看到了两个选项,请选择后者。

因此,首先将您的分支重命名为它在存储库中应该具有的名称。一旦您的本地分支名称是您想在存储库中看到的名称,您只需运行

git push -f origin HEAD 

这样新的本地分支(HEAD)就会被推到远程主分支(origin)的顶部。它会推送到远程。

看起来像:

$ git push -f origin HEAD
Enumerating objects: 21, done.
Counting objects: 100% (21/21), done.
Delta compression using up to 8 threads
Compressing objects: 100% (9/9), done.
Writing objects: 100% (11/11), 1019 bytes | 1019.00 KiB/s, done.
Total 11 (delta 4), reused 0 (delta 0), pack-reused 0
remote:
remote: To create a merge request for my_new_remote_branch_name_that_is_now_the_name_of_HEAD, visit:
remote:   https://gitlab.com/my_URL_to_the_repo
remote:
To gitlab.com:my_URL_to_the_repo
 * [new branch]        HEAD -> my_new_remote_branch_name_that_is_now_the_name_of_HEAD

git origin HEAD 命令只是 git push -u origin HEAD:my_new_remote_branch_name_that_is_now_the_name_of_HEAD 的简写形式。这与以下命令相同:git push --set-upstream origin HEAD:my_new_remote_branch_name_that_is_now_the_name_of_HEAD

只有当你想要选择一个与 HEAD 不同的远程分支名称时,才需要使用答案中提到的长命令。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接