How to avoid specifying absolute file path while git-add

20,753

Solution 1

For unix-like systems you can always use the star to point to files, e.g.

 git add *DSManager.java

will include all DSManager.java files git can find within your source tree starting in your current working directory.

Solution 2

Here is another way to add files. Supported at the very least in git 1.7.1.

$ git add -i
           staged     unstaged path
  1:    unchanged      +61/-61 a/very/long/path/that/we/really/dont/want/to/type.txt
  2:    unchanged        +1/-1 another/very/long/path/that/we/really/dont/want/to/type.txt

*** Commands ***
  1: status       2: update       3: revert       4: add untracked
  5: patch        6: diff         7: quit         8: help
What now> 2

Press 2 to select update, or type u.

           staged     unstaged path
  1:    unchanged      +61/-61 a/very/long/path/that/we/really/dont/want/to/type.txt
  2:    unchanged        +1/-1 another/very/long/path/that/we/really/dont/want/to/type.txt
Update>> 2

Press the number corresponding to the file you want to stage. Separate multiple numbers with a comma, e.g. 1,2.

           staged     unstaged path
  1:    unchanged      +61/-61 a/very/long/path/that/we/really/dont/want/to/type.txt
* 2:    unchanged        +1/-1 another/very/long/path/that/we/really/dont/want/to/type.txt
Update>>

Just press [enter] here.

updated one path

*** Commands ***
  1: status       2: update       3: revert       4: add untracked
  5: patch        6: diff         7: quit         8: help
What now> q
Bye.

Finally type 7 or q to quit.

Solution 3

With bash, you can set "globstar" (shopt -s globstar) and then do:

git add **/DSManger.java

to add all files called DSManager.java present below the current directory.

(**/ matches all directories and subdirectories.)

Solution 4

I'm not sure if I understand your question.

To add all files (not yet added), use:

git add .

If you need to add all but one file, you cold add all, then remove the files using:

git reset HEAD <file>

You can also add all files in a subdirectory with

git add subdir/

One thing that I know can be annoying is when you rename files, you need to add the new filename and git rm the old name. When renaming a directory this can be annoying. This (unix only) git alias solves this problem (put it in your ~/.gitconfig file:

[alias] ;add after this heading or create this heading if it does not exist
        addremove = !git add . && git ls-files --deleted | xargs --no-run-if-empty git rm

This adds all new files and removes all deleted files and stages it to the index.

Solution 5

I believe you can just say "git add DSManger.java" if your terminal window is currently cd into the proper folder (src_test/com/abc/product/server/datasource/manager/aats). So just do:

cd src_test/com/abc/product/server/datasource/manager/aats
git add DSManger.java

Otherwise, I can't think of any other way unless you make a separate repo.enter image description here

Share:
20,753
Vaman Kulkarni
Author by

Vaman Kulkarni

Updated on July 08, 2022

Comments

  • Vaman Kulkarni
    Vaman Kulkarni almost 2 years

    Using git add command becomes tedious once the file path becomes lengthy. For e.g. git add src_test/com/abc/product/server/datasource/manager/aats/DSManger.java
    Is it possible to bypass specifying absolute file path? May be using some kind of pattern or something?

    I know that we can use git gui. But I want to do it using cmd line.

    Thanks in advance for the inputs.