Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git checkout file from branch without changing index

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.

like image 229
CharlieBrown Avatar asked May 03 '18 15:05

CharlieBrown


2 Answers

git restore --source=<tree> --worktree <pathspec>...

See the manpage.

like image 183
Niklas Holm Avatar answered Oct 20 '22 16:10

Niklas Holm


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

like image 26
Francesco Avatar answered Oct 20 '22 17:10

Francesco