Skip to content

Commit

Permalink
Merge pull request progit#904 from rpjday/tools_commit_amend
Browse files Browse the repository at this point in the history
Minor rewording/grammar tweaks to "Rewriting History"
  • Loading branch information
ben authored Oct 27, 2017
2 parents 505ca8c + ced255f commit 99dc02f
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions book/07-git-tools/sections/rewriting-history.asc
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[[_rewriting_history]]
=== Rewriting History

Many times, when working with Git, you may want to revise your commit history for some reason.
Many times, when working with Git, you may want to revise your local commit history.
One of the great things about Git is that it allows you to make decisions at the last possible moment.
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.
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.
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.

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.
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.

[NOTE]
====
Expand All @@ -18,24 +18,38 @@ In short, you should avoid pushing your work until you're happy with it and read
[[_git_amend]]
==== Changing the Last Commit

Changing your last commit is probably the most common rewriting of history that you’ll do.
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.
Changing your most recent commit is probably the most common rewriting of history that you’ll do.
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.

If you only want to modify your last commit message, it’s very simple:
If you simply want to modify your last commit message, that's easy:

[source,console]
----
$ git commit --amend
----

That drops you into your text editor, which has your last commit message in it, ready for you to modify the message.
When you save and close the editor, the editor writes a new commit containing that message and makes it your new last commit.
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.
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.

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.
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.
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.

You need to be careful with this technique because amending changes the SHA-1 of the commit.
It’s like a very small rebase – don’t amend your last commit if you’ve already pushed it.
It’s like a very small rebase -- don’t amend your last commit if you’ve already pushed it.

[TIP]
.An amended commit may (or may not) need an amended commit message
====
When you amend a commit, you have the opportunity to change both the commit message and the content of the commit.
If you amend the content of the commit substantially, you should almost certainly update the commit message to reflect that amended content.
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:
[source,console]
----
$ git commit --amend --no-edit
----
====

[[_changing_multiple]]
==== Changing Multiple Commit Messages
Expand Down

0 comments on commit 99dc02f

Please sign in to comment.