Tag: visual studio

How to do a fast forward merge in Visual Studio – 092

Yay!! I’m over 25% of the way into this series!! Which also means OMG is 2017 already 25% over yet?!?!

Today’s tip demos how you can do a straight forward merge in Visual Studio. No conflicts, etc.

Suppose you have have a method called DoNothing() that lives in a donothing branch.

1. Switch to the branch you want as a target

Double-click on the branch name. The bolded name will be the target branch. Checking out the target branch mimics Git command line functionality.

master branch checked out and bolded

2. Right-click the target branch and select Merge From…

Merge From... context menu

3. Click the merge button

We’ll get to what the checkbox means in a bit. As if I could stay away from blogging about a checkbox in VS!  =D

Merging donothing into master

And if all goes well, Team Explorer will give you a message

Repository updated to commit id in Team Explorer status bar

We’ll cover merge conflicts later.

Note this was a fast forward merge because there was nothing to merge, so there’s only the linear graph. A fast-forward is where Git realizes that you could have done each commit on that target branch, so it makes it look like that in graph form.

linear graph in commit history

To visualize what we’ve done in Git, here’s the setup. We have one commit in donothing that’s ahead of master.

branch donothing one commit ahead

Now we checkout master and do a git merge

git merge from donothing into master

Note how the tool assumes it’s a fast-forward merge. The tool must assume this because the tool doesn’t have a concept of a working directory or staging, so it can’t diff any files to detect conflicts. So the tool assumes there are no conflicts, hence a fast-forward merge.

We’ll look at non-fast forward merges next. 

How to fix the "I’ve started making changes on master. How do I get those to another branch?" using Visual Studio – 090

For 20 years, Visual Studio users have started their workday by launching VS and start coding. Now we have to break that “muscle memory” by stopping to think, “Hmm which branch am I on? Do I need to switch branches first?”

You are allowed to switch to a branch, provided that branch doesn’t already have different changes to the same file. You’ll see the happy case, then the #sadtrombone case.

Help wanted:  This tip is only for saved changes, *not* committed changes. I don’t know how to undo the “oh shit! I’ve committed on master!” *from within Visual Studio* without dropping to the command line. If someone knows, please share in the comments or on twitter! aka please get my attention – references to Samurai Jack seem to work very well 🙂

Happy Case

Suppose you have a Console Application and you need to add a new file called IDoNothing.cs – yep, how I roll.

IDoNothing.cs being added in Changes window

Note that I haven’t committed anything else. These files are only saved.

Team Explorer will still allow you to switch branches. Go to Team Explorer – Branches, and switch to the desired branch.

donothing branch switched to

Then you can commit your IDoNothing.cs file into the donothing branch.

#sadtrombone case

From a previous tip, we’ve made changes to the output in our decorations branch. So let’s switch back to master and make some changes to Program.cs, e.g. adding a new method call.

DoingNothing() method added to Program.cs

We’ve saved the changes, but haven’t committed them. Now when we try to switch to decorations branch we get an error message.

error message: cannot switch becaues uncommitted changes

Yes, that reminds me of the old joke about the helicopter that’s lost over Microsoft HQ. When they the folks on the ground where they are at, they hear, “You’re in a helicopter.” The pilot says, “Thank you!” and plots a new course. The passenger says, “WTF? How do you know what to do after that answer?” He said, “it’s clearly MSFT. You ask a question and get the most technically accurate, but yet not really useful for the given situation answer back.”

