How to view changes to your working directory in Visual Studio (and via command line) – 064

Yep this might seem trivial, but two things: 1. I discovered something new (see tomorrow’s tip) and 2. I’m playing with formatting of VS vs CLI

How to in Visual Studio

In Visual Studio, if you want to compare your current changes (i.e. changes in your working directory) to those in your last commit (where HEAD is at), you can right click on the file in Solution Explorer, in the Editor, in Team Explorer, aka practically anywhere, and select  Compare with Unmodified…

Compare with Unmodified

This command will open a diff of the file at HEAD and the file in the working directory,

Program.cs;HEAD vs. Program.cs

where you can see the changes in the diff side-by-side, where the Console.WriteLine was added.

Diff showing Console.WriteLine() message being added

How to via Command Line

Here’s how to do the same via the command line. Open the command prompt and navigate to your solution.

PSA: If you don’t have the Productivity Power Tools installed, go get them! Then you can right-click Program.cs and select “open in command prompt.”

To compare changes from your working directory (i.e. what you haven’t staged or committed yet) to your last commit (where that HEAD pointer thingy is at), run the following command

git diff

and now you’ll see the equivalent diff in the command line interface

command line showing "git diff"

How to interpret the blue pad lock icon when using Git in Solution Explorer – 063

This blue pad lock icon got me the first time I opened a Git-based solution. At first I thought, “wait a sec… I just created this repo and solution, why are these files locked??” Anyone who has used Visual Studio for a while knows what I’m referring to.

tl;dr – when using Git, the blue pad lock icon means “file is unchanged,” but why didn’t they update the tooltip? In TFVC, SlnExp uses “Checked-in” but in Git, SlnExp uses “Checked in” (no dash), so why not change the words?

For non-windows readers who are interested in what TFVC (a centralized VCS) looks like or those who want more context, here’s a brief overview of what a Solution looks like under Team Foundation Version Control (aka TFS, aka VSO, aka VSTS, etc.)

Using Team Foundation Version Control in Solution Explorer

When you first create a project, you’ll notice that the files have a green + icon, indicating that this file is newly added, but not checked into source control. 

Newly added file to TFVC

And after you’ve check-in your new files,

Team Explorer - Pending Changes window showing "check in"

you’ll now see the blue pad icon

Checked-in icon showing in Solution Explorer

where you can now Right Click to “check out” your file or just simply start editing to “check out” your file.

Checked out to me icon in Solution Explorer

Using Git in Solution Explorer

Let’s pause for a second and consider the workflow of Git being DVCS. So “check in” and “check out” states don’t directly map. But Visual Studio users are used to icons have certain meaning. My guess (and this is just my guess, considering I haven’t been on the team in ages) is that they went with the existing set of check-in and check-out icons, hence mapping their semantic meaning to the Git states. Sure, it’s a little strange at first (as mentioned above), but then you adjust and go on with your life.

For example, this is a Git repo with `git status` that has nothing to commit and working tree clean.

git repo saying "checked in" in Solution Explorer

So although we’re supposed to interpret this as “unchanged”, it’s interesting that the tooltip doesn’t have the dash between Checked and In. At first when I started writing this blog post, I figured SlnExp was using the exact same icons and tooltips, but clearly something is different in a Git repo. I tried having only changes locally versus pushing up to GitHub, but that doesn’t add the dash back.

So despite the missing dash, I’ll adjust and go on with my life 🙂

How to create your own local Git remote repo that’s not hosted on a Git server (–bare option) – 062

I highly recommend Ed Thomson’s Git for Visual Studio O’Reilly course. It’s one of the several training courses I’ve taken to put together this series. In fact, stop reading this series and go watch Ed’s course now!

Ed mentions that you are not required to clone from a Git server. He shows how he could clone from F:\shared.git, where the F: drive is where he mounted a UNC share. This got me thinking… what is `shared.git` (is it a file, folder, etc?) and how do you create or get one locally.

After a bunch of SO’ing and reading several answers, I’ve learned that you can create your own local remote repo (I seriously have no idea what this type of repo is called. bare repo? Help me out in the comments if you know.)

1. Create the shared.git folder

This `shared.git` thingy is actually a folder. You can created it by creating a new folder somewhere but calling it `shared.git` as the folder name, e.g.

shared.git as a folder name

2. Create a bare repo inside shared.git

cd inside the shared.git folder and run the command

git init –bare

This command creates a bare repo, which apparently is a repo without a working directory, meaning you can’t do something like `ls` to see the contents of the git repo. But clearly it has all the underlying plumbing when you do a `ls –la` on the folder’s contents.

command prompt showing creating and viewing contents of a bare git repo

3. Create a local git repository to contain your actual files, like you normally would

I’m going to head back over to my \Source\Repos directory and create a new local git repository using the command

git init

command prompt showing git init a new repo

And I’ll create a README.md file with some content and add/commit it to the repo, as you’ve done a 100 times by now.

command prompt showing adding a readme file to a new repo

4. Push your changes to your bare remote

Now it’s time to pretend that your shared.git repo is like all the other git repos you’ve ever interacted with that is hosted on a server.

Remember your training. The command is `git push server-name branch-name`, so in my case, I’ll run

git push ~/Documents/shared.git master

Note I’m not passing in a `username@server` because I’m using Git Shell, and that’s been taken care of for me. 

pushing your changes to a local bare git remote

Remember we can’t do a `git ls-files` here in the shared.git folder because it is a bare repo and has no working directory. However, we can note that the objects folder has had its timestamp updated.

command prompt exploring contents of shared.git

5. Clone from Visual Studio

All that setup! Now, let’s get back to Ed’s example…

Now I paste in the location to my shared.git remote.

Team Explorer cloning from shared.git

and now hitting clone we’ll see an info message at the top of Team Explorer indicating that the repo was cloned successfully.

Double-click to switch to the newly cloned shared.git repository.

Double click in Team Explorer to switch repositories

Note Visual Studio is just going to look at you because there’s no solution file to open. That’s okay. We can old school open that readme via File – Open – File and selecting the readme over at C:\Users\saraf\Source\Repos\shared (or wherever your default repo location is at.)

And volia we see the readme.txt file open in Visual Studio showing our “hello world” contents. (Hello Visual Studio File Tab Channel my old friend. I’ve come to blog about you again.)

readme.txt file opened in Visual Studio

6. Go Explore!

Now you can go explore and play with your newly created local git remote thingy (I’ll update name when I find out what it is technically called). I’ll leave it up to you to make changes here in VS, push those changes back up (or over?) to shared.git, and then use your original `firstrepo` repo to pull down any changes from Visual Studio.

And I mean it when I say check out Ed’s excellent Git for Visual Studio videos.

How to change the default local location for your Git repos in Team Explorer – 061

By default, Team Explorer sets your default location for your Git repos at \Users\<username>\Source\Repos, as seen in yesterday’s example

Local Git Repositories path in Team Explorer

If you want to change this location, perhaps to remove the Source directory from the path to save you a few extra clicks, you can go to Team Explorer – Settings

Team Explorer - Settings

and then click Git – Global Settings

Git - Global Settings

From there you can change the path.

updated default repository location

Notes:

  1. This change only affects new cloned projects moving forward. Your previously cloned repos will remain in their original directory.
  2. GitHub Desktop uses a different repo (\Users\<username>\Documents\GitHub) than Visual Studio. This change in Team Explorer doesn’t change the default location in GitHub Desktop. If you want both GitHub Desktop and Team Explorer to use same location, you’ll need to change the default location in GitHub Desktop as well.