You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/03-git-branching/1-git-branching.asc
-84Lines changed: 0 additions & 84 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -493,90 +493,6 @@ Conflicts:
493
493
494
494
You can modify that message with details about how you resolved the merge if you think it would be helpful to others looking at this merge in the future – why you did what you did, if it's not obvious.
495
495
496
-
==== Undoing Merges
497
-
498
-
Now that you know how to create a merge commit, you'll probably make some by mistake.
499
-
One of the great things about working with Git is that it's okay to make mistakes, because it's possible (and in many cases easy) to fix them.
500
-
501
-
Merge commits are no different.
502
-
Let's say you started work on a topic branch, accidentally merged it into `master`, and now your commit history looks like this:
There are two ways to approach this problem, depending on what your desired outcome is.
508
-
509
-
===== Fix the refs
510
-
511
-
If the unwanted merge commit only exists on your local repository, the easiest and best solution is to move the branches so that they point where you want them to.
512
-
In most cases, if you follow the errant `git merge` with `git reset --merge ORIG_HEAD`, this will reset the branch pointers so they look like this:
513
-
514
-
.History after `git reset --merge`
515
-
image::images/undomerge-reset.png[History after `git reset --merge`.]
516
-
517
-
We covered `reset` back in <<_reset>>, so it shouldn't be too hard to figure out what's going on here.
518
-
Here's a quick refresher: `reset --hard` usually goes through three steps:
519
-
520
-
. Move the branch HEAD points to.
521
-
In this case, we want to move `master` to where it was before the merge commit (`C6`).
522
-
. Make the index look like HEAD.
523
-
. Make the working directory look like the index.
524
-
525
-
In the case of `--merge`, Git is extra careful with steps 2 and 3 to preserve any changes you've made in the working directory or the index, but otherwise works as though this were a `--hard` reset.
526
-
527
-
The downside of this approach is that it's rewriting history, which can be prolematic with a shared repository.
528
-
Check out <<_rebase_peril>> for more on what can happen; the short version is that if other people have the commits you're rewriting, you should probably avoid `reset`.
529
-
This approach also won't work if any other commits have been created since the merge; moving the refs would effectively lose those changes.
530
-
531
-
===== Reverse the commit
532
-
533
-
If moving the branch pointers around isn't going to work for you, Git gives you the option of making a new commit which undoes all the changes from an existing one.
534
-
Git calls this operation a ``revert'', and in this particular scenario, you'd invoke it like this:
The `-m 1` flag indicates which parent is the ``mainline'' and should be kept.
543
-
When you invoke a merge into `HEAD` (`git merge topic-branch`), the new commit has two parents: the first one is `HEAD` (`C6`), and the second is the tip of the branch being merged in (`C4`).
544
-
In this case, we want to undo all the changes introduced by merging in parent #2 (`C4`), while keeping all the content from parent #1 (`C6`).
545
-
546
-
The history with the revert commit looks like this:
547
-
548
-
.History after `git revert -m 1`
549
-
image::images/undomerge-revert.png[History after `git revert -m 1`.]
550
-
551
-
The new commit `^M` has exactly the same contents as `C6`, so starting from here it's as if the merge never happened, except that the now-unmerged commits are still in `HEAD`'s history.
552
-
Git will get confused if you try to merge `topic-branch` into `master` again:
553
-
554
-
[source,shell]
555
-
----
556
-
$ git merge topic-branch
557
-
Already up-to-date.
558
-
----
559
-
560
-
There's nothing in `topic-branch` that isn't already reachable from `master`.
561
-
What's worse, if you add work to `topic-branch` and merge again, Git will only bring in the changes _since_ the reverted merge:
562
-
563
-
.History with a bad merge
564
-
image::images/undomerge-revert2.png[History with a bad merge.]
565
-
566
-
The best way around this is to un-revert the original merge, since now you want to bring in the changes that were reverted out, *then* create a new merge commit:
There are two ways to approach this problem, depending on what your desired outcome is.
1813
+
1814
+
===== Fix the references
1815
+
1816
+
If the unwanted merge commit only exists on your local repository, the easiest and best solution is to move the branches so that they point where you want them to.
1817
+
In most cases, if you follow the errant `git merge` with `git reset --merge ORIG_HEAD`, this will reset the branch pointers so they look like this:
1818
+
1819
+
.History after `git reset --merge`
1820
+
image::images/undomerge-reset.png[History after `git reset --merge`.]
1821
+
1822
+
We covered `reset` back in <<_reset>>, so it shouldn't be too hard to figure out what's going on here.
1823
+
Here's a quick refresher: `reset --hard` usually goes through three steps:
1824
+
1825
+
. Move the branch HEAD points to.
1826
+
In this case, we want to move `master` to where it was before the merge commit (`C6`).
1827
+
. Make the index look like HEAD.
1828
+
. Make the working directory look like the index.
1829
+
1830
+
In the case of `--merge`, Git is extra careful with steps 2 and 3 to preserve any changes you've made in the working directory or the index, but otherwise works as though this were a `--hard` reset.
1831
+
1832
+
The downside of this approach is that it's rewriting history, which can be prolematic with a shared repository.
1833
+
Check out <<_rebase_peril>> for more on what can happen; the short version is that if other people have the commits you're rewriting, you should probably avoid `reset`.
1834
+
This approach also won't work if any other commits have been created since the merge; moving the refs would effectively lose those changes.
1835
+
1836
+
===== Reverse the commit
1837
+
1838
+
If moving the branch pointers around isn't going to work for you, Git gives you the option of making a new commit which undoes all the changes from an existing one.
1839
+
Git calls this operation a ``revert'', and in this particular scenario, you'd invoke it like this:
The `-m 1` flag indicates which parent is the ``mainline'' and should be kept.
1848
+
When you invoke a merge into `HEAD` (`git merge topic-branch`), the new commit has two parents: the first one is `HEAD` (`C6`), and the second is the tip of the branch being merged in (`C4`).
1849
+
In this case, we want to undo all the changes introduced by merging in parent #2 (`C4`), while keeping all the content from parent #1 (`C6`).
1850
+
1851
+
The history with the revert commit looks like this:
1852
+
1853
+
.History after `git revert -m 1`
1854
+
image::images/undomerge-revert.png[History after `git revert -m 1`.]
1855
+
1856
+
The new commit `^M` has exactly the same contents as `C6`, so starting from here it's as if the merge never happened, except that the now-unmerged commits are still in `HEAD`'s history.
1857
+
Git will get confused if you try to merge `topic-branch` into `master` again:
1858
+
1859
+
[source,shell]
1860
+
----
1861
+
$ git merge topic-branch
1862
+
Already up-to-date.
1863
+
----
1864
+
1865
+
There's nothing in `topic-branch` that isn't already reachable from `master`.
1866
+
What's worse, if you add work to `topic-branch` and merge again, Git will only bring in the changes _since_ the reverted merge:
1867
+
1868
+
.History with a bad merge
1869
+
image::images/undomerge-revert2.png[History with a bad merge.]
1870
+
1871
+
The best way around this is to un-revert the original merge, since now you want to bring in the changes that were reverted out, *then* create a new merge commit:
0 commit comments