Git rebase 删除最近的更改?

标签 git

我有一个 master 分支和两个工作分支,ab。分支 b 是从与 a 相同的点创建的,但涉及不同的文件(只有一个共享文件,d.py)。分支 b 刚刚被 pull 入 master

                (a)   F---H-J----L---
                     /
(master)A--B--C--D--E--------------M
                     \            /
                 (b)  G----I----K

在我对分支 a 的 pull 请求中,Github 现在说

This branch has conflicts that must be resolved

Use the web editor or the commandline to resolve conflicts.

Conflicting files

clippy/d.py

我想我可以做显而易见的事情,git pull --rebase master 并最终得到如下所示的历史记录:

                             F---H-J----L--
                            /            
A--B--C--D--E--------------M
             \            /
              G----I----K

然后 merge 到master就万事大吉了

                             F---H-J----L
                            /            \
A--B--C--D--E--------------M--------------N
             \            /
              G----I----K

当我尝试 merge 时,这就是我得到的结果

You are currently rebasing branch 'bug/1500/Things' on '1234567'.
  (fix conflicts and then run "git rebase --continue")
  (use "git rebase --skip" to skip this patch)
  (use "git rebase --abort" to check out the original branch)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   apps/a.py
    modified:   apps/b.py
    modified:   apps/c.py

Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

    both modified:   apps/d.py

而且我认为一切都会很好。但后来我看了一下 a.py 并意识到我所有的更改都来自提交 FHG L 消失了。 b.pyc.py 等也是如此。据我所知,rebase 中唯一的变化是 d.py 中的变化 这是两个分支之间共享的文件。 我中止了 rebase 并再次尝试使用 git rebase master 但同样的事情发生了。

为什么 git 这样做?我怎样才能成功地 rebase 我的提交以 merge 它?

最佳答案

如果master和你的a有冲突分支,rebase 将在存在冲突的地方停止,并希望您修复它。

首先,要获得分支的原始状态,您可以执行 git rebase --abort .这将撤消尝试的 rebase 。

如果你还在rebase的中间,你可以做git diff看看冲突在哪里。你会在冲突文件中看到类似这样的内容

<<<<<<< HEAD
...
=======
....
>>>>>>> branch-a

您可以通过选择要保留的代码并删除 <<<< 来编辑文件以修复冲突。 ===== >>>>>>>线。

完成文件和冲突的编辑后,您可以运行 git add .然后 git rebase --continue . rebase 将继续将您的提交移动到 HEAD .在任何时候,您可能会或可能不会遇到更多冲突。如果不这样做,您会看到 rebase 成功运行,然后您就可以将更改推送到您的分支。然而,由于您已经重新定位,您将只能执行强制推送,因为您的提交引用将与远程上的引用发生变化。

关于Git rebase 删除最近的更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49944913/

相关文章:

git - VS 中的 TFS/GIT 无法切换到 master,因为有未提交的更改

git - 当你是 git 的原始仓库时,你如何进行本地 pull ?

git - 仅在本地分支或本地和远程分支上为新功能创建分支?

git - 使用 GIT 实现 Dropbox 类型的功能

django - Git - 如何将本地存储库提交到另一个本地存储库的子文件夹?

git - 在 emacs 中使用 git

windows - 如何在 Git Bash for windows 中更改 RGB 颜色?

git - 使用 git 在 Visual Studio 2015 中提交之间的差异

git commit 打开两个编辑器 Pane 而不是一个来输入消息

GitHub:我可以查看我是否对 fork 进行了更改吗?