Tracking Commits Across Branches with Git and SourceTree

When it comes to Git, SourceTree is definitely my tool of choice. However, I was surprised to find that there doesn’t appear to be any sort of built-in commit tracking to see which branches do and don’t contain a commit, similar to changeset tracking in Visual Studio. Now, that said, it’s pretty easy to do with Git, there’s just nothing that I could find baked into the SourceTree UI (Am I wrong? Let me know!).

So, if I need to do this, I click theĀ Terminal button in SourceTree and run one of the following commands:

git branch --contains 
git branch -r --contains 
git branch -a --contains 

The -r and -a parameters can be used to check just Remote or All (local+remote) branches.

Now, SourceTree may not have this functionality built-in, but it can be added easily with a Custom Action. Here’s how you can create a custom action to track a commit across branches.

  1. In SourceTree (Windows, 1.6.21.0), go to Tools > Options and select the Custom Actions tab
  2. Click the Add button to create a new custom action
  3. Enter a caption, and select the option to Show Full Output; for Script to run, enter the path to git.exe; and enter the parameters (note the use of $SHA)
    create-custom-action
  4. Click OK to save that bad boy

With the custom action created, you can run it by right-clicking a commit and choosing Custom Actions > Track in Remote Branches.
context-menu

If you selected the option to show full output, the branches containing the commit will be listed in SourceTree.
output

Advertisement

A caution about git branch names with ‘/’s

Git, you tricky bastard.

Let’s say I first create a branch named ‘wip/foo’:

git branch wip/foo

Git will create a file named ‘foo’ in the folder .git/refs/head/wip/. Now, suppose I attempt to create a new branch named ‘wip/foo/foo-offshoot’. git will return an error:

error: unable to create directory for .git/refs/heads/wip/foo/foo-offshoot
fatal: Failed to lock ref for update: No such file or directory

via coderwall.com : establishing geek cred since 1305712800.

Introducing Quick File Search | Bitbucket Blog

I’m glad I found this, I’ve really been needing/wanting it. In Bitbucket, you can press ‘F’ to bring up a quick-search dialog and type in any file name to find & jump directly to a file in the repository. Until now, I’ve been manually clicking through directories and hating every second of it! (But this is apparently old news. Using ‘.’ to open the Omnibar is the way to go now. Exact same idea, though!)

Introducing Quick File Search | Bitbucket Blog.

git – How to list branches that contain a given commit?

Need to see which branch(es) contain a Git commit? Try these commands!

List local branches that contain a commit:

git branch --contains <commit>

List remote branches that contain a commit:

git branch -r --contains <commit>

List all (local & remote) branches that a commit:

git branch -a --contains <commit>

via git – How to list branches that contain a given commit? – Stack Overflow.

Visual Studio Online and SourceTree

I really like Visual Studio Online, and I tend to use it for my various pet projects. It’s free for up to 5 users, and you get issue/backlog tracking and source control all in one spot. Perfect for my needs!

A few months back, we made the switch from TFS to Git at work. The Git integration with Visual Studio 2012 has proven suspect, so I’ve taken to using SourceTree for all of my source control needs, and I really like it. So, when it came time to create my next team project, I decided to go stick with Git/SourceTree in Visual Studio Online. Setup was pretty easy but not entirely intuitive, and you know what that means. Yep, we’re gettin’ bloggy wit it. I’m going to walk you through the steps of creating a new Visual Studio Online team project and connecting to it with SourceTree.

Create a Visual Studio Online Account

In order to create a new project, you’ll need a Visual Studio Online account. It’s free to get one, and it links to your Microsoft Live account. Head on over to visualstudio.com and sign up!

create-new-account

You’ll be prompted for an account URL, which will be whateveryouwant.visualstudio.com. Remember this, you’ll need it later.

Create a Team Project

Now that you’ve got your account, the next step is to create a new team project. If you browse to your account URL (whateveryouwant.visualstudio.com), you should see a link to create a new project. Click that and a dialog pops up to collect details about the new project. Note that you’ll want to change the version control option to Git.

create-new-project

Enable Alternate Credentials

Here’s the trickiest part of the setup: you need to enable alternate credentials in order for SourceTree to access the repository. Go to your user profile by clicking your name in the upper-right corner, then click My Profile.

click-my-profile

The User Profile dialog will be displayed. Click to the Credentials tab to enable alternate credentials, then enter a secondary user name and password.

enable-alternate-creds

Make note of these values, too. They’ll also be needed when you configure SourceTree.

Configure SourceTree

Okay, Visual Studio Online’s all ready to go at this point, and now it’s time to put it all together! Download and install SourceTree, if you haven’t already. Open it up and click the Clone/New button. For the source path/URL, enter the following:

https://USERNAME:PASSWORD@ACCOUNT.visualstudio.com/defaultcollection/_git/PROJECT

Note that USERNAME and PASSWORD will be the secondary user name and password you created in your Visual Studio Online profile. ACCOUNT will be the value entered for your Account URL when creating your Visual Studio Online account, and PROJECT will be the name of the Visual Studio Online team project.

clone-new-repository

With the URL entered correctly, SourceTree should recognize it as a Git repository and display a message. Change the destination path if you’d like, then click the Clone button and you’re done!

Selective Revert in Git

Uh oh, a mistake was made in your Git repository, and it needs to be undone. I use SourceTree for the majority of my Git-in’, and reverting changes is really easy. Just right-click a commit, choose “Reverse Commit,” answer “Yes” in the confirmation box, and the deed is one. On one hand, it’s nice because it’s so simple. On the other, it doesn’t give you much control.

I recently had to deal with a bad merge that contained many files. Some of the merge was legit, but part of it was also undoing valid changes. I wanted to partially revert the commit and pick which files/changes to keep. Unfortunately, I couldn’t find a way to do this in SourceTree, but it’s pretty simple to do from the command line.

git revert can be run with -n or –no-commit to revert a commit but not automatically commit the result.

git revert --no-commit <sha-1>

My commit was a merge, and undoing merges can be more complicated. For my purpose, simply adding -m 1 was sufficient.

git revert --no-commit -m 1 <sha-1>
%d bloggers like this: