Git Pathspecs and How to Use Them
In the Git command<pathspec></pathspec>
Parameters: Flexible use of the powerful functions of Git
When reviewing the Git command documentation, you may notice that many commands contain<pathspec></pathspec>
Options. At first, you might think this is just a technical statement of "path" and can only accept directories and file names. However, after a deeper understanding, you will find that the Git command<pathspec></pathspec>
Much stronger than you think.
<pathspec></pathspec>
It is a mechanism used by Git to limit the scope of Git commands, which limits the execution scope of commands to a subset of the repository. Even if you don't realize that, you may already be using it<pathspec></pathspec>
Now. For example, in the command git add README.md
,<pathspec></pathspec>
It's README.md
. but<pathspec></pathspec>
Ability to achieve more refined and flexible operations.
study<pathspec></pathspec>
The advantage is that it significantly enhances the functionality of many Git commands. For example, with git add
you can just add files in a single directory; with git diff
you can check for changes made to file names with only extension .scss
; you can also use git grep
to search all files, but exclude files in /dist
directory.
also,<pathspec></pathspec>
Helps write more general Git alias. For example, I have an alias called git todo
which will search for the string "todo" in all my repository files. However, I want it to display all instances of the string, even if they are not in my current working directory. use<pathspec></pathspec>
, we can achieve this.
File or directory
use<pathspec></pathspec>
The most direct way is to use the directory and/or file name. For example, using git add
, you can do the following: .
, src/
and README
are the commands for each command, respectively<pathspec></pathspec>
.
git add . # Add the current working directory (CWD) git add src/ # Add src/ directory git add README # Add only README files
You can also add multiple to one command<pathspec></pathspec>
:
git add src/ server/ # Add src/ and server/ directories
Sometimes, you may see commands<pathspec></pathspec>
There is a --
in front of it. This is used to eliminate<pathspec></pathspec>
ambiguity between the other parts of the command.
Wildcard
In addition to files and directories, you can also use *
, ?
, and []
to match patterns. The *
symbol is used as a wildcard, and it will match /
in the path — in other words, it will search for subdirectories.
git log '*.js' # Record all .js files in CWD and subdirectories git log '.*' # Record all 'hidden' files and directories in CWD git log '*/.*' # Record all 'hidden' files and directories in subdirectories git log '*/.*' # Record all 'hidden' files and directories in subdirectories
Quotes are very important, especially when using *
. They prevent your shell (such as bash or ZSH) from extending wildcards by itself. For example, let's see how git ls-files
lists files with and without quotes.
# Sample directory structure# # . # ├── package-lock.json # ├── package.json # └── data # ├── bar.json # ├── baz.json # └── foo.json git ls-files *.json # package-lock.json # package.json git ls-files '*.json' # data/bar.json # data/baz.json # data/foo.json # package-lock.json # package.json
Since the shell extends *
in the first command, the command received by git ls-files
is git ls-files package-lock.json package.json
. Quotes make sure Git is the party that extends wildcards.
You can also use ?
characters as wildcards for a single character. For example, to match mp3 or mp4 files, you can do the following.
git ls-files '*.mp?'
Square bracket expression
You can also use "square bracket expressions" to match individual characters in a collection. For example, if you want to match TypeScript or JavaScript files, you can use [tj]
. This will match t
or j
.
git ls-files '*.[tj]s'
This will match the .ts
file or the .js
file. In addition to using characters, you can also refer to certain sets of characters in square bracket expressions. For example, you can use [:digit:]
in square bracket expressions to match any decimal number, or use [:space:]
to match any space character.
git ls-files '*.mp[[:digit:]]' # mp0, mp1, mp2, mp3, ..., mp9 git ls-files '*[[:space:]]*' # Match any path containing spaces
To learn more about square bracket expressions and how to use them, check out the GNU manual.
Magic signature
<pathspec></pathspec>
Also has a special tool called "Magic Signature" which can be used for your<pathspec></pathspec>
Unlock some extra features. These "magic signatures" are passed through<pathspec></pathspec>
The beginning of the :(signature)
is called. If you don't understand, don't worry: some examples should help.
top
top
signature tells Git to match the pattern from the root of the Git repository instead of the current working directory. You can also use abbreviation /
instead of :(top)
.
git ls-files ':(top)*.js' git ls-files ':/*.js' # abbreviation
This lists all files in the repository with the .js
extension. Use top
signature, which can be called in any subdirectories in the repository. I found this especially useful when writing generic Git alias!
git config --global alias.js 'ls-files -- ':(top)*.js''
You can use git js
to get a list of all JavaScript files in your project anywhere in your repository.
icase
icase
signature tells Git to ignore case when matching. This is very useful if you don't care about the case of file names - for example, it's very useful for matching jpg files, which sometimes use the uppercase extension JPG.
git ls-files ':(icase)*.jpg'
literal
The literal
signature tells Git to treat all characters literally. This option can be used if you want to treat characters like *
and ?
as themselves rather than wildcards. Unless the file name of your repository contains *
or ?
, I don't think this signature will be used frequently.
git log ':(literal)*.js' # Return the log of the file '*.js'
glob
When I started learning<pathspec></pathspec>
When I noticed that wildcards work differently than I am used to. Usually, I see a single asterisk *
as a wildcard that doesn't match anything in the directory, while a continuous asterisk ( **
) as a "deep" wildcard will match the name in the directory. If you prefer this style of wildcards, you can use glob
magic signatures!
This is very useful if you want to have more granular control over how the project directory structure is searched. For example, let's see how these two git ls-files
search for React projects.
git ls-files ':(glob)src/components/*/*.jsx' # "Top" jsx component git ls-files ':(glob)src/components/**/*.jsx' # "All" jsx components
attr
Git can set "properties" for specific files. You can set these properties using the .gitattributes
file.
<code># .gitattributes src/components/vendor/* vendored # 设置“vendored”属性src/styles/vendor/* vendored</code>
Use attr
magic signatures for your<pathspec></pathspec>
Set attribute requirements. For example, we may want to ignore the above documents from the vendor.
git ls-files ':(attr:!vendored)*.js' # Search for non-vendored js files git ls-files ':(attr:vendored)*.js' # Search for vendored js files
exclude
Finally, there is the magic signature of " exclude
" (abbreviated as :!
or :^
). This signature works differently than other magic signatures. Resolve all other<pathspec></pathspec>
After that, all the exclude
signatures will be parsed<pathspec></pathspec>
, and then remove it from the returned path. For example, you can search for all .js
files while excluding .spec.js
test files.
git grep 'foo' -- '*.js' ':(exclude)*.spec.js' # Search for .js files, exclude .spec.js git grep 'foo' -- '*.js' ':!*.spec.js' . # The same abbreviation as above
Combination signature
There is nothing to limit you to a single<pathspec></pathspec>
Use multiple magic signatures! You can use multiple signatures by separating your magic words with commas in brackets. For example, if you want to match from the bottom of the repository (using top
), case-insensitive (using icase
), only using authored code (using attr
to ignore vendor files), and using glob-style wildcards (using glob
), you can do the following.
git ls-files -- ':(top,icase,glob,attr:!vendored)src/components/*/*.jsx'
The two magic signatures you can't combine are glob
and literal
, because they both affect how Git handles wildcards. This is mentioned in the Git Glossary and is perhaps my favorite sentence I've read in any document.
Glob magic is incompatible with literal magic.
<pathspec></pathspec>
It is an integral part of many Git commands, but its flexibility is not immediately visible. By learning how to use wildcards and magic signatures, you can double your ability on the Git command line.
The above is the detailed content of Git Pathspecs and How to Use Them. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

It's out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That's like this.

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...
