Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To combine all commits on a branch using Git you can use the Git rebase command. The following steps outline the process:

  1. Checkout the branch you want to combine all commits into.

    $ git checkout <branch_name>
    
  2. Use the rebase command with the interactive flag to open the interactive rebase UI.

    $ git rebase -i <base_branch>
    

    The <base_branch> is the branch where you want to merge the changes. If you want to merge the changes to the master branch, then use the keyword "master".

  3. The interactive rebase UI will open up in your default text editor.

  4. In the interactive rebase UI, replace the word "pick" with "squash" or "fixup" for each commit you want to combine.

    The "squash" command will combine the commit with the previous commit, while "fixup" will combine the commit with the commit before it but drop the commit message.

    pick 1e2f39d Commit message 1
    squash d82135e Commit message 2
    
    # Rebase 1e2f39d..d82135e onto master
    #
    # Commands:
    #  p, pick <commit> = use commit
    #  r, reword <commit> = use commit, but edit the commit message
    #  e, edit <commit> = use commit, but stop for amending
    #  s, squash <commit> = use commit, but meld into previous commit
    #  f, fixup <commit> = like "squash", but discard this commit's log message
    #  x, exec <command> = run command (the rest of the line) using shell
    
    1. Save and close the file. A new window will appear and allow you to edit the commit message if need be.

    2. Create a new branch with the combined commits.

    $ git checkout -b <new_branch_name>
    
    1. Push the new branch to the remote repository.
    $ git push origin <new_branch_name>
    

    Note: It is recommended that you only rebase on local branches and not on remote branches that have already been pushed to the remote repository.