6

I often make the mistake of forgetting to stage new files when creating a commit. How can I configure git to warn and/or block me from committing when there are unstaged files?

Note, I'm talking about NEW files. Not unstaged changes to existing files. I know I can auto-include those with -a

Michael Gummelt
  • 702
  • 4
  • 15
  • 2
    I don't know if this is possible, but I don't think it's a good idea. There are often unstaged files that you don't want to include in the repository. `git status` will show you a list of all unstaged files. – mkrieger1 May 04 '15 at 22:32
  • Of course I don't want to be forbidden from committing with unstaged files, but I do want to be alerted. – Michael Gummelt May 06 '15 at 02:40
  • And if you do forget to add some files, you can always `commit --amend`. – mkrieger1 May 06 '15 at 07:53

3 Answers3

4

This isn't really a possible thing unless you elect to write a pre-commit hook for it.

However, it's also not a good idea. Depending on the project you're working on, there are files you do not want to commit due to their nature (auto generated, large binary blobs, system configuration, system keys, etc). Also, if you're doing some more advanced work with Git, like splitting hunks of commits out, the warning will become noise at best.

This is more or less a discipline issue, and I mean this in the best way possible. One must be accustomed to performing the steps to check if something (rather, the correct things) is/are staged before commiting. This means you have to get in the habit of typing out these commands often:

git status
git add . # or whatever variant of add you need to work with
git status
git commit
git push (ideally when ready)
Community
  • 1
  • 1
Makoto
  • 104,088
  • 27
  • 192
  • 230
  • The first reason given here (some files shouldn't be committed) doesn't really seem like a good reason since they should be excluded from possible commit candidates via .gitignore. Or is there some point to that that I'm missing? – JLRishe Jan 31 '21 at 07:47
  • @JLRishe I work on a project that has a busted POM file looking for a specific project in a specific path that doesn't match my development environment. Until we can move to Gradle to fix the issue the POM was trying to solve, I have to stash or revert changes I make to the local POM so our builds don't fail. For obvious reasons, we can't add the POM to the .gitignore file. This is why the steps of checking what you're committing are important. – Makoto Jan 31 '21 at 14:53
3

Might not be exactly what you want, but for me it does the trick. My bash prompt shows different statuses based on the status of the repo i'm in. This also checks for untracked files.

export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWSTASHSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true
export PS1='${debian_chroot:+($debian_chroot)}\[\033[01;33m\]\w\[\033[01;32m\]$(__git_ps1) \[\033[01;36m\]$ \[\033[00m\]'

This will give you some useful information while working on the command line. Notice the '%' when you add a new untracked file.

/tmp/myrepo (master) $ touch newfile.txt
/tmp/myrepo (master %) $ git add newfile.txt 
/tmp/myrepo (master +) $ git commit -m 'added newfile'
[master 9fe6399] added newfile
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 newfile.txt
/tmp/myrepo (master) $ 
crea1
  • 11,077
  • 3
  • 36
  • 46
0

You can use a pre-commit hook to warn you about or prevent you from committing with newly created but unstaged files present.

Essentially, the hook will have to save previously seen unstaged files to a file to refer to them during later commits.

In the hook, if git ls-files -o --exclude-standard --exclude-from=[file] returns nothing, you're golden. Else, append the output to [file] and either output the result of the previous command as a warning or exit with a nonzero statuscode.

If you chose to abort instead of continuing with the commit and want to leave a file unstaged, just attempt to commit again: The hook will have seen it before and ignore it.

I've written a rather more advanced, interactive version called git-stagelight, which you can use.

hyperfekt
  • 169
  • 2
  • 9