One of the biggest reasons to user Git, is the ability to commit often without affecting the rest of your team since you are committing locally. When pushing your changes to the shared repository, you may not want to show all your changes in history, as this can turn into a lot of clutter, especially if you are committing every few minutes. Take for example, the history that is show on my TFS Team Project that uses Git for source control:
I had 6 commits in a span of 1/2 hour, and my team can probably do without seeing every single one of those commits in history.
The way that you can remove the clutter is by using git rebase, which allows you to squash multiple commits into a single one. So in effect, you are changing history by combining a lot of commits together.
In this example, I made 3 commits to the same file, but when I push my changes, I want my team to only see one commit.
To do so, you’ll need to jump out to command-line and enter this command (Note that I have msysgit installed, with command-line integration):
git rebase -i origin/master
This brings up a VIM editor that shows all the commits that I’ve made locally and have not been pushed to the origin. You could also do this when merging between local branches.
I then change the “pick” to “squash” on my 2nd and 3rd commits. This tells Git that those commits should be squashed into the first commit:
To save and exit, hit Escape, then :wq
Git will show you a status letting you know that it is rebasing, and then you are presented with a new VIM editor showing you your three combined commit messages. At that point, you can change the commit messages before the rebase completes. Saving that file the same way as we did before with :wq, gives you the results that you were looking for:
Now, back on Visual Studio, you can see that my 3 commits have been changed into a single commit:
And once I push my changes to the server, I see a single commit:
Yes, I know that it’s a bit of a pain to jump out of your IDE to do this, but once you do it a couple of times, it’s pretty straight forward. The Visual Studio Git tools will continue evolving so expect that you will need to jump out of the IDE with less frequency as time goes by.
If you want to get started with Git and be successful with it, while staying in Visual Studio, I created a Pluralsight course that you may want to check out: Git for Visual Studio Developers.