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!

The React ecosystem offers us a lot of libraries that all are focused on the interaction of drag and drop. We have react-dnd, react-beautiful-dnd,

There have been some wonderfully interconnected things about fast software lately.

I can't say I use background-clip all that often. I'd wager it's hardly ever used in day-to-day CSS work. But I was reminded of it in a post by Stefan Judis,

Animating with requestAnimationFrame should be easy, but if you haven’t read React’s documentation thoroughly then you will probably run into a few things

Perhaps the easiest way to offer that to the user is a link that targets an ID on the element. So like...

Listen, I am no GraphQL expert but I do enjoy working with it. The way it exposes data to me as a front-end developer is pretty cool. It's like a menu of

In this week's roundup, a handy bookmarklet for inspecting typography, using await to tinker with how JavaScript modules import one another, plus Facebook's

I've recently noticed an interesting change on CodePen: on hovering the pens on the homepage, there's a rectangle with rounded corners expanding in the back.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment