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/05-distributed-git/1-distributed-git.asc
+1Lines changed: 1 addition & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,4 @@
1
+
[[_distributed_git]]
1
2
== Distributed Git
2
3
3
4
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.
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:
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:
0 commit comments