Git合並最近的commit


合並commit的做法一般用在pull request的時候,把開發同一功能時的所有瑣碎的commit合並到一個(假裝自己的代碼是高質量代碼(手動滑稽))。主要使用的命令是git rebase 或者git reset,這兩個命令的區別可以看這里,一般推薦用git rebase,因為commit的歷史能保存下來。

1. git rebase

首先用git log查看commit歷史確定需要操作的commit,比如:

commit 0bca654809387dc226cfc4ff872f65601809f485
Author: CasiaFan <fanzongshaoxing@gmail.com>
Date:   Wed Aug 16 13:13:12 2017 +0800

    fix typo

commit b5bf9998e40dc165d7e3055bae47172de11225d4
Author: CasiaFan <fanzongshaoxing@gmail.com>
Date:   Wed Aug 16 10:02:30 2017 +0800

    add random vertical flip during image preprocessing

commit 70a4958184106b9ce848da34be34bdf0dbf36450
Author: CasiaFan <fanzongshaoxing@gmail.com>
Date:   Wed Aug 16 09:58:55 2017 +0800

    step by step running

commit d68608c2dbd41a1b89363f4b743bc5464b6749be
Author: CasiaFan <fanzongshaoxing@gmail.com>
Date:   Mon Aug 14 15:52:47 2017 +0800

    modify description

如果需要操作最近4個commit:

git rebase -i HEAD~4   # 操作對象為從倒數第4個commit開始到當前的commit
# git rebase -i d68608c2dbd41a1b89363f4b743bc5464b6749be  # 倒數第4個commit 的id

然后會跳出編輯頁面像這樣:

pick d68608c modify description
pick 70a4958 step by step running
pick b5bf999 add random vertical flip during image preprocessing
pick 0bca654 fix typo

# Rebase 529bf46..0bca654 onto 529bf46 (4 command(s))
#
# 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's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

把需要保留的commit之前保留pick,需要合並的前面改成squash,編輯保存退出以后輸入新的commit message,最后保存退出。

# This is a combination of 6 commits.
add random vertical flip for image preprocessing  # 合並后的commit
# The first commit's message is:
test
# This is the 2nd commit message:

object detection api usage step

# This is the 3rd commit message:

modify description

# This is the 4th commit message:

step by step running

# This is the 5th commit message:

add random vertical flip during image preprocessing

# This is the 6th commit message:

fix typo

這個時候再git log的日志如下:

commit 7f774d88eb6884c97c8b3c05a9268afa581d5b57
Author: Unknown <fanzongshaoxing@gmail.com>
Date:   Mon Aug 14 15:45:29 2017 +0800

    add random vertical flip for image preprocessing
    test
    
    object detection api usage step
    
    modify description
    
    step by step running
    
    add random vertical flip during image preprocessing
    
    fix typo

如果修改了一半不想合並了就用git rebase --abort刪除。

git reset

git reset --soft "HEAD~4"
git commit --amend

或者

git reset --hard HEAD~4  # 將branch狀態切到到4個commit之前
git merge --squash HEAD@{1}  # 將當前commit(此時為倒數第四個)之后的commit都合並
git commit # commit squashed changes

參考

https://stackoverflow.com/questions/5189560/squash-my-last-x-commits-together-using-git
https://stackoverflow.com/questions/2563632/how-can-i-merge-two-commits-into-one
http://zerodie.github.io/blog/2012/01/19/git-rebase-i/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM