Why would you want to stage multiple commits of the same file? Actually, I have no idea. This is why I’m writing this series so I can ask you all questions 🙂 Maybe there is no good reason to stage, just like making your bed every morning. Biggest waste of time EVER. You’re just going to mess it up less than half a day later… but I digress…
TBH I originally wrote today’s tip convinced I had found a bug in Team Explorer. I wanted to see what the workflow was like in Visual Studio when staging multiple commits for the same file. However, I confused myself by pressing the Commit Staged button, thinking it was the equivalent of git add <filename> (it’s not). In Team Explorer, doing Context Menu – Stage is the equivalent of git add <filename>, as you’ll see below…
To explain, let’s start with the command line for the expected behavior, then we’ll rinse and repeat using Team Explorer.
Multiple Stages using Same File using Command Line
First, I’ve written my code to be in a “baseline” state, e.g.
Next, I’ll add a new line
and via the command line, I’ll run git add Program.cs (or git add . since it is the only file modified) to add it to staging.
Running a git status confirms it’s staged to be committed.
Don’t git commit yet! Let’s stage another changes to the same file.
Back in Visual Studio, let’s add a Console.ReadLine() to make sure the console window doesn’t go away…
Now let’s run git add Program.cs again. Running git status confirms it’s still staged to be added.
Now let’s commit by running git commit -m “two changes added”
Now let’s rinse and repeat using Team Explorer…
Multiple Stages using Same File using Team Explorer
First, let’s get back to our baseline of just having Hello World. And I’ll hit Commit All to commit those changes.
Next, let’s make that first line change of Console.WriteLine(MyStrings.message); but this time instead of hitting Commit All, you want to right click and hit “Stage”
Now you’ll see the change has been staged in TE.
Now let’s add our Console.ReadLine() line…
and stage that commit…
and now under Staged Changes (1), you can right click and hit Compare with Unmodified to see that the 2 new lines have been added to the staging area.
Clicking Commit Staging in Team Explorer will now add these two new lines to the log.
And now checking the history (Status Bar – Click Master (or whatever branch) then View History shows the two commits (baseline and the two new lines).