Squashing commits in git(through Visual Studio)

I am gonna tell you how to squash commits in Git through Visual Studio but the steps are same for any git command line.
Before starting I am gonna warn you that rebase is a powerful git tool and care must be taken while applying it. Anyway here are the steps :-
  1. Let us have a few unsquashed commits as shown in the image below.
                                                

  2. Now to squash the commits we need to open the command prompt, for that click on "Actions" then select "Open Command Prompt" from the drop down.

  3. In the command prompt so opened type the command "git rebase -i HEAD~4" as shown in below image and press enter (Here in the command I have written "HEAD~4" since I have to squash last 4 commits, you can change the number depending on your requirement).
                       

  4. After pressing enter a new view will appear showing the last four commits that are to be rebased with a "pick" keyword at the beginning of each line. You need to leave the first line as it is and change the "pick" keyword to "squash" in the following lines (if you are not able to type then press "insert" key), so that your view seems like the one shown in the below image.                    

  5. After making the above said changes press "Esc" and then type ":x!" at the end of command window (as shown in above image) and press enter to save and exit.

  6. Now a new window will open up this one is for setting the comment for the squashed commit. The default comment contains comments for all the individual commits that are being squashed, but you can change that as well. For that just remove all the unnecessary uncommented lines and type your new message in an uncommented line. Just like I have changed my message to "squashed commit" in the below image.

  7. Again press "Esc"  and type ":x!" and hit enter to save and exit. After this your commits will get squashed into one. You can confirm that by going to Sync view, now you will see only a single commit here instead of four as is displayed in below image.

  8.  If you are using VSTS ten you might need to add workitem to this commit before pushing it, for that double click the commit from the scnc view and put the following line to the comment "Related Work Items: #1" here 1 is the ID of the work item. You can add more than one workitem by providing a comma separated list of workitem IDs. For reference see the below image.



No comments:

Post a Comment