2

I want to copy a few files from one directory of a git repository branch to another branch that doesn't have the files. With Windows, you can copy the files and they are saved to the clipboard as they are, such as passing the actual files to some temporary directory. This allows you to delete the original files and still paste them elsewhere; basically a cut operation. I know Mac OS doesn't have the ability to cut files, so I assume that is why when I copy files from branch A, checkout branch B, and try to paste the files to the same directory for branch B, the paste option is grayed out; the files that were being referenced no longer exist to be copied.

Is there a way to enable a temporary directory where all copied items aren't simply references to the items that are being copied?

4
  • Have you tried to just copy the file using cp in the terminal? That leaves the file in the original direcrory and puts a copy wherever you specify.
    – Natsfan
    Commented Oct 14, 2019 at 20:12
  • @jmh I don't want to copy the file from branch A to some other directory and then checkout branch B and move the file from the other directory to branch B. I want the copy operation to save the actual file from branch A to the clipboard so I can paste it on branch B. using the terminal and cp means I'm copying the file from one directory to another in one move. Unless you know how to do something like cp file/at/dir/file.sh $(git checkout branch-B)/file/at/dir/file.sh. I hope the idea behind that command makes sense; it's the same directory on two different vcs branches of the same project Commented Oct 14, 2019 at 20:17
  • @klanomath between branches, in the same directory? not directory a and b, but branches a and b in the same directory. Edit: when you cmd+alt+v you are simply telling the OS to move the file. you cannot move a file if it doesn't exist because changing branches deletes the file from that directory. Commented Oct 14, 2019 at 20:55
  • The functionality you look for is simply not a part of Finder (or cp for that matter). The best way to do this is described below in Tom's answer, but for the record I want to point out that first copying the files elsewhere, then switch branches and move the copies into the current branch's folder would be the "Finder way" to do it and basically be the same as the Ctrl-X Ctrl-V you're used to from Windows (Windows also keeps a temporary copy of the files around behind the scenes).
    – Gero
    Commented Oct 15, 2019 at 11:42

1 Answer 1

1

The git-checkout command is used to "checkout" files into your current branch from another branch. This would be safest since it would all remain in version control:

git checkout <branch_name> -- <paths>

Example,

git checkout develop
git checkout master
git checkout develop -- myfile.js

would checkout myfile.js from develop into master so you can commit it when ready.

1
  • Thanks Tom, I thought this was a good approach as well, but some of the filenames are long and at times there are many files that need to be moved between branches which makes the finder approach more feasible. Let's say the directory structure has slightly changed. For example, the structure was file/at/repo/dir/myfile.js but would now like it to be at file/at/component_name/entity/myfile.js. If it's a simple modification, please add the difference to your answer. I've already marked it as the answer but it would be great to have both the original and extensive answers here in one place. Commented Oct 15, 2019 at 15:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .