我如何强制“git pull”覆盖本地文件? [英] How do I force "git pull" to overwrite local files?

查看:1525
本文介绍了我如何强制“git pull”覆盖本地文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 git pull 上强制覆盖本地文件?



这种情况如下:


  • 团队成员正在修改我们正在处理的网站的模板



  • 他们通过邮件将图像发送给我

  • 图像目录中的一些图像(但忘记将它们添加到源代码管理下)我在源代码控制下添加了图像,并将它们与其他更改一起推送到GitHub。
  • 由于Git不想覆盖它们的文件,它们无法从GitHub提取更新。
  • >


我得到的错误是:


错误:Untracked工作树文件'public / images / icon.gif'将被合并覆盖。


如何强制Git覆盖他们?这个人是一个设计师 - 通常我手工解决所有冲突,所以服务器有最新的版本,他们只需要在他们的计算机上更新。

解决方案

重要提示:如果您有任何本地更改,它们将会丢失。无论是否存在 - hard 选项,任何未被推送的本地提交都将丢失。 [*]



如果您有任何未被Git追踪的文件(例如上传的用户内容),这些文件将不会受到影响。






我认为这是正确的做法:

  git fetch --all 

然后,您有两个选择:

  git reset --hard origin / master 

或者如果你在其他分支上:

  git reset --hard origin /< branch_name> 



说明:



<$ c $



然后 git reset / code>将主分支重置为您刚才获取的分支。 - hard 选项会更改工作树中的所有文件,以匹配 origin / master




[*] :值得注意的是,可以通过创建分支来维护当前的本地提交从 master 重置之前:

  git checkout master 
git分支new-branch-to-save-current-commit
git fetch --all
git reset --hard origin / master

之后,所有旧的提交将保存在 new-branch-to-save-current-commits 中。然而,未提交的更改(甚至是分段)将会丢失。确保存储并提交任何您需要的内容。


How do I force an overwrite of local files on a git pull?

The scenario is 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.

The errors I'm getting are:

error: Untracked working tree file 'public/images/icon.gif' would be overwritten by merge.

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 needs to update on their computer.

解决方案

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.


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.

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


[*]: 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

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.

这篇关于我如何强制“git pull”覆盖本地文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