【问题标题】:Mistakenly committed to master locally. How to move changes to branch? [duplicate]错误地致力于本地掌握。如何将更改移动到分支? [复制]
【发布时间】:2018-12-23 12:53:29
【问题描述】:

我做了一些可能会破坏代码的更改。我以为我在一个分支中,但是当我提交时我在 master 中。有什么办法可以将 4 个本地提交移动到本地分支然后推送?

【问题讨论】:

标签: git branch


【解决方案1】:

按照你所说的细节(4次提交),你可以这样做:

git branch new-branch-name-here
# new commits on branch
git checkout master
git reset HEAD~4
# move HEAD (master) 4 commits back, commits are no longer on master
# note: that's a ~ (tilde, above your Tab), not a - (dash).
git push origin new-branch-name-here
# push new branch with correct commits to remote (assumed origin)
git push -f origin master
# if you already pushed master before, clear commits from remote
# otherwise, this can be skipped if master wasn't yet pushed remotely

推送master-f是必填项,否则你的推送会被服务器拒绝。

一般来说,在 Git 中更改分支上的提交可以通过三个简单的步骤完成:

  1. 使用您的提交创建新分支
  2. “重新缠绕”其他分支,因此提交不在
  3. 将分支推送到远程(如有必要,使用-f

【讨论】:

  • 他从未提到他推送了这些提交。不需要冒险的push -f
【解决方案2】:

你也可以试试

git 重置 HEAD~

这将恢复本地提交并恢复更改。 然后创建一个新分支,提交并推送您的更改。

【讨论】:

  • 这对我有用,因为我只需要在 repo 上重新开始(而且我没有使用 rebase)。虽然我需要添加--hard。简单 FTW。
【解决方案3】:

您应该能够只创建新分支,然后将 master 重置为正确的提交。

git branch oopsies-feature-branch
git branch -f master THE_RIGHT_COMMIT_FOR_MASTER

【讨论】:

    猜你喜欢
    • 2012-06-02
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    相关资源
    最近更新 更多