简体   繁体   English

“git pull --rebase --autostash”不应该总是自动弹出存储吗? 即使发生冲突?

[英]Shouldn't "git pull --rebase --autostash" always pop the stash automatically? Even when there are conflicts?

In a previous job, a couple of years ago, we used git with an (old) plugin, called " git up ".几年前,在之前的一份工作中,我们将 git 与一个(旧)插件一起使用,称为“ git up ”。
https://github.com/aanand/git-up https://github.com/aanand/git-up
"This plugin is no longer maintained or supported." “不再维护或支持此插件。”
Updating your workspace used to be very easy:过去更新您的工作区非常容易:
just type "git up", see what files have conflicts, edit those to resolve the conflicts, do a "git add", and you are done.只需键入“git up”,查看哪些文件有冲突,编辑这些文件以解决冲突,执行“git add”,就完成了。

I am now working for another company.我现在在另一家公司工作。 They started to use git earlier this year.他们今年早些时候开始使用 git。 We don't have a clear and simple way for everyone to update our workspace.我们没有一个清晰简单的方法让每个人都更新我们的工作区。 I would like to introduce a very simple way to update your workspace.我想介绍一种非常简单的方法来更新您的工作区。 As simple as possible .尽可能简单 No commits, no merges.没有提交,没有合并。

It seems the simplest way to do this is by using "git pull" with the --rebase and --autostash flags.似乎最简单的方法是使用带有 --rebase 和 --autostash 标志的“git pull”。 You can do the same by setting these values in your .gitconfig file.您可以通过在 .gitconfig 文件中设置这些值来执行相同的操作。 Or with an alias:或者使用别名:

git config --global alias.up '!git fetch && git rebase --autostash origin'

This kinda works.这有点工作。 When there are no conflicts, the stash pops automatically.当没有冲突时,stash 会自动弹出。 Besides typing "git up" or "git pull --rebase --autostash", there is nothing I need to do.除了键入“git up”或“git pull --rebase --autostash”之外,我不需要做任何事情。

There is one issue.有一个问题。 When there are conflicts, git refuses to apply the stash back into my workspace.当出现冲突时,git 拒绝将 stash 应用回我的工作区。 It says:它说:

Applying autostash resulted in conflicts.
Your changes are safe in the stash.
You can run "git stash pop" or "git stash drop" at any time.
Successfully rebased and updated refs/heads/main.
$ git stash list
stash@{0}: autostash
$ 

When I then type "git stash pop" the stash is popped, my local changes are automatically edited into my workspace again (with the usual >>>> and <<<< markers).当我输入“git stash pop”时,stash 被弹出,我的本地更改会再次自动编辑到我的工作区中(使用通常的 >>>> 和 <<<< 标记)。 This always succeeds.这总是成功的。

Note, git always refuses to automatically pop the stash when I have changed a file that is also changed on the remote repository.请注意,当我更改了一个也在远程存储库上更改的文件时,git 总是拒绝自动弹出存储。 Not only when there is a real conflict (changes on the same lines).不仅是在发生真正的冲突时(同一行上的更改)。 But also when my change is nowhere near the change from the remote repository.而且当我的更改与远程存储库的更改相去甚远时。 This makes me suspect that the behaviour I see is really not intended.这让我怀疑我看到的行为真的不是故意的。

But why does --autostash not pop the stash automatically?但是为什么 --autostash 不会自动弹出存储? I spoke with my company's git support persons.我与我公司的 git 支持人员进行了交谈。 They also expect the stash to pop automatically.他们还希望藏匿处自动弹出。 Even when there are conflicts.即使有冲突。 I did a bit of googling.我做了一些谷歌搜索。 Nothing I found suggests that the stash should not pop automatically.我发现没有任何迹象表明藏匿处不应自动弹出。

