git:分支删除了一些提交

发布于 2024-09-08 08:57:26 字数 1237 浏览 2 评论 0原文

我今天遇到了一个奇怪的情况..我会尝试重现我认为发生的事情。

我想将推测性更改(对 a_file 进行)移至新分支,并在没有它的情况下继续在主分支上工作。

在带有 a_fileb_file dirty 的 master 上,我做了:

git checkout -b saved_feature

git commit a_file -m "Putting this modification on a different branch"

git checkout master

此时 git 抱怨 b_file 很脏 - 这是一个警告信号,但我没有'不认识它。

我仍在分支 saved_feature 上,所以我想我可以这样做:

git stash save

git checkout master

到目前为止一切顺利

git stash pop

此时我收到一条错误消息,指出无法合并存储。

我检查了日志 - 由于某种原因,有大约 6 天的提交最初提交到 master 分支,但不在日志中。这些提交在我创建的新分支上(我检查了是否已将它们提交到不同的分支)。

检查了 saved_feature 分支的日志后,我可以看到它们都在那里。

发生了什么?

接下来我尝试了 master

git merge saved_feature

但是快进选项因大量冲突而失败。

我还使用 git-svn 将更改推送到外部存储库,因此我再次在 master 上这样做:

git svn rebase

这恢复了一些之前从 svn 推送的提交。

然后我从 saved_feature 分支中挑选了其余的最新提交, 然后我做了一个 git stash pop ,修复了应该自动合并但没有自动合并的冲突,最后让我的 master 处于原来的状态。

谁能指出在这个令人遗憾的故事中我的思维模型和 git 分道扬镳的地方?我怎样才能避免再次陷入这样的混乱?

I had a weird situation today.. I'll try and recreate what I think happened.

I wanted to move a speculative change (made to a_file) to a new branch and continue working on the main branch without it.

On master with a_file and b_file dirty I did:

git checkout -b saved_feature

git commit a_file -m "Putting this modification on a different branch"

git checkout master

At this point git complained that b_file was dirty - this was a warning sign but I didn't recognise it.

I was still on branch saved_feature so I thought I could do:

git stash save

git checkout master

So far so good

git stash pop

At this point I get an error saying that the stash couldn't be merged.

I examine the log - for some reason there are about 6 days worth of commits that were originally committed to the master branch but which were not in the log. These commits were only on the new branch that I had created (I checked in case I had been committing them to a different branch).

After examining the log of the saved_feature branch, I can see that they are all there.

What happened?

I next tried on master:

git merge saved_feature

But the fast forward option failed with a ton of conflicts.

I also use git-svn to push changes to an external repository so on master again I did:

git svn rebase

This recovered some of the previously pushed commits from svn.

I then cherry-picked the rest of the most recent commits from the saved_feature branch,
I then did a git stash pop, fixed the conflicts that should have been automatically merged, but were not, and finally had my master in the state it was originally.

Could anyone point out in this sorry tale where my mental model and git parted ways & how I can avoid getting into messes such as these again?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空‖城人不在 2024-09-15 08:57:26

过去 6 天您可能正在处理 从主 HEAD 的祖先中分离 HEAD
(感谢 Chris Johnsen 建议最后一部分:它可能解释了一切)

假设您有:

x--x--x--x <- master
      ^
      |
      T1 (tag)

如果您执行'git checkout T1' 查看 T1 内容,繁荣:分离的 HEAD 就在那里。
如果您从 T1 进行一些修复,最终会得到

x--x--x--x <- master
      ^\
      | -y--y--y--y--y--y <- detached HEAD (a dirty, b dirty)
      |
     T1(tag)

现在的结果,此时 git checkout -b saving_feature 将创建一个包含过去 6 天(相当于)的分支提交到其中:

x--x--x--x <- master
      ^\
      | -y--y--y--y--y--y--a <- saved_feature (a committed, b dirty)
      |
     T1(tag)

,但是对于文件 b 来说,签出到 master 并不简单。
mastersaved_feature 的合并也不会是微不足道的(当然不是快进的)

无论如何,这就是为什么密切关注很重要您实际工作的当前分支的时间。

You were probably working those last 6 days on a detached HEAD from an ancestor of master HEAD.
(thank you Chris Johnsen for suggesting that last part: it explains potentially everything)

Suppose you have:

x--x--x--x <- master
      ^
      |
      T1 (tag)

If you do a 'git checkout T1' to see that T1 content, boom: detached HEAD right there.
If you do some fixes from T1, you end up with

x--x--x--x <- master
      ^\
      | -y--y--y--y--y--y <- detached HEAD (a dirty, b dirty)
      |
     T1(tag)

Now, a git checkout -b saved_feature at this point would create a branch with the last 6 (days worth of) commits in it:

x--x--x--x <- master
      ^\
      | -y--y--y--y--y--y--a <- saved_feature (a committed, b dirty)
      |
     T1(tag)

, but the checkout to master is not trivial for file b.
And a merge from master to saved_feature won't be trivial either (certainly not a fast-forward one)

Anyhow, that is why it is important to keep an eye at all time at the current branch you are actually working in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文