how to checkout a single file from git

Can SourceTree checkout a single file from an old git commit? In the Log view, I right-clicked on the file/commit that I want, but nothing on the menu looks like checkout. Also, how can I see the tree view for the old commit, to checkout a file that wasn't changed?

The closest I've found is on the File Status view; the right-click menu has Reset to Commit..., but isn't there a more direct way? It doesn't seem like this would allow the checkout of a file that wasn't already checked out.

I just started trying SourceTree today. I've been using GitX on Mac.

4 answers

1 accepted

2 votes
Accepted answer

The easiest way to do this is to find the file somewhere in one of the views, either using 'Show All' on the File Status View, or somewhere in the log where it's been changed, then right-click and select 'Log Selected...'. This will give you a history of that single file, and you can right-click on any point in time for that file and select 'Reset File to this Commit'.

Because a Git commit only has entries for files that actually changed, you do have to find a commit that included that file to be able to get a context menu for it. Of course on the command line you can specify any commit after the file changed, but that's because you're specifying it by hand. But the single file log is better anyway, because you can see explicitly where that file changed and make sure you get the correct version.

Thanks for the tip about searching and Log Selected... They look useful, too.

I was wondering if SourceTree had a view of all the files in a commit, not limited to the changed files, but it sounds like it doesn't.

If I could search in the Log view for a file that had been deleted, then I could checkout that file (i.e., get its Log Selected and Reset to Commit). But I don't see any way to search the Log. Am I just overlooking something? It seems strange for SourceTree to not provide any way to search its Log view.

Both Mercurial and Git only report the files in a commit, not the union of all files up to that commit, so that's why the content is like it is.

As for searching, you do this in the Search view (the 3rd item on the tab control in the top-left, or Cmd-3). You can search for file changes, commit comments or authors.

Ooh, I didn't see the Search view; I'll try it on Monday. Thanks for pointing it out.

As for the Log view, I guess I need to keep in mind that it's showing the log, not the commit. A commit in git contains all the files, not just the changed files. Git viewers can commonly browse the unchanged files in a git commit, e.g., in GitX or on github.com, but I guess SourceTree can't do that yet.

I just noticed that the Reset to Commit... is on the Log view's right-click menu too. I guess that's what I was looking for; I just didn't realize it meant checkout when I first saw that menu.

However, I still wonder how to checkout a file from that commit if it wasn't changed there and isn't already checked out.

0 votes

However, I still wonder how to checkout a file from that commit if it wasn't changed there and isn't already checked out.

If you are comfortable using the commandline you could use the following "workaround" as it does not seem to be possible in SourceTree.

Every commit on git refers to a tree, which is a snapshot of the whole project at the time of the commit.

To get the content of a particular file when a particular commit happened (even if that file didn't not get changed as part of the commit) you can use the following command:

git show COMMITID:local/path/to/file 

Using the git://github.com/atlassian/bamboo-git-plugin.git as an example, it looks like this:

# Random commit: b8be4b2

$> git show --stat b8be4b2

commit b8be4b27e0a649ad2a0dc10ed898847f93665aa2

Author: Marek Went <devnull@atlassian.com>

Date:   Mon Sep 26 18:22:58 2011 +0200

 

    BAM-9846 apply multiple repo for github repo

 

 .../bamboo/plugins/git/LoadGitHubRepositories.java |   35 +++++++++++++++++--

 .../bamboo/plugins/git/ghRepositoryEdit.ftl        |    3 +-

 .../bamboo/plugins/git/ghRepositoryView.ftl        |    1 -

 3 files changed, 33 insertions(+), 6 deletions(-)

The commit b8be4b2 affects 3 files, to get the content of an unrelated file at that commit, use:

git show b8be4b2:src/main/java/com/atlassian/bamboo/plugins/git/GitMavenPomAccessor.java

"Checking out" could then simply be redirecting that to local file:

git show b8be4b2:src/main/java/com/atlassian/bamboo/plugins/git/GitMavenPomAccessor.java &gt; src/main/java/com/atlassian/bamboo/plugins/git/GitMavenPomAccessor.java

Again, that doesn't answer your original SourceTree related question, but I hope it helps regardless :)

I had a non-responsive comment, so I've deleted it.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Oct 23, 2018 in Sourcetree

Tip from the team: configure your repos for hosting goodness!

Supported Platforms macOS Windows We recently introduced support for additional hosting services such as GitHub Enterprise, GitLab (Cloud, Community Edition, Enterprise Edition), and...

720 views 3 2
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you