189

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?
MaartenDev
  • 5,631
  • 5
  • 21
  • 33
kishore .
  • 2,123
  • 2
  • 16
  • 12
  • 1
    `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 using `git push ` such as `git push remote master` – Bhaskar Oct 24 '13 at 20:41
  • 3
    also note, you don't need to (and shouldn't) use `sudo` – dax Oct 24 '13 at 20:43
  • Thanks for the reply.If i do not use sudo i get permission denied error. – kishore . Oct 24 '13 at 20:47
  • 2
    Having 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). – Greg Hewgill Oct 24 '13 at 20:49
  • if 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 https://git-scm.com/book/en/v2/Git-Tools-Interactive-Staging . this will help you . – Kapila Ranasinghe Sep 17 '16 at 06:27
  • This 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* – Andrew Wasson May 03 '18 at 19:45

14 Answers14

550

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
Reggie Pinkham
  • 11,985
  • 4
  • 38
  • 36
  • 8
    This doesn't work when one of the file name includes a whitespace with git 1.9.5 – Maxime Helen Nov 22 '17 at 06:25
  • @Reggle Pinkham Your answer would be more helpful if you also mention how to add multiple file types, cause let's not forget the case where User has 1 file of each of 2 or 3 types(extensions) to be committed. So would this `git add *.js *.vue` work ? Or will have to use comma or quotes or slash to escape the asterisk ? – Vicky Dev Jan 03 '23 at 18:53
154

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.

dax
  • 10,779
  • 8
  • 51
  • 86
  • Will `-a` add new (unstaged) files before the commit? – SabreWolfy Jul 12 '16 at 12:23
  • `git commit -a` is shorthand for `git commit --all`, so yes, it will. – dax Jul 12 '16 at 15:15
  • 1
    `$ 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. – SabreWolfy Jul 13 '16 at 13:42
  • For completeness the easiest way to add multiple files of one type is using the asterisk, for example for html use "git add *.html" – Inyoka Jan 27 '17 at 05:51
58

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>>
Fabio Antunes
  • 22,251
  • 15
  • 81
  • 96
35

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...

nadalsol
  • 351
  • 3
  • 5
16

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 ,.

EliuX
  • 11,389
  • 6
  • 45
  • 40
11

Simply use single quotations around each file name to ensure any with spaces work as expected

git add 'file1' 'file2' 'file3' 
stevec
  • 41,291
  • 27
  • 223
  • 311
5
git init

a) for all files

git add -a

b) only specific folder

git add <folder1> <folder2> <etc.>
git commit -m "Your message about the commit"

enter image description here

git remote add origin https://github.com/yourUsername/yourRepository.git
git push -u origin master
git push origin master

if you are face this error than

 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/harishkumawat2610/Qt5-with-C-plus-plus.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Use this command

git push --force origin master
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Harish Kumawat
  • 312
  • 6
  • 10
4

When you change files or add a new ones in repository you first must stage them.

git add <file>

or if you want to stage all

git add .

By doing this you are telling to git what files you want in your next commit. Then you do:

git commit -m 'your message here'

You use

git push origin master

where origin is the remote repository branch and master is your local repository branch.

somi
  • 115
  • 8
  • Thank 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 – kishore . Oct 24 '13 at 20:50
  • No problem.:) Yes, you should use your branch names, origin master are just examples. – somi Oct 24 '13 at 20:59
4

To add all the changes you've made:

git add .

To add single folder:

git add directory path

To add multiple folders:

git add folder1 folder2 folder3 foldern

To commit all the changes:

git commit -m "message"

To push your committed changes from your local repository to your remote repository:

git push origin branch name
Ranjeet R Patil
  • 453
  • 6
  • 10
1

It sounds like git is launching your editor (probably vi) so that you can type a commit message. If you are not familiar with vi, it is easy to learn the basics. Alternatives are:

  • Use git commit -a -m "my first commit message" to specify the commit message on the command line (using this will not launch an editor)

  • Set the EDITOR environment variable to an editor that you are familiar with

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
1

If you want to stage and commit all your files on Github do the following;

git add -A                                                                                
git commit -m "commit message"
git push origin master
Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
Johnson Ogwuru
  • 25
  • 1
  • 10
1

I use the command: git add *CMakeLists.txt, in order to add all the CmakeLists.txt files across the sub-directories:

z:\Temp\proj_I2.21.09.24\CMakeLists.txt
z:\Temp\proj_I2.21.09.24\tools\t1\src\CMakeLists.txt
z:\Temp\proj_I2.21.09.24\tools\TAF\VV\CMakeLists.txt
z:\Temp\proj_I2.21.09.24\tools\TAF\Executable\CMakeLists.txt
z:\Temp\proj_I2.21.09.24\tools\TAF\Recording\CMakeLists.txt
z:\Temp\proj_I2.21.09.24\Design\original\CMakeLists.txt

caoanan
  • 554
  • 5
  • 20
0

Try this:

git add file1
git commit file1
git push 
git add file2
git commit file2 --amend
git push 

Note: git commit without -m option will open an editor for your commit message.

-1

git add fil1 file2 -- File names are separated with space

git add *.java -- add file with pattern

git add . -- Add entire directory recursively

  • 1
    This question already has a dozen answers. Are you _entirely sure_ that this answer brings something new? – ChrisGPT was on strike Aug 15 '23 at 14:23
  • Normally people add one file at a time , even if they have to add 2 files only,(not add all). so I have written that we can add 2 files at a time. – Abhishek Dwivedi Aug 22 '23 at 18:02
  • [The top-rated answer](https://stackoverflow.com/a/23621748/354577) covers your first case. [The second-rated, accepted answer](https://stackoverflow.com/a/19576183/354577) covers your third case. And [this answer](https://stackoverflow.com/a/35181754/354577) shows wildcards. – ChrisGPT was on strike Aug 22 '23 at 18:17
  • (The _entire question_ is how to add multiple files at once. Very few answers show how to add just a single file.) – ChrisGPT was on strike Aug 22 '23 at 18:23