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