Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to track but not stage and how to unstage but not untrack?

Tags:

git

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 ?

like image 717
Jeflopo Avatar asked Mar 27 '13 06:03

Jeflopo


People also ask

How do I Unstage untracked files?

The easiest way to unstage files on Git is to use the “git reset” command and specify the file you want to unstage. By default, the commit parameter is optional : if you don't specify it, it will be referring to HEAD.

How do you Unstage all changes?

To unstage commits on Git and discard all changes, use the “git reset” command with the “–hard” argument.

What does it mean to Unstage a file?

Unstaged changes are changes that are not tracked by the Git. For example, if you copy a file or modify the file. Git maintains a staging area(also known as index) to track changes that go in your next commit.


1 Answers

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)?".

like image 128
VonC Avatar answered Sep 22 '22 06:09

VonC