Home > Back-end >  How to git add multiple files from same folder without re-typing folder name?
How to git add multiple files from same folder without re-typing folder name?

Time:03-18

I had to write git add src/index.css src/logo.svg, repeating 'src/' multiple times.

Is there a way that I can git add multiple files from the same folder without having to type the same folder name for every file?

CodePudding user response:

In this case,

cd src
git add index.css logo.svg

If there are not any irrelevant files under src/,

git add src

or

cd src
git add .

By the irrelevant files, I mean the untracked files that are not ignored. There could be some new files under src/ just for test. If they are not ignored, git add src or git add . will add them too, which is not expected. If a file is added by mistake, one of the solutions is

git reset path/to/file

Suppose there is only a small number of irrelevant files, foo.js and bar.js, we can also add all and exclude them by

cd src
git add . -- ':!foo.js' ':!bar.js'

If we use git add . or git add folder, better is to always run git status and have a check.

If the files you want to add have already been tracked before, not new untracked files, we can skip git add and use git commit -a to add and commit them in one step. For a new untracked file, we need to explicitly add it.

  •  Tags:  
  • git
  • Related