美文网首页Git
Git combine multiple commits int

Git combine multiple commits int

作者: JaedenKil | 来源:发表于2019-02-19 15:06 被阅读1次
    • Scenario 1: On master branch, combine multiple commits into one.
    $ git log --oneline
    706ac8d (HEAD -> master) Add file03
    e62bfba Add file02
    6dd771d Add file01
    

    Say I want to combine 706ac8d and e62bfba into one commit.

    git rebase -i HEAD~2
    

    In the editor, change

    pick e62bfba Add file02
    pick 706ac8d Add file03
    # This is the original message
    

    to

    pick e62bfba Add file02
    squash 706ac8d Add file03
    # This is the new message, then save and exit editor, then another editor pops up to make you edit the commit message
    

    Change

    Add file02 
    Add file03
    # This is the original message
    

    to

    Add file02 and file03
    # Add file03
    

    Then save and exit.

    $ git log --oneline
    41b43e7 (HEAD -> master) Add file02 and file03
    6dd771d Add file01
    
    • Scenario 2: Squash multiple commits as one and merge into master
    $ git log --oneline
    706ac8d (HEAD -> master) Add file03
    e62bfba Add file02
    6dd771d Add file01
    
    $ git checkout -b dev
    Switched to a new branch 'dev'
    

    Make some changes on the new branch dev, it now looks like:

    $ git log --oneline
    82850ae (HEAD -> dev) Add file05
    d6ec04b Update file04 again
    f0962c3 Update file04
    a11af2c Add file04
    706ac8d (master) Add file03
    e62bfba Add file02
    6dd771d Add file01
    

    But as a matter of fact, when merging dev into master, the three new commits about file04 should be squashed into one commit.

    # Checkout to master
    $ git checkout master
    Switched to branch 'master'
    
    # Squash all changes
    $ git merge --squash dev
    Updating 706ac8d..82850ae
    Fast-forward
    Squash commit -- not updating HEAD
     file04 | 1 +
     file05 | 0
     2 files changed, 1 insertion(+)
     create mode 100644 file04
     create mode 100644 file05
    
    # Check git status
    $ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
            new file:   file04
            new file:   file05
    
    # All changes are now available for committing
    $ git commit -m "Add file04 and file05"
    [master fd92f0a] Add file04 and file05
     2 files changed, 1 insertion(+)
     create mode 100644 file04
     create mode 100644 file05
    
    # Show commit status
    $ git log --oneline
    fd92f0a (HEAD -> master) Add file04 and file05
    706ac8d Add file03
    e62bfba Add file02
    6dd771d Add file01
    

    相关文章

      网友评论

        本文标题:Git combine multiple commits int

        本文链接:https://www.haomeiwen.com/subject/ksffyqtx.html