Search code examples
gitresetauthor

Git show reset on a branch and who did it


we had a problem when working on a branch. Someone did a git reset making us loosing everything... Is there any way to get to know if a reset has been done on a branch and who did it? I promise nothing will happen to him :)

Thanks, Saverio


Solution

  • Following two links provide a reference of information you need:

    http://gitready.com/advanced/2009/01/17/restoring-lost-commits.html

    http://www.programblings.com/2008/06/07/the-illustrated-guide-to-recovering-lost-commits-with-git/

    Now let's understand the process:

    1. Run git reflog, copying data from your update:
    74a33f7f (HEAD -> env/svis, origin/env/svis) refs/remotes/origin/env/svis@{0}: update by push 
    b8d160aa refs/remotes/origin/env/svis@{1}: update by push 
    2bbd8d56 (tag: v1.0.0-prc-svis-snapshot1, tag: PTES) refs/remotes/origin/env/svis@{2}: fetch origin --recurse-submodules=no --progress --prune: fast-forward 
    54f5e0d2 refs/remotes/origin/env/svis@{3}: fetch origin --recurse-submodules
    
    • Top most line is the current head of the branch and as you move down it shows earlier heads, as shown by the numbering here {0} is current head, {1} is one before, {2} is one before them

    Now what you can do with this information

    First set of 8 characters are unique hash commits

    1. git show <Hash-Commit> (shows all the information about a commit)
    2. git fsck --lost-found (If reset is run will show the dangling commits, which is critical when we can't see the commits via reflog), these are unrerferenced heads
    3. git merge <Hash-Commit>, will recover to the lost commit

    Had this been a local reset case we could have done merge or pull to get back the original code. Other options are git rebase <Hash-Commit>.

    1. Prominent options remains reset and reset --hard, first one being soft reset, also there's a command git revert, which is interesting, as it create a reverse of a commit, I find this as a preferred option, check out the following link too as it explains many details in a much simpler manner

    https://opensource.com/article/18/6/git-reset-revert-rebase-commands

    Once you start using this info you can find many more details, based on hash-commits listed by the reflog. You can refer last commit using HEAD~1, one before that using HEAD~2