0

Hi folks I am new developer trying to learn.

I use git desktop where you right click on a file and select discard changes, if you don't want to add that file into commit. It revert it back to its original state.

Now I am working in terminal how can I do that in terminal?

By mistake I added some changes into a file and I don't want to keep those changes. Please help.

Please, I am not an advanced user therefore kindly keep it as simple as possible. I have not made any commit on that machine yet.

I am only interested in I googled it but could not find a simple layman'ish answer.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • It may help people answer accurately if you share the commands that you executed so we know exactly where you're up to. You can use `history` in terminal to show the commands you've executed (only show us relevant commands). – Adam Nierzad Feb 17 '20 at 09:21
  • 1
    Does this answer your question? [Hard reset of a single file](https://stackoverflow.com/questions/7147270/hard-reset-of-a-single-file) – phd Feb 17 '20 at 12:27
  • https://stackoverflow.com/search?q=%5Bgit%5D+discard+changes+file – phd Feb 17 '20 at 12:27

3 Answers3

3

You can simply use:

git checkout -- path/to/file

The -- is so that the filename or path cannot be inferred as a branch or remote/branch.

ruohola
  • 21,987
  • 6
  • 62
  • 97
0

You can do this in several ways

  1. You can checkout original file
git checkout file_path
  1. Stash changes and continue work
git stash file_path
  1. Revert head to the previous commit
git reset --hard HEAD~1
  1. Checkout to a specific commit
git reset --hard commit_sha
Dushyantha
  • 217
  • 2
  • 15
0

There are multiple ways to do it. I will mention the simplest of all. It has 2 step:

  1. Unstage the file to latest/current commit. Only required if the the changes to the file is staged.

    git reset HEAD <file>

  2. Undo the file changes.

    git checkout <file>

SUMIT PATRO
  • 166
  • 5