0

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

  • 1
    Check this http://gitready.com/advanced/2009/01/17/restoring-lost-commits.html It surely helps you recover the lost commits, which may be sweeter than finding the culprit – Mrinal Kamboj Feb 23 '21 at 11:41
  • Are you both working inside the same directory on the same computer? – evolutionxbox Feb 23 '21 at 11:46
  • 1
    Check this https://stackoverflow.com/questions/2540505/how-to-go-back-to-the-last-commit-in-the-history-after-i-used-git-reset-to-go-to – Mrinal Kamboj Feb 23 '21 at 11:49
  • Hi @MrinalKamboj thanks for your answer. At the moment we restored all our work by merging every single change again. So now everything works fine, although we spent a lot of time to recover everything. We need to know what happened and why. From now on I will consider your answer to restore lost commits – Saverio Mirko Viola Feb 23 '21 at 12:00
  • 1
    @SaverioMirkoViola yes that works better, since commits remains dangled till the point `git gc` comes in the picture – Mrinal Kamboj Feb 23 '21 at 12:02
  • @MrinalKamboj can I infer with git reflog what happened on our remote branch? I tried but I got something I cannot understand – Saverio Mirko Viola Feb 23 '21 at 13:11
  • @SaverioMirkoViola yes that shall be possible, what reflog shows you is different commits to a branch, now its possible for you to trace back to the lost commit, but this operation shall be done carefully, let me try and add an answert o explain the details – Mrinal Kamboj Feb 23 '21 at 13:29

2 Answers2

1

for all your git activity... I would go into whatever project directories you are worried about and run

git log --author=yourName

to see your recent changes.

or

git reflog

for all the logs of your repo

in your local machine maybe this can be help full

Harsh Shah
  • 1,386
  • 14
  • 19
  • 1
    This would help only when reset took place on local machine – Mrinal Kamboj Feb 23 '21 at 11:45
  • You have added `reflog` later, that will help over the repo, not just local – Mrinal Kamboj Feb 23 '21 at 11:49
  • I tried reflog on my remote branch: git reflog show origin/myBranch but I cannot understand the result. This is what I get (not all): 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 – Saverio Mirko Viola Feb 23 '21 at 13:13
1

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

Mrinal Kamboj
  • 11,300
  • 5
  • 40
  • 74