[Git] How to revert one file changes from one commit

Many times we might changed one file which we don't intent to do... but it was too late, until we found it, it is already push to repo.

 

Let's assume,

First, we checkout a feature branch, we have two files:

a.txt:

Init a

b.txt:

Init b

This is original state. Let's assign a commit id: a111.

 

Now we made a mistake, we changed a.txt by mistake, in the mean while we are also doing some work in b.txt:

a.txt:

I don't know this changes

b.txt:

I want to keep this changes

 

You can have as many commit as you want:

...

 

Now we found that a.txt shouldn't be changed

git checkout a111 src/a.txt // checkout <commit id> <filename>
git status
git add .
git commit -am "revert a.txt"
git push

 

[Git] How to revert one file changes from one commit

 

上一篇:Android中高级面试必知必会,阿里蚂蚁金服五面


下一篇:"Record not found or changed by another user"