1

I was working on local/feature, created remote/feature and pushed my work there.

A coworker pushed to remote/feature.

I noticed a mistake in local/feature, didn't want to create unnecessary commits on my feature branch, so I amended the commit to correct the mistake, and used git push --force to overwrite remote/feature.

I have the hash of my coworkers commit, is there any way I can restore it? Git doesn't recognize the hash locally.

E.D.
  • 639
  • 6
  • 14
  • 2
    The easiest solution would probably be for your coworker to re-push their changes that they should still have on their machine. In the future don't force push unless you know you're safe to do so.. – chevybow Jun 21 '18 at 19:44
  • Do you have access to the remote repo? – melpomene Jun 21 '18 at 19:45
  • @chevybow The coworker wasn't available at the time. And yes, I should have checked before force pushing. – E.D. Jun 21 '18 at 19:49
  • @melpomene I do have access to the remote repo. – E.D. Jun 21 '18 at 19:49
  • I also have access to the body of the overwritten commit through bitbucket, and could recreate the commit if that is the best fix. – E.D. Jun 21 '18 at 19:49
  • Then `git checkout -b choose_a_name THECOMMITHASH` should work. You'll have a new branch you can then merge or revert or whatever. – melpomene Jun 21 '18 at 19:51
  • @melpomene git fails with "reference is not a tree: THECOMMITHASH" when I run that. – E.D. Jun 21 '18 at 19:55
  • 1
    If you're using GitHub, maybe this will help... https://stackoverflow.com/a/35273807/5182932 – sp0gg Jun 21 '18 at 20:03

2 Answers2

1

I agree with all the comments.

As to the problem at hand, if you are in a hurry, recreate the commit and add --author="That Guy" to git commit to honour the author.

Also warn you colleague ASAP, so they know what is happening and don't spend time fixing problems that don't exist.

Vorac
  • 8,726
  • 11
  • 58
  • 101
1

Try this option recover from githubs-reflog suggested by @sp0gg in comment

Never user --force use --force-with-lease instead. it ensures that you don't overwrite other's work.

ref: https://developer.atlassian.com/blog/2015/04/force-with-lease/

rahul mishra
  • 1,390
  • 9
  • 16