Abhijit Mazumder
Abhijit Mazumder

Reputation: 9464

Git: How to overwrite a specific file both in staging/index and local

EDIT I am looking for a single command

Please note I am looking for file specific operation and not commit level operations. At commit level I know it can be done using different flags. So here is my scenario:

I have a single file: test.txt

My repo file contains a text:

first change

My staging file AND my working dir file contains a text:

first change second change

Now if I do git rest HEAD test.txt the state of affairs is following

  1. My repo file contains: first change [nothing changed here]
  2. My index file looks:first change [changed to match repo file]
  3. Working dir file : first change second change [nothing changed here]

What is the command to get into following state:

  1. My repo file contains: first change [nothing changed here]
  2. My index file looks: first change [changed to match repo file]
  3. Working dir file : first change [changed to match repo file]

Upvotes: 1

Views: 524

Answers (1)

Rohit Jain
Rohit Jain

Reputation: 213261

I always do this with git reset HEAD file followed by git checkout -- file.

You can also do this with a single command (this I came to know of recently):

git reset --hard HEAD file  # oops!! doesn't work on paths...

Seems like the previous command doesn't work on paths. In that case, you can use this one:

git checkout HEAD -- file

Note the usage of --. That will replace both staged and working copy of that file.

Upvotes: 4

Related Questions