Git 在 rebase 之前拉动

Git pull before rebasing

所以我 关于 git 中的变基,并得到了一些关于该做什么的很好的答案。但是,当我继续进行时,我 运行 遇到了一点我不明白的问题。

快速概览:

我 b运行 从另一个 b运行ch 中取出 (Branch2) - Branch1Branch1 是远程的,并且在我从它 运行ched 之后有很多提交。所有这些提交都不会被压缩。与此同时,我着手在 Branch2 中进行更改。现在我完成了我的更改并且必须在 Branch1.

之上重新设置 Branch2

当我在 Branch2 中执行 git status 时,它会列出我已更改的所有文件(这似乎是正确的)。但是,当我执行 git checkout Branch1git status 时,它会再次列出相同的文件。我不明白这一点,我的印象是每个 b运行ch 就像一个本地化环境,不会显示对其他 b运行ches 的更改。

另一件让我头晕目眩的事情是 - Branch1 自从我 运行 退出以来已经向前发展了。在变基之前,我想更新 Branch1 的本地副本,以便我在 Branch2 中的更改在 Branch1 的最新提交之上变基,所以我做了 git checkout Branch1git pull。但是我得到了:

error: Your local changes to the following files would be overwritten by merge:
    file...
Please, commit your changes or stash them before you can merge.
Aborting

我不明白:

  1. 为什么 branch2 中所做的更改显示在 Branch1git status 中?
  2. 如果我在 Branch1 上提交并推送我的更改,那么 git pull 我的提交将被放置在哪里,因为所有以前的提交包括我 b运行ched 的提交Branch1 中的 已被压扁。

When I do git status in Branch2 it lists all the files that I have changed (which seems right).

分支是指向提交的指针。 git status 显示已修改但未提交的文件。也许这对你来说似乎是正确的,但在你提交更改之前,签出不同的分支是一项冒险的操作。

However when I do a git checkout Branch1 and the git status it lists the same files again.

这是因为未提交的更改不在分支中,这意味着它们不在存储库中。它们仅在工作树中。

I dont understand this, I was under the impression that each branch is like a localized environment and does not show changes to other branches.

你的印象是正确的。

Another thing that has my head spinning is that - Branch1 has moved forward since I branched out from it.

由于您没有在 Branch2 中创建任何提交,从技术上讲,您没有从中创建 "branched out"。 Branch2 只是 Branch1 过去的提交,而不是真正的分支。在 Branch2 上提交您的更改,它将分支出来。

However I got :

error: Your local changes to the following files would be overwritten by merge:
   file...
Please, commit your changes or stash 

好吧,git 正在努力不破坏您的工作。它建议您如何做才能安全。


如果我理解正确你的愿望,你必须运行的命令如下:

# go back to Branch2
git checkout Branch2

# commit the changes in Branch2
git add .
git commit

# get the recent commits on Branch1 from the remote server
# there is no need to merge them
git fetch origin

# rebase Branch1 on top of the remote version of Branch1
# git pull produces the same result if you didn't commit anything on Branch1
git rebase origin/Branch1 Branch1

# rebase the commit(s) you created in Branch2 on top of Branch1
git rebase origin/Branch1 Branch2

# now you are on Branch2 and Branch1 is in the past of Branch2

祝你好运!

在切换回 branch1 之前,您似乎没有提交 branch2。您会将所有未提交的数据带回上一个分支。您有几个选择:首先 commit,运行 git checkout . 撤消它们,或者 运行 git stash 在切换之前。