11

In my git development I am using the following checkout command structure to bring in solitary files from other branches into my working directory.

git checkout [<tree-ish>] [--] <pathspec>…​

As per documentation, this also updates my index with the file as if I wanted to commit it - leaving me with the extra step of deleting it from my index if I don't want to commit the file. (We're doing development with binary files :( and this way of working is helpful for testing our individual work that may relate to the work of another developer that's working in parallel)

Is there any way to pull/checkout a singular file from a branch/commit such that it updates the working directory and NOT the index?


I figure I could alias the two steps into a single command, but I'm curious if there is already a (single) git command capable of accomplishing this.

Zoe
  • 23,712
  • 16
  • 99
  • 132
CharlieBrown
  • 143
  • 5
  • 1
    I was also curious if there is a way to do it. But it looks that git maintainer thinks it's not that useful in normal workflow ([link](http://git.661346.n2.nabble.com/Can-I-checkout-a-single-file-without-altering-index-tp5626080p5640789.html)) – Mariusz Pawelski Oct 17 '18 at 07:58
  • 1
    quote from the link above: _If you want to bypass the index, you can do so with cat-file or show; it just is not a useful operation in a normal workflow of building the next commit on top of the current one, and that is the only reason why there is no option such as "checkout --no-index HEAD~47 path". If somebody can write a convincing use case that shows why it is useful, such an option shouldn't be very hard to add. But I don't think of any._ – Mariusz Pawelski Oct 17 '18 at 08:00

2 Answers2

9

Another way would be

git show otherBranch:fileName > fileName

This creates a new file with the content of the file in the other branch

Anyway if your workflow requires this step to be done often, I would create an alias

Francesco
  • 3,658
  • 2
  • 16
  • 27
2
git restore --source=<tree> --worktree <pathspec>...

See the manpage.

Niklas Holm
  • 592
  • 5
  • 12