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 ###
2
9
3
10
If you've messed up the working tree, but haven't yet committed your
4
11
mistake, you can return the entire working tree to the last committed
5
12
state with
6
13
7
14
$ git reset --hard HEAD
8
15
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
+
9
35
If you make a commit that you later wish you hadn't, there are two
10
36
fundamentally different ways to fix the problem:
11
37
@@ -19,8 +45,7 @@ fundamentally different ways to fix the problem:
19
45
change, and cannot correctly perform repeated merges from
20
46
a branch that has had its history changed.
21
47
22
-
23
- ### Fixing a mistake with a new commit ###
48
+ #### Fixing a mistake with a new commit ####
24
49
25
50
Creating a new commit that reverts an earlier change is very easy;
26
51
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
39
64
intact any changes made since then. If more recent changes overlap
40
65
with the changes to be reverted, then you will be asked to fix
41
66
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