Forcing a git pull

Git pull is designed to prevent you from overwriting local changes.  Sometimes, though, you want to do just that, but there’s no out-of-the-box support for it.  This script comes to the rescue:

    #/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

More discussion is here.

Leave a comment

Your email address will not be published. Required fields are marked *