【发布时间】:2019-02-02 09:22:55
【问题描述】:
从我们的develop 分支,我用git checkout -b jsm/logging 创建了一个新分支。使用git push -u origin HEAD 进行更改、提交并推送到原点。做了一个 PR 并合并并删除了 remote 分支。进行了另一次调整,并用git commit --amend --no-edit -a 修改了我的最后一次提交。然后我检查了我的状态并使用git push -f 强制推送。令我惊讶的是,错误的分支被(力)推了!查看我的控制台日志(请注意,我将 g 别名为 git,st 是 status 的别名,co 是 checkout 的别名)。
旁注:我还注意到,例如,当我尝试推送 develop 时,Git 经常抱怨 master 不同步(需要先拉取)——但为什么它会与当我不在那个分支上时主人?好像有关系,不知道是什么问题。
控制台日志(“$”前的分支名称):
josh:~/Projects/my-project jsm/logging $ git commit --amend --no-edit -a
[jsm/logging 4cdb3dc] add logging
Date: Mon Aug 27 15:18:41 2018 -0400
1 file changed, 12 insertions(+), 6 deletions(-)
josh:~/Projects/my-project jsm/logging $ git st
## jsm/logging...origin/jsm/logging [ahead 1, behind 1]
josh:~/Projects/my-project jsm/logging $ git push -f
Counting objects: 1, done.
Writing objects: 100% (1/1), 685 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To git@github.com:my-org/my-project
+ 5a649bc...8d320d2 develop -> develop (forced update)
^^^^^^^ why is it pushing a different branch than I'm on?!
josh:~/Projects/my-project jsm/logging $ g co develop
Switched to branch 'develop'
Your branch is up-to-date with 'origin/develop'.
josh:~/Projects/my-project develop $ g co jsm/logging
Switched to branch 'jsm/logging'
Your branch and 'origin/jsm/logging' have diverged,
and have 1 and 1 different commit each, respectively.
(use "git pull" to merge the remote branch into yours)
josh:~/Projects/my-project jsm/logging $ git st
## jsm/logging...origin/jsm/logging [ahead 1, behind 1]
josh:~/Projects/my-project jsm/logging $ git push -fu origin jsm/logging
Counting objects: 11, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (11/11), 1.04 KiB | 0 bytes/s, done.
Total 11 (delta 5), reused 0 (delta 0)
remote: Resolving deltas: 100% (5/5), completed with 5 local objects.
To git@github.com:my-org/my-project
* [new branch] jsm/logging -> jsm/logging
Branch jsm/logging set up to track remote branch jsm/logging from origin.
git 配置
alias.st=status -sb
alias.co=checkout
alias.cob=checkout -b
pull.rebase=true
push.default=matching
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git@github.com:my-org/my-project
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.develop.remote=origin
branch.develop.merge=refs/heads/develop
branch.jsm/logging.remote=origin
branch.jsm/logging.merge=refs/heads/jsm/logging
【问题讨论】:
-
你的 git 配置是
push.default=matching。这意味着您总是在推送所有名称与原点分支匹配的分支。尝试使用push.default=current仅推送当前分支。不知道为什么没有推送jsm/logging,您确实正确设置了上游跟踪分支(git push -u origin HEAD)。 -
在这种情况下,我通常使用
git reflog来帮助我了解发生了什么。它向您显示您本地 HEAD 所在的所有状态,包括切换分支。我希望你也觉得这很有帮助。 -
而不是使用
git push -f。您可能需要指定您将哪个分支强制推送到原点。例如,git push -f origin <branch name> -
谢谢@knugie。我确实使用了 reflog,但在这种情况下它没有照亮任何东西,所以我将它从 OP 的控制台输出中排除。
-
@Nisarg -- 谢谢,我已经为分支设置了跟踪 (
push -u origin HEAD)。
标签: git github feature-branch