eush77
eush77

Reputation: 4088

How to interactively add untracked file in Git?

I often use git add -p to commit files in pieces in Git, but that doesn't work for untracked files:

$ git add -p file
No changes.

How can I add this file, but not stage all of it?

I want to add only part of it and commit and leave the rest unstaged.

Upvotes: 5

Views: 1356

Answers (1)

eush77
eush77

Reputation: 4088

You can do this in two steps:

  1. git add -N file

From the man page:

-N, --intent-to-add Record only the fact that the path will be added later. An entry for the path is placed in the index with no content. This is useful for, among other things, showing the unstaged content of such files with git diff and committing them with git commit -a.

This command will add the file empty, so it won't be untracked anymore, but none of it will be staged yet.

# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   file
#
# 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)
#
#   modified:   file
#
  1. git add -p file

Now the file is tracked and git add -p works as it should.

Upvotes: 15

Related Questions