Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+2 votes
2 views
in DevOps and Agile by (29.3k 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 (50.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 (62.9k 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! 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...