3

Is it possible to use regular expressions in, e.g., git commit ".*my_file.*" ?

I tried, and it seems to only interpret these as globs. I also tired a regex flag:

git commit -regex ".*my_file.*"`

Throws an error.

Does anyone know of a way to combine regular expressions with Git commands?

3
  • @NickVolynkin, globs can be used for both git add, and git commit, I just don't know whether it is possible to use regex, which would be wonderful for me.
    – makansij
    Jun 5, 2015 at 17:53
  • Oh, true. I've found that syntax in Git reference; never seen it before. Jun 5, 2015 at 18:02
  • 1
    Maybe you tried to add untracked files? From Git reference: " by listing files as arguments to the commit command, in which case the commit will ignore changes staged in the index, and instead record the current content of the listed files (which must already be known to Git);" Jun 5, 2015 at 18:22

5 Answers 5

2

The best way I can think of to do this is using the find command. For example, if you want only python files:

find -type f -regex ".*\.py$" -exec git commit {} -m "committing only and all python files" \;

Can anyone else think of something less unwieldy?

1

Not with Git itself. Git just receives a list of files passed from the shell, so it would be up to your shell to do regular expression matching for files. I do not think bash can do this, but other shells may be able to.

4
  • Git also understand -- <filepattern>, where <filepattern> can be a singly-quoted string containings globs (and thus not expanded by shell); those globs can be used to select a subset of files to add. For example: git add -- '*.el' would add all .el files in current and nested directories: globs act on the whole name, like -wholename in find.
    – coredump
    Jun 5, 2015 at 19:56
  • @coredump: That will work, but only if there is not a .el file in the current directory, in which case the shell will not expand the file glob pattern, instead passing a literal *.el to git add, which will then interpret it as a pattern spec.
    – mipadi
    Jun 5, 2015 at 20:10
  • 1
    Please have a look at this transcript: pastebin.com/KmejAU3i. It works, even when there is an .el file in current directory. Expansion is not done by the shell, because the glob is quoted.
    – coredump
    Jun 5, 2015 at 21:06
  • @coredump: Yes, sorry, quoting a file glob will prevent the shell from expanding it into a list of files, in which case it will get passed as-is to git add.
    – mipadi
    Jun 5, 2015 at 21:26
0

As mentioned by mipadi, arguments can be generated by the shell to produce the input for git.

For example, ls can be used in combination for this. Let's say I've got a git init-ed directory with the following files:

.git/
my_new_project_file.py
my_older_project_file.py
some_other_file.py

And I want to add everything but some_other_file.py. To do this:

ls my*project*.py | xargs git add

Checking my git status will show that both my_new_project_file.py and my_older_project_file.py have been staged, while some_other_file.py has been ignored.

n.b. ls doesn't support regex; just globbing.

2
  • 1
    That's cool, but git already supports globbing: git add "my*project*.py" does the same
    – makansij
    Jun 5, 2015 at 20:04
  • ls does neither; it is the shell which expands the globs before ls runs. Anyway, don't use ls in scripts; see also useless use of ls
    – tripleee
    Oct 15, 2023 at 8:56
0

As pointed out by Candic3, git supports globs:

git add *my_file*
1
  • 1
    Without quotes, it will be the shell that most likely interprets the glob in your example.
    – trusktr
    Oct 15, 2023 at 6:29
-1

The answers seem to indicate this is not possible with git alone, but requires a shell.

This seems not correct these days. This works for me in zsh:

git -C path/to/repo checkout '**package-lock.json'

In zsh shell, the content inside the quotes will not be expanded, it is passed along as-is, so git must be interpreting the glob.

I'd expect this to be cross-platform.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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