Git copy file from another branch without staging it
Asked Answered
P

2

11

I've found how you can copy a file wholesale from one branch to another:

Edit please note, if you think this question is a duplicate of this, please observe that that's the question I linked above^^, and what follows below explains the different functionality that I want.

$ git checkout directory/somefile.php feature-B

However this solution stages the changes already:

$ git status
On branch feature-A
Your branch is up-to-date with 'origin/feature-A'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   directory/somefile.php

But I don't want to add all the changes. I want to do an add -p and take most, but not all, of the changes. What I want is this:

$ git status
On branch feature-A
Your branch is up-to-date with 'origin/feature-A'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   directory/somefile.php

no changes added to commit (use "git add" and/or "git commit -a")

Of course, I can do git reset to unstage the changes, but I would like to get what I want the first time, without a second step.

How can I copy a file from another branch without staging it?

Photocopier answered 7/5, 2019 at 19:46 Comment(5)
@Hurlyburly This question you linked is literally the one I linked at the start of my question, and my question expounds on how I want different functionality from the answer in that question.Photocopier
Yes, and it contains the answer: git show (2 times).Hurlyburly
@Hurlyburly and it never says that that method will copy, but not stage, commits. In other words, it doesn't answer my question; it presents it as an alternative to git copy..., with no indication of a difference in functionality, so a user would assume that it behaves the same way. If someone is googling to find an answer to my question, there is no information in those answers to indicate that it meets their desired behavior.Photocopier
@Hurlyburly Which question in particular from the search contained the answer?Photocopier
@Hurlyburly not a duplicate. this is a more precise question and answer.Sukkah
E
11

you could use git show and then redirect... it's not very elegant, though

git show feature-B:directory/somefile.php > directory/somefile.php
Eisegesis answered 7/5, 2019 at 19:48 Comment(2)
Good enough to make an alias out of!Photocopier
copy = ! "gitcopy() { git show $1:$2 > $2 ; }; gitcopy" -- you need to make a bash function as an alias, in order to use parametersPhotocopier
R
16

You can use git restore

git restore --source feature-B directory/somefile.php

I didn't pass any option saying where it should restore the file and by befault it's working directory (without index/staging area). From documentation:

> -W
> --worktree
> -S
> --staged
> 
>     Specify the restore location. If neither option is specified, by default the working tree is restored. Specifying --staged will only restore the index. Specifying both restores both.

I wrote a bit more about the differences between git restore and other commands (git checkout, git show) in this answer.

Retrospection answered 25/3, 2020 at 18:11 Comment(0)
E
11

you could use git show and then redirect... it's not very elegant, though

git show feature-B:directory/somefile.php > directory/somefile.php
Eisegesis answered 7/5, 2019 at 19:48 Comment(2)
Good enough to make an alias out of!Photocopier
copy = ! "gitcopy() { git show $1:$2 > $2 ; }; gitcopy" -- you need to make a bash function as an alias, in order to use parametersPhotocopier

© 2022 - 2024 — McMap. All rights reserved.