45

I'm a bit new to git, and I fail to understand why git commit -a only stages changed and deleted files but not new files.

Can anyone explain why is it like this, and why there is no other commit flag to enable adding files and committing in one command?

BTW, hg commit -A adds both new and deleted files to the commit

Community
  • 1
  • 1
splintor
  • 9,924
  • 6
  • 74
  • 89
  • 1
    You might want to ask why it is on the git list. This could be a useful feature so if anyone cared enough to add it. – xenoterracide May 03 '10 at 16:44
  • I guess thet there is not `git commit -A` because 1.) adding new files is relatively uncommon; also most other VCS require explicit `scm add ` step 2.) `.gitignore` and like files are not perfect, which could lead to adding unwanted files to commit. – Jakub Narębski May 03 '10 at 16:53
  • 2
    This definitely should be the default `-a` behavior – juliangonzalez Apr 26 '16 at 20:21

5 Answers5

28

Git is about tracking changes. It relies on you to tell it which files are important enough to track. You can achieve the desired affect like so:

git add . ;git commit -a

Make sure your .gitignore file is updated.

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Kelly S. French
  • 12,198
  • 10
  • 63
  • 93
  • Yeah. This allows you to keep various files that you don't want to track around in the repository, and only add what you are certain you want to track. – Kzqai May 03 '10 at 16:27
  • 1
    the -a is redundant if done in the root of the repository. basically the questioner needs – xenoterracide May 03 '10 at 16:30
  • 4
    No, `-a` is not redundant. It ensures that deletions are also staged. – CB Bailey May 03 '10 at 16:37
  • 1
    I want to tell git, I just want to tell it as part of the commit command, and not with a separate add command. – splintor May 03 '10 at 18:11
  • 2
    Options: 1) combine the commands into a script and use it instead of the base commit. 2) submit a feature request. 3) checkout the source and add the feature yourself. – Kelly S. French May 03 '10 at 18:15
  • 3
    Thanks for listing my options. I'm still interested in the main reason it doesn't yet exist. Am I the only one missing it? I think it is even more important for newbies tutorial - "here, you see - I edited a file, and now in one command I tell git to see what was changed and add commit it to the repository.". I know I can write a script to do it, but that's a bad option when writing a tutorial, or showing someone how easy and comfortable git is. I'll try to look into submitting a request. – splintor May 03 '10 at 19:40
  • It's dangerous to automatically add untracked files because quite often you end up with files lying around that you don't want to get added to the repository. Build results, random OS files like Thumbs.db, etc. – Mike Weller May 03 '10 at 19:57
  • 1
    But the option does exist in git add -A. I just want to have the same option in git commit, for cases I know what I'm doing. – splintor May 03 '10 at 20:48
  • @splintor, I would guess that it doesn't exist as part of commit is the developers feel that which files to add to the staging area is an important enough decision to be kept separate from the commit. Commits on tracked files are routine, adding a new file is not. Look at it this way, maybe they didn't want to add an "Are you sure (Y/N)" prompt to a commit-with-add command. – Kelly S. French Nov 11 '10 at 15:16
  • 1
    What's wrong with one command `git add -A && git commit`? I mean, what would `git commit -A` have in favor if it existed? (apart from typing a few less characters.) I think the reason it isn't there (if not just because it can be done easily as mentioned) is that 99 out of 100 projects have at least one e.g. file that does not belong in the SCM repository, such as a configuration file or a file that keeps track of database versions etc. – Pelle Jan 27 '11 at 16:52
  • @BradTurek I think you posted the wrong link. I see no one short command in the link you posted to. – splintor Oct 17 '20 at 21:47
  • @splintor, and others that want just **one short command**, there is [a way to do it](https://stackoverflow.com/a/64395647/5432315). – Brad Turek Oct 18 '20 at 01:16
  • Thanks, @splintor. Sorry about that. I fixed it and deleted my old comment. – Brad Turek Oct 18 '20 at 01:17
14

I suggest another solution: using git commit --interactive -m "your commit message" will show you this menu

*** Commands ***
  1: [s]tatus     2: [u]pdate     3: [r]evert     4: [a]dd untracked
  5: [p]atch      6: [d]iff   7: [q]uit   8: [h]elp

allowing you to check status, add untracked files and so on using simple keystrokes.

Druid
  • 6,423
  • 4
  • 41
  • 56
Gonzalo Cao
  • 2,286
  • 1
  • 24
  • 20
10

I suspect the answer is simple (but I doubt I'll be popular for saying it!) -- there is likely no deliberate "why" to this, other than it's how it fell out when the developers implemented it. The priority of the Git project has never been on ease-of-use or user-friendliness.

Matt R
  • 9,892
  • 10
  • 50
  • 83
6

Kelly is correct but I think another factor is that so many people expect that behavior because CVS, Subversion, and most other tools do it that way.

If Git committed new files, you might notice that you had committed .o files long ago and even worse they might harm the build.

JasonSmith
  • 72,674
  • 22
  • 123
  • 149
  • 3
    I don't want it to be the default, but I want it to let me do it. I can do git add ., so why not allowing me to do it as part of the git commit command? – splintor May 03 '10 at 18:10
5

For Future sake you can stick with this solution from Ian Clelland,

git add -A && git commit -m "Your Message"

Since it won't be too visible from comment https://stackoverflow.com/a/2419270/5836034

Theophilus Omoregbee
  • 2,463
  • 1
  • 22
  • 33