Squash my last X commits together using Git

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

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

30条回答
  •  感动是毒
    2020-11-21 05:38

    ⚠️ WARNING: "My last X commits" might be ambiguous.

      (MASTER)  
    Fleetwood Mac            Fritz
          ║                    ║
      Add Danny  Lindsey     Stevie       
        Kirwan  Buckingham    Nicks                                              
          ║         ╚═══╦══════╝     
    Add Christine       ║          
       Perfect      Buckingham
          ║           Nicks            
        LA1974══════════╝                                    
          ║                  
          ║                  
        Bill <══════ YOU ARE EDITING HERE
      Clinton        (CHECKED OUT, CURRENT WORKING DIRECTORY)              
    

    In this very abbreviated history of the https://github.com/fleetwood-mac/band-history repository you have opened a pull request to merge in the the Bill Clinton commit into the original (MASTER) Fleetwood Mac commit.

    You opened a pull request and on GitHub you see this:

    Four commits:

    • Add Danny Kirwan
    • Add Christine Perfect
    • LA1974
    • Bill Clinton

    Thinking that nobody would ever care to read the full repository history. (There actually is a repository, click the link above!) You decide to squash these commits. So you go and run git reset --soft HEAD~4 && git commit. Then you git push --force it onto GitHub to clean up your PR.

    And what happens? You just made single commit that get from Fritz to Bill Clinton. Because you forgot that yesterday you were working on the Buckingham Nicks version of this project. And git log doesn't match what you see on GitHub.

提交回复
热议问题