How to add multiple files to Git at the same time
Solution 1
To add all the changes you've made:
git add .
To commit them:
git commit -m "MY MESSAGE HERE"
#-m is the message flag
You can put those steps together like this:
git commit -a -m "MY MESSAGE HERE"
To push your committed changes from your local repository to your remote repository:
git push origin master
You might have to type in your username/password for github after this. Here's a good primer on using git. A bit old, but it covers what's going on really well.
Solution 2
Use the git add command, followed by a list of space-separated filenames.
git add file-1 file-2 file-3
Include paths and .extensions, e.g.
git add images/logo.png scripts/app.js
Solution 3
As some have mentioned a possible way is using git interactive staging. This is great when you have files with different extensions
$ git add -i
staged unstaged path
1: unchanged +0/-1 TODO
2: unchanged +1/-1 index.html
3: unchanged +5/-1 lib/simplegit.rb
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now>
If you press 2
then enter
you will get a list of available files to be added:
What now> 2
staged unstaged path
1: unchanged +0/-1 TODO
2: unchanged +1/-1 index.html
3: unchanged +5/-1 lib/simplegit.rb
Update>>
Now you just have to insert the number of the files you want to add, so if we wanted to add TODO
and index.html
we would type 1,2
Update>> 1,2
staged unstaged path
* 1: unchanged +0/-1 TODO
* 2: unchanged +1/-1 index.html
3: unchanged +5/-1 lib/simplegit.rb
Update>>
You see the *
before the number? that means that the file was added.
Now imagine that you have 7 files and you want to add them all except the 7th? Sure we could type 1,2,3,4,5,6
but imagine instead of 7 we have 16, that would be quite cumbersome, the good thing we don't need to type them all because we can use ranges,by typing 1-6
Update>> 1-6
staged unstaged path
* 1: unchanged +0/-1 TODO
* 2: unchanged +1/-1 index.html
* 3: unchanged +5/-1 lib/simplegit.rb
* 4: unchanged +5/-1 file4.html
* 5: unchanged +5/-1 file5.html
* 6: unchanged +5/-1 file6.html
7: unchanged +5/-1 file7.html
Update>>
We can even use multiple ranges, so if we want from 1 to 3 and from 5 to 7 we type 1-3, 5-7
:
Update>> 1-3, 5-7
staged unstaged path
* 1: unchanged +0/-1 TODO
* 2: unchanged +1/-1 index.html
* 3: unchanged +5/-1 lib/simplegit.rb
4: unchanged +5/-1 file4.html
* 5: unchanged +5/-1 file5.html
* 6: unchanged +5/-1 file6.html
* 7: unchanged +5/-1 file7.html
Update>>
We can also use this to unstage files, if we type -number
, so if we wanted to unstage file number 1 we would type -1
:
Update>> -1
staged unstaged path
1: unchanged +0/-1 TODO
* 2: unchanged +1/-1 index.html
* 3: unchanged +5/-1 lib/simplegit.rb
4: unchanged +5/-1 file4.html
* 5: unchanged +5/-1 file5.html
* 6: unchanged +5/-1 file6.html
* 7: unchanged +5/-1 file7.html
Update>>
And as you can imagine we can also unstage a range of files, so if we type -range
all the files on that range would be unstaged. If we wanted to unstage all the files from 5 to 7 we would type -5-7
:
Update>> -5-7
staged unstaged path
1: unchanged +0/-1 TODO
* 2: unchanged +1/-1 index.html
* 3: unchanged +5/-1 lib/simplegit.rb
4: unchanged +5/-1 file4.html
5: unchanged +5/-1 file5.html
6: unchanged +5/-1 file6.html
7: unchanged +5/-1 file7.html
Update>>
Solution 4
You can also select multiple files like this
git add folder/subfolder/*
This will add all the files in the specified subfolder. Very useful when you edit a bunch of files but you just want to commit some of them...
Solution 5
If you want to add multiple files in a given folder you can split them using {,}
. This is awesome for not repeating long paths, e.g.
git add long/path/{file1,file2,...,filen}
Beware not to put spaces between the ,
.
Related videos on Youtube
kishore .
Updated on July 08, 2022Comments
-
kishore . almost 2 years
This will be my first git use. I have added new files ( a lot ) to the folder/project ( git local repository).
I went through online tutorials and forums and see i can do
git commit -a
So I go to the base folder of the repository and do a
sudo git commit -a
But then, some screens comes up and asks me to add a comment which i do. i do not know how to proceed or exit. I do not want to mess up so i did ctrl + Z and did not do anything.
Can you guys please outline the commands I need to use?
git commit -a
and
git push?
-
Bhaskar over 10 years
git commit -a
opens up an editor for you to type commit message. Enter a message you want to see as log and exit the editor. This completes the commit. Follow that up by pushing your changes to remote repository usinggit push <remote name> <branch name>
such asgit push remote master
-
dax over 10 yearsalso note, you don't need to (and shouldn't) use
sudo
-
kishore . over 10 yearsThanks for the reply.If i do not use sudo i get permission denied error.
-
Greg Hewgill over 10 yearsHaving used sudo previously, you probably have files in your working directory that are now mistakenly owned by root. At this point doing other operations without sudo will cause a permission denied error because you can't change those files owned by root. Your repository might be a bit of a mess and it might be best to start over (and don't use sudo).
-
Kapila Ranasinghe over 7 yearsif you want to add all files you can use ` git add -a ` .But if you want to add multiple selected files. you can use ` git add -i ' . please refer this git-scm.com/book/en/v2/Git-Tools-Interactive-Staging . this will help you .
-
Andrew Wasson about 6 yearsThis is old but I think the best way to specifically add the contents of several targeted directories is as follows: git add directory/subdirectory_1* directory/subdirectory_2*
-
-
kishore . over 10 yearsThank you!. On the last command git push origin master. I am actually working on a different branch name ( which was created from the master branch). SO do i need to eneter my branch name or origin master. Thanks again
-
somi over 10 yearsNo problem.:) Yes, you should use your branch names, origin master are just examples.
-
kishore . over 10 yearsThanks a lot everybody who replied. I successfully committed and pushed my files.
-
SabreWolfy almost 8 yearsWill
-a
add new (unstaged) files before the commit? -
dax almost 8 years
git commit -a
is shorthand forgit commit --all
, so yes, it will. -
SabreWolfy almost 8 years
$ man git-commit
includes this for-a
: "Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected.", which is why I asked. -
Kapila Ranasinghe over 7 yearspersonally i like to this way. it is easy to use when you have more files to commit and other operations.
-
Inyoka over 7 yearsFor completeness the easiest way to add multiple files of one type is using the asterisk, for example for html use "git add *.html"
-
lalithkumar almost 7 yearswhat is long/path/ here? @EliuX
-
EliuX almost 7 yearslong/path could be a path string with a very long length, so its more comfortable not repeat such part, without having to
cd
into it -
Maxime Helen over 6 yearsThis doesn't work when one of the file name includes a whitespace with git 1.9.5
-
Cris about 4 yearsOnce you ended, how do you finish the interactive staging?
-
Fabio Antunes about 4 years@Crparedes once you have added the files, if you press enter again it goes back to the menu, then you have to choose option
7
to quit from interactive staging. -
Jesús Sánchez over 3 yearsvery helpful for me
-
candyline over 3 yearssaved me 10 seconds + thousands to come in the future
-
datalifenyc almost 3 yearsJust wanted to add that wildcards are accepted:
git add *.py
to add all python files. -
Graham John over 2 yearsThis was very helpful for me; thanks.