1. Learn center
  2. Software Development
  3. Guides
  4. Git tutorial
  5. 如何使用 Git
  6. 如何在 Git 中管理历史记录
  7. 组合并移动提交
Git

Project and code management together.

Try it free

组合并移动提交

请转至您之前下载的git-tutorial/tutorial6目录。

当您检查此存储库的历史记录时,它将如下所示:

Current history

我们将修改带有“append description of the commit command”消息的提交。

为此,我们将使用 git rebase -i 命令

$ git rebase -i HEAD~~

您默认的文本编辑器将打开从 HEADHEAD~~ 的列表提交,如下所示。

pick 9a54fd4 append description of the commit command
  pick 0d4a808 append description of the pull command

  # Rebase 326fc9f..0d4a808 onto d286baa
  #
  # Commands:
  #  p, pick = use commit
  #  r, reword = use commit, but edit the commit message
  #  e, edit = use commit, but stop for amending
  #  s, squash = use commit, but meld into previous commit
  #  f, fixup = like "squash," but discard this commit log message
  #  x, exec = run command (the rest of the line) using shell
  #
  # If you remove a line here THAT COMMIT WILL BE LOST.
  # However, if you remove everything, the rebase will be aborted.
  #

在第一行,将单词pick更改为edit,然后保存并退出。

您将看到以下输出,并从该提交中签出。

Stopped at d286baa... append description of the commit command
  You can amend the commit now, with

          git commit --amend

  Once you are satisfied with your changes, run

          git rebase --continue

打开sample.txt文件,并进行如下所示的更改。

Anyone can learn Git with this tutorial and Backlog
add: Register a change in an index
commit: Record index state.
pull: Obtain the content of the remote repository

使用git commit --amend来提交更改。

$ git add sample.txt
$ git commit --amend

然后执行git rebase --continue完成 rebase 过程。

$ git rebase --continue

在您当前的分支上运行变基时,您可能会在您的更改中遇到冲突。若要继续,您必须手动解决这些冲突,然后运行git addgit rebase --continue。无需发出新的提交来解决冲突。

但是,如果您想停止变基,您可以调用git rebase --abort,这将 revert 并退出整个过程,且成功修改提交。

如果您在调用git rebase -i时,在多个行上指定edit,系统将提示您一次修改一个提交。

Subscribe to our newsletter

Learn with Nulab to bring your best ideas to life