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/tutorial7目錄。

當您檢查此儲存庫的歷史紀錄時,它將如下所示:

Current history

我們會將來自「issue1」分支的提交壓縮為單個提交,然後將其合併到主分支中。

切換到主分支,並使用「--squash」選項執行合併,如下所示。

$ git checkout main
  Switched to branch 'main'
$ git merge --squash issue1
  Auto-merging sample.txt
  CONFLICT (content): Merge conflict in sample.txt
  Squash commit -- not updating HEAD
  Automatic merge failed; fix conflicts and then commit the result.

Git 會偵測到衝突。我們必須在「sample.txt」檔案中手動解決衝突,並提交更改。

$ git add sample.txt
$ git commit
  [main 0d744a7] Conflicts:   sample.txt
    1 files changed, 4 insertions(+), 0 deletions(-)

我們現在已在主分支中新增了一個新提交,其中包括「issue1」分支中的所有提交。

您可以使用 git log 指令來驗證修訂歷史紀錄中的新變更。

Subscribe to our newsletter

Learn with Nulab to bring your best ideas to life