git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] stash: clean untracked files before reset

From
Nicolas Morey-Chaisemartin <nicolas@morey-chaisemartin.com>
Date
Aug 11, 2017, 17:14 UTC
Message-ID
<b87007d1-eb5a-f092-9e03-5c6549044b42@morey-chaisemartin.com>
In-Reply-To
<CAPuLQgNq_7Kdeio-59cbDjZ5mWC1VRd_1_KK33Gbka9KJ13vfQ@mail.gmail.com>

If calling git stash -u on a repo that contains a file that is not ignored any more due to a current modification of the gitignore file, this file is stashed but not remove from the working tree. This is due to git-stash first doing a reset --hard which clears the .gitignore file modification and the call git clean, leaving the file untouched. This causes git stash pop to fail due to the file existing.

This patch simply switches the order between cleaning and resetting and adds a test for this usecase.

Signed-off-by: Nicolas Morey-Chaisemartin <nicolas@morey-chaisemartin.com>
Reported-by: Sam Partington <sam@whiteoctober.co.uk>
---
 git-stash.sh                       | 11 ++++++-----
 t/t3905-stash-include-untracked.sh | 18 ++++++++++++++++++
 2 files changed, 24 insertions(+), 5 deletions(-)
diff --git a/git-stash.sh b/git-stash.sh
index 9b6c2da7b..39083b4d9 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -300,6 +300,12 @@ push_stash () {
 
 	if test -z "$patch_mode"
 	then
+		test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
+		if test -n "$untracked"
+		then
+			git clean --force --quiet -d $CLEAN_X_OPTION -- "$@"
+		fi
+
 		if test $# != 0
 		then
 			git reset -q -- "$@"
@@ -309,11 +315,6 @@ push_stash () {
 		else
 			git reset --hard -q
 		fi
-		test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
-		if test -n "$untracked"
-		then
-			git clean --force --quiet -d $CLEAN_X_OPTION -- "$@"
-		fi
 
 		if test "$keep_index" = "t" && test -n "$i_tree"
 		then
diff --git a/t/t3905-stash-include-untracked.sh b/t/t3905-stash-include-untracked.sh
index 193adc7b6..c1f84d3d5 100755
--- a/t/t3905-stash-include-untracked.sh
+++ b/t/t3905-stash-include-untracked.sh
@@ -211,4 +211,22 @@ test_expect_success 'stash push with $IFS character' '
 	test_path_is_file bar
 '
 
+cat > .gitignore <<EOF
+ignored
+ignored.d/*
+EOF
+
+test_expect_success 'stash previously ignored file' '
+which git &&
+	git reset HEAD &&
+	git add .gitignore &&
+	git commit -m "Add .gitignore" && 
+	>ignored.d/foo &&
+	echo "!ignored.d/foo" >> .gitignore &&
+	git stash save --include-untracked &&
+	test_path_is_missing ignored.d/foo &&
+	git stash pop &&
+	test_path_is_file ignored.d/foo
+'
+
 test_done
-- 
2.14.0.1.gd9597ce13
Previous: Sam PartingtonNext: Junio C Hamano
Message 2 of 5 in “Bug when stashing previously-ignored file plus associated .gitignore change”
  1. Sam PartingtonAug 11, 2017
  2. stash: clean untracked files before resetNicolas Morey-Chaisemartin, Aug 11, 2017
  3. Junio C HamanoAug 11, 2017
  4. Kevin DaudtAug 14, 2017
  5. Sam PartingtonAug 14, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.