Skip to contentSkip to conversation

rebase: add --forget to cleanup rebase, leave HEAD untouched

PATCH Superseded Nguyễn Thái Ngọc Duy 2 participants 10 messages Oct 26, 2016 – Nov 12, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rebase: add --forget to cleanup rebase, leave HEAD untouched

There are occasions when you decide to abort an in-progress rebase and move on to do something else but you forget to do "git rebase --abort" first. Or the rebase has been in progress for so long you forgot about it. By the time you realize that (e.g. by starting another rebase) it's already too late to retrace your steps. The solution is normally

rm -r .git/<some rebase dir>

and continue with your life. But there could be two different directories for <some rebase dir> (and it obviously requires some knowledge of how rebase works), and the ".git" part could be much longer if you are not at top-dir, or in a linked worktree. And "rm -r" is very dangerous to do in .git, a mistake in there could destroy object database or other important data.

Provide "git rebase --forget" for this exact use case.

---
 Documentation/git-rebase.txt           |  5 ++++-
 contrib/completion/git-completion.bash |  4 ++--
 git-rebase.sh                          |  6 +++++-
 t/t3407-rebase-abort.sh                | 24 ++++++++++++++++++++++++
 4 files changed, 35 insertions(+), 4 deletions(-)
Documentation/git-rebase.txt +4 -1
index de222c8..5a58fb3 100644
@@ -12,7 +12,7 @@ SYNOPSIS
 	[<upstream> [<branch>]]
 'git rebase' [-i | --interactive] [options] [--exec <cmd>] [--onto <newbase>]
 	--root [<branch>]
-'git rebase' --continue | --skip | --abort | --edit-todo
+'git rebase' --continue | --skip | --abort | --forget | --edit-todo
 
 DESCRIPTION
 -----------
@@ -252,6 +252,9 @@ leave out at most one of A and B, in which case it defaults to HEAD.
 	will be reset to where it was when the rebase operation was
 	started.
 
+--forget::
+	Abort the rebase operation but leave HEAD where it is.
+
 --keep-empty::
 	Keep the commits that do not change anything from its
 	parents in the result.
contrib/completion/git-completion.bash +2 -2
index 9c8f738..bb64fb4 100644
@@ -1733,10 +1733,10 @@ _git_rebase ()
 {
 	local dir="$(__gitdir)"
 	if [ -f "$dir"/rebase-merge/interactive ]; then
-		__gitcomp "--continue --skip --abort --edit-todo"
+		__gitcomp "--continue --skip --abort --forget --edit-todo"
 		return
 	elif [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
-		__gitcomp "--continue --skip --abort"
+		__gitcomp "--continue --skip --abort --forget"
 		return
 	fi
 	__git_complete_strategy && return
git-rebase.sh +5 -1
index 04f6e44..de712b7 100755
@@ -43,6 +43,7 @@ continue!          continue
 abort!             abort and check out the original branch
 skip!              skip current patch and continue
 edit-todo!         edit the todo list during an interactive rebase
+forget!            abort but keep HEAD where it is
 "
 . git-sh-setup
 set_reflog_action rebase
@@ -241,7 +242,7 @@ do
 	--verify)
 		ok_to_skip_pre_rebase=
 		;;
-	--continue|--skip|--abort|--edit-todo)
+	--continue|--skip|--abort|--forget|--edit-todo)
 		test $total_argc -eq 2 || usage
 		action=${1##--}
 		;;
@@ -399,6 +400,9 @@ abort)
 	finish_rebase
 	exit
 	;;
+forget)
+	exec rm -rf "$state_dir"
+	;;
 edit-todo)
 	run_specific_rebase
 	;;
