EN
Git - revert single file
5
points
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:
-
git checkout HEAD src/components/button.tsx
-
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.