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

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

    It seems like most answers here are focused on the master branch; however, there are times when I'm working on the same feature branch in two different places and I want a rebase in one to be reflected in the other without a lot of jumping through hoops.

    Based on a combination of RNA's answer and torek's answer to a similar question, I've come up with this which works splendidly:

    git fetch
    git reset --hard @{u}
    

    Run this from a branch and it'll only reset your local branch to the upstream version.

    This can be nicely put into a git alias (git forcepull) as well:

    git config alias.forcepull "!git fetch ; git reset --hard @{u}"

    Or, in your .gitconfig file:

    [alias]
      forcepull = "!git fetch ; git reset --hard @{u}"
    

    Enjoy!

提交回复
热议问题