Squash my last X commits together using Git

前端 未结 30 3374
醉酒成梦
醉酒成梦 2020-11-21 05:17

How can I squash my last X commits together into one commit using Git?

相关标签:
30条回答
  • 2020-11-21 05:28

    How can I squash my last X commits together into one commit using Git?

    git rebase -i HEAD~X
    

    The following content will be shown:

    pick 1bffc15c My earlier commit
    pick 474bf0c2 My recent commit
    
    # ...
    

    For the commits that you want to squash, replace pick with fixup, so it becomes:

    pick 1bffc15c My earlier commit
    fixup 474bf0c2 My recent commit
    
    # ...
    

    If it's open in vim (default interface within terminal), then press Esc on your keyboard, type :wq and Enter to save the file.

    Verify: Check git log

    0 讨论(0)
  • 2020-11-21 05:29

    Simple one-liner that always works, given that you are currently on the branch you want to squash, master is the branch it originated from, and the latest commit contains the commit message and author you wish to use:

    git reset --soft $(git merge-base HEAD master) && git commit --reuse-message=HEAD@{1}
    
    0 讨论(0)
  • 2020-11-21 05:31

    What about an answer for the question related to a workflow like this?

    1. many local commits, mixed with multiple merges FROM master,
    2. finally a push to remote,
    3. PR and merge TO master by reviewer. (Yes, it would be easier for the developer to merge --squash after the PR, but the team thought that would slow down the process.)

    I haven't seen a workflow like that on this page. (That may be my eyes.) If I understand rebase correctly, multiple merges would require multiple conflict resolutions. I do NOT want even to think about that!

    So, this seems to work for us.

    1. git pull master
    2. git checkout -b new-branch
    3. git checkout -b new-branch-temp
    4. edit and commit a lot locally, merge master regularly
    5. git checkout new-branch
    6. git merge --squash new-branch-temp // puts all changes in stage
    7. git commit 'one message to rule them all'
    8. git push
    9. Reviewer does PR and merges to master.
    0 讨论(0)
  • 2020-11-21 05:32

    What can be really convenient:
    Find the commit hash you want to squash on top of, say d43e15.

    Now use

    git reset d43e15
    git commit -am 'new commit name'
    
    0 讨论(0)
  • 2020-11-21 05:33

    If you use TortoiseGit, you can the function Combine to one commit:

    1. Open TortoiseGit context menu
    2. Select Show Log
    3. Mark the relevant commits in the log view
    4. Select Combine to one commit from the context menu

    This function automatically executes all necessary single git steps. Unfortunatly only available for Windows.

    0 讨论(0)
  • 2020-11-21 05:34
    git rebase -i HEAD^^
    

    where the number of ^'s is X

    (in this case, squash the two last commits)

    0 讨论(0)
提交回复
热议问题