Tag: git

How to undo all the changes to a specific file in your working directory using Visual Studio – 070

I always love Git documentation that includes warnings about destructive commands (like this one) because there’s no way to get this code back (aka a data loss scenario), unless the code happens to be in your favorite editor’s buffer or you still secretly use Ctrl+C Ctrl+V for emergencies – DONT JUDGE ME PEOPLE! =p For example, from unmodifying a modified file

It’s important to understand that git checkout -- [file] is a dangerous command. Any changes you made to that file are gone – you just copied another file over it. Don’t ever use this command unless you absolutely know that you don’t want the file.

I think to myself, “Um, Git, you know the code I’m writing… there’s a reason I never want to speak of this code again!!” It seems Git was designed for people who never have to StackOverflow with cut and paste attempts just to get something to compile and yet forgot to try on a throwaway branch first. It’s once I get it to compile I want to start over as if to convey in my logs, “yep I knew what I was doing all along…”

tl;dr – Today’s Git tip of the day is when you’re feeling, “um, yeah, let’s pretend that never happened.”

Undo changes in specific file in working directory using Team Explorer

In Visual Studio, let’s add a few lines of code in our Program.cs project and hit save. You’ll immediately see Team Explorer telling you that you have unstaged changes in your working directory. Note that I say “unstaged changes” and not “uncommited changes.” Remember, even though Team Explorer lets you jump over staging, there’s still a “middle layer” as seen in the past couple of tips.

Right-click Program.cs and click Undo.

Undo Changes command in TE

Now you’ll be prompted “are you sure” because like the Git documentation says, you won’t be able to get these changes back.

Are you sure VS prompt

Now we’re back to before we made any unstaged and/or uncommitted modifications. Even VS seems to know to pop these changes from its clipboard ring. Wow, I feel for whoever had to test those scenarios!

Undo changes in specific file in working directory using Command Line

I believe this is the command line equivalent of running git checkout — <filename> although I’m not sure what the actual underlying plumbing Team Explorer is doing.

If we want to rinse and repeat from the command line,

First, make some edits in Visual Studio and hit save. We can confirm these changes by doing a git status

git status shows Program.cs modified but not staged

Next we’ll run the dangerous (aka data loss) command git checkout — <filename>

 git checkout -- Program.cs

Although Git is like “Meh”, Visual Studio grabs your attention immediately with the old-faithful FILES HAVE CHANGED FILE HAVE CHANGE OMG FILES HAVE CHANGED WHAT DO WE DO?!?! dialog.

Files have chnaged. Reload in VS? dialog

Since we made these changes from the command line, Visual Studio needs to know what to do with the current text buffer. In this case, we purposefully made these changes, so we want to say Yes to All.

And we’ll see that we’re back to our baseline code and no more changes being tracked in Team Explorer.

There are no unstanged changes in working directory in TE

and Git from the command prompt confirms it.

nothing to commit, working tree clean

Aside: What if you had said “No” or “No to All”?

Well, I guess Git won’t be aware of what state your file really is in. If you try to git add or git commit the untracked changes in Program.cs, Git will ignore you. Literally. Git says “nothing to commit, working tree clean.” Team Explorer will also do the same by not giving you any options. But as soon as you hit Save in VS (even if the file is already saved) or even type another character in the text buffer, Git picks back up on the changes.

I guess the take-home message is to hit Yes to All. If you have a scenario where you’d want to say No to All in such a situation (perhaps making this “less dangerous” although there are many, many better ways), please let me know!

How to stage multiple changes within same file using Visual Studio – 069

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 MenuStage 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.

only Console.WriteLine("Hello World") showing

Next, I’ll add a new line

added Console.WriteLine(MyStrings.message);

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.

changeds to be committed: modified Program.cs

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…

added Console.Read();

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”

git log shows two commit histories as expected

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.

back to baseline commit in TE

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”

clicking stage in TE

Now you’ll see the change has been staged in TE.

Staged Changes (1) in TE 

Now let’s add our Console.ReadLine() line…

Console.ReadLine() added

and stage that commit…

Staging a change to an already staged commit in TE

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.

diff between the baseline commit and the 2 changes staged

Clicking Commit Staging in Team Explorer will now add these two new lines to the log.

Commit Staged

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).

history in Team Explorer showing last two commits

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.

How to make quick edits in the diff view in Visual Studio – 067

I always assumed that if you’re diff’ing the working directory against the last commit (i.e. Compare with Unmodified), both sides of the diff would be read-only. Turns out, only the last commit (left-hand side) is read-only.

For example, suppose you’ve made some quick edits in Program.cs (see yesterday’s tip), and you’ve opened the diff in the Editor by right-clicking and clicking Compare with Unmodified, you can still make edits on the right-hand side.

Added a new line of code on right-hand side

In this example, I’ve added a Console.ReadLine(), as anyone who has ever created a throwaway console app knows why. (I thought at one point in VS 20 year history if you switched from debug to release, the console app persisted until you closed it, but it looks like that’s no longer the case)

As soon as you hit save, you can confirm via the command line that Visual Studio is being honest that you can edit changes on this right-hand side.

git diff confirming changes to right-hand side