如何强制“git pull”覆盖本地文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1125968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I force "git pull" to overwrite local files?
提问by Jakub Troszok
How do I force an overwrite of local files on a git pull
?
如何强制覆盖本地文件git pull
?
The scenario is the following:
场景如下:
- A team member is modifying the templates for a website we are working on
- They are adding some images to the images directory (but forgets to add them under source control)
- They are sending the images by mail, later, to me
- I'm adding the images under the source control and pushing them to GitHub together with other changes
- They cannot pull updates from GitHub because Git doesn't want to overwrite their files.
- 一名团队成员正在修改我们正在开发的网站的模板
- 他们正在向图像目录添加一些图像(但忘记在源代码控制下添加它们)
- 他们通过邮件将图像发送给我
- 我在源代码管理下添加图像并将它们连同其他更改一起推送到 GitHub
- 他们无法从 GitHub 拉取更新,因为 Git 不想覆盖他们的文件。
This is the error I'm getting:
这是我得到的错误:
error: Untracked working tree file 'public/images/icon.gif' would be overwritten by merge
错误:未跟踪的工作树文件“public/images/icon.gif”将被合并覆盖
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.
如何强制 Git 覆盖它们?此人是一名设计师 - 通常,我会手动解决所有冲突,因此服务器具有他们只需要在其计算机上更新的最新版本。
回答by RNA
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.[*]
重要提示:如果您有任何本地更改,它们将丢失。无论是否有--hard
选项,任何未推送的本地提交都将丢失。[*]
If you have any files that are nottracked by Git (e.g. uploaded user content), these files will not be affected.
如果您有任何Git未跟踪的文件(例如上传的用户内容),这些文件将不会受到影响。
I think this is the right way:
我认为这是正确的方法:
git fetch --all
Then, you have two options:
然后,您有两个选择:
git reset --hard origin/master
OR If you are on some other branch:
或者如果您在其他分支机构:
git reset --hard origin/<branch_name>
Explanation:
解释:
git fetch
downloads the latest from remote without trying to merge or rebase anything.
git fetch
从远程下载最新版本,而无需尝试合并或重新设置任何内容。
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
然后将git reset
主分支重置为您刚刚获取的内容。该--hard
选项更改工作树中的所有文件以匹配origin/master
Maintain current local commits
维护当前的本地提交
[*]: It's worth noting that it is possible to maintain current local commits by creating a branch from master
before resetting:
[*]:值得注意的是,可以通过master
在重置之前创建一个分支来维护当前的本地提交:
git checkout master
git branch new-branch-to-save-current-commits
git fetch --all
git reset --hard origin/master
After this, all of the old commits will be kept in new-branch-to-save-current-commits
.
在此之后,所有旧提交都将保存在new-branch-to-save-current-commits
.
Uncommitted changes
未提交的更改
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
回答by Travis Reeder
Try this:
尝试这个:
git reset --hard HEAD
git pull
It should do what you want.
它应该做你想做的。
回答by David Avsajanishvili
WARNING: git clean
deletes all your untracked files/directories and can't be undone.
警告:git clean
删除所有未跟踪的文件/目录并且无法撤消。
Sometimes just clean -f
does not help. In case you have untracked DIRECTORIES, -d option also needed:
有时只是clean -f
没有帮助。如果您有未跟踪的目录,还需要 -d 选项:
# WARNING: this can't be undone!
git reset --hard HEAD
git clean -f -d
git pull
WARNING: git clean
deletes all your untracked files/directories and can't be undone.
警告:git clean
删除所有未跟踪的文件/目录并且无法撤消。
Consider using -n
(--dry-run
) flag first. This will show you what will be deleted without actually deleting anything:
首先考虑使用-n
( --dry-run
) 标志。这将向您显示将删除的内容而不实际删除任何内容:
git clean -n -f -d
Example output:
示例输出:
Would remove untracked-file-1.txt
Would remove untracked-file-2.txt
Would remove untracked/folder
...
回答by Richard Kersey
Like Hedgehog I think the answers are terrible. But though Hedgehog's answer might be better, I don't think it is as elegant as it could be. The way I found to do this is by using "fetch" and "merge" with a defined strategy. Which should make it so that your local changes are preserved as long as they are not one of the files that you are trying to force an overwrite with.
像刺猬一样,我认为答案很糟糕。但是,尽管 Hedgehog 的回答可能更好,但我认为它并没有达到应有的优雅。我发现这样做的方法是使用具有定义策略的“获取”和“合并”。只要它们不是您试图强制覆盖的文件之一,就可以保留本地更改。
First do a commit of your changes
首先提交您的更改
git add *
git commit -a -m "local file server commit message"
Then fetch the changes and overwrite if there is a conflict
然后获取更改并在发生冲突时覆盖
git fetch origin master
git merge -s recursive -X theirs origin/master
"-X" is an option name, and "theirs" is the value for that option. You're choosing to use "their" changes, instead of "your" changes if there is a conflict.
“-X”是选项名称,而“theirs”是该选项的值。如果存在冲突,您选择使用“他们的”更改,而不是“您的”更改。
回答by Johanneke
Instead of doing:
而不是做:
git fetch --all
git reset --hard origin/master
I'd advise doing the following:
我建议做以下事情:
git fetch origin master
git reset --hard origin/master
No need to fetch all remotes and branches if you're going to reset to the origin/master branch right?
如果您要重置到 origin/master 分支,则无需获取所有遥控器和分支,对吗?
回答by Jakub Troszok
It looks like the best way is to first do:
看起来最好的方法是首先做:
git clean
To delete all untracked files and then continue with the usual git pull
...
要删除所有未跟踪的文件,然后继续通常的git pull
...
回答by Hedgehog
警告,如果您的 gitignore 文件中有任何 directory/* 条目,则这样做将永久删除您的文件。
Some answers seem to be terrible. Terrible in the sense of what happened to @Lauri by following David Avsajanishvili suggestion.
有些答案似乎很糟糕。遵循 David Avsajanishvili 的建议,就@Lauri 发生的事情而言,这是可怕的。
Rather (git > v1.7.6):
而是(git > v1.7.6):
git stash --include-untracked
git pull
Later you can clean the stash history.
稍后您可以清除存储历史记录。
Manually, one-by-one:
手动,一一:
$ git stash list
stash@{0}: WIP on <branch>: ...
stash@{1}: WIP on <branch>: ...
$ git stash drop stash@{0}
$ git stash drop stash@{1}
Brutally, all-at-once:
粗暴地,一次性:
$ git stash clear
Of course if you want to go back to what you stashed:
当然,如果你想回到你藏匿的东西:
$ git stash list
...
$ git stash apply stash@{5}
回答by Vishal
You might find this command helpful to throw away local changes:
您可能会发现此命令有助于丢弃本地更改:
git checkout <your-branch> -f
And then do a cleanup (removes untracked files from the working tree):
然后进行清理(从工作树中删除未跟踪的文件):
git clean -f
If you want to remove untracked directories in addition to untracked files:
如果除了未跟踪的文件之外还想删除未跟踪的目录:
git clean -fd
回答by Lloyd Moore
Instead of merging with git pull
, try this:
与其合并git pull
,不如试试这个:
git fetch --all
git fetch --all
followed by:
其次是:
git reset --hard origin/master
.
git reset --hard origin/master
.
回答by Chris BIllante
The only thing that worked for me was:
唯一对我有用的是:
git reset --hard HEAD~5
This will take you back five commits and then with
这将带你回五次提交,然后
git pull
I found that by looking up how to undo a Git merge.
我通过查找如何撤消 Git 合并发现了这一点。