squashing commits with rebase/ Combining Multiples Commits into One


  • Squashing commits:
    Combining multiples commits on github to one single commit. In this way you are rewriting your commits history.

    Warning!!!
    Just don’t rewrite your history if it’s been shared with others. If other team mates are working on the bases of commits which you are going to delete then it will cause plenty of conflicts.


    1. First Way to Squash Commits and rebase into Master

    1. Lets say you have to combine last four commits you made.

    
    git rebase -i HEAD~4
    
    a file will open in terminal
    
    pick 9987568 4th commit message here
    pick 6347568 3rd commit message here
    pick e2d4v67 2nd commit message here 
    pick 3876ccb 1st commit message here
    
    # Rebase 63238da..390ecb onto 68634a
    #
    # Commands:
    #  p, pick = use commit
    #  e, edit = use commit, but stop for amending
    #  s, squash = use commit, but meld into previous commit
    #
    # If you remove a line here THAT COMMIT WILL BE LOST.
    # However, if you remove everything, the rebase will be aborted.
    
    

    2. Change the file like this

    
    pick 9987568 4th commit message here
    squash 6347568 3rd commit message here
    squash e2d4v67 2nd commit message here 
    squash 3876ccb 1st commit message here
    

    I’ve changed 2nd, 3rd and 4th line first word from pick to squash

    3. Save and close the file, another editor pops up with the following:

    
    # This is a combination of 4 commits.
    # The first commit's message is:
     1st commit message here
    
    # This is the 2nd commit message:
    
     2nd commit message here
    
    # This is the 3rd commit message:
    
     3rd commit message here
    
    # This is the 4th commit message:
    
     4th commit message here
    
    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    # Explicit paths specified without -i nor -o; assuming --only paths...
    # Not currently on any branch.
    # Changes to be committed:
    #   (use "git reset HEAD ..." to unstage)
    #
    #	new file:   app/views/layouts/admin.html.erb
    #	modified:   config/config.yml
    #	modified:   config/database.yml
    #	modified:   lib/xyz.rb
    #
    

    4. Remove all messages in file and write new generic message.

    
    # This is a combination of 4 commits.
      
     Generic Message for all commits
    
    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    # Explicit paths specified without -i nor -o; assuming --only paths...
    # Not currently on any branch.
    # Changes to be committed:
    #   (use "git reset HEAD ..." to unstage)
    #
    #	new file:   app/views/layouts/admin.html.erb
    #	modified:   config/config.yml
    #	modified:   config/database.yml
    #	modified:   lib/xyz.rb
    #
    

    5. Forcefully Push on git

    git push origin BRANCH_NAME -f

    Now have a look at history you will see single commit with new message.

    You can check this one too

    
    git rebase feature_invitation
    git push -f
    


    2. Second Way to Squash Commits and rebase into Master

    
    git rebase -i master 
    git push -f
    git checkout master
    git rebase feature_invitation
    git push -f
    


    3. Third Way to Squash Commits and rebase into Master

    
    git checkout master
    git merge --squash featuer_branch
    git commit -m 'your_commit_message'
    git push
    


    4. The Way to Rebase Commits and merge into Master [With Out Squash]

    
    git checkout master
    git merge --squash featuer_branch
    git commit -m 'your_commit_message'
    git push
    

    I am Senior Software Engineer. I love reading, writing, sharing,developing, hiking, movies, trips, mountains, brooks, hills etc.

  • Tagged with: , , ,
    Posted in bitbucket, git, github

    Leave a comment

    StackOverFlow
    Categories
    Archives