0

Is there a way to fetch differences from a different branch or commit without introducing them to your local history? Just fetching all the changes as unstaged code?

For example

Branch1:

(HEAD) abc1 : added file A.txt

Branch2:

(HEAD) abc2 : added file B.txt
 |
abc1 : added file A.txt

so at Branch1 i want to fetch all the differences from Branch2 without adding them to Branch1 history. As a result I expect to have A.txt from commit abc1 and an unstaged file B.txt

Ben
  • 3,989
  • 9
  • 48
  • 84

1 Answers1

1

git checkout command is able to pick a single file (or many files) from specific version without changing a branch. Note that command git checkout is kind of Swiss army knife tool and is able to do many stuff.

In your case it should be:

git checkout branch2 -- B.txt

This should create file B.txt in staged state.

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • I didn't downvote but here OP is asking how to get diff changes in the Branch 1 itself, not checkout Branch 2, which in my view is a bizzare requirement – Mrinal Kamboj Mar 04 '21 at 14:00
  • This is some explanation, but question is a bit fuzzy. Last sentence says: `As a result I expect to have A.txt from commit abc1 and an unstaged file B.txt`. And part `fetch all the differences` doesn't necessary means he just need to see a diff. – Marek R Mar 04 '21 at 14:03
  • 1
    My guess is as good as yours until and unless OP provides a clarity, in my view we can do a better job using `cherry-pick` – Mrinal Kamboj Mar 04 '21 at 14:05
  • Question says "fetching all the changes as unstaged code"; so that's actually pretty clear, even if the wording is a little awkward. `checkout` isn't a bad answer, but it shoudl be clarified that this makes them *staged* changes, wehreas ideally OP asked for unstaged. Doesn't matter in the example (can just unstage the file), but might matter in a real usage. – Mark Adelsberger Mar 04 '21 at 14:56