Git Pull While Ignoring Local Changes?

前端 未结 12 1768
醉话见心
醉话见心 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: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.

提交回复
热议问题