Combining multiple commits before pushing in Git

This question already has an answer here:

  • Squash my last X commits together using Git 23 answers

  • What you want to do is referred to as "squashing" in git. There are lots of options when you're doing this (too many?) but if you just want to merge all of your unpushed commits into a single commit, do this:

    git rebase -i origin/master
    

    This will bring up your text editor ( -i is for "interactive") with a file that looks like this:

    pick 16b5fcc Code in, tests not passing
    pick c964dea Getting closer
    pick 06cf8ee Something changed
    pick 396b4a3 Tests pass
    pick 9be7fdb Better comments
    pick 7dba9cb All done
    

    Change all the pick to squash (or s ) except the first one:

    pick 16b5fcc Code in, tests not passing
    squash c964dea Getting closer
    squash 06cf8ee Something changed
    squash 396b4a3 Tests pass
    squash 9be7fdb Better comments
    squash 7dba9cb All done
    

    Save your file and exit your editor. Then another text editor will open to let you combine the commit messages from all of the commits into one big commit message.

    Voila! Googling "git squashing" will give you explanations of all the other options available.


    If you have lots of commits and you only want to squash the last X commits, find the commit ID of the commit from which you want to start squashing and do

    git rebase -i <that_commit_id>
    

    Then proceed as described in leopd's answer, changing all the pick s to squash es except the first one.


    You can do this with git rebase -i , passing in the revision that you want to use as the 'root':

    git rebase -i origin/master
    

    will open an editor window showing all of the commits you have made after the last commit in origin/master . You can reject commits, squash commits into a single commit, or edit previous commits.

    There are a few resources that can probably explain this in a better way, and show some other examples:

    http://book.git-scm.com/4_interactive_rebasing.html

    and

    http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html

    are the first two good pages I could find.

    链接地址: http://www.djcxy.com/p/23450.html

    上一篇: Git合并提交

    下一篇: 在推入Git之前合并多个提交