Git – Revert File to Previous Commit

Sometimes it happens that you make some changes to a file but later realize that it was a mistake and these changes have to be discarded.

In Git you can revert the changes made to a file if you haven’t committed them yet, as well as you can revert a file to any previous commit.

Here i will show how to revert a single file to a specific revision and how to reset an uncommitted file to the initial master’s state.

Cool Tip: Want to look back to see what has happened with one particular file? Find out the commit history of this file, history of diffs and up to the history of renames! Read more →

Git – Revert Changes to File

Revert (reset) changes to a file if they haven’t been committed yet:

$ git checkout -- <file>

Git – Revert File to Previous Commit

Revert (reset) a single file to a specific revision:

$ git checkout <commit_hash> -- <file>

9 Replies to “Git – Revert File to Previous Commit”

  1. Thanks

  2. For git checkout, I remember that there is some shorthand notation for the number of previous commits that you want to revert back to.

    eg HEAD~1 HEAD~2 . could you go into detail on that in another article?

  3. thanks helped

  4. Thank you – perfect!!!

  5. What does the

    means?

Leave a Reply