Git undo changes by merge
I rolled my local repo back to a commit and then made some local changes. Now I wanted to update (simply overwrite) the remote with my local repo. But when I did a git pull, the terminal showed bunch of conflicts. I don’t want the remote changes, I just want my local files to be committed.
I read somewhere to do
git checkout --ours
I ran it like
git checkout --ours .
But after running the command when I did
git status It showed the previous state.
4 Solutions collect form web for “Git undo changes by merge”
As I understand you want to overwrite your remote branch with whatever is there in the local branch.
This can be achieved with using the
-f force option, or with
--force-with-lease option in the git push command.
Get your local branch in the state you want and do:
git push -f origin <remoteBranch>
Be careful though
, this action has consequences, I highly recommend reading this answer: https://stackoverflow.com/a/10510482/1695393
Also, when you perform
git pull, git would try to merge remote and local branch, which would undo your work of preparing the local branch. Since you want to “overwrite” remote, I would advise to skip pulling.
if you’re still in this unclean state right now, you might want to do a
git reset --hard to reset your local repository to the state it was in before your last pull that caused this.
git checkout --ours just supports to checkout one file. So you type
git checkout --ours . it will not work as you expect.
So in this case, you have to run
git checkout --ours [path_to_your_conflict_file]
If you have too many conflicted files, you can run the command below:
for f in `git ls-files -m`;do git checkout --ours $f; done;
Do you want to keep the history in the remote repo? Has anyone else pulled from it? If the answer to both these questions is “no”, then you probably just want to get rid of the conflicts and force push.
git reset --hard <commit to keep> git push --force origin <remote branch>
If you do want to keep the history in the remote repo and/or somebody has pulled, you can do this:
git reset --hard origin/<remote branch> # Erase changes (conflicts) in your working copy git reset --soft <commit to keep> git commit --all --message "Commit message with reason for restoring" git push
As @dubes writes, beware of the possible negative consequences of force pushing.