I am trying to create a personalized function to wrap git add.

Since I'm not a bit fan of having to type git add a/long/path/to/some/file/that/has/changed.java I am trying to create some sort of fuzzy matching function so that I just have to type a part of the filename and add all matching filenames, for example gadd ged would match the file above.

My gadd function is:

# wildcard git add
function gadd
    if [ ! -z "$1" ]; then
        command git add *$1*
        cls #some other function that actually works
        echo $'No wildcard specified, doing nothing.'

But that does not seem to work with the dot character (git add .) that adds all changed files.

What should I try?

  • I am on Linux. I am by no means an expert but I read that by using $1 I can reference the parameter passed to the function from the terminal. Is this mistaken? The * signs are wildcard operators for git and actually work fine with all strings except .. – anpel Jul 23 '16 at 17:56
  • The $1 is indeed the first parameter. How would calling gadd ged call git add a/long/path/to/some/file/that/has/changed.java with your current function? Where does that translation take place? – Julie Pelletier Jul 23 '16 at 17:58
  • My understanding is that command git add *$1* will execute git add *$1*, so in the case of gadd ged it will execute git add *ged*. git allows the use of wildcards for file matching so git add *ged* matches (and adds) the file a/long/path/to/some/file/that/has/changed.java. – anpel Jul 23 '16 at 18:02
  • How does it guess where the file is? I suppose you're trying this from the a/long/path/to/some/file/that/has/ directory but that doesn't fit with your question if you need to pass the long path that you don't want to type. – Julie Pelletier Jul 23 '16 at 18:04
  • git allows you to use wildcards as shown here – anpel Jul 23 '16 at 18:09

Please use "" (quote) with $1, it may help.

So it would be looking as "*$1*".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.