Copying a missing file from another git branch


It looks as if my question has also been asked here but I don't find the answer wholly applies to the question so I'll raise it again.

It happens quite often that I merge the changes on my local git branch with another git branch by using the (context click menu) Git -> Compare with Branch ... function. In the side-by-side viewer window it's easy to cherry-pick the changes and copy whatever is necessary. 

However, this can't cope with (the fairly common, you'd think) situation where the remote branch happens to have a new file. In such cases, the local panel of the side-by-side viewer is (obviously) blank. It turns out that there is absolutely no way to copy that new file from that other branch to the active branch within the "Git Compare UI", so to speak. 

Of course, I can:

  • exit the viewer;
  • figure out in what project module exactly the new file is supposed to live (since that's ain't necessarily clear from the package name alone) ;
  • navigate there;
  • right-click "Create new Java file";
  • *slap forehead* when I find that I forgot to copy the contents of that new file - again;
  • cmd-click project;
  • choose Git -> Compare with Branch -> select branch;
  • find the new file and open in the side-by-side editor;
  • cmd-a *select all*;
  • cmd-c *copy*;
  • cmd-v *paste* in some external Text editor;
  • go to destination package location - or create manually if not yet there;
  • right-click "Create new Java file";
  • go to text editor and cmd-c *copy* class name;
  • cmd-v *paste* class name in Create new Java File Dialog;
  • click next;
  • go back to text editor and cmd-c *copy* file contents;
  • cmd-v *paste* file contents in new Java file dialog; and
  • save the file

... and I can't help but wonder if something like what I mocked up below would be as helpful to others as it would for me?

评论操作 固定链接

While the Copy file feature might be useful, and there is a request for that - see, at the moment you could copy just the entire content of the file from the diff (on your screenshot)m and create a new one in the current branch.

The window is not modal, so you could see the path, etc, so it should much easier than you described to create a file in the current branch.

评论操作 固定链接

I'd love to see a copy file option as well. Dmitry's solution may work for text files, but I'm often wanting to grab non-text files like images and license files from another branch. It's of course possible for me to get the files in other ways, but I use an IDE to make my life easier and this is another area in which it could help me out.

评论操作 固定链接

This is one of the main things I am missing in jetbrains IDEs

评论操作 固定链接

There is actually an option to get some specific revision of the file into the Local changes and then commit it.

To do this, use the Show History for a file, enable showing all branches, pick the revision you need and use the Get action form context.

To open history for a file that exists only in the other branch, you could try the Open History for Revision from the Log.

评论操作 固定链接

Ok thanks, I did not know the Get action, but its very helpful. However this requires me to (please correct me if I am wrong):

a) find the commit that last changed the file I want to pick from the other branch in the 'compare with branch' window (the 'Show history for revision' action will only show the revision up to the selected commit in the log) and

b) find this commit in the log


a) can be cumbersome when there are many commits and b) is also a bit annoying because its not so easy to copy the commit message and search for it in the git log: If I hit ctrl+C the whole row is copied (including the date etc.) and I also can not select and right click copy only the commit message. Here it would be handy to either have something like 'go to commit in log' or at least 'copy commit message' to be able to paste and search for it in the git log. Btw its also a bit strange that I cannot search by commit hash in the log window.

Git integration is quite amazing in IntelliJ so I would really look forward to a better solution to pick files from another branch. Currently my preferred solution is still typing 'git checkout otherbranch -- here/comes/the/often/annoyingly/long/path/to/the/file/iam/looking/for' in the terminal. Unfortunately I can not right click copy the file path in the 'compare with branch' window either ...


评论操作 固定链接

Yep, there should be a way, and there is a request to add the functionality to the Compare branches dialog mentioned above -

What I proposed with Get is actually a workaround

评论操作 固定链接

1) Save diff between branches to patch:

git diff --no-prefix master..branch-name > branch-name.patch

2) Apply patch in IDEA

评论操作 固定链接

Starting from 2018.3, the action to copy files will appear in the Compare branches dialog.


评论操作 固定链接

2021.1 (phpstorm) there no copy action, but...

1) Open git tab (alt+9) -> select file, right click -> Open Repository Version

2) File save as.. (ctrl+shift+s) -> OK