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

前端 未结 30 3523
失恋的感觉
失恋的感觉 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:15

    Despite the original question, the top answers can cause problems for people who have a similar problem, but don't want to lose their local files. For example, see Al-Punk and crizCraig's comments.

    The following version commits your local changes to a temporary branch (tmp), checks out the original branch (which I'm assuming is master) and merges the updates. You could do this with stash, but I've found it's usually easier to simply use the branch / merge approach.

    git checkout -b tmp
    git add *; git commit -am "my temporary files"
    git checkout master
    
    git fetch origin master
    git merge -s recursive -X theirs origin master
    

    where we assume the other repository is origin master.

提交回复
热议问题