t/t3407-rebase-abort.sh +24 -0
index a6a6c40..6bc5e71 100755
@@ -99,4 +99,28 @@ testrebase() {
 testrebase "" .git/rebase-apply
 testrebase " --merge" .git/rebase-merge
 
+test_expect_success 'rebase --forget' '
+	cd "$work_dir" &&
+	# Clean up the state from the previous one
+	git reset --hard pre-rebase &&
+	test_must_fail git rebase master &&
+	test_path_is_dir .git/rebase-apply &&
+	head_before=$(git rev-parse HEAD) &&
+	git rebase --forget &&
+	test $(git rev-parse HEAD) = $head_before &&
+	test ! -d .git/rebase-apply
+'
+
+test_expect_success 'rebase --merge --forget' '
+	cd "$work_dir" &&
+	# Clean up the state from the previous one
+	git reset --hard pre-rebase &&
+	test_must_fail git rebase --merge master &&
+	test_path_is_dir .git/rebase-merge &&
+	head_before=$(git rev-parse HEAD) &&
+	git rebase --forget &&
+	test $(git rev-parse HEAD) = $head_before &&
+	test ! -d .git/rebase-merge
+'
+
 test_done
Signature · 1 line
2.8.2.524.g6ff3d78
#1 lore raw
Quoted 8 lines · Nguyễn Thái Ngọc Duy

There are occasions when you decide to abort an in-progress rebase and move on to do something else but you forget to do "git rebase --abort" first. Or the rebase has been in progress for so long you forgot about it. By the time you realize that (e.g. by starting another rebase) it's already too late to retrace your steps. The solution is normally

rm -r .git/<some rebase dir>

Quoted 8 lines

and continue with your life. But there could be two different directories for <some rebase dir> (and it obviously requires some knowledge of how rebase works), and the ".git" part could be much longer if you are not at top-dir, or in a linked worktree. And "rm -r" is very dangerous to do in .git, a mistake in there could destroy object database or other important data.

Provide "git rebase --forget" for this exact use case.

Two and a half comments.

- The title says "leave HEAD untouched". Are my working tree files and my index also safe from this operation, or is HEAD the only thing that is protected?

- I think I saw a variant of this gotcha for an unconcluded cherry-pick that was left behind, which the bash-prompt script did not notice but the next "git cherry-pick" did by complaining "you are in the middle" or something like that. Perhaps we would want to have a similarly sounding option to help that case, too, not in this patch but as another patch on the same theme?

- Would it have helped if bash-prompt were in use? I am not saying that this patch becomes unnecessary if you use it; I am trying to see if it helps its users by reminding them what state they are in.

author
#2 lore raw
Quoted 17 lines · Junio C Hamano
Quoted 15 lines · Nguyễn Thái Ngọc Duy

There are occasions when you decide to abort an in-progress rebase and move on to do something else but you forget to do "git rebase --abort" first. Or the rebase has been in progress for so long you forgot about it. By the time you realize that (e.g. by starting another rebase) it's already too late to retrace your steps. The solution is normally

rm -r .git/<some rebase dir>

and continue with your life. But there could be two different directories for <some rebase dir> (and it obviously requires some knowledge of how rebase works), and the ".git" part could be much longer if you are not at top-dir, or in a linked worktree. And "rm -r" is very dangerous to do in .git, a mistake in there could destroy object database or other important data.

Quoted 7 lines
Quoted 1 line

Provide "git rebase --forget" for this exact use case.

Two and a half comments.

- The title says "leave HEAD untouched". Are my working tree files and my index also safe from this operation, or is HEAD the only thing that is protected?

Everything is protected. I will rephrase the title a bit. The option is basically a safe form of "rm -r .git/rebase-{apply,merge}".

Quoted 6 lines

- I think I saw a variant of this gotcha for an unconcluded cherry-pick that was left behind, which the bash-prompt script did not notice but the next "git cherry-pick" did by complaining "you are in the middle" or something like that. Perhaps we would want to have a similarly sounding option to help that case, too, not in this patch but as another patch on the same theme?

That would be nice. I don't put lots of git info on my shell prompt though, so it does not help me. And it's probably difficult to report the right thing too. Sometimes in the middle of rebase I would switch to another branch, look or do stuff, then "git checkout -" back. I don't think we can make the prompt script clever enough to see my intention.

Quoted 4 lines

- Would it have helped if bash-prompt were in use? I am not saying that this patch becomes unnecessary if you use it; I am trying to see if it helps its users by reminding them what state they are in.

Since I don't use it because I want to keep shell prompt short and light, it doe not help me. But it looks like git-prompt.sh does print rebase in progress and others (only checked the code, didn't test it).

Signature · 1 line
Duy
#3 lore raw
Quoted 20 lines · Duy Nguyen
Quoted 19 lines · Junio C Hamano
Quoted 16 lines · Nguyễn Thái Ngọc Duy

There are occasions when you decide to abort an in-progress rebase and move on to do something else but you forget to do "git rebase --abort" first. Or the rebase has been in progress for so long you forgot about it. By the time you realize that (e.g. by starting another rebase) it's already too late to retrace your steps. The solution is normally

rm -r .git/<some rebase dir>

and continue with your life. But there could be two different directories for <some rebase dir> (and it obviously requires some knowledge of how rebase works), and the ".git" part could be much longer if you are not at top-dir, or in a linked worktree. And "rm -r" is very dangerous to do in .git, a mistake in there could destroy object database or other important data.

