国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
File or directory
Wildcard
Square bracket expression
Magic signature
top
icase
literal
glob
attr
exclude
Combination signature
Home Web Front-end CSS Tutorial Git Pathspecs and How to Use Them

Git Pathspecs and How to Use Them

Apr 16, 2025 am 11:53 AM

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is 'render-blocking CSS'? What is 'render-blocking CSS'? Jun 24, 2025 am 12:42 AM

CSS blocks page rendering because browsers view inline and external CSS as key resources by default, especially with imported stylesheets, header large amounts of inline CSS, and unoptimized media query styles. 1. Extract critical CSS and embed it into HTML; 2. Delay loading non-critical CSS through JavaScript; 3. Use media attributes to optimize loading such as print styles; 4. Compress and merge CSS to reduce requests. It is recommended to use tools to extract key CSS, combine rel="preload" asynchronous loading, and use media delayed loading reasonably to avoid excessive splitting and complex script control.

External vs. Internal CSS: What's the Best Approach? External vs. Internal CSS: What's the Best Approach? Jun 20, 2025 am 12:45 AM

ThebestapproachforCSSdependsontheproject'sspecificneeds.Forlargerprojects,externalCSSisbetterduetomaintainabilityandreusability;forsmallerprojectsorsingle-pageapplications,internalCSSmightbemoresuitable.It'scrucialtobalanceprojectsize,performanceneed

Does my CSS must be on lower case? Does my CSS must be on lower case? Jun 19, 2025 am 12:29 AM

No,CSSdoesnothavetobeinlowercase.However,usinglowercaseisrecommendedfor:1)Consistencyandreadability,2)Avoidingerrorsinrelatedtechnologies,3)Potentialperformancebenefits,and4)Improvedcollaborationwithinteams.

CSS Case Sensitivity: Understanding What Matters CSS Case Sensitivity: Understanding What Matters Jun 20, 2025 am 12:09 AM

CSSismostlycase-insensitive,butURLsandfontfamilynamesarecase-sensitive.1)Propertiesandvalueslikecolor:red;arenotcase-sensitive.2)URLsmustmatchtheserver'scase,e.g.,/images/Logo.png.3)Fontfamilynameslike'OpenSans'mustbeexact.

What is Autoprefixer and how does it work? What is Autoprefixer and how does it work? Jul 02, 2025 am 01:15 AM

Autoprefixer is a tool that automatically adds vendor prefixes to CSS attributes based on the target browser scope. 1. It solves the problem of manually maintaining prefixes with errors; 2. Work through the PostCSS plug-in form, parse CSS, analyze attributes that need to be prefixed, and generate code according to configuration; 3. The usage steps include installing plug-ins, setting browserslist, and enabling them in the build process; 4. Notes include not manually adding prefixes, keeping configuration updates, prefixes not all attributes, and it is recommended to use them with the preprocessor.

What are CSS counters? What are CSS counters? Jun 19, 2025 am 12:34 AM

CSScounterscanautomaticallynumbersectionsandlists.1)Usecounter-resettoinitialize,counter-incrementtoincrease,andcounter()orcounters()todisplayvalues.2)CombinewithJavaScriptfordynamiccontenttoensureaccurateupdates.

CSS: When Does Case Matter (and When Doesn't)? CSS: When Does Case Matter (and When Doesn't)? Jun 19, 2025 am 12:27 AM

In CSS, selector and attribute names are case-sensitive, while values, named colors, URLs, and custom attributes are case-sensitive. 1. The selector and attribute names are case-insensitive, such as background-color and background-Color are the same. 2. The hexadecimal color in the value is case-sensitive, but the named color is case-sensitive, such as red and Red is invalid. 3. URLs are case sensitive and may cause file loading problems. 4. Custom properties (variables) are case sensitive, and you need to pay attention to the consistency of case when using them.

What is the conic-gradient() function? What is the conic-gradient() function? Jul 01, 2025 am 01:16 AM

Theconic-gradient()functioninCSScreatescirculargradientsthatrotatecolorstopsaroundacentralpoint.1.Itisidealforpiecharts,progressindicators,colorwheels,anddecorativebackgrounds.2.Itworksbydefiningcolorstopsatspecificangles,optionallystartingfromadefin

See all articles