Git:我如何强制“git pull"?覆盖本地文件?
如何强制覆盖 git pull
上的本地文件?
How do I force an overwrite of local files on a git pull
?
场景如下:
- 一名团队成员正在修改我们正在开发的网站的模板
- 他们正在向图像目录添加一些图像(但忘记在源代码控制下添加它们)
- 他们稍后通过邮件将图像发送给我
- 我正在源代码管理下添加图像,并将它们连同其他更改一起推送到 GitHub
- 他们无法从 GitHub 提取更新,因为 Git 不想覆盖他们的文件.
这是我得到的错误:
错误:未跟踪的工作树文件public/images/icon.gif"将被合并覆盖
error: Untracked working tree file 'public/images/icon.gif' would be overwritten by merge
如何强制 Git 覆盖它们?此人是一名设计师 - 通常,我会手动解决所有冲突,因此服务器拥有他们只需要在计算机上更新的最新版本.
How do I force Git to overwrite them? The person is a designer - usually, I resolve all the conflicts by hand, so the server has the most recent version that they just need to update on their computer.
⚠ 重要提示:如果您有任何本地更改,它们将会丢失.有或没有 --hard
选项,任何未被推送的本地提交都将丢失.[*]
如果您有任何未被 Git 跟踪的文件(例如上传的用户内容),这些文件将不会受到影响.
⚠ Important: If you have any local changes, they will be lost. With or without --hard
option, any local commits that haven't been pushed will be lost.[*]
If you have any files that are not tracked by Git (e.g. uploaded user content), these files will not be affected.
首先,运行 fetch 将所有 origin/
引用更新为最新:
First, run a fetch to update all origin/<branch>
refs to latest:
git fetch --all
备份您当前的分支:
git branch backup-master
那么,你有两个选择:
git reset --hard origin/master
或者如果您在其他分支:
OR If you are on some other branch:
git reset --hard origin/<branch_name>
说明:
git fetch
从远程下载最新的,无需尝试合并或重新设置任何内容.
Explanation:
git fetch
downloads the latest from remote without trying to merge or rebase anything.
然后 git reset
将 master 分支重置为您刚刚获取的内容.--hard
选项更改工作树中的所有文件以匹配 origin/master
Then the git reset
resets the master branch to what you just fetched. The --hard
option changes all the files in your working tree to match the files in origin/master
[*]:值得注意的是,可以通过在重置之前从 master
创建一个分支来维护当前的本地提交:
[*]: It's worth noting that it is possible to maintain current local commits by creating a branch from master
before resetting:
git checkout master
git branch new-branch-to-save-current-commits
git fetch --all
git reset --hard origin/master
此后,所有旧提交将保存在 new-branch-to-save-current-commits
中.
After this, all of the old commits will be kept in new-branch-to-save-current-commits
.
但是,未提交的更改(即使是暂存的)也会丢失.确保存储并提交您需要的任何内容.为此,您可以运行以下命令:
Uncommitted changes, however (even staged), will be lost. Make sure to stash and commit anything you need. For that you can run the following:
git stash
然后重新应用这些未提交的更改:
And then to reapply these uncommitted changes:
git stash pop