简体   繁体   中英

git checkout does not undo committed file

I have edited, added and committed some files. When I run:

git diff --stat --cached origin/development

one of the files shouldn't have been changed and I would like to undo the changes to that one file.

Based on the diagram below I ran:

git checkout file_name

but:

git difftool --cached origin/development

still shows the file contains the changes.

How do I undo the changes to the one file and why did git checkout not work?

在此处输入图像描述

As you have mentioned, you have committed some files and now you want to unstage those changes.

Assuming the commit as the latest, you can follow these commands:

1. git reset --soft HEAD~1

2. git reset HEAD.

Now you have all the files as unstaged.

If you want any specific file unstaged, after executing 1 , do like this:

  • git reset HEAD <filename>

I think you have confused git diff and git show here.

You didn't make the commit nor did you stage any changes yet

Use git checkout your-file

You didn't make the commit yet but you staged the change already

Use git reset HEAD your-file

You already made the commit

Make sure you don't have any pending changes (stash them or discard them), copy your commit hash (see COMMIT_HASH) then:

git reset HEAD~1
git checkout your-file
git add your-other-file ...
git commit -c COMMIT_HASH

Note: this assumes that the commit is the top of your branch

If your commit isn't the top of your branch, I'd recommend getting familiar with interactive rebase if and only if you're not touching a public branch. ie don't rewrite public history.

There is a missing parameter in your git checkout command where you should pass the commit that you want to revert the file to, for example:

> git log

commit 11111
Date:  today

    File broken

commit 22222
Date:  yesterday

    File working fine

Then revert the changes to your file:

git checkout 22222 file_name

And the changes will be reverted for file_name and placed in the staging area:

git diff --staged

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM