Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

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

You must be logged in to vote

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 depend…

Replies: 8 comments · 9 replies

Comment options

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.

You must be logged in to vote
2 replies
@Subhamstar
Comment options

fatal: bad revision 'HEAD~1'
error: src refspec master does not match any
error: failed to push some refs to 'https://github.com/Subhamstar/JS.git'

@Subhamstar
Comment options

very helpful

Comment options

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 iOS bug report: strange backstack behavior when navigating to a file #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.

You must be logged in to vote
2 replies
@Subhamstar
Comment options

its work

@Daniel-Georgiev
Comment options

helped me alot when I ws in a very tight situation

Comment options

This worked! Thank you so much for you help!

You must be logged in to vote
1 reply
@Subhamstar
Comment options

its work thank you

Comment options

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

git revert
You must be logged in to vote
0 replies
Comment options

This is not effective at all for rebasing.

You must be logged in to vote
0 replies
Comment options

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.

You must be logged in to vote
0 replies
Comment options

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.

You must be logged in to vote
0 replies
Comment options

I've found the hashes of commits involved on https://github.com/[account]/[repository]/activity ... then I did

  • git fetch origin [hash]
  • git checkout [hash]
  • git branch -f master
  • git checkout master
  • git push origin -f
You must be logged in to vote
4 replies
@ngothanhthien
Comment options

thanks Hkmaly! It work for me

@deepakshroff
Comment options

git fetch origin [hash]
git checkout [hash]
git branch -f master
git checkout master
git push origin -f

@Subhamstar
Comment options

its work thankyou

@pillo79
Comment options

+1, should be the accepted answer, this works even when the commit was never in the local repo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Morty Proxy This is a proxified and sanitized view of the page, visit original site.