Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to revert a file in Git that has been renamed

Tags:

git

I'd like to revert a file that has been renamed, say it's now called B, to what it was in an earlier commit, say it was called A, how would I do this while still preserving the history? File B has been pushed.

I can see the entire history of file B, including when it was named A, using:

git log --follow pathToFileB

This shows me a list of commits which this file was involved in, but I'm not sure what to do from there.

Normally, I'd do git checkout commitId:pathToFile, but this doesn't seem to work in this case.

like image 270
lbj-ub Avatar asked Nov 14 '14 22:11

lbj-ub


People also ask

How do I restore a replaced file in git?

If you have modified, added and committed changes to a file, and want to undo those changes, then you can again use git reset HEAD~ to undo your commit. Similar to the previous example, when you use git reset the modifications will be unstaged.

What happens if you rename a file in git?

Git keeps track of changes to files in the working directory of a repository by their name. When you move or rename a file, Git doesn't see that a file was moved; it sees that there's a file with a new filename, and the file with the old filename was deleted (even if the contents remain the same).

How do I undo a file in git?

If you have committed changes to a file (i.e. you have run both git add and git commit ), and want to undo those changes, then you can use git reset HEAD~ to undo your commit.

How do I undo a staged change in git?

Staged files are those which go into your next commit. If you accidentally added files to the staged area, you can undo this by typing git restore --staged <file> , so in this case, it would be git restore --staged lib.


1 Answers

You can overwrite file B with the old contents of file A with:

git show commitId:pathToFileA > pathToFileB

You can read more in this answer to a similar question https://stackoverflow.com/a/888623/4231110

like image 52
Justin Howard Avatar answered Oct 19 '22 02:10

Justin Howard