Provide "git rebase --forget" for this exact use case.

Quoted 8 lines
Quoted 5 lines

Two and a half comments.

- The title says "leave HEAD untouched". Are my working tree files and my index also safe from this operation, or is HEAD the only thing that is protected?

Everything is protected. I will rephrase the title a bit. The option is basically a safe form of "rm -r .git/rebase-{apply,merge}".

We are not in a hurry, as it is not likely that this will hit 2.11 even if we saw a rerolled version yesterday, but it would be nice to cook it on 'next' so that it can be on 'master' early after the upcoming release.

Thanks.

#4 lore raw

PATCH v2 1/1 rebase: add --forget to cleanup rebase, leave everything else untouched

In reply to Nguyễn Thái Ngọc Duy

There are occasions when you decide to abort an in-progress rebase and move on to do something else but you forget to do "git rebase --abort" first. Or the rebase has been in progress for so long you forgot about it. By the time you realize that (e.g. by starting another rebase) it's already too late to retrace your steps. The solution is normally

rm -r .git/<some rebase dir>

and continue with your life. But there could be two different directories for <some rebase dir> (and it obviously requires some knowledge of how rebase works), and the ".git" part could be much longer if you are not at top-dir, or in a linked worktree. And "rm -r" is very dangerous to do in .git, a mistake in there could destroy object database or other important data.

Provide "git rebase --forget" for this exact use case.

---
v2 changes just the subject line

 Documentation/git-rebase.txt           |  5 ++++-
 contrib/completion/git-completion.bash |  4 ++--
 git-rebase.sh                          |  6 +++++-
 t/t3407-rebase-abort.sh                | 24 ++++++++++++++++++++++++
 4 files changed, 35 insertions(+), 4 deletions(-)
Documentation/git-rebase.txt +4 -1
index de222c8..5a58fb3 100644
@@ -12,7 +12,7 @@ SYNOPSIS
 	[<upstream> [<branch>]]
 'git rebase' [-i | --interactive] [options] [--exec <cmd>] [--onto <newbase>]
 	--root [<branch>]
-'git rebase' --continue | --skip | --abort | --edit-todo
+'git rebase' --continue | --skip | --abort | --forget | --edit-todo
 
 DESCRIPTION
 -----------
@@ -252,6 +252,9 @@ leave out at most one of A and B, in which case it defaults to HEAD.
 	will be reset to where it was when the rebase operation was
 	started.
 
+--forget::
+	Abort the rebase operation but leave HEAD where it is.
+
 --keep-empty::
 	Keep the commits that do not change anything from its
 	parents in the result.
contrib/completion/git-completion.bash +2 -2
index 21016bf..3143cb0 100644
@@ -1734,10 +1734,10 @@ _git_rebase ()
 {
 	local dir="$(__gitdir)"
 	if [ -f "$dir"/rebase-merge/interactive ]; then
-		__gitcomp "--continue --skip --abort --edit-todo"
+		__gitcomp "--continue --skip --abort --forget --edit-todo"
 		return
 	elif [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
-		__gitcomp "--continue --skip --abort"
+		__gitcomp "--continue --skip --abort --forget"
 		return
 	fi
 	__git_complete_strategy && return
git-rebase.sh +5 -1
index 04f6e44..de712b7 100755
@@ -43,6 +43,7 @@ continue!          continue
 abort!             abort and check out the original branch
 skip!              skip current patch and continue
 edit-todo!         edit the todo list during an interactive rebase
+forget!            abort but keep HEAD where it is
 "
 . git-sh-setup
 set_reflog_action rebase
@@ -241,7 +242,7 @@ do
 	--verify)
 		ok_to_skip_pre_rebase=
 		;;
-	--continue|--skip|--abort|--edit-todo)
+	--continue|--skip|--abort|--forget|--edit-todo)
 		test $total_argc -eq 2 || usage
 		action=${1##--}
 		;;
@@ -399,6 +400,9 @@ abort)
 	finish_rebase
 	exit
 	;;
+forget)
+	exec rm -rf "$state_dir"
+	;;
 edit-todo)
 	run_specific_rebase
 	;;
