66

I often forgot that I have some new files and directly do,

git commit -a -m "My commit message"

This only commits changed files, and I have to do add the remaining files in a new commit. This means that there are two separate commits although logically they are part of the same task.

The easiest way I know to include untracked files in the commit via two consecutive commands:

git add -A
git commit -a -m "My commit message"

Is it possible to have the same effect like the above in one command?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Louis Rhys
  • 34,517
  • 56
  • 153
  • 221
  • 7
    Why not `git commit --amend` the previous commit once you add the untracked files? – Richard Apr 21 '13 at 05:05
  • `git add -A` has this message in the man page: `If no is given, the current version of Git defaults to "."; in other words, update all files in the current directory and its subdirectories. This default will change in a future version of Git, hence the form without should not be used. ` – Mark Lakata Dec 12 '16 at 23:42
  • you can always use `git add -A && git commit -a -m "my message"` – gmansour Dec 29 '20 at 17:08

5 Answers5

23

Create a file in your execution path called (no extension): git-add-commit-untracked

Put this in it:

#!/bin/bash
message=${0}
git add -A
git commit -am "$message"

Then: git-add-commit-untracked "Commit message"

You can use a shorter name for the file though. I left it lengthy for illustrative purposes.

Whymarrh
  • 13,139
  • 14
  • 57
  • 108
d_ethier
  • 3,873
  • 24
  • 31
  • 7
    This could be done simpler as a git alias. See my answer here: http://stackoverflow.com/questions/16068968/is-it-possible-to-skip-the-staging-area-and-also-commit-untracked-new-files-t/16069173#16069173 – Klas Mellbourn Apr 21 '13 at 07:41
  • the `git add -A` is useful to know in of itself – Austin A Aug 20 '19 at 16:23
21

You can define an alias to add all before commit. Just put this lines into your ~/.gitconfig file :

[alias]
        ca = !sh -c 'git add -A && git commit -m \"$1\"' -

Then use your alias like this :

$ git ca 'Your commit message'
Thierry Nowak
  • 206
  • 2
  • 6
8

Here are a few options:

  • Use git commit --amend so that you end up with only one commit after adding the untracked files (but not if you've already pushed the previous commit);
  • Use git commit --interactive;
  • Create an alias or script that automatically adds new files (examples in other answers). Here are two aliases that I use for exactly this purpose:

    [alias]
        untracked = ls-files --other --exclude-standard
        add-untracked = !git add $(git untracked)
    
  • Squash your two commits using git rebase -i, git reset <commit prior to first commit> and git commit, or git merge --squash onto another branch.

However, you cannot override a builtin command such as commit with an alias, so it is still up to you to remember to add the files that you want to the index before committing.

Unfortunately as well, you cannot specify paths along with git commit -a; I just tried git commit -a $(git untracked) and it told me fatal: Paths with -a does not make sense. So to answer your basic question, I think a git script would be the only non-interactive way.

Richard
  • 3,316
  • 30
  • 41
  • 4
    However useful this is, it *does answer different question* then the auhor has asked. – gorn Nov 20 '14 at 19:42
  • @gorn I'm not sure what question you think was asked, but this answer provides several options and refers to other answers as well. – Richard Jan 21 '15 at 14:19
  • 1
    Question is quite clear "Is it possible to have the same effect like the above in one command?", so basically "How to do the whole action in one command and avoid the mistake of not adding some files" and this answere adresses "How to repair consequences after the mistake has been done". I think is is confusing to newcomeres and that is why I comment here. – gorn Jan 22 '15 at 16:09
  • 1
    @gorn Sometimes, the answer is 'no', or 'try a different approach.' – Richard Jan 23 '15 at 17:37
0

It's not possible using the default git interface. However, git supports aliases.

Using an alias, it's possible to invoke a different git command, or even run arbitrary shell scripts. Create a script containing the two commands, and alias it.

See the wiki for more information on aliases, and this question for this specific scenario.

Edit: note that this is not such a good idea. The chances are you'll end up adding unnecessary files by mistake, or having a useless alias due to non-versioned files in the tree.

Community
  • 1
  • 1
salezica
  • 74,081
  • 25
  • 105
  • 166
0

Yeah, I had the same problem like you in the past. I preferred to use git command, but I got a big troubles because the guard in rails changed many files without alert :)

So now, whenever i commit new code, I try to use the UI to do this (git gui) and keep in mind never use git commit -a because you don't sure which files were modified! After rebase new code, please you gitk to view the git tree, so you can know the lasted commitment, committers, commit files and so on.

duykhoa
  • 2,227
  • 1
  • 25
  • 43