I accidentally force pushed to my repo #23242
-
I accidentally force pushed to my repo, and I want to go back to the previous version. What do I do? |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments
-
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.
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. |
Beta Was this translation helpful? Give feedback.
-
If I’m understanding correctly, you’re talking about a scenario like the following:
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:
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:
Then we can force push again to restore the repository on GitHub to where it was before. |
Beta Was this translation helpful? Give feedback.
-
This worked! Thank you so much for you help! |
Beta Was this translation helpful? Give feedback.
-
Wouldn’t “git revert” work better in this case if it’s only him altering his repo and not multiple people?
|
Beta Was this translation helpful? Give feedback.
-
This is not effective at all for rebasing. |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
I've found the hashes of commits involved on https://github.com/[account]/[repository]/activity ... then I did
|
Beta Was this translation helpful? Give feedback.
If I’m understanding correctly, you’re talking about a scenario like the following:
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…