34

I've got a file foo.txt in branch A, and I want to check out foo.txt from branch B into the index. On the command line, I would do this:

$ git checkout B -- foo.txt

I want to be able to do this in magit by going to log, finding the commit I'm interested in (e.g l o B), finding foo.txt, and running some command, but I can't figure out how to do it.

Emoses
  • 573
  • 5
  • 8
  • This is now a feature request https://github.com/magit/magit/issues/2184. Thanks for looking at it, maintainers – Emoses Aug 19 '15 at 18:11

3 Answers3

47

As of magit 2.11.0 this is possible directly from the reset dispatcher (X f and you may specify the revision and file):

enter image description here

This can be done programmatically within magit using magit-file-checkout which accepts a version spec and the filename (thanks to @Emoses' feature request and @Kyle Meyer's implementation). Previously the function was named magit-checkout-file (from v2.3.0 - v2.9.0).

ebpa
  • 7,319
  • 26
  • 53
3

I'm not aware of a builtin way to do this.

Assuming you're on branch A, another approach would be to diff branch B (d r ..B), and then move point to the file you're interested in. Pressing a will apply those changes to the working tree (rather than the index).

I have a command in my configuration to reset or checkout a file from a revision, but, in its current state, it doesn't fit exactly what you're asking for because it won't offer a good revision default when in Magit Revision mode.

(defun km/magit-reset-file (rev file &optional checkout)
  "Reset FILE from revision REV.

If prefix argument CHECKOUT is non-nil, checkout FILE from REV
instead.

\(git reset REV -- FILE)
\(git checkout REV -- FILE)"
  (interactive
   (let ((rev (magit-read-branch-or-commit "Revision")))
     (list rev (magit-read-file-from-rev rev "File") current-prefix-arg)))
  (magit-with-toplevel
    (magit-run-git (if checkout "checkout" "reset")
                   rev "--" file)))
Kyle Meyer
  • 6,914
  • 26
  • 22
2

If you use evil-collection, the command is O instead of X to reset using other branch.

enter image description here

Tommy
  • 153
  • 7