-1

enter image description here

Accidently I commit and push. Now some of my file gone. How can I get back these file using git command?

  • 3
    `git revert`? This may help https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit – Serg Mar 04 '23 at 20:04

1 Answers1

2

If the files were only removed in the last commit do git log -1 --name-only. This will give you a list of the files. Using git checkout HEAD~2 -- <filenames>, you can get the files back as they were before being deleted.

If they were in multiple commits do git log --name-only <name of folder> and not the commit that they were removed in. Then you can do git checkout <SHA>~ -- <file> and this will get them back. The ~ is a shortcut for the previous commit.

The above is useful if there were other changes that you want to keep in the commit. If not, you can just git revert the commit and things will be returned as well.

Schleis
  • 41,516
  • 7
  • 68
  • 87