Languages
[Edit]
EN

Git - revert single file

5 points
Created by:
Kate_C
2943

In this short article we would like to show how in Git revert sinle file to last version using Bash (or any other command line).

Quick solution:

git checkout HEAD path/to/my/file

Practical example

Let's suppose, we have modified few files and we want to revert only src/components/button.tsx.

Use one of the below commands:

  1. git checkout HEAD src/components/button.tsx
  2. git checkout -- src/components/button.tsx

Where: HEAD and -- indicate last version.

Full example:

john@ubuntu-pc:/home/john/Projects/shop$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   src/components/button.tsx
	modified:   package.json

john@ubuntu-pc:/home/john/Projects/shop$ git checkout HEAD src/components/button.tsx

john@ubuntu-pc:/home/john/Projects/shop$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   package.json

We can see the line with modified:   button.tsx dissapeard - button.tsx is restored to last version.

Hey ūüĎč
Would you like to know what we do?
  • Dirask is online IT community for professionals and hobbyist to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.