I was trying to fix a problem where I couldn't pull or push. It seemed there was a single file causing a problem. I ran git push -f origin master
and now most of the files in the remote repo (Bitbucket) are gone. How do I get them back with this commit history? I tried git revert HEAD~1
and it didn't work (it changed 1 file but didn't restore any). I ran git log
and I can see all my commits there, so I guess git still knows of them in some sense - I very much don't get git.

- 756
- 5
- 21
-
git is a versioning system: every commit is still there *but*. When force pushing, you are taking full responsability and accountability on whatever happens next. Identify a commit you like, then try `git checkout
` – Daemon Painter Aug 04 '20 at 07:42 -
@DaemonPainter `Identify a commit you like` how? Through `git log`? – northerner Aug 04 '20 at 07:43
-
yes, git log, or via bitbucket check the graph or the commit list – Daemon Painter Aug 04 '20 at 07:43
-
Could you show an example of your git log output ? – Aserre Aug 04 '20 at 07:44
-
@DaemonPainter what do I do after running `checkout`? – northerner Aug 04 '20 at 07:51
-
to try and provide good feedback I'd need some info. What were you doing? Why weren't you able to push? Why weren't you able to pull? Is there another copy of that repo that hasn't receive the updates you forced push? If so, consider force pushing from that again :) Remember to backup your edits somehow. – Daemon Painter Aug 04 '20 at 09:38
-
@DaemonPainter tbh git confuses the heck out of me and reading documentation doesn't seem to help. When I log into Bitbucket (through the web interface) in the master branch of the repo I was working in, 90% of the files are now gone. I still have them locally but the reason I'm using Git is so I can see old versions and the commit message. I had been pushing from to the same remote repo from two different local folders with dissimilar contents and I realize now this isn't how Git is intended to be used. In short, now that I don't see the files on bitbucket.org, how do I get them back? – northerner Aug 04 '20 at 09:44
-
I know it can be confusing, especially when you run commands not knowing what the outcome would be. No panic :) on your PC, what were you doing on your PC when you decided to do a force push? Where you merging? Rebasing? What commands did you run exactly. – Daemon Painter Aug 04 '20 at 09:46
-
https://stackoverflow.com/search?q=%5Bgit%5D+undo+force+push – phd Aug 04 '20 at 09:58
-
@DaemonPainter I had wanted to add a new file I created to the remote repo. [Somehow it got stuck](https://stackoverflow.com/questions/63220176/cant-add-new-file-to-remote-repo-says-i-need-to-pull-but-already-did) so that I couldn't push or pull so I thought the force option to push would be useful. I don't think I was merging. git also gives `(master|REBASE 2/3)` and I have no clue what that means. – northerner Aug 04 '20 at 10:01
-
means you were in the middle of a rebase, to exit it either complete it or `git rebase --abort`. It gives it now, after you forced push? Did you run anything but `git log` / `git status` in the meantime? – Daemon Painter Aug 04 '20 at 10:08
-
@DaemonPainter yes, `$ git revert HEAD~1` and `git reset --hard 8b68175` and `git checkout
` – northerner Aug 04 '20 at 10:13
1 Answers
When you forced push you local master
branch to origin you basically forced the remote branch to be the same as your local branch.
If files are missing on Bitbucket it means they are also missing on your local branch. So first, fix the issue on your local master branch and then force push it again.
Running git log
will only show you the commits that are part of your branch. If are missing some files you may not see those commits.
Running git reflog
will give you full history, including switching between branches, merges and rebases.
Use the reflog to identify your 'last known good' commit. Once you find it - tag it (just so it will be easier to find) and start a branch it.
You can then compare this branch to master
branch and make sure it is the commit you are looking for.
Update
Once you have the commit hash reset your master branch to it and force push:
git reset --hard <hash>
git push --force origin master

- 13,146
- 5
- 30
- 48
-
I think I found one in the reflog, so how do I go back to it and restore the history of the files that were deleted? – northerner Aug 04 '20 at 07:56
-
-
-
Git is such a confusing tool. Let me as a simple question. Why is the hash value so much shorter when running `log` vs `reflog`? For example `commit 5e34ec6922d6021b5a4d9b02355086bd02434c18 (HEAD -> master, origin/master)` vs `5e23ec6 (HEAD -> master, origin/master) HEAD@{0}: rebase: updating HEAD` – northerner Aug 04 '20 at 20:46
-
See here: https://stackoverflow.com/questions/43665836/in-git-what-is-the-difference-between-long-and-short-hashes – Igal S. Aug 05 '20 at 06:56