One of the reasons Git is pretty awesome is its ability to stage changes. I only recently discovered why it is so awesome to stage changes just a couple of months ago. But then again, I was using Ctrl+C, Ctrl+V as my VCS until 2 years ago.
I get ahead of myself all the time and I’ll make several updates at once and then my commit is no longer scope to one functional unit of change. For example, in my Hello World example, let’s say I wanted to make the following changes:
- Add a ReadLine() so I have to press Enter to dismiss the console window
- Add a new class that contains the actual “Hello World” string to be used – just trivial example to demonstrate making changes to a separate file.
And at this point I realize, “oh no! I forgot to commit part 1.” So now my commits become larger than I want.
What you can do is stage the commit for Part 1, e.g. in Team Explorer – Changes select the file you want to stage, and right-click and click Stage
Now you’ll see two sections in Team Explorer. Your staged changes and your working directory.
Now you’ll see that the Commit All button has changed to a Commit Staged button.
To confirm what is happening, open the command prompt, and run git status. You’ll see that you’ve essentially did a git add Program.cs but via Team Explorer.
Finish the commit by clicking the Commit Staged button.
Now finish part 2 by committing the newly added strings class (and changes to the corresponding .csproj file)
And now if you view the log in Visual Studio – from status bar(!!) click the up arrow,
You’ll see the history with the last two commits
and the command line will confirm, using git log
Now you’ll see that what would have been originally one big commit is now two separate commits thanks to staging.