Tag: visual studio

How to use Visual Studio as your external merge tool – 106

Hmm, I wonder what could possibly be my tip for the day after yesterday’s tip!

In yesterday’s scenario, you saw how to use VS to do a diff. In today’s tip, you’ll see how to do a merge when you do `git merge branch-name` and get into a merge conflict. Hopefully you’re feeling a bit more confident when you see merge conflict now. (Yeah that’s actually my hope for myself.)

First, go to Team Explorer – Settings – Git – Global Settings and for Merge tool, click Use Visual Studio

Merge Tool: Use Visual Studio

Suppose you have a file in one branch (mine is called readme) that contains a conflict. When you go to merge into master, you’ll see a merge conflict.

You can open the mergetool by running

$ git mergetool

git mergetool prompting to open in VS

Follow the instructions and hit Enter.

Now the VS Merge tool appears

VS merge tool

Clean up your readme.txt in the bottom part of the screen (or however you want to use the merge tool).

Now we’ll see that we have changes in our working directory.

need to do git add to stage changes from merge

You can do a `git add readme.txt` to add this file to staging.

file added to staging

And now we commit our merge commit via `git commit -m “merged readme”`

committing our merge commit

and the `git log` confirms our merge.

How to use Visual Studio as your external Git difftool – 105

Thanks again to Ed Thomson’s Git for Visual Studio O’Reilly course! I searched for a solid hour how to manually configure VS as your external diff and merge tool. I knew I had seen it somewhere. Then I remembered Ed’s course!

Go to Team Explorer – Settings – Git – Global Settings. Then for either difftool or mergetool or both click Use Visual Studio

Diff Tool: Use Visual Studio

Suppose you have at least one commit and you’ve started making changes to that file in your working directory.

Now you can do

git difftool

which will bring up a prompt asking you to confirm launching your external diff tool vsdiffmerge (the tool VS uses).

Press ‘Y’

git difftool prompt

And now VS will launch and open with the diff tool showing the original Source (i.e. the file in the last commit) vs the changed file Target (the one you changed in the working directory)

I wish I was a messenger and all the news was good - diff in VS

BTW the reason why the tab is shown on the right side of the file tab channel in VS instead of the left is because this is a temporary file that isn’t listed Solution Explorer. Back in the day, you’d have to add this to the miscellaneous project, but not sure how that all works today. But I digress….  🙂

So let’s see what the Use Visual Studio link did by using these two commands

$ git config diff.tool

$ git config difftool.vsdiffmerge.cmd

git config diff.tool and git config difftool.vsdiffmerge.cmd

And the `difftool.vsdiffmerge.cmd` line is the magic line I was hunting the Internet for. Thanks Ed’s course!

How to fix the oh no! I committed on master from within Visual Studio – 100

Previously, I’ve blogged about how to get uncommitted changes off of master onto a new branch and I’ve blogged about how to get committed changes off of master onto a new branch from command line. Today’s tip fixes the oh no! I’ve been committing on master this entire time from within Team Explorer, thanks to blog reader Luke Kolodziej

Let’s say you’re motoring along and you realize that you’ve been checking into master (or some other wrong branch) this entire time.

history of master branch showing oops! commits

Don’t delete your .git folder! There’s a better way!

First, create a branch but do not check it out!

Go to Team Explorer – Branches, then Right Click on master and select New Local Branch From. Now in the TE window, give your new branch a name, but uncheck the checkout branch.

Checkout branch unchecked

Click Create Branch.

Now you’ll go back to your History – Master tab and do a Reset – Delete Changes (–hard) on 2nd to last commit. We’re defaulting to –hard because we have no uncommitted changes in working directory or in staging.

Reset - Delete Changes in master on previous commit

Now hit Refresh on the History – Master tab and you’ll see that your commits are no longer on master.

I need to be on my own branch commit gone from master

But where did this commit go?

Remember the visualization from the previous tips. Just because we rolled back the HEAD pointer doesn’t mean the commit is lost. Remember git reflog holds the truth!

Okay, now if you switch to the newline branch (e.g. using the status bar button at the bottom right), and going to View History (either from Branches – Actions – View History or from Status bar – branches list – View History)

I need to be on my own branch still on newline

I still to this day feel freaked out that this works. One day I’ll be able to conceptualize master, HEAD, branches, etc as pointers versus the other way around (i.e. commits suddenly disappearing if they don’t show up in the git history). One day.

How to stay cool when Debug – Start is disabled and Select Startup Item is shown instead in VS 2017 – 099

Another What? What? What? moment for me was when I used Team Explorer to connect to a solution-based repo (and I “opened” the solution), but Debug – Start (F5) was disabled. Instead of Start, I saw the following “Select Startup Item…”

Select Startup Item

Stay cool honey bunny*

It took me a bit of time to figure this one out, but what happened was when I “opened” the solution (the Open… link to the left in the image), I really opened the Solution Explorer – Folder View

Show Folder View link

This Folder View feature is awesome when you have a repo that doesn’t contain a solution, but you want to use VS to edit the files and use TE to do your Git operations. For example, you can clone Your GitHub Moment of Zen app as a non-solution based repo. It’s an electron app, meaning it’s javascript.

But let’s say you have a legit sln-based project, but you clicked Show Folder View instead of Open… meaning you’re in the Folder View state

Folder View state in Solution Explorer

Sure you could open the solution via File – Open – File, or you could click on the Solution Explorer toolbar button dropdown and select the desired .sln file. I’m assuming you could have more than one solution file in your repo at the base level, because why not?

dropdown for opening a solution in SE

And volia! You’re solution is now opened in Solution Explorer. No more “Folder View”

good old Solution Explorer

And of course you can switch back to just folder view using the same dropdown button.

switching back to folder view

*I’ve heard that the kids graduating from college (COLLEGE) don’t know about Back to the Future. :swoons: I just hope they know about Pulp Fiction.