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: en/book/02-git-basics/chapter2.asc
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -626,10 +626,12 @@ image::images/18333fig0202-tn.png[The gitk history visualizer.]
626
626
627
627
You can see the commit history in the top half of the window along with a nice ancestry graph. The diff viewer in the bottom half of the window shows you the changes introduced at any commit you click.
628
628
629
+
[[_undoing]]
629
630
=== Undoing Things
630
631
631
632
At any stage, you may want to undo something. Here, we’ll review a few basic tools for undoing changes that you’ve made. Be careful, because you can’t always undo some of these undos. This is one of the few areas in Git where you may lose some work if you do it wrong.
There are two ways to approach this problem, depending on what your desired outcome is.
409
+
410
+
===== Fix the refs
411
+
412
+
If the unwanted merge commit only exists on your local repository, the solution is to move the branches so that they point where you want them to.
413
+
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:
414
+
415
+
.History after `git reset --merge`
416
+
image::images/undomerge-reset.png[History after `git reset --merge`.]
417
+
418
+
We covered `reset` back in <<_reset>>, so it shouldn't be too hard to figure out what's going on here.
419
+
Here's a quick refresher: `reset --hard` usually goes through three steps:
420
+
421
+
1. Move the ref that `HEAD` points to. In this case, we want to move `master` to where it was before the merge commit (`C4`)
422
+
2. Make the index look like `HEAD`.
423
+
3. Make the working directory look like the index.
424
+
425
+
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.
426
+
427
+
The downside of this approach is that it's rewriting history, which can be prolematic with a shared repository.
428
+
Check out <<_rebase_peril>> for more on what can happen, but if other people have the commits you're rewriting, you should probably avoid `reset`.
429
+
430
+
===== Reverse the commit
431
+
432
+
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.
433
+
Git calls this operation a ``revert'', and in this particular scenario, you'd invoke it like this:
434
+
435
+
[source,shell]
436
+
----
437
+
$ git revert -m 1
438
+
----
439
+
440
+
The `-m 1` flag tells Git which parent to keep.
441
+
When you invoke a merge into `HEAD` (`git merge experiment`), the new commit has two parents: the first one is `HEAD` (`C4`), and the second is the tip of the branch being merged in (`C3`).
442
+
In this case, we want to undo all the changes introduced by merging in parent #2 (`C3`), while keeping all the content from parent #1 (`C4`).
443
+
444
+
The history with the revert commit looks like this:
445
+
446
+
.History after `git revert -m 1`
447
+
448
+
Take a look at <<_undoing>> for more information on Git's various methods of undoing things.
400
449
401
-
- `git reset --merge ORIG_HEAD`
402
-
- `git revert -m 1 sha`
403
450
404
451
=== Branch Management
405
452
@@ -618,9 +665,8 @@ In Git, there are two main ways to integrate changes from one branch into anothe
618
665
619
666
==== The Basic Rebase
620
667
621
-
If you go back to an earlier example from <<_basic_merging>> (see <<rbdiag_a>>), you can see that you diverged your work and made commits on two different branches.
668
+
If you go back to an earlier example from <<_basic_merging>>, you can see that you diverged your work and made commits on two different branches.
0 commit comments