Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you git add a new file without staging it?

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 
like image 885
bitmask Avatar asked Jun 18 '12 16:06

bitmask


People also ask

Can I commit without staging git?

If you run git commit right now, though, the index / staging-area still has the original git checkout versions, not the modified work-tree versions. So you have to git add the changed files to copy them into the index. This is kind of a hassle, so Git has git add -u and also git commit -a .

Is git add the same as staging?

Staging is used to track new files in Git and update existing files. All file changes must be staged before they can be committed, and git add is the tool we use to add file contents into Git's staging area. The git add command can be used to stage one file, multiple files, or stage changes in an entire directory.


1 Answers

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):

diff-lib.c: adjust position of i-t-a entries in diff

Problem:

Entries added by "git add -N" are reminder for the user so that they don't forget to add them before committing. These entries appear in the index even though they are not real. Their presence in the index leads to a confusing "git status" like this:

On branch master Changes to be committed:         new file:   foo  Changes not staged for commit:         modified:   foo 

If you do a "git commit", "foo" will not be included even though "status" reports it as "to be committed".

Solution:

This patch changes the output to become

On branch master Changes not staged for commit:         new file:   foo  no changes added to commit 

That means:

Treat such paths as "yet to be added to the index but Git already know about them"; "git diff HEAD" and "git diff --cached HEAD" should not talk about them, and "git diff" should show them as new. + files yet to be added to the index.

like image 160
VonC Avatar answered Sep 21 '22 12:09

VonC