Tag: visual studio

How to diff a file between two branches using VS as your difftool – 107

Suppose you want to diff a specific file between two branches. The best* git command I’ve found is in this SO answer (only because I’ve found a couple of other sites reference it)

$ git diff branch-name path/to/file

The SO answer says to use `..` in front of branch-name, but I’ve found it works without it. There are WAY too many variants of these commands to keep track of.

I’d love to figure out where this command option is in the official docs but it is pretty overwhelming list.

In a previous tip, you saw how to use VS as your external diff tool. As the blog post title describes, here’s how to do it

$ git difftool branch-name path/to/file

for example, the name of my branch in the image below is `readme` and my filename is `readme.txt`.

I don’t have to provide a path to the readme file since it is at the same location as my current directory.

image

and volia Visual Studio appears**

diff of two different pearl jam song lyrics across branches

* I have no idea if this is the best answer. I’ve read that there’s old syntax and new syntax. I’d love to hear people’s suggestions in the comments!

**I’ve noticed in VS 2017 that VS must be running. Otherwise, the IDE is launched, but the tool doesn’t appear. If you re-run the command via command line, the dif will appear. YMMV

And yeah, I wonder what genre of music I listen to when I write 🙂

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.

How to not quit your career when Git (via Visual Studio) adds <<<< HEAD into your code – 094

I studied Shotokan Karate for 20 years. At the start of each class, you’d say the Dojo Kun. At the beginning, I viewed the sayings as the rules of engagement, similar to FIFA laws. But over the years and decades, it starts to bleed into your subconscious as a way of viewing other situations in life. The most important of these rules is the last, “Refrain from violent behavior.”  The idea is you’re supposed to avoid any fights, etc. at all costs. Because of this philosophy, I have argued that karate is really the study of non-violence. I could go on and on about the importance of the only winning fight is the one that isn’t started, there’s no such thing as a first punch in karate, and so forth.

The first time I saw <<< HEAD and >>>> branch-name edited into my code*, my coffee mug went flying across the room.

Today’s tip is a walkthrough of resolving a merge conflict from within Visual Studio. We’ll cover command line in upcoming tips. Must… pace… self…

Suppose you have some existing output text in our ConsoleApplication in master. And you’re going to merge in text from a branch called decorations that also includes output text. Because both branches have changes in the same section, you’ll get a merge conflict. Let’s see how Visual Studio 2017 handles it.

First, do everything as shown in previous tips…

merging from decorations into master

But this time when you hit Merge, you’ll see the following

Merge in Progress - Conflicts: 1 message

Take a deep breath.

Now take another deep breath.

Resist the temptation to “fix” your code by removing the <<< HEAD and >>> decorations modifications. Let your build errors light up like rush hour traffic in your VS code margin gutter thingy.

Now click Conflicts: 1

You’ll be given an option to either Merge or simply take either the code from decorations or master w/o “merging”

Merge button for a conflicting file

Clicking the Merge button will take make use of that <<<< HEAD and >>> decorations gobbledygook by presenting you with a merge tool (in this case presented within VS).

Suppose you want both changes. You can click the checkboxes on both sides and you’ll see the result in the bottom screen.

VS merge tool

Notice how the resulting merge shows the Console.WriteLine() at the wrong location, which makes sense. The two code blocks were merged in a way that makes sense.

The bottom window is editable. A quick cut and paste puts the WriteLine() before the ReadLine() so it can be seen before the keypress to exit.

editing the resulting window screen

Hit Save and then hit Accept Merge at the top of the merge tool

Accept Merge button at the top of merge tool

And yet another button!! O_O

Now back in Team Explorer, click Commit Merge.

Commit Merge

Finally, we’re going to finish the merge by typing in a custom commit message and clicking Commit Staged

committing the merge with a custom message

And now when you view history, you’ll see the merge into master.

History - master w merge from decorations

*What I’ve realized from learning RoR and Node.js last year is that when you’re not using a full end-to-end IDE (e.g. text editors like atom, vi, sublime, etc.) you need another way to communicate changes and other sort of “metadata.” Hence the content of the code is modified, e.g. adding the <<<< HEAD directly into the file or adding a readme.md file to your repo (which doesn’t participate in the build). Once you start to know the backstory why things are the way they are, the world becomes a lot safer for coffee mugs.

How to do a non-fast forward merge in Visual Studio – 093

Yesterday’s tip was about how to do a fast forward merge, which shows all the commits still in a linear graph.

linear graph from a fast forward

Today’s tip is how to do a non-fast forward merge, meaning the graph history will show the branch being merged in

graph showing the branch merge for non-fast forward

Note: I had to use Visual Studio 2015 for this demo. There seems to be a bug in Visual Studio 2017 where the checkbox isn’t respecting local repo settings.

Let’s suppose you have a branch called test that you want to merge into master, but you want to retain the merge history in the graph.

Follow all the steps from yesterday’s tip, but today, you’ll uncheck the Commit changes after merging

image

Now you’ll get a message in Visual Studio. note: VS 2017 ignored the unchecked box and did the commit anyways.

merge in progress message

Train yourself to take a deep breath whenever you see a message from Git.

Switch to the Changes page, write a commit message, and hit commit staged.

committing a merge in progress in Changes window

And now your git history shows the merge from the branch.

merge from the branch now in the graph history

Visualizing what we did

We have a commit on the test branch we wish to merge, but retain the history.

git visualization tool showing a test branch

Now we’ll do a merge, but notice we’re using the –no-ff option, which means “no fast-forward” this time.

test being merged into master using no fast foward

Note: looks like the visualization tool requires at least 2 commits to demo the –no-ff option. Otherwise, you’ll get an error.

Does someone want to open a bug in the tool’s repository and call First! in the comments below?  I’m too tired from trying to stay awake to watch Samurai Jack. Why Comedy Central? Why did you think it was an okay April Fool’s Day joke to put some random cartoon on at the 8pm slot??  It’s only 9:47pm and I can’t stay awake to log bugs, much less make it to 11pm for Samurai Jack repeat. Or what if they don’t show Samurai Jack and repeat the cartoon?! See what your April Fool’s joke has done?!  I‘m off to bed.