简体   繁体   中英

How to restore the deleted commits in Git Repository

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM