How do I force “git pull” to overwrite local files?

前端 未结 30 3849
失恋的感觉
失恋的感觉 2020-11-21 11:35

How do I force an overwrite of local files on a git pull?

The scenario is the following:

  • A team member is modifying the t
30条回答
  •  抹茶落季
    2020-11-21 12:08

    Like Hedgehog I think the answers are terrible. But though Hedgehog's answer might be better, I don't think it is as elegant as it could be. The way I found to do this is by using "fetch" and "merge" with a defined strategy. Which should make it so that your local changes are preserved as long as they are not one of the files that you are trying to force an overwrite with.

    First do a commit of your changes

     git add *
     git commit -a -m "local file server commit message"
    

    Then fetch the changes and overwrite if there is a conflict

     git fetch origin master
     git merge -s recursive -X theirs origin/master
    

    "-X" is an option name, and "theirs" is the value for that option. You're choosing to use "their" changes, instead of "your" changes if there is a conflict.

提交回复
热议问题