starboy_jb
starboy_jb

Reputation: 935

How to undo a git rm after commit

I have a file name file.txt.

I have made some changes in the file.txt.

I commit and push these changes.

But now I realize that I don't need any change in the file in the current branch. So mistakenly I have done git rm file.txt. And then commit and pushed these deletion changes. But it is showing removed this file on my branch.

All I want is that I don't want to see any change in the file.txt in my branch.

Could anyone please help me here? I have searched on StackOverflow a lot. There are many answers but could not find my case.

Upvotes: 0

Views: 762

Answers (2)

starboy_jb
starboy_jb

Reputation: 935

So finally, I got the answers that I want. Here is the steps that I followed.

git checkout deletion_commit_id^ -- file.txt

This will reverted the deletion and then add, commit and push.

After that, I have done undoing of all the changes of this file.

git checkout origin/master file.txt.

And then add, commit and push.

Upvotes: 1

iamdhavalparmar
iamdhavalparmar

Reputation: 1218

git reset HEAD

Should do it. If you don't have any uncommitted changes that you care about, then

git reset --hard HEAD

should forcibly reset everything to your last commit. If you do have uncommitted changes, but the first command doesn't work, then save your uncommitted changes with git stash:

git stash
git reset --hard HEAD
git stash pop

Upvotes: 0

Related Questions