What's a <pathspec> in the git command?
Asked Answered
S

3

31

I have updated, modified and removed files in my application and I am now ready to commit. Here is the status:

C:\G\ab\WebAdminApp>git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   WebAdminApp.csproj
        modified:   WebAdminApp.csproj.user
        modified:   app/admin/controllers/ContentController.ts
        deleted:    app/admin/interfaces/IEnumService.ts
        modified:   app/admin/interfaces/IHomeController.d.ts
        modified:   lib/pagedown/Markdown.Sanitizer.ts
        deleted:    lib/typings/global.ts
        modified:   package.json
        modified:   ../abilitest-admin.v12.suo

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        app/interfaces/IEnumService.d.ts
        app/interfaces/IUtilityService.d.ts
        ../npm-debug.log

no changes added to commit (use "git add" and/or "git commit -a")

When I enter:

git add . 

It gives me a message saying:

C:\G\ab\WebAdminApp>git add .
warning: You ran 'git add' with neither '-A (--all)' or '--ignore-removal',
whose behaviour will change in Git 2.0 with respect to paths you removed.
Paths like 'WebAdminApp/app/admin/interfaces/IEnumService.ts' that are
removed from your working tree are ignored with this version of Git.

* 'git add --ignore-removal <pathspec>', which is the current default,
  ignores paths you removed from your working tree.

* 'git add --all <pathspec>' will let you also record the removals.

I would like everything I did on my local PC to be committed and then I want the master on GITHUB to reflect this.

Can someone explain what does it mean and what should I now enter so all the changes can be committed with a git commit ? Sorry it's not clear to me. Is the directory or ?

Swingeing answered 30/12, 2014 at 20:14 Comment(3)
Yes I read the message. It suggests use of "gitadd -all <pathspec>" . Did you read my question where I am asking. What is <pathspec>. I am sure that's very clear to a person who knows git but as a new user do you think we all know what <pathspec> means ?Swingeing
@SamanthaJ A “pathspec” refers to how you specify paths to things in Git, including the use of wildcards. These are used in the .gitignore file, but also on the command-line (git add *.c).Andreaandreana
@jubobs - Thanks. So in this case where I want to include everything should I just do git add -all . or git add -all / ?Swingeing
J
28

This answer was mostly derived from Git Pathspecs and How to Use Them. I haven't copied everything over, so look into the link to dig deeper


The pathspec is the mechanism that git uses for limiting the scope of a git command to a subset of the repository. If you have used much git, you have likely used a pathspec whether you know it or not. For example, in the command git add README.md, the pathspec is README.md. However, it is capable of much more nuance and flexibility.

So, why should you learn about pathspecs? Since it is a part of many commands, these commands become much more powerful with an understanding of pathspecs. With git add, you can add just the files within a single directory. With git diff, you can examine just the changes made to filenames with an extension of .scss. You can git grep all files except for those in the /dist directory.

File or directory

git add .      # add CWD (current working directory)
git add ..     # add parent directory and its subdirectories
git add src/   # add src/ directory
git add README # add only README directory

Also note that the git add command takes [<pathspec>...]. The ... means possibility of multiple occurence. So you can do:

git add /content /images

and that would all changes under both directories.

if you ever do ls -a, it will list all files and 'directory entries', it will include . and .. for more see here

Wildcards

💡Note the single quotes

git log '*.js' # logs all .js files in CWD and subdirectories
git log '.*'   # logs all 'hidden' files and directories in CWD
git log '*/.*' # logs all 'hidden' files and directories in subdirectories

An example using git add:

git add '*/*.swift' # stages all 'swift' files

top

The top signature tells git to match the pattern from the root of the git repository rather than the current working directory. You can also use the shorthand :/ rather than :(top).

git ls-files ':(top)*.js'
git ls-files ':/*.js' # shorthand

icase

The icase signature tells git to not care about case when matching .. [e.g.] this could be useful for matching jpg files, which sometimes use the uppercase extension JPG.

 git ls-files ':(icase)*.jpg'

exclude

Lastly, there is the “exclude'” magic signature (shorthand of :! or :^)... [e.g.] you can search through all of your .js files while excluding the .spec.js test files.

 git grep 'foo' -- '*.js' ':(exclude)*.spec.js' # search .js files excluding .spec.js
 git grep 'foo' -- '*.js' ':!*.spec.js' .       # shorthand for the same

Other good use cases of pathspec

See the change made for a single file in a specific commit:

git show 78b0383148bdfeca4a85aa445949cfe1a131c4a6 config.yml

Search the commit messages for a certain string for a specific file. For more on that see here

git log -S "plus sign" -- Documentation/git-branch.txt

Restore files at certain path(s) to its previous commit. See here and here

git checkout c5f567 -- file1/to/restore file2/to/restore # identify the commit with the SHA1
git checkout origin/main -- src/main/java/HelloWorld.java # identify the commit with a branch

Jessiejessika answered 30/3, 2020 at 2:9 Comment(0)
N
8

From the Git Glossary:

[A pathspec is a pattern] used to limit paths in Git commands.

Pathspecs are used on the command line of "git ls-files", "git ls-tree", "git add", "git grep", "git diff", "git checkout", and many other commands to limit the scope of operations to some subset of the tree or worktree.

As an example, the command git add :/*.ts will recursively add to the index all of the files that end with .ts starting at the root of the repository (respecting the various ways of ignoring files).

Nolly answered 15/3, 2016 at 20:58 Comment(2)
:/ as git root is very helpful for checking out a file without using relative path. TYVMBougainville
You don't need a double-asterisk. git add :/*.ts is enoughWhopping
W
1

If you want the files you deleted to be deleted in the repository as well, do git add --all .. If you do not want them deleted in the repository, do git add --ignore-removal ..

Webfooted answered 7/6, 2015 at 19:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.