git “将被合并覆盖”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35450049/
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
What does "would be overwritten by merge" mean?
提问by datps
When pulling from a team based Git remote repository, I get this message:
从基于团队的 Git 远程存储库中提取时,我收到以下消息:
"C:\Program Files (x86)\Git\bin\git.exe" pull --progress "origin" +refs/heads/master:refs/remotes/origin/master
Updating 71089d7..4c66e71
error: Your local changes to the following files would be overwritten by merge:
Source/Reporting/Common/Common.Dal.csproj
Please, commit your changes or stash them before you can merge.
Aborting
Done
What rule (or feature) in Git makes sure that the file I modified in my working directory does NOTget overwritten by the pull?
Git中哪些规则(或功能),确保让,在我的工作目录修改的文件我确实不是由拉会被覆盖?
In other words, in what circumstances it willbe overwritten by a pull? or... what do I need to do to force a pull to overwrite the file I just modified?
换句话说,在什么情况下它会被pull覆盖?或者...我需要做什么来强制拉动覆盖我刚刚修改的文件?
回答by slebetman
What rule would produce a "would be overwritten" warning?
什么规则会产生“将被覆盖”警告?
If you've modified a file that also have modifications in the remote repository but have not committed it.
如果您修改了一个在远程存储库中也有修改但尚未提交的文件。
What rule would avoid a "would be overwritten" warning?
什么规则可以避免“将被覆盖”警告?
If there are no uncommitted files that also have modifications in the remote repo.
如果远程仓库中没有未提交的文件也有修改。
What do I need to do...
我需要做什么...
It depends on what you actually want:
这取决于你真正想要什么:
You want to force a pull to overwrite the file
Obviously, if you really want this, you don't care about the changes you've just made and don't mind deleting them. If so you simply do:
git reset --hard git pull
You want both your changes and the changes from the pull
The easiest way to handle this in my opinion is to commit your changes then do a pull. Then if there is a merge conflict use the usual mechanisms to resolve the merge (hint: configure your difftool and mergetool so you can easily resolve conflicts using a GUI tool like meld or diffmerge etc.). Just do:
git add $the_affected_file git commit git pull
You want both changes but you're not ready to commit
Personally I don't think you should ever be not ready to commit. But it happens from time to time that you have partly broken code that you're debugging and you really don't want to commit. In this case you can stash you changes temporarily then unstash it after pulling:
git stash git pull git stash pop
If there are conflicts after popping the stash resolve them in the usual way. Note: you may want to do a
git stash apply
instead ofpop
if you're not ready to lose the stashed code due to conflicts.
您想强制拉动以覆盖文件
显然,如果你真的想要这个,你不关心你刚刚所做的更改,也不介意删除它们。如果是这样,您只需执行以下操作:
git reset --hard git pull
你想要你的改变和拉动的改变
在我看来,处理此问题的最简单方法是提交更改,然后执行拉取操作。然后,如果发生合并冲突,请使用通常的机制来解决合并问题(提示:配置 difftool 和 mergetool,以便您可以使用诸如 meld 或 diffmerge 等 GUI 工具轻松解决冲突)。做就是了:
git add $the_affected_file git commit git pull
你想要两个改变,但你还没有准备好提交
就我个人而言,我认为你不应该不准备承诺。但是有时会发生您正在调试的部分损坏的代码并且您真的不想提交。在这种情况下,您可以暂时隐藏更改,然后在拉动后取消隐藏:
git stash git pull git stash pop
如果弹出存储后出现冲突,请以通常的方式解决它们。注意:如果您还没有准备好因冲突而丢失隐藏的代码,您可能想要做一个
git stash apply
而不是pop
。
回答by Zbynek Vyskovsky - kvr000
The message means that you have local modifications to your file which are not committed. When running pull
, the files in your worktree are updated from remote repository. If git finds that the file is modified by both you and committed and in remote repository, it will simply try merging the changes and update both index and work tree. But for only locally modified files and not yet committed, it will stop.
该消息意味着您对未提交的文件进行了本地修改。运行时pull
,工作树中的文件将从远程存储库更新。如果 git 发现该文件被您和已提交并在远程存储库中修改,它只会尝试合并更改并更新索引和工作树。但仅对于本地修改的文件且尚未提交的文件,它会停止。
So you have to either commit
the changes first or stash
them as suggested in the message. There is no way how to avoid it as it would lead into inconsistent index and work tree.
因此,您必须先进行commit
更改,或者stash
按照消息中的建议进行更改。没有办法如何避免它,因为它会导致索引和工作树不一致。
Typical scenario to keep the local changes is (as given in git help stash
):
保持本地更改的典型场景是(如 中给出的git help stash
):
git pull # first attempt of pull
...
# Here you see the complains about locally modified files
git stash # will save your changes into stash
git pull # pull now succeeds as there are no locally modified files
git stash pop # pop the stash and apply the changes
回答by Tacocat
Just as the message indicates, you have made some changes to a file and you did not commit those changes to your repository. Someone else has made changes to that same file and when you try to pull the latest revision from your remote repository, you will lose whatever local changes you made to your file due to being overwritten by the other's changes.
正如消息所示,您对文件进行了一些更改,但您没有将这些更改提交到您的存储库。其他人对同一文件进行了更改,当您尝试从远程存储库中提取最新版本时,由于被其他人的更改覆盖,您将丢失对文件所做的任何本地更改。
If you want to keep the changes you have made, commit your changes to the repository before pulling and then try to merge: git merge origin/master
. If you don't care for those changes, you can discard them: git stash drop
.
如果要保留所做的更改,请在拉取之前将更改提交到存储库,然后尝试合并:git merge origin/master
。如果您不关心这些更改,则可以丢弃它们:git stash drop
.
UPDATE:
更新:
To answer your question on keeping your change locally without committing to the repository...
要回答有关在本地保留更改而不提交到存储库的问题...
Just stash everything that you've changed, pull all the new stuff, and then pop back out your stash, i.e.
把你改变的所有东西都藏起来,拉出所有的新东西,然后弹出你的藏品,即
git stash
git pull
git stash pop
Take note that on stash pop there will be conflicts in this case (the message had already made that very clear).
请注意,在 stash pop 上会在这种情况下发生冲突(消息已经很清楚了)。
Resolving the conflict is easy if you know that what you want is your what you put in the stash, i.e. your local changes. You just use
如果您知道您想要的是您放入存储中的内容,即您的本地更改,那么解决冲突就很容易了。你只要用
git checkout --theirs -- Common.Dal.csproj