Usman Sharif HH
Usman Sharif HH

Reputation: 43

Git discard all changes

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.

Upvotes: 0

Views: 2226

Answers (3)

ruohola
ruohola

Reputation: 24038

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.

Upvotes: 3

SUMIT PATRO
SUMIT PATRO

Reputation: 166

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>

Upvotes: 0

Dushyantha
Dushyantha

Reputation: 217

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

Upvotes: 0

Related Questions