0

I have 2 repositories, for frontend and backend. The remote backend repository was very different from the local and because i work alone, i decided to overwrite remote by deleting .git folder on my local repo, initiate new by git init and push all by:

git push --force dev master

but ironically I added the wrong branch by:

git remote add dev frontend-repo

and overwrite my frontend-repository which I worked last month.

I canceled the last commit by:

git revert {commit_id}

and now I have an empty frontend repository that I worked on for a month and untouched repository that i wanted to change. Localy i have all commit history and the repository itself, question - is there any way to restore this history or even completely undo recent changes?

mason
  • 31,774
  • 10
  • 77
  • 121
Mnemonic Pie
  • 29
  • 1
  • 5
  • In which repositories on which computers did you run these commands, respectively? – mkrieger1 Oct 12 '19 at 20:11
  • If this is not too long ago, the deleted commits are still present in your repository, but are unreachable, because they are not referenced anymore. You might therefore be able to find the deleted commits with `git fsck`. See [this question](https://stackoverflow.com/q/36621730/2311167) , – Adrian W Oct 12 '19 at 20:21
  • The phrase "commit history" is perhaps redundant: in Git, commits *are* history. Each commit has its own unique hash ID, and each commit refers to its immediate parent(s) by hash ID. You either have a commit or you don't. If you have the commit, the *commit* determines its parents, and therefore its history. – torek Oct 12 '19 at 20:23
  • Note that Git generally *finds* commits, however, by starting from a *branch name*. The branch name holds the hash ID of one (1) commit. That one commit lists its parent(s), which are its immediate history. Each parent commit lists *its* parent(s), and so on. So the history from branch name `master` is: whatever commit hash ID `master` holds. Put the right hash ID in the name `master` and you have whatever history you like. – torek Oct 12 '19 at 20:24
  • Each repository has *its own* branch names. `git push --force` just has one Git call up some other Git and say: *set your branch name to !* If you can log in on the machine that has the repository, you can use `git reflog`, `git fsck`, and others as in the answers below. If not, you can try to find the right hash IDs in whatever repository copies you have, and `git push --force` again. – torek Oct 12 '19 at 20:27
  • @torek thanks a lot for the explanation and advice. I would mark it with the answer, if I could – Mnemonic Pie Oct 12 '19 at 20:47

2 Answers2

2

Use your git reflog to restore your history and branches. Full explanation and samples codes can be found here:

How to move HEAD back to a previous location? (Detached head) & Undo commits


git reflog

You can always use the reflog as well.
git reflog will display any change which updated the HEAD and checking out the desired reflog entry will set the HEAD back to this commit.

Every time the HEAD is modified there will be a new entry in the reflog

git reflog
git checkout HEAD@{...}

This will get you back to your desired commit

enter image description here


CodeWizard
  • 128,036
  • 21
  • 144
  • 167
0

There is a git command called git reflog where you can see all your local changes and you can revert by the hashes

Doompickaxe
  • 194
  • 1
  • 9