Git: How To Revert A Branch Merge
来源:互联网 发布:淘宝店铺专享红包 编辑:程序博客网 时间:2024/04/30 03:52
This was a stumper for me a while back. Jason Meridth posted on reseting / reverting git commits and I had a question in the comments. My specific situation was about having a branch merged at the wrong time and how to undo that merge. Here’s an example of the situation, shown with gitk:
In this scenario, the “test” branch was not supposed to be merged yet… i missed some changed, or i needed to do something else with the master branch, etc. Whatever the reason, I wanted to rollback to the point where test and master were not yet merged. As i stated in the comments on jason’s post, I ran around in circles trying to get ‘git revert’ to do what I wanted, with no luck. Lucky for me, Nick Quranto hada blog post on reverting with git, pointed me toward an in-depth read on the subject and was happy to answer my questions via email.
It turned out the answer was much more simple than what I was trying to do. Since I merged the test branch into the master branch, I can run this from the master branch:
1: git reset --hard HEAD^
Here, you can see that this moves the HEAD of master back to the previous commit (Note: for more info on what HEAD^ means, seeGit Treeishes in the git docs):
Apparently the “revert” command differs from the “reset” command in a very important way: revert will take the changes from whatever commit you specify and undo them, creating a new commit for the new version. In contrast to that, “reset” will move the pointer that represents your branch’s location to whatever point you specify. So, doing a reset on the test master branch allowed me to move the current location of HEAD back to the previous commit on that branch (Another Note: I know I’m messing up the detail on how reset vs. revert works… this is just a basic high level experience report, rather than an in-depth discussion on what really goes on).
The result looks like this in gitk:
This is exactly what I wanted: the master and test branch are no longer merged and I can continue working on either / both of them independently with the ability to merge them together at some later point in time.
- Git: How To Revert A Branch Merge
- Git: How to revert a merge commit that's already pushed to remote branch?
- Revert to a previous branch
- how to rename a git branch
- How do I use 'git reset --hard HEAD' to revert to a previous commit?
- How to Push a New Local Branch to a Remote Git Repository
- git tips - merge, revert
- Revert to a previous Git commit
- 如何重新命名Git分支(how to rename a git branch locally and remotely?)
- Revert a merge commit
- git revert to revision
- git---如何解决git pull之后报错:Please enter a commit to explain why this merge is necessary?(merge branch)
- TARDiS:A Branch-and-Merge Approach To Weak Consistency
- How to delete the remote branch in git
- How to Use Kdiff3 as a 3-way Merge Tool With Mercurial, Git, and Tower.app
- How to unshelve a shelveset into another branch with TFS
- git的branch以及merge
- git merge不同branch文件
- 快速排序(C.A.R.Hoare版)
- apple的一个专利
- ok6410学习笔记(11.kobject学习记录)
- IOS: iPhone键盘通知与键盘定制
- 从程序员到项目经理(8):程序员加油站 -- 不要死于直率
- Git: How To Revert A Branch Merge
- Linux内核-信号之signal处理函数
- 从程序员到项目经理(9):程序员加油站 -- 再牛也要合群
- Hibernate 常用方法
- 计算机科学中最重要的32个算法
- 从程序员到项目经理(10):程序员加油站 --要执着但不要固执
- SEH异常处理学习总结 - ToBeroOTer的专栏 - CSDNBlog
- C运行时库函数
- Exported service does not require permission