有没有一种方法来做一个git拉,忽略任何本地文件的变化,而不吹走目录,并必须执行一个git克隆?
当前回答
如果你使用的是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 .
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'
这将获取当前分支并尝试快速跳转到master:
git fetch && git merge --ff-only origin/master
如果你使用的是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
对于接受的答案,我会得到冲突错误
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别