Why doesn't git commit -a add new files?
Asked Answered
A

5

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

Artiodactyl answered 3/5, 2010 at 16:0 Comment(3)
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.Furring
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 <file> step 2.) .gitignore and like files are not perfect, which could lead to adding unwanted files to commit.Bunsen
This definitely should be the default -a behaviorHalsy
S
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.

Serrulation answered 3/5, 2010 at 16:23 Comment(13)
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.Melano
the -a is redundant if done in the root of the repository. basically the questioner needsFurring
No, -a is not redundant. It ensures that deletions are also staged.Blabbermouth
I want to tell git, I just want to tell it as part of the commit command, and not with a separate add command.Artiodactyl
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.Serrulation
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.Artiodactyl
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.Salonika
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.Artiodactyl
@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.Serrulation
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.Geibel
@BradTurek I think you posted the wrong link. I see no one short command in the link you posted to.Artiodactyl
@splintor, and others that want just one short command, there is a way to do it.Haemolysin
Thanks, @splintor. Sorry about that. I fixed it and deleted my old comment.Haemolysin
B
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.

Bezonian answered 9/7, 2012 at 20:7 Comment(0)
J
11

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.

Joella answered 12/5, 2013 at 17:33 Comment(0)
N
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.

Nelsen answered 3/5, 2010 at 16:26 Comment(1)
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?Artiodactyl
A
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://mcmap.net/q/13899/-how-can-i-stage-and-commit-all-files-including-newly-added-files-using-a-single-command

Azal answered 29/6, 2017 at 12:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.