Skip to content

Commit 7b593b5

Browse files
committed
Additional text for undoing mistakes.
Talk about git-checkout and mention git-rebase -i. Re-organize section to give more top-down clarity.
1 parent c0b0757 commit 7b593b5

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

text/21_Redoing_Git_Reset_and_Revert/0_ Redoing_Git_Reset_and_Revert.markdown

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
1-
## Undoing in Git - Reset and Revert ##
1+
## Undoing in Git - Reset, Checkout and Revert ##
2+
3+
Git provides multiple methods for fixing up mistakes as you
4+
are developing. Selecting an appropriate method depends on whether
5+
or not you have committed the mistake, and if you have committed the
6+
mistake, whether you have shared the erroneous commit with anyone else.
7+
8+
### Fixing un-committed mistakes ###
29

310
If you've messed up the working tree, but haven't yet committed your
411
mistake, you can return the entire working tree to the last committed
512
state with
613

714
$ git reset --hard HEAD
815

16+
This will throw away any changes you may have added to the git index
17+
and as well as any outstanding changes you have in your working tree.
18+
In other words, it causes the results of "git diff" and "git diff --cached"
19+
to both be empty.
20+
21+
If you just want to restore just one file, say your hello.rb, use
22+
linkgit:git-checkout[1] instead
23+
24+
$ git checkout -- hello.rb
25+
$ git checkout HEAD hello.rb
26+
27+
The first command restores hello.rb to the version in the index,
28+
so that "git diff hello.rb" returns no differences. The second command
29+
will restore hello.rb to the version in the HEAD revision, so
30+
that both "git diff hello.rb" and "git diff --cached hello.rb"
31+
return no differences.
32+
33+
### Fixing committed mistakes ###
34+
935
If you make a commit that you later wish you hadn't, there are two
1036
fundamentally different ways to fix the problem:
1137

@@ -19,8 +45,7 @@ fundamentally different ways to fix the problem:
1945
change, and cannot correctly perform repeated merges from
2046
a branch that has had its history changed.
2147

22-
23-
### Fixing a mistake with a new commit ###
48+
#### Fixing a mistake with a new commit ####
2449

2550
Creating a new commit that reverts an earlier change is very easy;
2651
just pass the linkgit:git-revert[1] command a reference to the bad
@@ -39,4 +64,20 @@ In this case git will attempt to undo the old change while leaving
3964
intact any changes made since then. If more recent changes overlap
4065
with the changes to be reverted, then you will be asked to fix
4166
conflicts manually, just as in the case of <<resolving-a-merge,
42-
resolving a merge>>.
67+
resolving a merge>>.
68+
69+
#### Fixing a mistake by modifying a commit ####
70+
71+
If you have just committed something but realize you need to fix
72+
up that commit, recent versions of linkgit:git-commit[1] support an
73+
**--amend** flag which instructs git to replace the HEAD commit
74+
with a new one, based on the current contents of the index. This
75+
gives you an opportunity to add files that you forgot to add or
76+
correct typos in a commit message, prior to pushing the change
77+
out for the world to see.
78+
79+
If you find a mistake in an older commit, but still one that you
80+
have not yet published to the world, you use linkgit:git-rebase[1]
81+
in interactive mode, with "git rebase -i" marking the change
82+
that requires correction with **edit**. This will allow you
83+
to amend the commit during the rebasing process.

0 commit comments

Comments
 (0)