Yogi
Yogi

Reputation: 1579

Why does git checkout <<file>> not work when the same file is in index?

I have 2 files in my git folder 1.txt and 2.txt. I added a line in one of the files (1.txt) and moved it to staging. When I try to check the same file out from local repository, why doesn't it work?

Even after the checkout the file has the same content as in the local folder and does not replicate the older file content from the local repository. Why does checkout command remove my newly added line adding new text? I am a bit confused by this behaviour. Please, refer to the picture below for my series of commands.

enter image description here

Upvotes: 1

Views: 226

Answers (1)

axiac
axiac

Reputation: 72425

This page of the Git book explains it pretty well.

git checkout copies the file from the index into the working tree (discards the unstaged changes). But you have no unstaged changes, you just added 1.txt to the index.

What you want is probably git reset followed by git checkout.

git reset with a file path as argument copies the file from HEAD into the index. It effectively reverts the effect of git add for that file.

git checkout, assuming the file in the index matches the file in HEAD, undoes the editing you did on that file. Warning: you lose the changes.

Upvotes: 1

Related Questions