2

We've all experienced that painful moment when a new feature works fine locally but breaks when deployed because we forgot to add a new file.

Is there a way to make git warn or automatically add new files when git commit -a is executed?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
TheOne
  • 10,819
  • 20
  • 81
  • 119
  • Always run a `git status` before committing, do you always commit blindly without checking what's in your index (to be committed stuff)? Besides, that's what `git commit -a` does... Commit every non-ignored file in your working tree. – KurzedMetal Jun 07 '12 at 13:14
  • 1
    @KurzedMetal, `git commit -a` does not consider untracked files -- to cite the part of the manual describing the `--all` option: "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. " – kostix Jun 07 '12 at 13:57
  • Yeah, my mistake there, still, doing a `git status` before `git commit` is the recommended. If you blindly `git add .` or something like that, you may be adding non-ignored junk to the project history. Which isn't easy to fix if it is pushed to the remote. – KurzedMetal Jun 07 '12 at 14:12

2 Answers2

3

A pre-commit hook like this should alarm about leftover files:

#!/bin/bash
. git-sh-setup # for 'die' cmd
git status --porcelain | while IFS= read -r line;
do
         if [[ $line == \?\?* ]] ; then # if the file begins with ?? it's untracked by git
                die "Uncommited files left!" # this will always terminate commit
                # say "Uncommited files left!" # this will just print the warning
         fi
done

Just add this to your repo's pre-commit hooks and voile :)

EDIT: you should also consider using some continuous integration toolkit like Hudson or Buildbot - it can perform a lot more comprehensive check then looking for missing files :)

EDIT2: unfortunately I didn't succeed in using read inside the loop so I think it might not be possible to make this hook ask for your action.

pielgrzym
  • 1,647
  • 3
  • 19
  • 28
1
git config --global alias.commita '!git add . && git commit -a'

After that just use:

git commita ..

instad of git commit -a. That will add new files too.

Fatih Arslan
  • 16,499
  • 9
  • 54
  • 55