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>
This question is related to
git
Use the other answers if you don't mind losing local changes. This method can still wreck your remote if you choose the wrong commit hash to go back to.
If you just want to make the remote match a commit that's anywhere in your local repo:
git log
to find the commit you want to the remote to be at. git log -p
to see changes, or git log --graph --all --oneline --decorate
to see a compact tree.Run a command like:
git push --force <remote> <commit-ish>:<the remote branch>
e.g.
git push --force origin 606fdfaa33af1844c86f4267a136d4666e576cdc:master
or
git push --force staging v2.4.0b2:releases
I use 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'`
Do one thing, get the commit's SHA no. such as 87c9808 and then,
error: failed to push some refs to 'https://github.com/YOURREPOSITORY/AndroidExperiments.git' 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.**
git push --force origin master
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
My 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
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!
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 solved problem like yours by this commands:
git reset --hard <commit-hash>
git push -f <remote> <local branch>:<remote branch>
Sourcetree: resetting remote to a certain commit
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 push
The advantage of this is that it does not delete history, and neither does revert a particular code changes (git revert)
Check more here https://www.atlassian.com/git/tutorials/undoing-changes#git-checkout
Source: Stackoverflow.com