I accidentally force pushed to my repo

I accidentally force pushed to my repo, and I want to go back to the previous version. What do I do?

3 Likes

The ‘git revert’ option undoes a commit by creating another commit. This is important if your repo is being worked on with other collaboraters becasue it doesn’t alter the commit history by re-writing the commit you want to remove.

If you just pushed your changes and you want to go back to the previous version, the following revert command will make that change for you.

git revert HEAD~1
git push origin master

The above is saying that you want to revert the changes to HEAD by 1, meaning the last commit, make a new commit that undoes those changes, and then push this new commit to the origin branch, in this case the master branch.

4 Likes

If I’m understanding correctly, you’re talking about a scenario like the following:

  1. You have a repo named ben3eee/some-repo
  2. You create a few commits on branch some-branch
  3. You push some-branch to ben3eee/some-repo on GitHub using git push
  4. You squash the commits into one using git rebase -i
  5. You  force push some-branch to ben3eee/some-repo on GitHub using git push -f
  6. You now want to restore some-branch to the way it was before step #4

The great thing about Git though is that it does it’s very best to never lose data, so the version of the repository before step #4 is still available as long as too much time hasn’t passed. The exact definition of “too much time” is kind of fuzzy and depends on how many other changes you’ve made between step #4 and step #6. For this example, I’m going to assume that you realize your mistake right away and no other actions were taken other than the ones in the list above.

The command that can help is called git reflog. You can enter git reflog and see all of the actions that have changed your local repository, including switching branches and rebases, going back quite a ways. I’ve created a simple reflog that shows the scenario I described above:

b46bfc65e (HEAD -> test-branch) HEAD@{0}: rebase -i (finish): returning to refs/heads/test-branch
b46bfc65e (HEAD -> test-branch) HEAD@{1}: rebase -i (squash): a
dd7906a87 HEAD@{2}: rebase -i (squash): # This is a combination of 2 commits.
a3030290a HEAD@{3}: rebase -i (start): checkout refs/heads/master
0c2d866ab HEAD@{4}: commit: c
6cab968c7 HEAD@{5}: commit: b
a3030290a HEAD@{6}: commit: a
c9c495792 (origin/master, origin/HEAD, master) HEAD@{7}: checkout: moving from master to test-branch
c9c495792 (origin/master, origin/HEAD, master) HEAD@{8}: pull: Fast-forward

You can see at HEAD@{7} I performed a checkout moving from master to test-branch. I then created three commits, “a”, “b” and “c”. Then I rebased them, arriving at HEAD@{0}. The notation HEAD@{number} is the position of HEAD at “number” changes ago. So HEAD@{0} is HEAD where HEAD is now and HEAD@{4} is HEAD four steps ago. We can see from the reflog above that HEAD@{4} is where we need to go in order to restore the branch to where it was before the rebase and 0c2d866ab is the commit ID (also sometimes called “SHA”) for that commit.  So in order to restore test-branch to the state we want, we can issue the command:

git reset --hard HEAD@{4}

Then we can force push again to restore the repository on GitHub to where it was before.

39 Likes

This worked! Thank you so much for you help!

2 Likes

Wouldn’t “git revert” work better in this case if it’s only him altering his repo and not multiple people?

git revert

This is not effective at all for rebasing.

2 Likes

Would that work if there was a rewriting of history involved? revert can work for the simple case, but force commits usually rewrite history and do much more complicated changes.

I accidently pushed a brach in another owner repo. Now i am getting the same message. Question is whether whole team will be notified by my changes or not. Or this message is displayed only my username.