Suppose you’ve added a new file to your solution in a branch called `experiment` for experimental purposes. You are happy with this new file and want to merge this back into master. Well, let’s say you (or someone like you*) made a change in master.
Since this is on your local machine, you can decide “do I want to have the history of the experiment branch or do I want a clean, linear timeline?” Guess which adventure you’re going to choose today 🙂
Here’s the visual setup.
Notice how the tip of experiment has a commit that’s not in Master, and vice versa, the tip of master has a commit that’s not in experiment.
First, we’ll use Team Explorer to rebase experiment unto master. BTW I love how Team Explorer uses full sentences to confirm your intent.
Now after the rebase, you’ll see two things:
- The commit from master is now shown in the experiment branch (recall the visualization from previous posts – the master pointer is now behind experimental pointer)
- The commit ID for “added experimental code” has changed (You can compare to the first image in this blog post to verify)
And lastly, let’s get master caught up to experiment by doing a merge. Yay for full sentences in the below image!
And now when you refresh Master history, you’ll see the changes.
*I couldn’t resist. I listened to way too much Matchbox 20 in my youth, “I wish the real world would just stop hassling me .”
Following yesterday’s tip, you can also add a custom Git command as an external tool.
For example, my output window shows the output of `git log –oneline –graph –decorate`
Go to Tools – External Tools and add a custom tool similar to the screenshot I have below, where Title = `git log`, Command = `git.ext`, Arguments = `log –oneline –decoreate –graph`. If you click the Right Arrow button to the right of Initial Directory, you can choose where the current directory is for git commands. I’m using Current Solution $(SolutionDir) which is an internal variable to Visual Studio.
After you add it, you’ll see your command appear under Tools menu next to External Tools.
You can add Git commands to your main menu bar in Visual Studio.
For today’s example, you’ll see me add a Git menu with a command to open Git Bash in a command prompt.
Part 1 – Add Git Bash as an External Tool
I promised myself I wouldn’t blog about VS tips in this series. I’m sure if you searched you’ll find my old posts how the External Tools works 🙂
Just note the order in which Git Bash is listed above. In my example, it is External Tools 3. This will be needed in a second.
Part 2 – Create a Git menu in VS
Same story as above. I’m certain you can find my ancient posts, so here’s the quick version. Go to Tools – Customize – Commands
Then click Add New Menu and click Modify Selection (yeah, that’s some ancient UI) to give it a name, e.g. Git, and an icon and such if you want.
Lastly, switch the Menu bar: to Git (or whatever you called your menu). Click Add New Menu and select Tools – External Command <your number from above>
And you should be good to go!
Thanks again to Ed’s O’Reilly course on Git for Visual Studio for the inspiration!
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.
and volia Visual Studio appears**
* 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 🙂