-2

I've updated a file throughout my work on my Git repository on the master branch. I'm currently on another branch based on a much older commit to master, so the version of the file I'm seeing is out of date. I have not touched the file on my current branch.

I want to checkout the fully-updated version of that file. I know that the most recently updated version of that file (even across all branches) is the one I want, but I do not know which commit that is.

Is there a way to checkout the most recently updated version of a given file across all branches and commits? (I'm hoping for a one-liner.)

jvriesem
  • 1,859
  • 3
  • 18
  • 40

1 Answers1

1

If your file is dir/foo, then the way to get the most recent commit across all branches is as follows (more info here or the git-rev-list(1) Manual Page):

git rev-list -1 --branches=* dir/foo

The way to checkout a file based on a commit is (more info here):

git checkout <commit> dir/foo

Therefore, a one-liner solution (in, e.g., BASH) is:

git checkout `git rev-list -1 --branches=* dir/foo` dir/foo
jvriesem
  • 1,859
  • 3
  • 18
  • 40