Commit and automatically add all untracked files

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.


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'

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.