Git 拉取而忽略本地更改?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4157189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git Pull While Ignoring Local Changes?
提问by markdorison
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
?
有没有一种方法可以git pull
忽略任何本地文件更改而不会破坏目录并且不必执行git clone
?
回答by Cascabel
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:
如果您的意思是希望 pull 覆盖本地更改,那么进行合并就好像工作树是干净的一样,那么,请清理工作树:
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.
如果有未跟踪的本地文件,您可以git clean
用来删除它们。使用git clean -f
删除未跟踪文件,-df
删除未跟踪的文件和目录,并-xdf
删除未跟踪或忽略的文件或目录。
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:
另一方面,如果您想以某种方式保留本地修改,则可以在拉动之前使用 stash 将它们隐藏起来,然后再重新应用它们:
git stash
git pull
git stash pop
I don't think it makes any sense to literally ignorethe changes, though - half of pull is merge, and it needs to merge the committed versions of content with the versions it fetched.
不过,我认为从字面上忽略更改没有任何意义- 拉取的一半是合并,它需要将已提交的内容版本与其获取的版本合并。
回答by Artur Barseghyan
For me the following worked:
对我来说,以下工作有效:
(1) First fetch all changes:
(1) 首先获取所有更改:
$ git fetch --all
(2) Then reset the master:
(2)然后重置master:
$ git reset --hard origin/master
(3) Pull/update:
(3) 拉取/更新:
$ git pull
回答by Victor
You just want a command which gives exactly the same result as rm -rf local_repo && git clone remote_url
, right? I also want this feature. I wonder why git does not provide such a command (such as git reclone
or git sync
), neither does svn provide such a command (such as svn recheckout
or svn sync
).
您只需要一个与 给出完全相同结果的命令rm -rf local_repo && git clone remote_url
,对吗?我也想要这个功能。我想知道为什么 git 没有提供这样的命令(例如git reclone
or git sync
),svn 也没有提供这样的命令(例如svn recheckout
or svn sync
)。
Try the following command:
尝试以下命令:
git reset --hard origin/master
git clean -fxd
git pull
回答by Dr Beco
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 reallystart 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'
回答by Luca C.
git fetch --all && git reset --hard origin/master
回答by Seth Johnson
回答by Strahinja Kustudic
If you are on Linux:
如果您使用的是 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.
for 循环将删除在本地存储库中更改的所有跟踪文件,因此git pull
可以正常工作。
最好的一点是,只有被跟踪的文件会被 repo 中的文件覆盖,所有其他文件将保持不变。
回答by gil
shortest way to do it is:
最短的方法是:
git pull --rebase --autostash
回答by Pablo Pazos
this worked for me
这对我有用
git fetch --all
git reset --hard origin/master
git pull origin master
with the accepted answer I get conflict errors
使用接受的答案,我收到冲突错误
回答by Cassiano Franco
I usually do:
我通常这样做:
git checkout .
git pull
In the project's root folder.
在项目的根文件夹中。