我刚刚向错误的分支做出了完美的承诺。 我如何撤消在我的主分支中的最后一次提交,然后采取这些相同的更改,并将它们放入我的升级分支?


当前回答

我最近做了同样的事情,当我应该提交给其他分支时,我不小心提交了一个更改给master。但我没有推动任何东西。

如果你只是提交了错误的分支,并且从那以后没有改变任何东西,也没有推送到回购,那么你可以执行以下操作:

// rewind master to point to the commit just before your most recent commit.
// this takes all changes in your most recent commit, and turns them into unstaged changes. 
git reset HEAD~1 

// temporarily save your unstaged changes as a commit that's not attached to any branch using git stash
// all temporary commits created with git stash are put into a stack of temporary commits.
git stash

// create other-branch (if the other branch doesn't already exist)
git branch other-branch

// checkout the other branch you should have committed to.
git checkout other-branch

// take the temporary commit you created, and apply all of those changes to the new branch. 
//This also deletes the temporary commit from the stack of temp commits.
git stash pop

// add the changes you want with git add...

// re-commit your changes onto other-branch
git commit -m "some message..."

注意:在上面的例子中,我用git重置HEAD~1来重卷1提交。但如果你想倒带n次提交,你可以做git reset HEAD~n。

同样,如果你最终提交了错误的分支,并且在意识到你提交了错误的分支之前写了更多的代码,那么你可以使用git stash来保存你正在进行的工作:

// save the not-ready-to-commit work you're in the middle of
git stash 

// rewind n commits
git reset HEAD~n 

// stash the committed changes as a single temp commit onto the stack. 
git stash 

// create other-branch (if it doesn't already exist)
git branch other-branch

// checkout the other branch you should have committed to.
git checkout other-branch

// apply all the committed changes to the new branch
git stash pop

// add the changes you want with git add...

// re-commit your changes onto the new branch as a single commit.
git commit -m "some message..."

// pop the changes you were in the middle of and continue coding
git stash pop

注意:我使用这个网站作为参考 https://www.clearvision-cm.com/blog/what-to-do-when-you-commit-to-the-wrong-git-branch/

其他回答

如果你有一个干净的(未修改的)工作副本

回滚一次提交(确保你注意到了下一步的提交散列):

git reset --hard HEAD^

将该提交拉到不同的分支:

git checkout other-branch
git cherry-pick COMMIT-HASH

如果您已修改或未跟踪更改

还需要注意的是,git reset -hard会杀死你可能拥有的任何未跟踪和修改的更改,所以如果你有这些更改,你可能更喜欢:

git reset HEAD^
git checkout .

如果你想要应用你的更改的分支已经存在(例如分支开发),遵循下面fotanus提供的说明,那么:

git checkout develop
git rebase develop my_feature # applies changes to correct branch
git checkout develop # 'cuz rebasing will leave you on my_feature
git merge develop my_feature # will be a fast-forward
git branch -d my_feature

显然,如果你愿意,你可以使用tempbranch或任何其他分支名称来代替my_feature。

同样,如果适用的话,将stash弹出(apply)延迟到合并目标分支之后。

如果你还没有推送你的更改,你也可以做一个软重置:

git reset --soft HEAD^

这将恢复提交,但将提交的更改放回索引中。假设分支之间是相对最新的,git会让你签入另一个分支,然后你可以简单地提交:

git checkout branch
git commit -c ORIG_HEAD

-c ORIG_HEAD部分对于不再输入提交消息很有用。

对我来说,这个问题的解决方法是恢复我之前推送的提交,然后将该提交选择到另一个分支。

git checkout branch_that_had_the_commit_originally
git revert COMMIT-HASH
git checkout branch_that_was_supposed_to_have_the_commit
git cherry pick COMMIT-HASH

您可以使用git log来找到正确的散列,并且您可以随时推送这些更改!

因此,如果你的场景是你已经提交给master,但打算提交给另一个分支(可能不存在,也可能不存在),但你还没有推送,这很容易修复。

// if your branch doesn't exist, then add the -b argument 
git checkout -b another-branch
git branch --force master origin/master

现在你对master的所有提交都将在另一个分支上。

来自:http://haacked.com/archive/2015/06/29/git-migrate/