有没有办法做一个忽略任何本地文件更改的git pull
而不会吹走目录并且必须执行git clone
?
如果你的意思是你想让pull覆盖本地更改,那么就像工作树是干净的那样进行合并,那么,清理工作树:
git reset --hard
git pull
如果有未跟踪的本地文件,您可以使用git clean
删除它们。使用git clean -f
删除未跟踪的文件,使用-df
删除未跟踪的文件和目录,使用-xdf
删除未跟踪或忽略的文件或目录。
另一方面,如果你想以某种方式保留局部修改,你可以使用藏匿在拉动之前隐藏它们,然后再重新应用它们:
git stash
git pull
git stash pop
我认为忽略这些变化是没有任何意义的 - 虽然拉动的一半是合并,它需要将提交的内容版本与它所获取的版本合并。
对我来说,以下工作:
(1)首先获取所有更改:
$ git fetch --all
(2)然后重置主人:
$ git reset --hard origin/master
(3)拉/更新:
$ 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'
你只需要一个与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 fetch --all && git reset --hard origin/master
查看git stash将所有本地更改放入“存储文件”并恢复到上次提交。此时,您可以应用隐藏的更改,或将其丢弃。
如果你在Linux上:
git fetch
for file in `git diff origin/master..HEAD --name-only`; do rm -f "$file"; done
git pull
for循环将删除在本地仓库中更改的所有跟踪文件,因此git pull
将正常工作。
最好的事情是,只有被跟踪的文件将被repo中的文件覆盖,所有其他文件将保持不变。
这对我有用
git fetch --all
git reset --hard origin/master
git pull origin master
接受的答案我得到了冲突错误
这将获取当前分支并尝试快进到master:
git fetch && git merge --ff-only origin/master