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

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

    I summarized other answers. You can execute git pull without errors:

    git fetch --all
    git reset --hard origin/master
    git reset --hard HEAD
    git clean -f -d
    git pull
    

    Warning: This script is very powerful, so you could lose your changes.

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

    Based on my own similar experiences, the solution offered by Strahinja Kustudic above is by far the best. As others have pointed out, simply doing hard reset will remove all the untracked files which could include lots of things that you don't want removed, such as config files. What is safer, is to remove only the files that are about to be added, and for that matter, you'd likely also want to checkout any locally-modified files that are about to be updated.

    That in mind, I updated Kustudic's script to do just that. I also fixed a typo (a missing ' in the original).

    #/bin/sh
    
    # Fetch the newest code
    git fetch
    
    # Delete all files which are being added,
    # so there are no conflicts with untracked files
    for file in `git diff HEAD..origin/master --name-status | awk '/^A/ {print $2}'`
    do
        echo "Deleting untracked file $file..."
        rm -vf "$file"
    done
    
    # Checkout all files which have been locally modified
    for file in `git diff HEAD..origin/master --name-status | awk '/^M/ {print $2}'`
    do
        echo "Checking out modified file $file..."
        git checkout $file
    done
    
    # Finally merge all the changes (you could use merge here as well)
    git pull
    
    0 讨论(0)
  • 2020-11-21 12:00

    These four commands work for me.

    git reset --hard HEAD
    git checkout origin/master
    git branch -D master
    git checkout -b master
    

    To check/pull after executing these commands

    git pull origin master
    

    I tried a lot but finally got success with these commands.

    0 讨论(0)
  • 2020-11-21 12:04

    I just solved this myself by:

    git checkout -b tmp # "tmp" or pick a better name for your local changes branch
    git add -A
    git commit -m 'tmp'
    git pull
    git checkout master # Or whatever branch you were on originally
    git pull
    git diff tmp
    

    where the last command gives a list of what your local changes were. Keep modifying the "tmp" branch until it is acceptable and then merge back onto master with:

    git checkout master && git merge tmp
    

    For next time, you can probably handle this in a cleaner way by looking up "git stash branch" though stash is likely to cause you trouble on the first few tries, so do first experiment on a non-critical project...

    0 讨论(0)
  • 2020-11-21 12:05
    git fetch --all
    

    then if you are on the master branch

    git reset --hard origin/master
    

    else

    git reset --hard origin/master<branch_name>
    
    0 讨论(0)
  • 2020-11-21 12:07

    Try this:

    git reset --hard HEAD
    git pull
    

    It should do what you want.

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