Yes, true, you cannot switch because of uncommitted changes, but why is this different than before? Because decorations branch already has a different Program.cs file than what’s in our current branch (master).  In the happy case, by adding a new file, I knew there would be no conflict (and given it’s a Console Application, i don’t have many options for a demo 🙂 But when modifying Program.cs, there’s a conflict, so VS says to see Output Window for details:

> Cannot complete the operation because of existing changes to the following file:
    ConsoleApp1/Program.cs

Now that’s a useful answer! 🙂

How to commit and visualize code on different branches in Visual Studio – 089

Yesterday’s tip visualized what happens in Visual Studio (and in Git) when you create a branch. Today’s tip visualizes committing code independently in different branches.

First, you’ll see how to commit to separate branches in Visual Studio, then we’ll rinse and repeat in the Git Visualization tool.

Committing to Different Branches in Visual Studio

In this example, I’ve rolled back to the Console Application with only output message. We’re going to add the ChangeColor() method and call on a branch called addColor.

ChangeColor() committed to addColor branch in Team Explorer

Once you’ve pressed Commit All to addColor, you’re going to discover that you want to experiment with decorations. You can use Team Explorer – branches to create a new branch called decorations.

You’ll want to use master because you want to keep the addColor changes separate from your decorations branches.

Creating a new local branch from master in Team Explorer

and you’ll want to verify you were on master in the dropdown

creating branch from master called decorations

Add your decorations to your project file

adding more output decoration text

And now commit those changes to your decorations branch.

committing to decoration branch

And now we have 2 different commits on 2 different branches

3 branches in Team Explorer

Visualization of what we just did

First, we’ll want to create and checkout a branch called addColor and make some changes there.

git commit, checkout -b addColor, commit visualization

Next, we’ll want to switch back to master and create a new branch from there.

git checkout master, git checkout -b decorations visualization

Finally, we’ll make a commit on the decorations branch.

git commit on decorations branch visualization

I keep trying to remind myself that whenever I do a checkout, reset, etc. I’m not really going forward and backward in time, but rather I’m moving to a different nodes on a graph. Visualizing what git is doing helps break my timeline mental model.

How to create a branch in Visual Studio – 088

It seems that I have to write these tips in triplicate: 1. command line, 2. Visual Studio, 3. git visualization tool. But that’s been the only way to prove to myself I’m grasping the concepts.

Command line

A college French professor once gave me the advice to never use contractions in class unless I was prepared to never ask him to slow down. I had just learned the equivalent of “I do not know” vs “I don’t know” (something like that).

Applying that advice to software, I don’t want to start using git shortcuts by combining commands until it is clear what the two commands are independently doing. (Yep, I’ll show the shortcut in a second… )

First, you’ll want to create a branch:

> git branch my-branch

Next, you’ll want to switch to that branch:

> git checkout my-branch

git branch addColor; git checkout addColor

Git Visualization

Okay that’s pretty straight forward, but what’s happening conceptually?

git visualization of creating and switching a branch

We are on master when we created a branch called addColor while on master and then switched to addColor.

The take home message is that addColor has everything that master has because we created the branch addColor while on Master.

Git Command Line Shortcut

Before we jump into the IDE, let’s take a sneak peek at that shortcut.

Note: in case anyone is following along at home, I first switch back to master to delete the addColor and then recreate using the shortcut.

The shortcut is

> git checkout -b addColor

deleting branch and then recreating using the shortcut

This shortcut says to checkout to addColor and if it doesn’t exist, create it.

Visual Studio

When you’re in Team Explorer, you can go to Branches, right click on the branch you want your new branch to be based on, right-click, and select New Local Branch From…

New Local Branch From... command in Team Explorer - Branches

Then give your new branch a name (and verify in the drop down you picked the correct branch) and leave the checkbox checked…

checkout branch option in Team Explorer

If you have the Checkout branch checkbox, you’re telling VS to create the branch and do the checkout so you’re now on the addColor branch instead of master.

If you uncheck it, it is the equivalent of creating the branch on the command line, but staying on master.

To confirm you’ve created and switched to the addColor branch, you’ll see that addColor is now in bold.

branches showing addColor now checked out

And the branch is also shown in the Visual Studio status bar.

addColor from status bar

How to revert changes in Visual Studio – 085

Yesterday’s tip talked about how to use `git revert` from the command line. Today’s tip describes the functional equivalent in Visual Studio.

TBH I clicked the wrong command in VS initially when I started writing this post. I clicked “reset ” then the “–hard” option, which should have been a hint. Revert doesn’t have the soft, mixed, or hard options. But, I was able to fix my git history (by going to the command line) to write this tip without deleting my .git folder and starting over, which is a first for me. So perhaps all this work writing out these tips is working!

Let’s say you have a console application that shows a blue background. And you’re like “no.”

console app with blue background

You want to remove this commit altogether. If you’ve been committing in small, atomic chunks of code, you should be able to revert this background color change. But don’t take my word for it. I wouldn’t know because I’m still trying to train myself to do small commits.

Going to the history shows where this change was introduced, i.e. “added ChangeColors()”. You can revert this commit by right-click and selecting “Revert.”

History - Revert on selected commit

Click Yes on the confirmation prompt.

If you refresh History, you’ll see the new commit.

Revert "added ChangeColors()"

You can double-click to open that commit’s details. If you edit the commit message, the Amend Message option will become available.

reverted commits details

Double-clicking on the Program.cs file listed under Changes for the Commit Details pane shows what’s been removed or “reverted” from the codebase.

Changes for Program.cs showing the ChangeColors method removed

And to verify we are back to our familiar console application background, let’s run the project.

default black background for console app

How to use the VS status bar buttons as a shortcut to Team Explorer panes – 083

Something new in Visual Studio 2015 and still there in Visual Studio 2017 is the ability to click buttons(!!!) in the status bar O_O.

You can switch branches without having to touch the Team Explorer pane.

branches button shown in status bar w list of branches to checkout

You can jump to the Connect pane, which will save you time when you’re debating “is it the Home icon or the green Plug icon?”

repo button in status bar showing Connect pane

Yeah, I guess I need to get around to deleting deletemetoday one of these days…

You can jump to the Changes pane, regardless whether you have uncommitted changes (or staged changes as shown in my screenshot – yeah SDET skills die hard).

This button won’t automatically commit your changes, but only takes you to the pane.

pen or pencil icon button for Changes pane

And last but not least, you can jump to the Sync pane. Again, it doesn’t perform any commands other than just navigating to the pane.

up arrow button for Sync pane

Looks like you can click the Line, Column, and Character status bar “buttons” and new UI appears for Visual Studio 2017!! (provided you have a file opened).

Must focus on Git and GitHub tips
Must focus on Git and GitHub tips
Must focus on Git and GitHub tips
Must focus on Git and GitHub tips
Must focus on Git and GitHub tips
Must focus on Git and GitHub tips
Must focus on Git and GitHub tips
Must focus on Git and GitHub tips

Okay, it looks like if you double-click on any of those status bar buttons, you’ll get a new Go To Line dialog, which seems to be part of a global search window…

new Go to Line window in VIsual Studio 2017

but it doesn’t seem to let you specify columns or character positions.

Yeah, old habits die hard, even when trying to focus.

How to find when a line was last changed in Visual Studio – 080

Previously, I mentioned how to use Blame on GitHub.com to find the commit that changed a specific line. Today we’re going to rinse and repeat this scenario within Visual Studio.

I told think anyone ever realized how scared I was of the world (if not plain neurotic) as a small child. I remember once in first grade (6 years old) being asked by the teacher for the answer to a multiple choice question. I thought it was ‘D’ but the answer was ‘C’. When the teacher said no that was wrong, I broke down sobbing from fear and embarrassment. I didn’t know what was going to happen to me for getting it wrong. I thought perhaps I’d get into serious trouble when I got home or lose my recess or something. Unfortunately, the teacher completely misread why I was crying (she thought I was acting out for attention) and scolded me, etc., which was my first foray into the self-fulfilling prophecy*.

I share this story because the first time I saw “Blame” on GitHub, I felt the same emotion as I did as a little kid. Why would I ever want to put myself into a position of having people call me out publicly via my code and blame me? But as I said in my previous post, Blame doesn’t mean “to imply blame” but rather “look up the commit that changed this line of code.”

I was happy to see this command referred to as Annotate in Visual Studio 2015, but a bit saddened to see now called Blame (Annotate) in Visual Studio 2017. I get it. I get it. When in Rome… but I often muse what would be the equivalent of commercial airline pilots who have to reskill like we do in the software industry. Would they start lowering the landing gear prior to take off? Would they refer to Air Traffic Controller as Air Traffic View or Air Traffic Model? (see what I did there? #PointsMe!)

In today’s scenario, I want to know two things:

  1. When was a specific line of code changed? as in, which commit changed a given line of code?
  2. What changed in that line?

1.  open the file that contains the line in question (either via Double-Click in Solution Explorer or File – Open – File provided you are connected to the repo in Team Explorer)

2. anywhere in the file, right-click and select Source Control – Blame (Annotate)

Source Control - Blame (Annotate) in context menu for file

The Blame / Annotate window will appear on the left hand side. This window shows for each line what was the last commit that modified it.

Annotated window shown for a given file

In the above example, lines 1-2 were last modified by commit ID 44af5057 (most likely my initial commit). But line 3, the one I’m interested in, was last modified in commit fd80ad89.

As any long time Visual Studio user will do… let’s click the fd80ad89 and see what happens!

Commit details for the last commit that modified line 3

Seeing the Commit Details appear in Team Explorer answers the part one of my scenario – the when. Now it is time to figure out the what – what was changed in the line…

TE - Commit Details - Changes - main.js - Compare with Previous

By clicking on the Compare with Previous… command in the context menu for the main.js file listed in the Changes list for that commit, you’ll see the file diff view appear.

"Tray" shown added to list of const

And now we  have “the what” for what has changed in the file. The “Tray” const was added to the list, giving it a type of Electron.Tray, so I could put the app in the Taskbar tray (or Mac tray thingy at top of screen).

*Sometimes I wonder if I’m the reincarnation of Rod Serling (technically, he died before I was born) or a zen master looking for a challenge. But then again, I’ve always hated smoking, so I’m not the former. And during the Visual Studio 2017 Launch Event when they were showing my Happy Birthday Visual Studio video, I was at home yelling at the Yoga DVD Instructor over the “share  your breath with the community” breathing stuff, so it can’t be the later either.