使用git rebase编辑提交

时间:2014-01-19 00:38:54

标签: git git-rebase

我在编辑提交时遇到问题。

我有2个.php文件的2次提交。我的目标是编辑它们。我读到了rebase -i,这是我认为应该做的事情:

  • 首先我将pick更改为edit;
  • 保存并退出;
  • rebase停止;
  • 输入git commit --amend;
  • 进行更改,保存并关闭文本编辑器;
  • 输入git rebase --continue

在此之后,我相信rebase再次停止,我必须再次为第二次提交做这件事。

但是在我输入git rebase --continue之后我得到了这个:

file1.php: needs update
You must edit all merge conflicts and then
mark them as resolved using git add

问题是什么,我该怎么办?

3 个答案:

答案 0 :(得分:5)

当你停下来换篮时,你必须:

  • 编辑文件
  • 添加对索引的更改 - git add changedFile
  • 修改更改 - git commit --amend
  • 继续重新定位 - git rebase --continue

根据您的描述,您可能忘记添加对索引的更改。在这种情况下,git commit --amend什么都不做(修改没有变化)。另外,在编辑文件之前你有git commit --amend这也是错误的(你必须修改你在文件上做过的更改)。

尝试按照我给出的顺序应用步骤。

答案 1 :(得分:1)

(代表OP发布解决方案)。

我找到了正确的方法:

git rebase -i --root (I wasn't able to find both commits using HEAD~2)
pick->edit for both commits
Ctrl+X and Y and ENTER
edit the first commit
git add file1.php
git commit --amend
git rebase --continue
edit the second commit
git add file2.php
git commit --amend
git rebase --continue
git push -f

希望这有助于至少一个人开始他们的git体验。 Szpak,你是一个很大的帮助。感谢。

答案 2 :(得分:0)

如果您在独自工作的功能分支上:

首先对文件进行更改并创建一个新的提交。

git commit -m "message here"

确保您在您的功能分支

git checkout your_feature_branch
git rebase -i parent_branch (the branch the your_feature_branch was forked from)

编辑器将弹出选项。将第 2 行的标记从 squash 更改。然后,您将可以选择编辑提交消息。您可以删除一条提交消息并使用不同的文本进行更新。

Shift+zz (exit for vi)

如果您在此处尝试 git push your_feature_branch,您将收到以下错误:

$ git push your_feature_branch
To https://your_repo.git
 ! [rejected]        your_feature_branch -> your_feature_branch (non-fast-forward)
error: failed to push some refs to 'https://your_repo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

由于您所做的本地更改,这是预期的 - 以克服此用途:

git push your_feature_branch --force

上面的命令会将更改推送到远程仓库。

注意: --force 的使用不是标准的或不推荐的,因此只有在您知道自己在做什么时才使用它。在这种情况下是允许的,因为假设您是唯一在 your_feature_branch 上工作的人

我假设您在功能分支上独自工作。

相关问题