简体   繁体   English

为什么从特定的 GitHub 存储库合并到本地 Git 存储库会导致本地存储库中发生未提交的更改?

[英]Why does merging from a particular GitHub repository into a local Git repository result in uncommitted changes in the local repository?

I have 2 GitHub repos, let's call them GH1 and GH2 , and 2 corresponding local repos, let's call them LR1 and LR2 that have them as remotes.我有 2 个 GitHub 存储库,我们称它们为GH1GH2 ,以及 2 个相应的本地存储库,我们称它们为LR1LR2 ,它们将它们作为遥控器。 For clarity, here is the local repo to remote repo mapping:为清楚起见,这里是本地仓库到远程仓库的映射:
LR1 ---> GH1 LR1 ---> GH1
LR2 ---> GH2 LR2 ---> GH2

I manually upload (and commit) the same file, let's call it A.jpg , to both GitHub repos using the GitHub web UI.我使用 GitHub Web UI 手动上传(并提交)同一个文件,我们称之为A.jpg到两个 GitHub 存储库。 I get different results when I try to merge from the remotes back down to my local repos.当我尝试从遥控器合并回我的本地存储库时,我得到了不同的结果。

I use git on the command line for the purposes of this problem.我在命令行上使用 git 来解决这个问题。

One of local repos ( LR1 ) behaves as I would expect: git fetch gets the change from the remote but doesn't merge it.本地存储库之一( LR1 )的行为与我预期的一样: git fetch从远程获取更改但不合并它。 git merge merges A.jpg and fast-forwards the local repo. git merge合并A.jpg并快进本地存储库。 git status shows that there is "nothing to commit, working directory clean" git status显示“没有什么可提交,工作目录干净”

The other one ( LR2 ) behaves differently than I would expect: git fetch gets the change from the remote but doesn't merge it.另一个( LR2 )的行为与我预期的不同: git fetch从远程获取更改但不合并它。 git merge merges A.jpg and fast-forwards the local repo. git merge合并A.jpg并快进本地存储库。 git status shows that A.jpg is modified and is an uncommitted change. git status显示A.jpg被修改并且是未提交的更改。

git pull behaves the same way, with respect to the 2 local repos, but for the purposes of breaking this problem down, I thought I would explicitly call out the 2 commands ( git fetch and git merge ) that do the same thing as a pull.对于 2 个本地存储库, git pull行为方式相同,但为了解决这个问题,我想我会明确调用 2 个命令( git fetchgit merge ),它们与 pull 执行相同的操作.

I assume that I have the 2 local git repos configured differently and that is causing the issue.我假设我有 2 个本地 git repos 配置不同,这导致了问题。 What would cause this behaviour?什么会导致这种行为?

I'll share my understanding of the problem and how I resolved this, thanks to the helpful comments on this post.感谢这篇文章的有用评论,我将分享我对这个问题的理解以及我如何解决这个问题。

The problem has everything to do with the SMB mount, file permissions, and git configuration.问题与 SMB 挂载、文件权限和 git 配置有关。 The problem is that when I use git to sync from the remote to my local repo and a new file needs to be created by git in my local repo, git is creating a new file using its default setting for permissions (644) and the SMB mount, under which the local repo existed, has no setting configured for file mode so it was using its default (755).问题是,当我使用 git 从远程同步到我的本地 repo 并且需要通过 git 在我的本地 repo 中创建一个新文件时,git 正在使用其默认权限设置 (644) 和 SMB 创建一个新文件本地存储库所在的 mount 没有为文件模式配置设置,因此它使用其默认值 (755)。 The end result was that git thought it was creating a file with 644 permissions but as soon it got created it assumed 755 permissions because that's what the mount forces for new files...and so git thought that the file it just created had changed and deemed it as an uncommitted locally changed file.最终结果是 git 认为它正在创建一个具有 644 权限的文件,但是一旦创建它就假定了 755 权限,因为这是新文件的挂载力......所以 git 认为它刚刚创建的文件已经改变了,将其视为未提交的本地更改文件。 This was resolved by a few changes:这是通过一些更改解决的:

  • Changed the SMB mount in /etc/fstab to include the file mode ( file_mode=0644 ) that matches what git wants更改了 /etc/fstab 中的 SMB 挂载以包含与 git 想要的匹配的文件模式( file_mode=0644
  • Once I did this and unmounted/remounted it, all of the files in my local repo now show as uncommitted local changes because the permission (644) is different than what my local repo is aware of and my git settings are sensitive to that.一旦我这样做并卸载/重新安装它,我本地存储库中的所有文件现在都显示为未提交的本地更改,因为权限 (644) 与我的本地存储库所知道的不同,并且我的 git 设置对此很敏感。 I verified it by checking the git settings我通过检查 git 设置来验证它

git config --get core.fileMode

  • This was resolved by changing the git configuration for the repository.这是通过更改存储库的 git 配置解决的。 In the .git folder in the local repo root, I edited the config file so that filemode = false .在本地存储库根目录的 .git 文件夹中,我编辑了配置文件,使filemode = false
  • Once I did this, git status shows that there are no uncommitted local changes.一旦我这样做了, git status显示没有未提交的本地更改。

This git documentation explains well how this could happen, which is very close to what happened to me (ie cloned remote repo to local repo on local disk before it was on SMB mount and then eventually moved the local repo to an SMB mount)这个 git 文档很好地解释了这是如何发生的,这与我发生的情况非常接近(即在 SMB 挂载之前将远程仓库克隆到本地磁盘上的本地仓库,然后最终将本地仓库移动到 SMB 挂载)

Now, new files added to the GitHub repo and then pulled down to the local repo no longer show up as locally uncommited changes.现在,添加到 GitHub 存储库然后下拉到本地存储库的新文件不再显示为本地未提交的更改。 Rather, they are merged properly and transparently.相反,它们被正确且透明地合并。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM