Skip to content

Commit 98cf2d6

Browse files
authored
Merge pull request #1484 from HonkingGoose/feature/add-section-dropping-commits-with-rebase
2 parents 030f211 + 0d52cc6 commit 98cf2d6

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

book/07-git-tools/sections/rewriting-history.asc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,27 @@ This changes the SHA-1s of the three most recent commits in your list, so make s
299299
Notice that the last commit (`f7f3f6d`) in the list is unchanged.
300300
Despite this commit being shown in the script, because it was marked as ``pick'' and was applied prior to any rebase changes, Git leaves the commit unmodified.
301301

302+
==== Deleting a commit
303+
304+
If you want to get rid of a commit, you can delete it using the `rebase -i` script.
305+
In the list of commits, put the word ``drop'' before the commit you want to delete (or just delete that line from the rebase script):
306+
307+
[source,console]
308+
----
309+
pick 461cb2a This commit is OK
310+
drop 5aecc10 This commit is broken
311+
----
312+
313+
Because of the way Git builds commit objects, deleting or altering a commit will cause the rewriting of all the commits that follow it.
314+
The further back in your repo's history you go, the more commits will need to be recreated.
315+
This can cause lots of merge conflicts if you have many commits later in the sequence that depend on the one you just deleted.
316+
317+
If you get partway through a rebase like this and decide it's not a good idea, you can always stop.
318+
Type `git rebase --abort`, and your repo will be returned to the state it was in before you started the rebase.
319+
320+
If you finish a rebase and decide it's not what you want, you can use `git reflog` to recover an earlier version of your branch.
321+
See <<ch10-git-internals#_data_recovery>> for more information on the `reflog` command.
322+
302323
[NOTE]
303324
====
304325
Drew DeVault made a practical hands-on guide with exercises to learn how to use `git rebase`.

0 commit comments

Comments
 (0)