有没有一种方法可以git pull
忽略任何本地文件更改而无需将目录吹走并且必须执行git clone
?
14 回答
如果你的意思是你希望拉覆盖本地更改,就好像工作树是干净的一样进行合并,那么,清理工作树:
git reset --hard
git pull
如果有未跟踪的本地文件,您可以使用git clean
它们来删除它们。
git clean -f
删除未跟踪的文件-df
删除未跟踪的文件和目录-xdf
删除未跟踪或忽略的文件或目录
另一方面,如果您想以某种方式保留本地修改,则可以在拉取之前使用 stash 将它们隐藏起来,然后再重新应用它们:
git stash
git pull
git stash pop
不过,我认为从字面上忽略这些更改没有任何意义——拉的一半是合并,它需要将提交的内容版本与它获取的版本合并。
对我来说,以下工作:
(1) 首先获取所有更改:
$ git fetch --all
(2)然后复位master:
$ git reset --hard origin/master
(3) 拉取/更新:
$ git pull
您只需要一个给出与 完全相同结果的命令rm -rf local_repo && git clone remote_url
,对吧?我也想要这个功能。我想知道为什么 git 不提供这样的命令(例如git reclone
or git sync
),svn 也不提供这样的命令(例如svn recheckout
or 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 pull --rebase --autostash
git fetch --all && git reset --hard origin/master
这对我有用
git fetch --all
git reset --hard origin/master
git pull 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 循环将删除所有在本地 repo 中更改的跟踪文件,因此git pull
可以正常工作。
最好的一点是,只有跟踪的文件将被 repo 中的文件覆盖,所有其他文件将保持不变。
我通常这样做:
git checkout .
git pull
在项目的根文件夹中。
这将获取当前分支并尝试快进到 master:
git fetch && git merge --ff-only origin/master
很晚了,但有人会发现这很有用。(为我工作)
- git restore <文件名> 或 git restore 。
- git 拉
.gitignore
“只要您最初没有将不需要的文件提交到任何分支,就可以将不需要的文件添加到 .gitignore。”
你也可以运行:
git update-index --assume-unchanged filename
https://chamindac.blogspot.com/2017/07/ignoring-visual-studio-2017-created.html
此外,可以保留本地提交的更改并将它们作为新提交推送。当我的本地提交一团糟时,我会使用这些步骤。
- git reset --soft origin/main
- 混帐藏匿
- git pull --rebase
- git stash 弹出