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.
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.
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).
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With