t/t3407-rebase-abort.sh +24 -0
index a6a6c40..6bc5e71 100755
@@ -99,4 +99,28 @@ testrebase() {
 testrebase "" .git/rebase-apply
 testrebase " --merge" .git/rebase-merge
 
+test_expect_success 'rebase --forget' '
+	cd "$work_dir" &&
+	# Clean up the state from the previous one
+	git reset --hard pre-rebase &&
+	test_must_fail git rebase master &&
+	test_path_is_dir .git/rebase-apply &&
+	head_before=$(git rev-parse HEAD) &&
+	git rebase --forget &&
+	test $(git rev-parse HEAD) = $head_before &&
+	test ! -d .git/rebase-apply
+'
+
+test_expect_success 'rebase --merge --forget' '
+	cd "$work_dir" &&
+	# Clean up the state from the previous one
+	git reset --hard pre-rebase &&
+	test_must_fail git rebase --merge master &&
+	test_path_is_dir .git/rebase-merge &&
+	head_before=$(git rev-parse HEAD) &&
+	git rebase --forget &&
+	test $(git rev-parse HEAD) = $head_before &&
+	test ! -d .git/rebase-merge
+'
+
 test_done
Signature · 1 line
2.8.2.524.g6ff3d78
#5 lore raw

Re: [PATCH v2] rebase: add --forget to cleanup rebase, leave everything else untouched

Quoted 2 lines · Nguyễn Thái Ngọc Duy

---
v2 changes just the subject line

That's not sufficient, is it? What you did in the documentation would raise the same "Hmph, is this only about HEAD?" and unlike the commit subject, it will carve it in stone for end-users.

author
#6 lore raw

Re: [PATCH v2] rebase: add --forget to cleanup rebase, leave everything else untouched

Quoted 8 lines · Junio C Hamano
Quoted 2 lines · Nguyễn Thái Ngọc Duy

---
v2 changes just the subject line

That's not sufficient, is it? What you did in the documentation would raise the same "Hmph, is this only about HEAD?" and unlike the commit subject, it will carve it in stone for end-users.

Oops. I forgot about git-rebase.txt. How about this?

--forget::
Abort the rebase operation but leave HEAD, the index and
working tree untouched.

And think that's all the things rebase touches.

Signature · 1 line
Duy
author
#7 lore raw

Re: [PATCH v2] rebase: add --forget to cleanup rebase, leave everything else untouched

Quoted 10 lines · Duy Nguyen
Quoted 8 lines · Junio C Hamano
Quoted 2 lines · Nguyễn Thái Ngọc Duy

---
v2 changes just the subject line

That's not sufficient, is it? What you did in the documentation would raise the same "Hmph, is this only about HEAD?" and unlike the commit subject, it will carve it in stone for end-users.

Quoted 5 lines

Oops. I forgot about git-rebase.txt. How about this?

--forget::
Abort the rebase operation but leave HEAD, the index and
working tree untouched.

Or, since --abort describes it as "reset HEAD to the original branch", we could write "Abort the rebase operation. Unlike --abort, HEAD is not restored back to the original branch". Index and worktree are implied by "not restored". Not sure if it's too subtle.

Signature · 1 line
Duy
#8 lore raw

Re: [PATCH v2] rebase: add --forget to cleanup rebase, leave everything else untouched

Quoted 13 lines · Duy Nguyen
Quoted 12 lines · Duy Nguyen
Quoted 8 lines · Junio C Hamano
Quoted 2 lines · Nguyễn Thái Ngọc Duy

---
v2 changes just the subject line

That's not sufficient, is it? What you did in the documentation would raise the same "Hmph, is this only about HEAD?" and unlike the commit subject, it will carve it in stone for end-users.

Oops. I forgot about git-rebase.txt. How about this?

Quoted 8 lines
Quoted 3 lines

--forget::
Abort the rebase operation but leave HEAD, the index and
working tree untouched.

Or, since --abort describes it as "reset HEAD to the original branch", we could write "Abort the rebase operation. Unlike --abort, HEAD is not restored back to the original branch". Index and worktree are implied by "not restored". Not sure if it's too subtle.

I think you are moving in the right direction, even though "Abort it; unlike --abort, does this instead" is a bit hard to follow.

#9 lore raw

PATCH v3 1/1 rebase: add --forget to cleanup rebase, leave everything else untouched

In reply to Nguyễn Thái Ngọc Duy

There are occasions when you decide to abort an in-progress rebase and move on to do something else but you forget to do "git rebase --abort" first. Or the rebase has been in progress for so long you forgot about it. By the time you realize that (e.g. by starting another rebase) it's already too late to retrace your steps. The solution is normally

rm -r .git/<some rebase dir>

and continue with your life. But there could be two different directories for <some rebase dir> (and it obviously requires some knowledge of how rebase works), and the ".git" part could be much longer if you are not at top-dir, or in a linked worktree. And "rm -r" is very dangerous to do in .git, a mistake in there could destroy object database or other important data.

Provide "git rebase --forget" for this exact use case.

---
v3 rewrote the desscription of --forget in git-rebase.txt to be more or less along the same line as --abort. What happens to the index and worktree is also stated to avoid misunderstanding.

 Documentation/git-rebase.txt           |  7 ++++++-
 contrib/completion/git-completion.bash |  4 ++--
 git-rebase.sh                          |  6 +++++-
 t/t3407-rebase-abort.sh                | 24 ++++++++++++++++++++++++
 4 files changed, 37 insertions(+), 4 deletions(-)
Documentation/git-rebase.txt +6 -1
index de222c8..1e16c70 100644
@@ -12,7 +12,7 @@ SYNOPSIS
 	[<upstream> [<branch>]]
 'git rebase' [-i | --interactive] [options] [--exec <cmd>] [--onto <newbase>]
 	--root [<branch>]
-'git rebase' --continue | --skip | --abort | --edit-todo
+'git rebase' --continue | --skip | --abort | --forget | --edit-todo
 
 DESCRIPTION
 -----------
@@ -252,6 +252,11 @@ leave out at most one of A and B, in which case it defaults to HEAD.
 	will be reset to where it was when the rebase operation was
 	started.
 
+--forget::
+	Abort the rebase operation but HEAD is not reset back to the
+	original branch. The index and working tree are also left
+	unchanged as a result.
+
 --keep-empty::
 	Keep the commits that do not change anything from its
 	parents in the result.
contrib/completion/git-completion.bash +2 -2
index 21016bf..3143cb0 100644
@@ -1734,10 +1734,10 @@ _git_rebase ()
 {
 	local dir="$(__gitdir)"
 	if [ -f "$dir"/rebase-merge/interactive ]; then
-		__gitcomp "--continue --skip --abort --edit-todo"
+		__gitcomp "--continue --skip --abort --forget --edit-todo"
 		return
 	elif [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
-		__gitcomp "--continue --skip --abort"
+		__gitcomp "--continue --skip --abort --forget"
 		return
 	fi
 	__git_complete_strategy && return
git-rebase.sh +5 -1
index 04f6e44..de712b7 100755
@@ -43,6 +43,7 @@ continue!          continue
 abort!             abort and check out the original branch
 skip!              skip current patch and continue
 edit-todo!         edit the todo list during an interactive rebase
+forget!            abort but keep HEAD where it is
 "
 . git-sh-setup
 set_reflog_action rebase
@@ -241,7 +242,7 @@ do
 	--verify)
 		ok_to_skip_pre_rebase=
 		;;
-	--continue|--skip|--abort|--edit-todo)
+	--continue|--skip|--abort|--forget|--edit-todo)
 		test $total_argc -eq 2 || usage
 		action=${1##--}
 		;;
@@ -399,6 +400,9 @@ abort)
 	finish_rebase
 	exit
 	;;
