我刚刚向错误的分支做出了完美的承诺。 我如何撤消在我的主分支中的最后一次提交,然后采取这些相同的更改,并将它们放入我的升级分支?
当前回答
用于在错误的分支上进行多次提交
如果对你来说,它只是1次提交,那么有很多其他更简单的重置解决方案可用。对于我来说,我无意中在主分支上创建了大约10个提交,而不是,让我们称之为目标,我不想失去提交历史。
你能做的,也是拯救我的,就是把这个答案作为参考,用一个4步的过程,也就是:
从master创建一个新的临时分支temp 将temp合并到最初用于提交的分支中,即目标 在主服务器上撤销提交 删除临时分支temp。
以下是上述步骤的详细说明:
Create a new branch from the master (where I had accidentally committed a lot of changes) git checkout -b temp Note: -b flag is used to create a new branch Just to verify if we got this right, I'd do a quick git branch to make sure we are on the temp branch and a git log to check if we got the commits right. Merge the temporary branch into the branch originally intended for the commits, i.e. target. First, switch to the original branch i.e. target (You might need to git fetch if you haven't) git checkout target Note: Not using -b flag Now, let's merge the temporary branch into the branch we have currently checkout out target git merge temp You might have to take care of some conflicts here, if there are. You can push (I would) or move on to the next steps, after successfully merging. Undo the accidental commits on master using this answer as reference, first switch to the master git checkout master then undo it all the way back to match the remote using the command below (or to particular commit, using appropriate command, if you want) git reset --hard origin/master Again, I'd do a git log before and after just to make sure that the intended changes took effect. Erasing the evidence, that is deleting the temporary branch. For this, first you need to checkout the branch that the temp was merged into, i.e. target (If you stay on master and execute the command below, you might get a error: The branch 'temp' is not fully merged), so let's git checkout target and then delete the proof of this mishap git branch -d temp
其他回答
如果你还没有推送你的更改,你也可以做一个软重置:
git reset --soft HEAD^
这将恢复提交,但将提交的更改放回索引中。假设分支之间是相对最新的,git会让你签入另一个分支,然后你可以简单地提交:
git checkout branch
git commit -c ORIG_HEAD
-c ORIG_HEAD部分对于不再输入提交消息很有用。
如果你遇到这个问题,并且你有Visual Studio,你可以做以下事情:
右键单击分支并选择View History:
右键单击要返回的commit。并根据需要恢复或重置。
因此,如果你的场景是你已经提交给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/
如果您已经执行了更改,则需要在重置HEAD后强制执行下一次推送。
git reset --hard HEAD^
git merge COMMIT_SHA1
git push --force
警告:硬重置将撤销工作副本中任何未提交的修改,而强制推将完全覆盖远程分支的状态与本地分支的当前状态。
以防万一,在Windows上(使用Windows命令行,而不是Bash),它实际上是四个^^^^而不是一个,所以它是
git reset --hard HEAD^^^^
这个话题晚了4年,但这可能对某些人有帮助。
如果你忘记在提交之前创建一个新的分支,并在master上提交所有的分支,无论你做了多少次提交,下面的方法都更容易:
git stash # skip if all changes are committed
git branch my_feature
git reset --hard origin/master
git checkout my_feature
git stash pop # skip if all changes were committed
现在你的主分支等于origin/master,所有的新提交都在my_feature上。注意,my_feature是一个本地分支,而不是远程分支。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别