Rebase 使推送变得不可能

Rebase is making push impossible

我在 Git 使用变基时遇到了一些问题,这里是 "draw" 的情况,我想要什么以及我做了什么。

Repo state (initial)

   --- C1 --- C5 --- C6 --- C7 (master & origin/master)
        \
         \--- C2 --- C3 --- C4 (my-branch & origin/my-branch)
Repo state (wanted)

   --- C1 --- C5 --- C6 --- C7 --- C2 --- C3 --- C4 (my-branch & origin/my-branch)
                             |
                (master & origin/master)

所以,在这种情况下,我这样做了:

$ git checkout my-branch
$ git rebase origin/master

但是回购变成了这个状态:

   --- C1 --- C5 --- C6 --- C7 --- C2 --- C3 --- C4 (my-branch)
        \                    |
         \       (master & origin/master)
          \
           \--- C2 --- C3 --- C4 (origin/my-branch)

所以,当我尝试推送 "my-branch" 时,git 抛出一个错误告诉我我的分支有一些未拉更改所以我无法推送。

我做了什么(我知道这是一个不好的解决方案):

$ git push origin --delete my-branch
$ git push

所以,远程分支被删除并重新创建。

我确定还有其他解决方案,但我找不到我的想法做错了什么。

预先感谢您的帮助。

以后你可以简单地使用git-merge:

git-merge - Join two or more development histories together

Incorporates changes from the named commits (since the time their histories diverged from the current branch) into the current branch. This command is used by git pull to incorporate changes from another repository and can be used by hand to merge changes from one branch into another.

例如:

git checkout my-branch
git merge master

这会将 master 中的提交带到 my-branch 中,而不更改 master 中的任何内容。

一个非常有用的提示是学习如何使用 git-reflog and git-reset 撤消您可能已经执行的不需要的变基。由于您已经删除了远程存储库,因此执行此操作没有任何意义,但如果您再次需要它,这里有一个示例:

git reflog
# This will print a history of repository changes.
# Pick the latest ref before the rebase - let's suppose it's HEAD@{3}

# CAUTION: Be sure you understand what git-reset --hard does
git reset --hard HEAD@{3}

现在您的存储库应该恢复到执行变基之前的状态。

注意 - 关于与 git-reset 一起使用的 --hard 标志:

--hard Resets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded.