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/07-git-tools/sections/rewriting-history.asc
+25-11Lines changed: 25 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
1
[[_rewriting_history]]
2
2
=== Rewriting History
3
3
4
-
Many times, when working with Git, you may want to revise your commit history for some reason.
4
+
Many times, when working with Git, you may want to revise your local commit history.
5
5
One of the great things about Git is that it allows you to make decisions at the last possible moment.
6
6
You can decide what files go into which commits right before you commit with the staging area, you can decide that you didn’t mean to be working on something yet with `git stash`, and you can rewrite commits that already happened so they look like they happened in a different way.
7
-
This can involve changing the order of the commits, changing messages or modifying files in a commit, squashing together or splitting apart commits, or removing commits entirely – all before you share your work with others.
7
+
This can involve changing the order of the commits, changing messages or modifying files in a commit, squashing together or splitting apart commits, or removing commits entirely -- all before you share your work with others.
8
8
9
-
In this section, you’ll cover how to accomplish these very useful tasks so that you can make your commit history look the way you want before you share it with others.
9
+
In this section, you’ll see how to accomplish these tasks so that you can make your commit history look the way you want before you share it with others.
10
10
11
11
[NOTE]
12
12
====
@@ -18,24 +18,38 @@ In short, you should avoid pushing your work until you're happy with it and read
18
18
[[_git_amend]]
19
19
==== Changing the Last Commit
20
20
21
-
Changing your last commit is probably the most common rewriting of history that you’ll do.
22
-
You’ll often want to do two basic things to your last commit: change the commit message, or change the snapshot you just recorded by adding, changing and removing files.
21
+
Changing your most recent commit is probably the most common rewriting of history that you’ll do.
22
+
You’ll often want to do two basic things to your last commit: simply change the commit message, or change the actual content of the commit by adding, removing and modifying files.
23
23
24
-
If you only want to modify your last commit message, it’s very simple:
24
+
If you simply want to modify your last commit message, that's easy:
25
25
26
26
[source,console]
27
27
----
28
28
$ git commit --amend
29
29
----
30
30
31
-
That drops you into your text editor, which has your last commit message in it, ready for you to modify the message.
32
-
When you save and close the editor, the editor writes a new commit containing that message and makes it your new last commit.
31
+
The command above loads the previous commit message into an editor session, where you can make changes to the message, save those changes and exit.
32
+
When you save and close the editor, the editor writes a new commit containing that updated commit message and makes it your new last commit.
33
33
34
-
If you’ve committed and then you want to change the snapshot you committed by adding or changing files, possibly because you forgot to add a newly created file when you originally committed, the process works basically the same way.
35
-
Make the changes you think you forgot, stage those changes, and the subsequent `git commit --amend` _replaces_ the previous commit with your new, improved commit.
34
+
If, on the other hand, you want to change the actual _content_ of your last commit, the process works basically the same way -- first make the changes you think you forgot, stage those changes, and the subsequent `git commit --amend` _replaces_ that last commit with your new, improved commit.
36
35
37
36
You need to be careful with this technique because amending changes the SHA-1 of the commit.
38
-
It’s like a very small rebase – don’t amend your last commit if you’ve already pushed it.
37
+
It’s like a very small rebase -- don’t amend your last commit if you’ve already pushed it.
38
+
39
+
[TIP]
40
+
.An amended commit may (or may not) need an amended commit message
41
+
====
42
+
When you amend a commit, you have the opportunity to change both the commit message and the content of the commit.
43
+
If you amend the content of the commit substantially, you should almost certainly update the commit message to reflect that amended content.
44
+
45
+
On the other hand, if your amendments are suitably trivial (fixing a silly typo or adding a file you forgot to stage) such that the earlier commit message is just fine, you can simply make the changes, stage them, and avoid the unnecessary editor session entirely with:
0 commit comments