From 2929e7cc5f472c45c0428bb88a60e06c962e38ea Mon Sep 17 00:00:00 2001 From: HonkingGoose <34918129+HonkingGoose@users.noreply.github.com> Date: Sun, 9 Aug 2020 14:40:17 +0200 Subject: [PATCH 1/3] Create section: drop commit with git rebase -i Fixes #824 --- .../sections/rewriting-history.asc | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/book/07-git-tools/sections/rewriting-history.asc b/book/07-git-tools/sections/rewriting-history.asc index c0403ce6a..58456535e 100644 --- a/book/07-git-tools/sections/rewriting-history.asc +++ b/book/07-git-tools/sections/rewriting-history.asc @@ -299,6 +299,34 @@ This changes the SHA-1s of the three most recent commits in your list, so make s Notice that the last commit (`f7f3f6d`) in the list is unchanged. 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. +==== Deleting a commit + +If you want to get rid of a commit, you can delete it using the `rebase -i` script. +In the list of commits, put the word ``drop'' before the commit you want to delete: + +[source,console] +---- +pick 461cb2a This commit is OK +drop 5aecc10 This commit is broken +---- + +[CAUTION] +==== +Do not delete/drop a commit that other commits depend on! +It's a bad idea to drop the first commit in the `rebase -i` script list. +The following commits depend on the first commit being present, as the root of all changes that follow. +==== + +Avoid doing this: +[source,console] +---- +drop 5aecc10 This commit is broken +pick c6be4c9 This commit is OK but depends on changes made in 5aecc10 +---- + +Git will complain about the broken state, and ask you to resolve the conflicts manually. +You can also just back out of the rebase by using `git rebase --abort`. + [NOTE] ==== Drew DeVault made a practical hands-on guide with exercises to learn how to use `git rebase`. From 3fa7295af6685f729e1f72ed417e3765ca9a2fd4 Mon Sep 17 00:00:00 2001 From: HonkingGoose <34918129+HonkingGoose@users.noreply.github.com> Date: Sun, 16 Aug 2020 08:37:14 +0200 Subject: [PATCH 2/3] Add second method to delete a commit Co-authored-by: Ben Straub --- book/07-git-tools/sections/rewriting-history.asc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book/07-git-tools/sections/rewriting-history.asc b/book/07-git-tools/sections/rewriting-history.asc index 58456535e..b54b011c9 100644 --- a/book/07-git-tools/sections/rewriting-history.asc +++ b/book/07-git-tools/sections/rewriting-history.asc @@ -302,7 +302,7 @@ Despite this commit being shown in the script, because it was marked as ``pick'' ==== Deleting a commit If you want to get rid of a commit, you can delete it using the `rebase -i` script. -In the list of commits, put the word ``drop'' before the commit you want to delete: +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): [source,console] ---- From 1ab5006e53bce4c4ba561e9ad01ae9e4b4253544 Mon Sep 17 00:00:00 2001 From: HonkingGoose <34918129+HonkingGoose@users.noreply.github.com> Date: Sun, 16 Aug 2020 08:43:16 +0200 Subject: [PATCH 3/3] Rewrite of proposed section by Ben Co-authored-by: Ben Straub --- .../sections/rewriting-history.asc | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/book/07-git-tools/sections/rewriting-history.asc b/book/07-git-tools/sections/rewriting-history.asc index b54b011c9..12cedbf9c 100644 --- a/book/07-git-tools/sections/rewriting-history.asc +++ b/book/07-git-tools/sections/rewriting-history.asc @@ -310,22 +310,15 @@ pick 461cb2a This commit is OK drop 5aecc10 This commit is broken ---- -[CAUTION] -==== -Do not delete/drop a commit that other commits depend on! -It's a bad idea to drop the first commit in the `rebase -i` script list. -The following commits depend on the first commit being present, as the root of all changes that follow. -==== +Because of the way Git builds commit objects, deleting or altering a commit will cause the rewriting of all the commits that follow it. +The further back in your repo's history you go, the more commits will need to be recreated. +This can cause lots of merge conflicts if you have many commits later in the sequence that depend on the one you just deleted. -Avoid doing this: -[source,console] ----- -drop 5aecc10 This commit is broken -pick c6be4c9 This commit is OK but depends on changes made in 5aecc10 ----- +If you get partway through a rebase like this and decide it's not a good idea, you can always stop. +Type `git rebase --abort`, and your repo will be returned to the state it was in before you started the rebase. -Git will complain about the broken state, and ask you to resolve the conflicts manually. -You can also just back out of the rebase by using `git rebase --abort`. +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. +See <> for more information on the `reflog` command. [NOTE] ====