I want to discard all changes done after commit <commit-hash> .
So I did:
git reset --hard <commit-hash>Now I want to do the same with my remote. How can I do this? I have done some commits (and pushes) after <commit-hash> and I just want to discard them all. Is just something went terribly wrong in the way and I don't want to make it worse than it is already. ;(
I basically want to rewind my origin/master to <commit-hash>
11 Answers
Assuming that your branch is called master both here and remotely, and that your remote is called origin you could do:
git reset --hard <commit-hash> git push -f origin masterHowever, you should avoid doing this if anyone else is working with your remote repository and has pulled your changes. In that case, it would be better to revert the commits that you don't want, then push as normal.
Update: you've explained below that other people have pulled the changes that you've pushed, so it's better to create a new commit that reverts all of those changes. There's a nice explanation of your options for doing this in this answer from Jakub Narębski. Which one is most convenient depends on how many commits you want to revert, and which method makes most sense to you.
Since from your question it's clear that you have already used git reset --hard to reset your master branch, you may need to start by using git reset --hard ORIG_HEAD to move your branch back to where it was before. (As always with git reset --hard, make sure that git status is clean, that you're on the right branch and that you're aware of git reflog as a tool to recover apparently lost commits.) You should also check that ORIG_HEAD points to the right commit, with git show ORIG_HEAD.
Troubleshooting:
If you get a message like "! [remote rejected] a60f7d85 -> master (pre-receive hook declined)"
then you have to allow branch history rewriting for the specific branch. In BitBucket for example it said "Rewriting branch history is not allowed". There is a checkbox named Allow rewriting branch history which you have to check.
Using some other answers can result in unnecessary loss of local state. Local changes are not inherently required to change a remote. This method can still wreck your remote if you choose the wrong commit to go back to, but even then you can usually find the right commit and try again.
You must have the desired commit somewhere in your local repo that you want the remote to match.
- Do not do any resetting.
- Use
git logto find the commit you want to the remote to be at. Usegit log -pto see changes, orgit log --graph --all --oneline --decorateto see a compact tree. - Copy the commit's hash, tag, or (if it's the tip) its branch name.
- Run a command like:
e.g.git push --force <remote> <commit-ish>:<the remote branch>
orgit push --force origin 606fdfaa33af1844c86f4267a136d4666e576cdc:mastergit push --force staging v2.4.0b2:releases
If the forced push fails, it's likely disabled by the remote. This may be worked around by temporarily changing one or both of receive.denyNonFastForwards and receive.denyDeletes. If your remote is hosted on a service without shell access, it probably has settings you can change to allow forced pushes.
I use a convenient alias (git go) for viewing history as in step 2, which can be added like so:
git config --global alias.go 'log --graph --all --decorate --oneline' 5 I solved problem like yours by this commands:
git reset --hard <commit-hash>
git push -f <remote> <local branch>:<remote branch> 0 On GitLab, you may have to set your branch to unprotected before doing this. You can do this in [repo] > Settings > Repository > Protected Branches. Then the method from Mark's answer works.
git reset --hard <commit-hash>
git push -f origin master 2 If you want a previous version of file, I would recommend using git checkout.
git checkout <commit-hash>Doing this will send you back in time, it does not affect the current state of your project, you can come to mainline git checkout mainline
but when you add a file in the argument, that file is brought back to you from a previous time to your current project time, i.e. your current project is changed and needs to be committed.
git checkout <commit-hash> -- file_name
git add .
git commit -m 'file brought from previous time'
git pushThe advantage of this is that it does not delete history, and neither does revert a particular code changes (git revert)
Check more here
3My two cents to the previous answers: if
git push --force <remote> <the-hash>:<the remote branch>still doesn't work, you might want to edit <your-remote-repo>.git/config file's receive section:
[receive] #denyNonFastforwards = true denyNonFastforwards = false 1 Let's assume that your branch is called master both locally and remotely, and that your remote is called origin you could do:
git reflog to get all the commit history, your commit hash has format like this: e34e1ff
git reset --hard <commit-hash>
git push -f origin master I faced the same problem recently and it got resolved with two steps:
- Reset it to local git with
git reset --hard HEAD~1here HEAD~1 is most recent commit. - Push that in to the desired branch forcefully
git push -f origin main.
That's it.
Sourcetree: resetting remote to a certain commit
- If you have pushed a bad commit to your remote (origin/feature/1337_MyAwesomeFeature) like below picture
- Go to Remotes > origin > feature > 1337_MyAwesomeFeature
- Right click and choose "Delete origin/feature/1337_MyAwesomeFeature" (Or change name of it if you want a backup and skip step 4.)
- Click "Force delete" and "OK".
- Select your older commit and choose "Reset current branch to this commit"
- Choose which mode you want to have (Hard if you don't want your last changes) and "OK".
Push this commit to a new origin/feature/1337_MyAwesomeFeature
Your local feature branch and your remote feature branch are now on the previous (of your choice) commit
If your branch is not development or production, the easiest way to achieve this is resetting to a certain commit locally and create a new branch from there. You can use:
git checkout 000000
(where 000000 is the commit id where you want to go) in your problematic branch and then simply create a new branch:
git remote add [name_of_your_remote]
Then you can create a new PR and all will work fine!
Do one thing, get the commit's SHA no. such as 87c9808 and then,
- move yourself ,that is your head to the specified commit (by doing git reset --hard 89cef43//mention your number here )
- Next do some changes in a random file , so that the git will ask you to commit that locally and then remotely Thus, what you need to do now is. after applying change git commit -a -m "trial commit"
- Now push the following commit (if this has been committed locally) by git push origin master
- Now what git will ask from you is that
error: failed to push some refs to '' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again.**
- Thus now what you can do is
git push --force origin master
- And thus, i hope it works :)