Mistakenly committed to master locally. How to move changes to branch?
本问题已经有最佳答案,请猛点这里访问。
我做了一些可能破坏代码的更改。我以为我在一个分支,但当我承诺的时候,我是在主人。是否有任何方法可以将4个本地提交移动到本地分支,然后进行推送?
根据你所说的细节(4个承诺),你可以这样做:
1 2 3 4 5 6 7 8 9 10 11 | 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 |
号
推
一般来说,在Git中更改分支的提交可以通过三个简单步骤完成:
你也可以试试
git reset HEAD~
号
这将还原本地提交并还原更改。然后创建一个新的分支,提交并将更改推送到那里。
您应该能够创建新的分支,然后将
1 2 | git branch oopsies-feature-branch git branch -f master THE_RIGHT_COMMIT_FOR_MASTER |