Suppose I made changes to a file a.java
in repository repo1
and committed my changes to a local branch branch1
. Next I came to know that I can not directly push my change to repo1
. Rather I have to fork the repo and then create a branch and PR from there. So now I forked repo1
to repo2
and created a branch. origin/branch2
and checked it out. Now how can I compare the file a.java
from repo1
branch branch1
and bring the changes to repo2
branch branch2
? It would be nice if I can do it in Intellij.

- 5,132
- 15
- 53
- 91
-
I think you can just merge the two branches as described in [this post](https://stackoverflow.com/questions/21360077/how-to-merge-between-two-local-repositories). – flaxel Mar 05 '21 at 19:27
-
Looks to me like you can just copy and paste the file content and no further steps should be necessary. – Tom Mar 05 '21 at 19:30
-
There is a blog post on how to keep the fork up-to-date, please see: https://blog.jetbrains.com/idea/2017/01/git-questions-how-to-keep-a-git-fork-up-to-date/ Also you can compare files with a diff viewer, please see: https://www.jetbrains.com/help/idea/comparing-files-and-folders.html – Ruslan Kuleshov Mar 08 '21 at 09:29
1 Answers
If the file is visible from your local repository (as in the other two repos are set as remotes) then git doesn't care.... you can provide the path to the two files and it will diff them (it really doesn't require any relation between the files... it can be 2 completely different projects altogether):
git diff repo1/branch1:path-to-file repo2/branch2:path-to-file2
That should work.
Now.. getting one IDE to do that kind of stuff? I don't think you will be able to pull it off. Might need to consider checking out the file from one branch to your local branch so that then you can pick the parts you want to keep and the ones you want to discard:
git checkout repo1/branch1 -- path-to-file
Or
git show repo1/branch1:path-to-file > path-locally # if the files do not have the same path in the 2 branches
Then feel free to check the things you want to keep on the IDE.
important
I am assuming your working tree is clean and you have no pending changes on the files you want to play with.

- 26,375
- 3
- 36
- 60