Skip to content

Commit 199901d

Browse files
committed
move undoing merges to ch7
1 parent 2aa4b2f commit 199901d

File tree

2 files changed

+85
-84
lines changed

2 files changed

+85
-84
lines changed

book/03-git-branching/1-git-branching.asc

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -493,90 +493,6 @@ Conflicts:
493493

494494
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.
495495

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:
503-
504-
.Accidental merge commit
505-
image::images/undomerge-start.png[Accidental merge commit.]
506-
507-
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:
535-
536-
[source,shell]
537-
----
538-
$ git revert -m 1 HEAD
539-
[master b1d8379] Revert "Merge branch 'topic-branch'"
540-
----
541-
542-
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:
567-
568-
[source,shell]
569-
----
570-
$ git revert ^M
571-
[master 09f0126] Revert "Revert "Merge branch 'topic-branch'""
572-
$ git merge topic-branch
573-
----
574-
575-
.History after re-merging a reverted merge
576-
image::images/undomerge-revert3.png[History after re-merging a reverted merge.]
577-
578-
In this example, `M` and `^M` cancel out.
579-
`^^M` effectively merges in the changes from `C3` and `C4`, and `C8` merges in the changes from `C7`, so now `topic-branch` is fully merged.
580496

581497
=== Branch Management
582498

book/07-git-tools/1-git-tools.asc

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,6 +1798,91 @@ Or, to compare what is in your `rack` subdirectory with what the `master` branch
17981798
$ git diff-tree -p rack_remote/master
17991799
----
18001800

1801+
==== Undoing Merges
1802+
1803+
Now that you know how to create a merge commit, you'll probably make some by mistake.
1804+
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.
1805+
1806+
Merge commits are no different.
1807+
Let's say you started work on a topic branch, accidentally merged it into `master`, and now your commit history looks like this:
1808+
1809+
.Accidental merge commit
1810+
image::images/undomerge-start.png[Accidental merge commit.]
1811+
1812+
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:
1840+
1841+
[source,shell]
1842+
----
1843+
$ git revert -m 1 HEAD
1844+
[master b1d8379] Revert "Merge branch 'topic-branch'"
1845+
----
1846+
1847+
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:
1872+
1873+
[source,shell]
1874+
----
1875+
$ git revert ^M
1876+
[master 09f0126] Revert "Revert "Merge branch 'topic-branch'""
1877+
$ git merge topic-branch
1878+
----
1879+
1880+
.History after re-merging a reverted merge
1881+
image::images/undomerge-revert3.png[History after re-merging a reverted merge.]
1882+
1883+
In this example, `M` and `^M` cancel out.
1884+
`^^M` effectively merges in the changes from `C3` and `C4`, and `C8` merges in the changes from `C7`, so now `topic-branch` is fully merged.
1885+
18011886
=== Notes
18021887

18031888
=== Bundle

0 commit comments

Comments
 (0)