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:
xxxxxxxxxx
1
git checkout HEAD path/to/my-file
Let's suppose, we have modified few files and we want to revert only src/components/button.tsx
.
Use one of the below commands:
-
xxxxxxxxxx
1git checkout HEAD src/components/button.tsx
-
xxxxxxxxxx
1git checkout -- src/components/button.tsx
Where: HEAD
and --
indicate last version.
Full example:
xxxxxxxxxx
1
john@ubuntu-pc:/home/john/Projects/shop$ git status
2
3
On branch master
4
Your branch is up to date with 'origin/master'.
5
6
Changes not staged for commit:
7
(use "git add <file>..." to update what will be committed)
8
(use "git checkout -- <file>..." to discard changes in working directory)
9
10
modified: src/components/button.tsx
11
modified: package.json
12
13
14
john@ubuntu-pc:/home/john/Projects/shop$ git checkout HEAD src/components/button.tsx
15
16
17
john@ubuntu-pc:/home/john/Projects/shop$ git status
18
19
On branch master
20
Your branch is up to date with 'origin/master'.
21
22
Changes not staged for commit:
23
(use "git add <file>..." to update what will be committed)
24
(use "git checkout -- <file>..." to discard changes in working directory)
25
26
modified: package.json
We can see the line with modified: button.tsx
disappeared - button.tsx
is restored to the last version.