0

I am working on master branch . I used the following command.

git pull origin master

git rm <some files>

git commit -m "meesage"
git push origin master 

I want to get back the file I deleted from the remote:( Please respond.

I tried :

git reset head 
git reset --hard head

Nothing worked

Delfin
  • 607
  • 7
  • 18
  • Possible duplicate of [Git + Rails: How to restore files deleted with "git rm -r"?](https://stackoverflow.com/questions/5024288/git-rails-how-to-restore-files-deleted-with-git-rm-r) – phd Jan 16 '18 at 05:26

1 Answers1

1

You need to tell it to go back 1 revision. One way to do that is to specify HEAD~:

git reset --hard HEAD~

Without the tilde, you are just telling it to restore to the state since the most recent revision, which is the one that removed the file.

Keith Bennett
  • 4,722
  • 1
  • 25
  • 35
  • Hi that works locally. But when I try to pull and then push the code again from remote server . The pull again removes the files that were deleted. And yeah I cannot push these chanegs without a pull. – Delfin Jan 16 '18 at 04:53
  • Yes, dealing with a remote server is another dimension. It's best to recover the files and then add them to a _new_ revision, and push that. – Keith Bennett Jan 16 '18 at 04:59
  • Yes you can push that to remote, you have to force it though. If a commit is in remote that is not in local, it typically means that someone else pushed a commit to the repo. Normally, you would then rebase your changes on top of that and push the result. In this case though, you have to tell git that you know what you are doing. – Ulrich Eckhardt Jan 16 '18 at 04:59
  • Concerning the advise to restore the file in a new commit, that's possible, too, it's just a `git revert REVISION` where `REVISION` is the commit you want to undo. However, assuming all users of the repo are smart enough to handle an upstream rebase (or to learn it), purging the faulty commit is probably the cleaner solution in this case. YMMV though. – Ulrich Eckhardt Jan 16 '18 at 05:02
  • I don't think changing the history is a good idea when others may be affected. That's why I suggest creating a new revision with the re-added files. Am I missing something? – Keith Bennett Jan 16 '18 at 05:32
  • @KeithBennett that helped me clear a lot of doubts .Thank you – Delfin Jan 16 '18 at 10:27