Git Pull While Ignoring Local Changes?

前端 未结 12 1701
醉话见心
醉话见心 2020-12-04 04:29

Is there a way to do a git pull that ignores any local file changes without blowing the directory away and having to perform a git clone?

相关标签:
12条回答
  • 2020-12-04 05:00

    For me the following worked:

    (1) First fetch all changes:

    $ git fetch --all
    

    (2) Then reset the master:

    $ git reset --hard origin/master
    

    (3) Pull/update:

    $ git pull
    
    0 讨论(0)
  • 2020-12-04 05:02

    If you are on Linux:

    git fetch
    for file in `git diff origin/master..HEAD --name-only`; do rm -f "$file"; done
    git pull
    

    The for loop will delete all tracked files which are changed in the local repo, so git pull will work without any problems.
    The nicest thing about this is that only the tracked files will be overwritten by the files in the repo, all other files will be left untouched.

    0 讨论(0)
  • 2020-12-04 05:02

    This will fetch the current branch and attempt to do a fast forward to master:

    git fetch && git merge --ff-only origin/master
    
    0 讨论(0)
  • 2020-12-04 05:05

    The command bellow wont work always. If you do just:

    $ git checkout thebranch
    Already on 'thebranch'
    Your branch and 'origin/thebranch' have diverged,
    and have 23 and 7 different commits each, respectively.
    
    $ git reset --hard
    HEAD is now at b05f611 Here the commit message bla, bla
    
    $ git pull
    Auto-merging thefile1.c
    CONFLICT (content): Merge conflict in thefile1.c
    Auto-merging README.md
    CONFLICT (content): Merge conflict in README.md
    Automatic merge failed; fix conflicts and then commit the result.
    

    and so on...

    To really start over, downloading thebranch and overwriting all your local changes, just do:


    $ git checkout thebranch
    $ git reset --hard origin/thebranch
    

    This will work just fine.

    $ git checkout thebranch
    Already on 'thebranch'
    Your branch and 'origin/thebranch' have diverged,
    and have 23 and 7 different commits each, respectively.
    
    $ git reset --hard origin/thebranch
    HEAD is now at 7639058 Here commit message again...
    
    $ git status
    # On branch thebranch
    nothing to commit (working directory clean)
    
    $ git checkout thebranch
    Already on 'thebranch'
    
    0 讨论(0)
  • 2020-12-04 05:09

    If you mean you want the pull to overwrite local changes, doing the merge as if the working tree were clean, well, clean the working tree:

    git reset --hard
    git pull
    

    If there are untracked local files you could use git clean to remove them. Use git clean -f to remove untracked files, -df to remove untracked files and directories, and -xdf to remove untracked or ignored files or directories.

    If on the other hand you want to keep the local modifications somehow, you'd use stash to hide them away before pulling, then reapply them afterwards:

    git stash
    git pull
    git stash pop
    

    I don't think it makes any sense to literally ignore the changes, though - half of pull is merge, and it needs to merge the committed versions of content with the versions it fetched.

    0 讨论(0)
  • 2020-12-04 05:09

    this worked for me

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

    with the accepted answer I get conflict errors

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