+forget)
+	exec rm -rf "$state_dir"
+	;;
 edit-todo)
 	run_specific_rebase
 	;;
t/t3407-rebase-abort.sh +24 -0
index a6a6c40..6bc5e71 100755
@@ -99,4 +99,28 @@ testrebase() {
 testrebase "" .git/rebase-apply
 testrebase " --merge" .git/rebase-merge
 
+test_expect_success 'rebase --forget' '
+	cd "$work_dir" &&
+	# Clean up the state from the previous one
+	git reset --hard pre-rebase &&
+	test_must_fail git rebase master &&
+	test_path_is_dir .git/rebase-apply &&
+	head_before=$(git rev-parse HEAD) &&
+	git rebase --forget &&
+	test $(git rev-parse HEAD) = $head_before &&
+	test ! -d .git/rebase-apply
+'
+
+test_expect_success 'rebase --merge --forget' '
+	cd "$work_dir" &&
+	# Clean up the state from the previous one
+	git reset --hard pre-rebase &&
+	test_must_fail git rebase --merge master &&
+	test_path_is_dir .git/rebase-merge &&
+	head_before=$(git rev-parse HEAD) &&
+	git rebase --forget &&
+	test $(git rev-parse HEAD) = $head_before &&
+	test ! -d .git/rebase-merge
+'
+
 test_done
Signature · 1 line
2.8.2.524.g6ff3d78