78

I have two concise questions:

  • How I can track files but without staging them ?
  • How I can unstage files for commit without untracking them ?

NOTE: I know that I can do an initial commit to track the files and start from there with my files tracked. But is possible to specifically do what I'm asking above ?

I tried to use git add -N <expr> but it tracks the file and add it for commit:

PS C:\> git add -N fileA
PS C:\> git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   fileA
#
# 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:   README.md
#       modified:   composer.lock
#       modified:   fileA
#

If I do git reset HEAD fileA or git rm --cached fileA unstages it but also untracks the file. This command git rm fileA suggest me to use the flag -f that removes the file fisically.

So, It is possible to only track but not stage, and to only unstage but not untrack files ?

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
Jeflopo
  • 2,192
  • 4
  • 34
  • 46
  • I'm just curious what would be the purpose of doing that? – Wakan Tanka Nov 09 '16 at 21:55
  • @WakanTanka untracked files aren't treated as being part of the repository by tools like `git ls-files` or `git clean`, but you may want to use these tools at some point between creating the file and including it in a commit. – jayhendren Apr 18 '18 at 19:19
  • 1
    @WakanTanka or, to quote `git add --help`, "This is useful for, among other things, showing the unstaged content of such files with `git diff` and committing them with `git commit -a`." – jayhendren Apr 18 '18 at 19:22
  • Just to give an example of a program, the silver searcher (Ag) searches only tracked changes so this is extremely useful – Emobe Jun 04 '20 at 05:20
  • Another reason; I am trying to commit changes in different groups for a clearer commit history. I like to see a clear view of what is tracked but not staged, in one place. – daaronr Feb 04 '22 at 19:00

2 Answers2

64

Update (May 2015)

I tried to use git add -N <expr> but it tracks the file and add it for commit:

That is no longer the case with the upcoming Git 2.5 (Q2 2015).
See "File doesn′t get into the commit after using git add -N"


Original answer (March 2013)

How I can unstage files for commit without untracking them ?

this is the official way:

git reset HEAD fileA

But since it is a new file, you would untrack it as well (remove it from the index, without any previous commit referencing it).

Starting tracking a file means having it in a index (stage) or a commit.

I would recommend making a branch for those files, in order to add them/commit them there.
See "What is Tracked files and Untracked files in the context of GIT?"

  • Tracked files are files that were in the last snapshot; they can be unmodified, modified, or staged.
  • Untracked files are everything else — any files in your working directory that were not in your last snapshot and are not in your staging area (index)

That means that, for a new file, unstaged it means untrack it.

tracked files in git
(Source: Pro Git Book, 2.2 Git Basics - Recording Changes to the Repository)
(Thank you, louisfischer, for the update/fix in the comments)

See also "git - how to tell if a file is git tracked (by shell exit code)?".

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Then for a new file to be tracked is mandatory to have been staged (added to the index) before ? What you're saying is that's not possible to just track without adding that new file to the index first. If so, its obvious that the reverse, unstage but keep tracked can only be done If files were already tracked. That's right ? – Jeflopo Mar 27 '13 at 07:03
  • @JesúsFlores "unstage but keep" tracked means the local modification are not deleted, but are not tracked by git. The existing file before those modification is still tracked because it was previously committed. – VonC Mar 27 '13 at 07:16
  • @JesúsFlores I have edited the answer to clarify the notion of tracked file. – VonC Mar 27 '13 at 07:32
  • I landed on this interesting thread, but when I saw the diagram of a file status life cycle something didn't feel right. After some research I found [another diagram from the git documentation that seems more accurate](https://git-scm.com/book/en/v2/images/lifecycle.png) – louisfischer Oct 18 '17 at 10:04
  • @louisfischer Thank you and good point. I have fixed the picture and referenced your comment in the answer for more visibility. – VonC Oct 18 '17 at 11:43
1

Track all of your files with git add. Commit all of the changes you want to commit and push to remote. Then stash your new file with git stash. Your file will be tracked but not committed. So in your example you would start by un-staging fileA. Then run git add README.md and git add composer.lock. git commit -m "Committing fun stuff.". Now we need to track the new file without staging it so we simply do git add fileA followed by git stash. When you're ready to commit fileA you can run git stash pop to commit/push to remote.

lucerna
  • 11
  • 1