How to add an alias for 'git add **/'

Faraz Durrani

I can't seem to add an alias for git add **/

I think those two asterisks is causing an issue, or it could be that forward slash. How do I solve this?

So far I have tried

alias ga='git add **/' 

When I run the above alias in my terminal, I issue this command ga somename.java which would be an equivalent if alias was not there would be git add **/somename.java.

I have also tried adding more asterisks, slashes, dollar sign followed by a quote, etc.

Gilles 'SO- stop being evil'

ga somename.java is short for git add **/ somename.java. The first argument to the alias is not concatenated to the last word inside the alias. You can think of it this way: the space that you type after ga is not removed.

To do anything more complex than give a command an alternate name or pass arguments to a command, use a function instead of an alias.

Here's a zsh version which is pretty versatile.

function ga {
  git add **/$^~@(.N)
}
alias ga='noglob ga'

Here's how it works for ga foo* bar:

  1. The alias ga expands to noglob ga.
  2. Thanks to the noglob precommand modifier, noglob ga foo* bar does not expand the wildcard in foo*, and instead passes foo* literally as an argument to ga.
  3. Since ga (the one in noglob ga …) isn't the first word in the command, it is not looked up as an alias, and therefore it refers to the function, which is called with the arguments foo* and bar.
  4. $^~@ uses the ${^spec} and ${~spec} parameter expansion forms. The modifier ^ causes **/ to be prepended to each element of the array $@, resulting in **/foo* and **/bar. Without this modifier, **/$@ would expand to **/foo* and bar.
  5. $~@ causes the wildcard characters in the arguments to be expanded now. This way, the pattern foo* is not expanded by itself: what's expanded is **/foo*, so this matches things like sub/dir/foobar.
  6. The . glob qualifier causes only regular files to be matched. Make it -. to also match symbolic links to regular files, or @. to match all symbolic links in addition to regular files.
  7. The N glob qualifier causes patterns that match nothing to be omitted. Don't put (N) if you prefer to have an error if one of the patterns doesn't match anything.

About the only nice thing that's missing here is completion, which is doable but I think not worth the trouble here.

In bash, you can't have it as nice. There's no way to prevent wildcards from being expanded immediately, so ga foo* will be equivalent to ga foo1 foo2 if the directory content is

.git …
foo1
foo2
subdir
subdir/foobar

which will miss subdir/foobar.

In bash, since you need to quote wildcards in arguments, you might as well rely on Git's own pattern matching in a pathspec. Note in particular that * matches directory separators, i.e. it has the shell ** behavior built in.

function ga {
  local x
  for x in "$@"; do
    git add "*/$x"
  done
}
ga 'foo*' bar

On a final note, if you keep your .gitignore up-to-date and commit often, you'll rarely need anything other than git add ..

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related