Digot
Digot

Reputation: 207

Git rm with wildcard doesn't find existing files

I'm trying to execute a git remove using wilcards.

git rm -r -f $artname-*-$buildtype

The $artname variable is something like "core" and the $buildtype is something like "SNAPSHOT.jar"

So if there is a file on the remote repo called "core-1.0-SNAPSHOT", it should be removed using the wildcard.

But this doesn't work. The variables do resolve but the wildcard just doesn't seem to work.

This is the error that I'm getting

fatal: pathspec 'core-*-SNAPSHOT.jar' did not match any files

I hope, that someone can help me!

Upvotes: 2

Views: 985

Answers (2)

ferdymercury
ferdymercury

Reputation: 816

A oneliner solution avoiding untracked and .gitignored files could be:

git ls-files $artname-*-$buildtype | tr '\n' '\0' | xargs -0 -L1 -I '$' git rm -r -f '$'

Upvotes: 1

ivan_pozdeev
ivan_pozdeev

Reputation: 36018

According to the git-rm documentation, possible causes may be:

  • bash expanding the glob before it reaches git (due to "core-*-SNAPSHOT.jar" in the error message, it didn't - in your specific case - because it didn't match anything in the directory you're in). Quote the argument to prevent this: git rm "$artname-*-$buildtype"
  • files you're interested in are untracked (git's search behaves as if they don't exist)
  • leading directory name is required for -r to work (the docs say so but a simple test with git 2.7.4 shows it actually isn't)

Upvotes: 4

Related Questions