So my question: What can I expect?所以我的问题是:我能期待什么? Should the stash always pop automatically when I use --autostash?当我使用 --autostash 时,stash 是否应该总是自动弹出 Or is the behaviour I see normal?还是我看到的行为正常? Is this a bug?这是一个错误吗? Did our local git-team change something in the executables or global config?我们本地的 git-team 是否更改了可执行文件或全局配置中的某些内容? (They say they didn't). (他们说他们没有)。

Behaviour is the same with 2.26.2 and 2.33.行为与 2.26.2 和 2.33 相同。 Thanks.谢谢。

The behavior (er, behaviour) you see is normal, because the operation is still in progress .您看到的行为(呃,行为)正常的,因为操作仍在进行中 The autostash should be popped—or rather, dropped—after the operation is complete.在操作完成后,自动存储应该被弹出——或者更确切地说,是丢弃。 It's true that in this case, the rebase portion of the operation is done, but the stash did not apply cleanly, so the git stash apply step has failed:确实,在这种情况下,操作的 rebase部分已经完成,但是 stash 没有完全应用,所以git stash apply步骤失败了:

Applying autostash resulted in conflicts.

(I think Git did a git reset --hard to erase the attempt to git stash apply , making you do it on your own at this point, but I don't use autostash. Note that running git stash pop will get conflicts and will only apply , not drop, the stash.) (我认为Git 做了一个git reset --hard来消除对git stash apply的尝试,让你在这一点上自己做,但我不使用 autostash。请注意,运行git stash pop发生冲突,并且会只应用,而不是丢弃,藏匿处。)

I personally dislike autostash because it's overly complicated: it consists of a multi-step operation, involving making stash commits, rebasing, then applying-and-dropping the stash commits.我个人不喜欢 autostash,因为它过于复杂:它包含一个多步骤操作,包括进行 stash 提交、rebase,然后应用和删除 stash 提交。 Instead of stashing , I recommend that you just commit before rebasing .相反积攒的,我建议你重订之前刚刚提交 This way the last commit is part of the rebase, so there's just one operation to finish.这样,最后一次提交是 rebase 的一部分,因此只需完成一个操作。

You can use git reset to remove the rebased commit once you're all done, if that's even needed: I like to have smaller commits that I can combine later when they're "ready for prime time" as it were, and leaving a bunch of small "this isn't what I want but it's progress" commits in my in-progress work helps that out.完成后,您可以使用git reset删除重新提交的提交,如果甚至需要的话:我喜欢较小的提交,我可以稍后在它们“准备好迎接黄金时间”时进行合并,并留下一个一堆小的“这不是我想要的,但它是进步”在我正在进行的工作中的提交有助于解决这个问题。 Eventually, I run a git rebase -i , or series of them, to sort and combine the mini-commits.最终,我运行了一个git rebase -i或一系列的命令来对小型提交进行排序和组合。

(Not to make this a mini-rant against git stash , but ... git stash itself is bad in general. Avoid it.) (不是为了让这成为对git stash的小咆哮,而是...... git stash本身通常很糟糕。避免它。)

It turns out this is a bug.事实证明这是一个错误。 When using autostash, the stash is supposed to pop automatically.使用 autostash 时,stash 应该自动弹出。 Even when there are conflicts.即使有冲突。 As I expected.正如我所料。 Somewhere earlier this year (or last year) someone broke that functionality.今年早些时候(或去年)有人破坏了该功能。 I checked the lastest version ("next") of the git source code.我检查了 git 源代码的最新版本(“下一个”)。 And the pop again happens automatically.弹出再次自动发生。

There is one remaining problem.还有一个问题。 After the pop, and after the changes get applied without problems, I expect the stash to get deleted (dropped) automatically too.在弹出之后,并且在没有问题的情况下应用更改之后,我希望存储也会自动删除(丢弃)。 That doesn't happen.那不会发生。 I don't know if that is intentionally, or also a bug.我不知道这是故意的,还是一个错误。 I suspect it is a bug.我怀疑这是一个错误。 If everything applies fine, why keep the stash?如果一切正常,为什么要保留藏匿处? I bet nobody knows and nobody dares to make a decision to fix this or not.我敢打赌没有人知道,也没有人敢决定是否解决这个问题。

The sad thing is: I asked a whole bunch of people whether current behaviour (no automatic pop) is intentional or a bug.可悲的是:我问了一大群人当前的行为(没有自动弹出)是故意的还是错误的。 Colleagues, the guy who installed/maintains our git, our in-house git support people, our local git-experts, our external git-support team.同事,安装/维护我们的 git 的人,我们内部的 git 支持人员,我们当地的 git 专家,我们的外部 git 支持团队。 And here.和这里。 I got a zillion answers.我得到了无数的答案。 None of them were correct.他们没有一个是正确的。 Most didn't even answer the question: "I don't like rebase, I don't like autostash, I don't like stash, why don't you do what I do, that is way better".大多数人甚至没有回答这个问题:“我不喜欢 rebase,我不喜欢 autostash,我不喜欢 stash,你为什么不做我所做的,那更好”。 And nobody actually answered my question.没有人真正回答我的问题。 I don't blame the people here who tried to answer, but after 2 months rowing against the stream, I got a bit irritated.我不怪这里试图回答的人,但在逆流划船2个月后,我有点恼火。 Sorry.对不起。

I'm happy I persisted.我很高兴我坚持了下来。 I'm happy the problem will be fixed soon.我很高兴这个问题很快就会得到解决。 Now the only thing remaining is to make sure the stash gets dropped when we don't need it anymore.现在唯一剩下的就是确保当我们不再需要它时,它会被丢弃。 If necessary, I'll fix it myself and offer the fix to the git repository.如有必要,我会自己修复它并将修复提供给 git 存储库。

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

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