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 # 設(shè)置“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

Undress AI Tool
Undress images for free

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

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

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)

There are three ways to create a CSS loading rotator: 1. Use the basic rotator of borders to achieve simple animation through HTML and CSS; 2. Use a custom rotator of multiple points to achieve the jump effect through different delay times; 3. Add a rotator in the button and switch classes through JavaScript to display the loading status. Each approach emphasizes the importance of design details such as color, size, accessibility and performance optimization to enhance the user experience.

To deal with CSS browser compatibility and prefix issues, you need to understand the differences in browser support and use vendor prefixes reasonably. 1. Understand common problems such as Flexbox and Grid support, position:sticky invalid, and animation performance is different; 2. Check CanIuse confirmation feature support status; 3. Correctly use -webkit-, -moz-, -ms-, -o- and other manufacturer prefixes; 4. It is recommended to use Autoprefixer to automatically add prefixes; 5. Install PostCSS and configure browserslist to specify the target browser; 6. Automatically handle compatibility during construction; 7. Modernizr detection features can be used for old projects; 8. No need to pursue consistency of all browsers,

Themaindifferencesbetweendisplay:inline,block,andinline-blockinHTML/CSSarelayoutbehavior,spaceusage,andstylingcontrol.1.Inlineelementsflowwithtext,don’tstartonnewlines,ignorewidth/height,andonlyapplyhorizontalpadding/margins—idealforinlinetextstyling

Use the clip-path attribute of CSS to crop elements into custom shapes, such as triangles, circular notches, polygons, etc., without relying on pictures or SVGs. Its advantages include: 1. Supports a variety of basic shapes such as circle, ellipse, polygon, etc.; 2. Responsive adjustment and adaptable to mobile terminals; 3. Easy to animation, and can be combined with hover or JavaScript to achieve dynamic effects; 4. It does not affect the layout flow, and only crops the display area. Common usages are such as circular clip-path:circle (50pxatcenter) and triangle clip-path:polygon (50%0%, 100 0%, 0 0%). Notice

Setting the style of links you have visited can improve the user experience, especially in content-intensive websites to help users navigate better. 1. Use CSS's: visited pseudo-class to define the style of the visited link, such as color changes; 2. Note that the browser only allows modification of some attributes due to privacy restrictions; 3. The color selection should be coordinated with the overall style to avoid abruptness; 4. The mobile terminal may not display this effect, and it is recommended to combine it with other visual prompts such as icon auxiliary logos.

TheCSSPaintingAPIenablesdynamicimagegenerationinCSSusingJavaScript.1.DeveloperscreateaPaintWorkletclasswithapaint()method.2.TheyregisteritviaregisterPaint().3.ThecustompaintfunctionisthenusedinCSSpropertieslikebackground-image.Thisallowsfordynamicvis

To create responsive images using CSS, it can be mainly achieved through the following methods: 1. Use max-width:100% and height:auto to allow the image to adapt to the container width while maintaining the proportion; 2. Use HTML's srcset and sizes attributes to intelligently load the image sources adapted to different screens; 3. Use object-fit and object-position to control image cropping and focus display. Together, these methods ensure that the images are presented clearly and beautifully on different devices.

Different browsers have differences in CSS parsing, resulting in inconsistent display effects, mainly including the default style difference, box model calculation method, Flexbox and Grid layout support level, and inconsistent behavior of certain CSS attributes. 1. The default style processing is inconsistent. The solution is to use CSSReset or Normalize.css to unify the initial style; 2. The box model calculation method of the old version of IE is different. It is recommended to use box-sizing:border-box in a unified manner; 3. Flexbox and Grid perform differently in edge cases or in old versions. More tests and use Autoprefixer; 4. Some CSS attribute behaviors are inconsistent. CanIuse must be consulted and downgraded.
