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

后端 未结 3 1470
轻奢々
轻奢々 2021-02-02 05:46

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 a

相关标签:
3条回答
  • 2021-02-02 06:16

    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 to git add -p but also has an interface for adding new files.

    0 讨论(0)
  • 2021-02-02 06:21

    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.

    0 讨论(0)
  • 2021-02-02 06:29

    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.

    0 讨论(0)
提交回复
热议问题