619

有没有一种方法可以git pull忽略任何本地文件更改而无需将目录吹走并且必须执行git clone?

4

14 回答 14

1013

如果你的意思是你希望拉覆盖本地更改,就好像工作树是干净的一样进行合并,那么,清理工作树:

git reset --hard
git pull

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

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

另一方面,如果您想以某种方式保留本地修改,则可以在拉取之前使用 stash 将它们隐藏起来,然后再重新应用它们:

git stash
git pull
git stash pop

不过,我认为从字面上忽略这些更改没有任何意义——拉的一半是合并,它需要将提交的内容版本与它获取的版本合并。

于 2010-11-11T17:25:24.863 回答
450

对我来说,以下工作:

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

$ git fetch --all

(2)然后复位master:

$ git reset --hard origin/master

(3) 拉取/更新:

$ git pull
于 2015-03-23T08:42:37.853 回答
45

您只需要一个给出与 完全相同结果的命令rm -rf local_repo && git clone remote_url,对吧?我也想要这个功能。我想知道为什么 git 不提供这样的命令(例如git recloneor git sync),svn 也不提供这样的命令(例如svn recheckoutor svn sync)。

尝试以下命令:

git reset --hard origin/master
git clean -fxd
git pull
于 2018-03-12T08:18:25.817 回答
30

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

$ 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'
于 2014-11-24T00:10:45.233 回答
15

最短的方法是:

git pull --rebase --autostash
于 2019-12-19T12:40:13.590 回答
11
git fetch --all && git reset --hard origin/master
于 2017-09-12T15:26:54.530 回答
11

这对我有用

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

接受的答案我得到冲突错误

于 2019-04-19T02:39:41.177 回答
8

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

于 2010-11-11T17:23:58.190 回答
8

如果您在 Linux 上:

git fetch
for file in `git diff origin/master..HEAD --name-only`; do rm -f "$file"; done
git pull

for 循环将删除所有在本地 repo 中更改的跟踪文件,因此git pull可以正常工作。
最好的一点是,只有跟踪的文件将被 repo 中的文件覆盖,所有其他文件将保持不变。

于 2012-11-05T23:40:17.803 回答
8

我通常这样做:

git checkout .
git pull

在项目的根文件夹中。

于 2019-08-25T00:36:49.117 回答
2

这将获取当前分支并尝试快进到 master:

git fetch && git merge --ff-only origin/master
于 2016-05-05T21:04:33.037 回答
2

很晚了,但有人会发现这很有用。(为我工作)

  1. git restore <文件名> 或 git restore 。
  2. git 拉
于 2021-12-24T05:14:12.120 回答
0

.gitignore

“只要您最初没有将不需要的文件提交到任何分支,就可以将不需要的文件添加到 .gitignore。”

你也可以运行:

git update-index --assume-unchanged filename

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

于 2019-08-13T01:46:22.100 回答
0

此外,可以保留本地提交的更改并将它们作为新提交推送。当我的本地提交一团糟时,我会使用这些步骤。

  1. git reset --soft origin/main
  2. 混帐藏匿
  3. git pull --rebase
  4. git stash 弹出
于 2021-10-29T11:17:48.533 回答