Cas*_*bel 740
如果你的意思是你想让pull覆盖本地更改,那么就像工作树是干净的那样进行合并,那么,清理工作树:
git reset --hard
git pull
Run Code Online (Sandbox Code Playgroud)
如果有未跟踪的本地文件,您可以使用git clean
它们删除它们.使用git clean -f
删除未跟踪文件,-df
删除未跟踪的文件和目录,并-xdf
删除未跟踪或忽略的文件或目录.
另一方面,如果你想以某种方式保留局部修改,你可以使用藏匿在拉动之前隐藏它们,然后再重新应用它们:
git stash
git pull
git stash pop
Run Code Online (Sandbox Code Playgroud)
我认为忽略这些变化是没有任何意义的- 虽然拉动的一半是合并,它需要将提交的内容版本与它所获取的版本合并.
Art*_*yan 260
对我来说,以下工作:
(1)首先获取所有更改:
$ git fetch --all
Run Code Online (Sandbox Code Playgroud)
(2)然后重置主人:
$ git reset --hard origin/master
Run Code Online (Sandbox Code Playgroud)
(3)拉/更新:
$ git pull
Run Code Online (Sandbox Code Playgroud)
Dr *_*eco 23
下面的命令总是不起作用.如果您这样做:
$ 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.
Run Code Online (Sandbox Code Playgroud)
等等...
要真正重新开始,下载分支并覆盖所有本地更改,只需执行以下操作:
$ git checkout thebranch
$ git reset --hard origin/thebranch
Run Code Online (Sandbox Code Playgroud)
这将工作得很好.
$ 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'
Run Code Online (Sandbox Code Playgroud)
Vic*_*tor 22
你只需要一个能给出完全相同结果的命令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
Run Code Online (Sandbox Code Playgroud)
Pab*_*zos 10
这对我有用
git fetch --all
git reset --hard origin/master
git pull origin master
Run Code Online (Sandbox Code Playgroud)
使用接受的答案,我收到冲突错误
如果您位于分支上,并且想要放弃分支上的任何本地更改并拉取远程分支,但遇到
Your branch and 'origin/<branch_name>' have diverged
,则可以通过以下方式在停留在分支上时解决:
git fetch --all
git reset --hard origin/<branch_name>
Run Code Online (Sandbox Code Playgroud)
如果你在Linux上:
git fetch
for file in `git diff origin/master..HEAD --name-only`; do rm -f "$file"; done
git pull
Run Code Online (Sandbox Code Playgroud)
for循环将删除在本地仓库中更改的所有跟踪文件,因此git pull
可以正常工作.
最好的事情是,只有被跟踪的文件将被repo中的文件覆盖,所有其他文件将保持不变.
我通常这样做:
git checkout .
git pull
Run Code Online (Sandbox Code Playgroud)
在项目的根文件夹中。