+2 votes
1 view
in DevOps and Agile by (30k points)
edited by

Sometimes git suggests git rm --cached to unstage a file, sometimes git reset HEAD file. When should I use which?

See the following code:

D:\code\gt2>git init

Initialized empty Git repository in D:/code/gt2/.git/

D:\code\gt2>touch a

D:\code\gt2>git status

# On branch master

#

# Initial commit

#

# Untracked files:

#   (use "git add <file>..." to include in what will be committed)

#

#       a

nothing added to commit but untracked files present (use "git add" to track)

D:\code\gt2>git add a

D:\code\gt2>git status

# On branch master

#

# Initial commit

#

# Changes to be committed:

#   (use "git rm --cached <file>..." to unstage)

#

#       new file:   a

#

D:\code\gt2>git commit -m a

[master (root-commit) c271e05] a

 0 files changed, 0 insertions(+), 0 deletions(-)

 create mode 100644 a

D:\code\gt2>touch b

D:\code\gt2>git status

# On branch master

# Untracked files:

#   (use "git add <file>..." to include in what will be committed)

#

#       b

nothing added to commit but untracked files present (use "git add" to track)

D:\code\gt2>git add b

D:\code\gt2>git status

# On branch master

# Changes to be committed:

#   (use "git reset HEAD <file>..." to unstage)

#

#       new file:   b

#

2 Answers

+1 vote
by (51.2k points)

As per the documentation 

git rm --cached <filePath>

does not unstage a file, it stages the removal of the file from the repo but leaves the file in your working tree (leaving you with an untracked file).

git reset -- <filePath> will unstage any staged changes for the given file(s).

if you use

git rm --cached on a new file that is staged

It would look like you had just unstaged it since it had never been committed before.

Refer: https://git-scm.com/docs/git-rm#Documentation/git-rm.txt---cached

+3 votes
by (63.4k points)

Remove directory from git but NOT local

If you want to remove this directory from git but not delete it entirely from the filesystem (local system)

In that case, you have to use:

git rm -r --cached myFolder

Remove directory from git from local as well

The below command will unstage all staged changes for the given file(s).

git reset -- <filePath>

I hope this helps! 

Welcome to Intellipaat Community. Get your technical queries answered by top developers !

Categories

...