public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
From: "Remy D. Farley" <one-d-wide@protonmail•com>
To: git@vger•kernel.org
Cc: "Remy D. Farley" <one-d-wide@protonmail•com>
Subject: Bug: git add :!x . exits with error when x is in .gitignore
Date: Sat, 31 Jan 2026 19:43:20 +0000	[thread overview]
Message-ID: <20260131194309.601838-1-one-d-wide@protonmail.com> (raw)

`git add :!x .`, which is executed as part `git stash :!x`, seems to treat
pathspec with and without exclude magic the same, exiting with error when "x"
is in gitignore file. If file "x" isn't present, `git add x` still exits with
error as expected (although with a different one), but `git add :!x .` succeeds.

Git-add manpage doesn't specify that exclude pathspecs are treated anyhow
differently from normal ones, so I'm not sure if it's really a bug. But it does
seem like one. I originally tried to exclude certain files from scripted stashes
using pathspec, but had to switch to reusing core.excludesFile (which is
probably a better fit for my use case).


```sh
$ sh repro.sh
[...]
+ echo x >.gitignore
+ echo x >x
+ git add -- ':!x' .
Saved working directory and index state WIP on main: c8a842d Init
The following paths are ignored by one of your .gitignore files:
x
hint: Use -f if you really want to add them.
hint: Disable this message with "git config set advice.addIgnoredFile false"
+ echo exited with code 1
exited with code 1
```


```sh
# repro.sh
rm -rf repro; mkdir repro; cd repro
trap 'echo exited with code $?' EXIT
set -euo pipefail -o xtrace

git init
git commit -m Init --allow-empty

# If we comment out either of the following lines, git add/stash commands below succeed
echo x >.gitignore
echo x >x

# Git add . is executed as part of git stash, as can be seen using strace -ffeexecve
git add -- ":!x" . # fails
# git stash --include-untracked -- ":!x" # fails

echo ok
```

---
 dir.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/dir.c b/dir.c
index b00821f2..0026bd65 100644
--- a/dir.c
+++ b/dir.c
@@ -2264,7 +2264,7 @@ static int exclude_matches_pathspec(const char *path, int pathlen,
 {
 	int i;
 
-	if (!pathspec || !pathspec->nr)
+	if (!pathspec || !pathspec->nr || pathspec->magic & PATHSPEC_EXCLUDE)
 		return 0;
 
 	GUARD_PATHSPEC(pathspec,
-- 
2.51.2



             reply	other threads:[~2026-01-31 19:43 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-31 19:43 Remy D. Farley [this message]
2026-02-02 18:04 ` Bug: git add :!x . exits with error when x is in .gitignore Tian Yuchen
2026-02-02 21:25   ` Remy D. Farley

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260131194309.601838-1-one-d-wide@protonmail.com \
    --to=one-d-wide@protonmail$(echo .)com \
    --cc=git@vger$(echo .)kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox