Languages
[Edit]
EN

Git - revert single file

5 points
Created by:
Kate_C
23510

In this short article, we would like to show how in Git revert single file to the 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 disappeared - button.tsx is restored to the last version.

Git

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join