Skip to content

Commit c3e2621

Browse files
committed
Merge pull request #43 from schacon/chapter-3-polish
Chapter 3 polish
2 parents 191c89b + 79dbb50 commit c3e2621

40 files changed

+47213
-45397
lines changed

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

Lines changed: 252 additions & 277 deletions
Large diffs are not rendered by default.
Loading
Loading
Loading
88.4 KB
Loading
Loading
Loading
Loading
Loading
Loading
Loading
39.1 KB
Loading
49.5 KB
Loading
64.5 KB
Loading
70.4 KB
Loading
72.5 KB
Loading
Loading
80.8 KB
Loading
Loading
Loading
Loading
Loading
Loading
93.6 KB
Loading
Loading
87.2 KB
Loading
Loading
74.7 KB
Loading
Loading
Loading
Loading
Loading
67.2 KB
Loading
Loading
Loading
Loading
63.9 KB
Loading

book/05-distributed-git/1-distributed-git.asc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
[[_distributed_git]]
12
== Distributed Git
23

34
Now that you have a remote Git repository set up as a point for all the developers to share their code, and you’re familiar with basic Git commands in a local workflow, you’ll look at how to utilize some of the distributed workflows that Git affords you.

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,6 +1798,92 @@ 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+
==== Undoing Merges
1803+
1804+
Now that you know how to create a merge commit, you'll probably make some by mistake.
1805+
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.
1806+
1807+
Merge commits are no different.
1808+
Let's say you started work on a topic branch, accidentally merged it into `master`, and now your commit history looks like this:
1809+
1810+
.Accidental merge commit
1811+
image::images/undomerge-start.png[Accidental merge commit.]
1812+
1813+
There are two ways to approach this problem, depending on what your desired outcome is.
1814+
1815+
===== Fix the references
1816+
1817+
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.
1818+
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:
1819+
1820+
.History after `git reset --merge`
1821+
image::images/undomerge-reset.png[History after `git reset --merge`.]
1822+
1823+
We covered `reset` back in <<_reset>>, so it shouldn't be too hard to figure out what's going on here.
1824+
Here's a quick refresher: `reset --hard` usually goes through three steps:
1825+
1826+
. Move the branch HEAD points to.
1827+
In this case, we want to move `master` to where it was before the merge commit (`C6`).
1828+
. Make the index look like HEAD.
1829+
. Make the working directory look like the index.
1830+
1831+
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.
1832+
1833+
The downside of this approach is that it's rewriting history, which can be prolematic with a shared repository.
1834+
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`.
1835+
This approach also won't work if any other commits have been created since the merge; moving the refs would effectively lose those changes.
1836+
1837+
===== Reverse the commit
1838+
1839+
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.
1840+
Git calls this operation a ``revert'', and in this particular scenario, you'd invoke it like this:
1841+
1842+
[source,shell]
1843+
----
1844+
$ git revert -m 1 HEAD
1845+
[master b1d8379] Revert "Merge branch 'topic-branch'"
1846+
----
1847+
1848+
The `-m 1` flag indicates which parent is the ``mainline'' and should be kept.
1849+
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`).
1850+
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`).
1851+
1852+
The history with the revert commit looks like this:
1853+
1854+
.History after `git revert -m 1`
1855+
image::images/undomerge-revert.png[History after `git revert -m 1`.]
1856+
1857+
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.
1858+
Git will get confused if you try to merge `topic-branch` into `master` again:
1859+
1860+
[source,shell]
1861+
----
1862+
$ git merge topic-branch
1863+
Already up-to-date.
1864+
----
1865+
1866+
There's nothing in `topic-branch` that isn't already reachable from `master`.
1867+
What's worse, if you add work to `topic-branch` and merge again, Git will only bring in the changes _since_ the reverted merge:
1868+
1869+
.History with a bad merge
1870+
image::images/undomerge-revert2.png[History with a bad merge.]
1871+
1872+
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:
1873+
1874+
[source,shell]
1875+
----
1876+
$ git revert ^M
1877+
[master 09f0126] Revert "Revert "Merge branch 'topic-branch'""
1878+
$ git merge topic-branch
1879+
----
1880+
1881+
.History after re-merging a reverted merge
1882+
image::images/undomerge-revert3.png[History after re-merging a reverted merge.]
1883+
1884+
In this example, `M` and `^M` cancel out.
1885+
`^^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.
1886+
18011887
=== Notes
18021888

18031889
=== Bundle

0 commit comments

Comments
 (0)