How to stage changes in Visual Studio in separate files – 068

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:

  1. Add a ReadLine() so I have to press Enter to dismiss the console window
  2. 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

Stage in Team Explorer

Now you’ll see two sections in Team Explorer. Your staged changes and your working directory.

Staged Changes in Team Explorer

Now you’ll see that the Commit All button has changed to a Commit Staged button.

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.

git status via command prompt confirming Program.cs staged

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)

Committing the newly added Strings.cs class via Team Explorer

And now if you view the log in Visual Studio – from status bar(!!) click the up arrow,

View History command from status bar

You’ll see the history with the last two commits

Local History shown in Visual Studio

and the command line will confirm, using git log

command line showing git log of the last few commits

Now you’ll see that what would have been originally one big commit is now two separate commits thanks to staging.

One thought on “How to stage changes in Visual Studio in separate files – 068

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s