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

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

    WARNING: git clean deletes all your untracked files/directories and can't be undone.


    Sometimes just clean -f does not help. In case you have untracked DIRECTORIES, -d option also needed:

    # WARNING: this can't be undone!
    
    git reset --hard HEAD
    git clean -f -d
    git pull
    

    WARNING: git clean deletes all your untracked files/directories and can't be undone.

    Consider using -n (--dry-run) flag first. This will show you what will be deleted without actually deleting anything:

    git clean -n -f -d
    

    Example output:

    Would remove untracked-file-1.txt
    Would remove untracked-file-2.txt
    Would remove untracked/folder
    ...
    
    0 讨论(0)
  • 2020-11-21 12:15

    The only thing that worked for me was:

    git reset --hard HEAD~5
    

    This will take you back five commits and then with

    git pull
    

    I found that by looking up how to undo a Git merge.

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

    I had a similar problem. I had to do this:

    git reset --hard HEAD
    git clean -f
    git pull
    
    0 讨论(0)
  • 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.

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

    First of all, try the standard way:

    git reset HEAD --hard # To remove all not committed changes!
    git clean -fd         # To remove all untracked (non-git) files and folders!
    

    Warning: Above commands can results in data/files loss only if you don't have them committed! If you're not sure, make the backup first of your whole repository folder.

    Then pull it again.

    If above won't help and you don't care about your untracked files/directories (make the backup first just in case), try the following simple steps:

    cd your_git_repo  # where 'your_git_repo' is your git repository folder
    rm -rfv *         # WARNING: only run inside your git repository!
    git pull          # pull the sources again
    

    This will REMOVE all git files (excempt .git/ dir, where you have all commits) and pull it again.


    Why git reset HEAD --hard could fail in some cases?

    1. Custom rules in .gitattributes file

      Having eol=lf rule in .gitattributes could cause git to modify some file changes by converting CRLF line-endings into LF in some text files.

      If that's the case, you've to commit these CRLF/LF changes (by reviewing them in git status), or try: git config core.autcrlf false to temporary ignore them.

    2. File system incompability

      When you're using file-system which doesn't support permission attributes. In example you have two repositories, one on Linux/Mac (ext3/hfs+) and another one on FAT32/NTFS based file-system.

      As you notice, there are two different kind of file systems, so the one which doesn't support Unix permissions basically can't reset file permissions on system which doesn't support that kind of permissions, so no matter how --hard you try, git always detect some "changes".

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

    You might find this command helpful to throw away local changes:

    git checkout <your-branch> -f
    

    And then do a cleanup (removes untracked files from the working tree):

    git clean -f
    

    If you want to remove untracked directories in addition to untracked files:

    git clean -fd
    
    0 讨论(0)
提交回复
热议问题