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


查看git stash,将所有本地更改放入“stash文件”,并恢复到上次提交。此时,您可以应用您所存储的更改,或者丢弃它们。


如果你的意思是你想要pull覆盖局部更改,就像工作树是干净的一样进行合并,好吧,清洁工作树:

git reset --hard
git pull

如果有未跟踪的本地文件,可以使用git clean删除它们。

Git clean -f删除未跟踪的文件 -df删除未跟踪的文件和目录 -xdf删除未跟踪或忽略的文件或目录

另一方面,如果你想以某种方式保留局部修改,你可以在拖动之前使用stash将它们隐藏起来,然后在之后重新应用它们:

git stash
git pull
git stash pop

但是,我认为忽略这些更改没有任何意义——pull的一半是merge,它需要将提交的内容版本与它获取的版本合并。


如果你使用的是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 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'

对我来说,以下方法是有效的:

(1)首先获取所有更改:

$ git fetch --all

(2)然后重置主机:

$ git reset --hard origin/master

注:对于github的用户,“master”在2020年10月被“main”取代。对于之后创建的项目,你可能需要使用“main”来代替,比如:

$ git reset --hard origin/main

(3)拉/更新:

$ git pull

这将获取当前分支并尝试快速跳转到master:

git fetch && git merge --ff-only origin/master

git fetch --all && git reset --hard 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 fetch --all
git reset --hard origin/master
git pull origin master

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


. gitignore

“向.gitignore添加不需要的文件,只要你最初没有将它们提交给任何分支。”

你也可以运行:

git update-index --assume-unchanged filename

https://chamindac.blogspot.com/2017/07/ignoring-visual-studio-2017-created.html


我通常这样做:

git checkout .
git pull

在项目的根文件夹中。


最简单的方法是:

git pull --rebase --autostash

此外,还可以保留来自本地提交的更改,并将其作为新的提交推送。当我在本地提交中出现混乱时,我使用这些步骤。

git reset --soft origin/main git 存储 git pull --rebase 去藏匿流行音乐


虽然很晚了,但有人会觉得这很有用。(为我工作过)

git restore < fileName>或git restore。 git拉


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

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