错误地致力于在本地掌握。如何将更改移动到分支?

Mistakenly committed to master locally. How to move changes to branch?

我做了一些可能会破坏代码的更改。我以为我在一个分支机构,但当我提交时我在 master 。有什么办法可以把本地的4个commit移动到本地的一个分支然后push吗?

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

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

你也可以试试

git reset HEAD~

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

根据您所说的细节(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. "re-wind" 其他分支所以提交 不是
  3. 推送分支到远程(如有必要,使用-f