有没有一种方法来做一个git拉,忽略任何本地文件的变化,而不吹走目录,并必须执行一个git克隆?


当前回答

这对我很有效

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

对于接受的答案,我会得到冲突错误

其他回答

你只是想要一个与rm -rf local_repo && git clone remote_url完全相同的命令,对吗?我也想要这个功能。我想知道为什么git没有提供这样的命令(比如git reclone或者git sync), svn也没有这样的命令(比如svn recheckout或者svn sync)。

试试下面的命令:

git reset --hard origin/master
git clean -fxd
git pull

下面的命令不会总是工作。如果你这样做了:

$ 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.

等等……

要真正从头开始,下载分支并覆盖所有本地更改,只需执行:


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

这就可以了。

$ 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'

我通常这样做:

git checkout .
git pull

在项目的根文件夹中。

如果您在一个分支上,并且希望丢弃分支上的任何本地更改并拉取远程分支,但遇到 你的分支和'origin/<branch_name>'已经分离, 它可以在停留在分支上时通过以下方法解决:

git fetch --all
git reset --hard origin/<branch_name>
git fetch --all && git reset --hard origin/master