3

I made changes in my working directory and did a 'git commit'

And then I do a 'git pull'. Git tries to pull changes in the remote tracking branch and automatically merge it for me. But there are some files with conflict.

So How can I just take the version in the remote branch and forget the change I made in a particular file?

I try to do 'git checkout -- chrome/browser/browser.cc', but it said I am in the middle of a merge and it won't let me do it.

# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       unmerged:   chrome/browser/browser.cc
#       unmerged:   chrome/renderer/render_view.cc
#       unmerged:   chrome/test/automation/automation_proxy_uitest.cc

Thank you.

3 Answers 3

5

It seems that git reset --hard <remote_commit> would achieve your purpose. This would throw away your local commits and point the HEAD of the current branch at the remote commit.

Also, have you tried git checkout -f? That might convince it to do what you asked.

1

If you wish to completely forget the last commit you made, do a git reset --hard HEAD^, which will return your master branch to its previous state - you can also do a git log, pick the commit you want to return to, and do git reset --hard <sha1>. If, however, other people have pulled the commits you now wish to ignore, it is safer to do a merge with the ours strategy, e.g. git checkout -b temp --track origin/master, git merge --strategy=ours master and git push. This will keep the history of the changes you have made, but will remove them from the resulting tree.

If it's just a few files you can of course just choose the fix the conflict by hand, choosing the versions from origin/master. Any decent merge tool should allow for that.

0

You can

git reset --hard HEAD

to revert your local changes, then just git pull your remote changes into the (now) clean local repository.

1
  • That will only revert local, uncommitted changes. It won't work in this case because the local branch has had commits to it. Novelocrat's solution (adding a commit hash instead of HEAD) will solve the problem.
    – MichaelM
    Oct 1, 2009 at 3:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.