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

前端 未结 30 3472
失恋的感觉
失恋的感觉 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 11:51

    It looks like the best way is to first do:

    git clean
    

    To delete all untracked files and then continue with the usual git pull...

    0 讨论(0)
  • 2020-11-21 11:53

    Reset the index and the head to origin/master, but do not reset the working tree:

    git reset origin/master
    
    0 讨论(0)
  • 2020-11-21 11:55

    git fetch --all && git reset --hard origin/master && git pull

    0 讨论(0)
  • 2020-11-21 11:56

    Bonus:

    In speaking of pull/fetch/merge in the previous answers, I would like to share an interesting and productive trick,

    git pull --rebase

    This above command is the most useful command in my Git life which saved a lot of time.

    Before pushing your newly commit to server, try this command and it will automatically synchronise the latest server changes (with a fetch + merge) and will place your commit at the top in the Git log. There isn't any need to worry about manual pull/merge.

    Find details in What does "git pull --rebase" do?.

    0 讨论(0)
  • 2020-11-21 11:58

    Just do

    git fetch origin branchname
    git checkout -f origin/branchname // This will overwrite ONLY new included files
    git checkout branchname
    git merge origin/branchname
    

    So you avoid all unwanted side effects, like deleting files or directories you wanted to keep, etc.

    0 讨论(0)
  • 2020-11-21 11:59

    I had the same problem. No one gave me this solution, but it worked for me.

    I solved it by:

    1. Delete all the files. Leave just the .git directory.
    2. git reset --hard HEAD
    3. git pull
    4. git push

    Now it works.

    0 讨论(0)
提交回复
热议问题