1

In a group project, If one member of the development team deletes a few commits using the command,

git reset --hard commit-number

and then pushes it to the repo using

git push origin master --hard

By executing this, the commits made after the "commit number" specified will be deleted from the remote repository.

Now how to find who deleted it and how to restore those deleted commits?

Note: In this case, none of the developers come forward to accept his mistake and so its difficult to find this out with a discussion. Is there a way to backtrack and find the username of the developer who deleted it?

Sorabh
  • 21
  • 1
  • 4
  • 1
    Possible duplicate of [Git - Can we recover deleted commits?](https://stackoverflow.com/questions/34751837/git-can-we-recover-deleted-commits) – Druta Ruslan Feb 16 '19 at 08:49
  • No here the question is how to find the person who deleted the commit and Is there any way to restore with finding that user – Sorabh Feb 17 '19 at 08:25
  • I don't think git keeps that information, but you should be able to see who pushed the reset if that happened. --- Also, `git-reset` does not delete commits. – evolutionxbox Feb 18 '19 at 09:49

2 Answers2

2

Please see this post or this

To get back to that commit you can use the reflog to look up it's ref.

Reference logs, or "reflogs", record when the tips of branches and other references were updated in the local repository.

Run this command:

git reflog

Scan the first few entries, and find the commit that was lost. Keep track of the identifier to that commit (you can use either the 1st or 2nd columns). Let's call the identifier "ID".

If you have not made any extra work since you did the reset --hard you can do:

git reset --hard ID
git push -f origin master

If you have made other work since the reset, you could cherry-pick if back onto your branch like this:

git cherry-pick ID
git push origin master
Masoud Mokhtari
  • 2,390
  • 1
  • 17
  • 45
  • 1
    This is true if I have deleted those commits. But here the question is how to restore if someone else has deleted the commit. And first how to find who in the team has deleted the commit. – Sorabh Feb 17 '19 at 08:23
0

To restore the deleted commits, as a first step you could check if any coworker still has their local version of the branch.

If you need to restore the shared version to what it was before the reset, be sure to agree on who has the expected version on their branch, and ask them to git push --force

For the part about knowing who done it, I'd recommend asking.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • 1
    Is there a way to backtrack and find the user who deleted the commits, if I could not find the coworker who deleted it? – Sorabh Feb 17 '19 at 08:45