如何使 git merge 处理对我的工作树的未提交更改?
我和一位同事目前都在主分支上工作。 我的工作树中有一些我不想提交的代码(调试语句等)。 现在,如果他对其中一些相同的文件提交更改,我无法合并它们:
$ git merge origin/master
Updating 1b8c5c6..eb44c23
error: Entry 'blah.java' not uptodate. Cannot merge.
来自颠覆背景,我习惯于当我从存储库中提取更改时自动合并我的工作树,如果存在冲突,我会自动合并工作树。手动解决它们。
我发现在 git 中执行此操作的最快方法是:
$ git stash
$ git merge origin/master
$ git stash pop
本质上,删除未提交的更改,进行合并,然后重新应用更改。 我如何告诉 merge 自动将我的工作树与我试图引入的更改合并?
A co-worker and I are both working on the master branch at the moment. I have some code in my working tree that I don't want to commit (debugging statements and the like). Now if he commits changes to some of those same files, I can't merge them:
$ git merge origin/master
Updating 1b8c5c6..eb44c23
error: Entry 'blah.java' not uptodate. Cannot merge.
Coming from a subversion background, I'm used to having my working tree automatically merged when I pull changes from the repository and if there are conflicts, I resolve them manually.
The quickest way I have found to do this in git is:
$ git stash
$ git merge origin/master
$ git stash pop
Essentially, removing my uncommitted changes, doing the merge and then re-applying the changes. How can I tell merge to automatically merge my working tree with the changes I'm trying to pull in?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
忘记你从颠覆中学到的一切。
始终在引入外部更改之前进行提交。
想象一下,您有一个大部分工作的树 - 也许并不完美,但您正在取得一些进展。 然后你去进行合并,你引入的代码造成了严重破坏(本身就有错误,有太多冲突需要处理,等等......)。 如果你能撤销它不是很好吗?
如果你承诺,你就可以。 如果你不这样做,你只会受苦。
请记住:您提交的内容不一定是您推动的内容,但您不提交的内容很容易就会丢失。
只要做安全、简单的事情,尽早承诺并经常承诺即可。
Forget everything you ever learned from subversion.
Always commit before introducing external changes.
Imagine you had a mostly-working tree -- maybe not perfect, but you're making some progress. Then you go to do a merge and the code you're bringing in just wreaked havoc (was buggy itself, too many conflicts to deal with, etc...). Wouldn't it be nice if you could just undo that?
If you commit, you can. If you don't, you're just going to suffer.
Remember: What you commit doesn't have to be what you push, but what you don't commit you can easily lose.
Just do the safe and easy thing and commit early and commit often.
据我所知,您能做的最好的事情就是使用
git stash
已有的功能。 我也觉得很奇怪,合并只想处理干净的树。As far as I can tell, the best you can do is what you already have with
git stash
. I too find it strange that merge wants to deal only with clean trees.git pull
将“正常工作”git stash 保存
git pull
git stash pop
git stash 保存
git pull
git stash pop
git 重置
git stash drop
git pull
将“正常工作”git pull --rebase
将“工作得更好”git pull
git add FILE
为每个冲突的文件git 提交
由于更清晰的历史记录,
git pull --rebase
仍然可以“工作得更好”有关详细说明,请参阅:https:// happygitwithr.com/pull-tricky.html
git pull
will "just work"git stash save
git pull
git stash pop
git stash save
git pull
git stash pop
git reset
git stash drop
git pull
will "just work"git pull --rebase
will "work even better" because of a cleaner historygit pull
git add FILE
for each conflicting FILEgit commit
git pull --rebase
could still "work even better" because of a cleaner historyFor a detailed explanation, please see: https://happygitwithr.com/pull-tricky.html
您无法告诉 git merge 合并对本地存储库进行了更改的文件的更改。 这可以防止您在合并失败时丢失更改。
使用 CVS 和 SVN 合并方法,如果您在更新之前没有手动复制文件并且在合并时打乱了文件,则必须手动重新编辑才能恢复到良好状态。
如果您在合并之前提交更改或隐藏它们,则一切都是可逆的。 如果合并进展不顺利,您可以尝试多种方法来解决问题,然后选择最有效的一种。
如果您确实提交了实验性或调试更改,则可以使用 git rebase 在通过 git merge 获得的提交之后移动它们,以便更轻松地删除它们或避免意外地将它们推送到存储库。
请注意,在已推送到共享存储库的分支上使用 git rebase 会给从该存储库拉取的每个人带来痛苦。
在这些情况下,我更喜欢使用 git stash,但我仅在合并更改我已编辑但未提交的文件时才使用它。
You cannot tell
git merge
to merge changes on files that have changes with respect to your local repository. This protects you from losing your changes on those times when a merge goes badly.With the CVS and SVN approach to merging, if you did not manually copy your files before the update and it scrambled them on merge, you have to manually re-edit to get back to a good state.
If you either commit your changes or stash them before doing a merge, everything is reversible. If the merge does not go well you can try several ways of making it work out and go with the one that works best.
If you do commit experimental or debug changes, you might use
git rebase
to move them after the commits you get viagit merge
to make it easier to get rid of them or to avoid pushing them to a repository accidentally.Note that using
git rebase
on a branch you have pushed to a shared repository will cause grief for everyone who is pulling from that repository.I prefer to use
git stash
in these cases, but I only use it if the merge changes files that I have edited and not committed.