To use git effectively (and as intended) I make small atomic commits, while I do have longer sessions where I do change not only one thing. Thus, I make heavy use of git add -p
. This doesn't work for completely new files, though, because I tend to forget them later on.
What I want to do is, tell git
that there is a new file, I want it to track, but not stage it:
Example: Running git status
produces:
# On branch my-current-branch
# Your branch is ahead of 'origin/my-current-branch' by 2 commits.
#
# Changes to be committed:
#
<<STAGED SECTION>> // A
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
<<UNSTAGED-YET-KNOWN SECTION>> // B
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
<<UNKNOWN SECTION>> // C
If I have a file foo
in the C section, and I say git add foo
it will go to the A section. If I say git add -N foo
it will go to both A and B. However, that would mean it would be included in the next commit, at least as the fact that there is a new file.
I want it to go in section B exclusively, such that I can later add it to A with git add -p
or git add foo
(or whatever).
Edit
Regarding the add -N
solution, this doesn't work because if I try to commit after having said add -N
and not having added it properly, git complains because it doesn't know how to handle empty files:
foo: not added yet
error: Error building trees
Maybe you could try writing some pre-commit hook that alerts you if you have untracked files. This will require you to always keep your git directory clean to work, though (and obviously you'll need to keep a up-to-date .gitignore).
Also try
git add -i
which is similar togit add -p
but also has an interface for adding new files.With Git 2.5,
git add -N/--intent-to-add
is actually the right solution.The new file won't be part of the next commit.
See commit d95d728 by Nguyễn Thái Ngọc Duy (
pclouds
) (merged in d0c692263):Problem:
Solution:
That means:
You could commit an empty file with that path before making your changes. If you've already written things there, move the file away, make a blank file, commit that, then add -p as normal and
git commit --amend
so you don't have an "Add blank file" commit.