I have two repositories.In A repository i added one folder from B repository.But now i have some changes in this folder in B repository.How i can added changes from B repo from only one folder?
I did git fetch git://repository.url/repo.git master:folder_name
but it's added all folders
Asked
Active
Viewed 84 times
0

Denis Murashko
- 119
- 2
- 6
-
That isn't relative to Java, even if java is the language of the code in the folders, it could python or C, that would be same – azro Apr 08 '22 at 06:53
-
Yes, you are right, I added java automatically – Denis Murashko Apr 08 '22 at 06:56
1 Answers
1
You've got two options:
A) Create a patch from repo_b
and apply it to repo_a
(easier).
B) Add repo_b
as a remote of repo_a
and merge unrelated histories. (more complicated).
Here's how you do option A:
cd repo_b
git whatchanged --reverse folder_name
git diff deadb33f HEAD > folder.patch
Above you are determining the point in repo_b
's history, where, from that point onwards you want to get all the changes. That commit should be deadb33f
.
cd repo_a
git checkout -b new_branch
git reset --hard decafbad
Where decafbad
is the commit where the two repos diverged.
patch -p1 < folder.patch
git add changed_folder
git commit -m "changes from repo_b"
This will, however, squash all the changes into one commit. If you want to keep the commit history from repo_b
, go with the method I linked to above (option B).

Andy J
- 1,479
- 6
- 23
- 40
-
When i try to do **git diff 3a4c9aa0f7ed84e04618ae8c936d4adeb2653a80 HEAD > absolute_path_folder** i have the issue - **Access is denied**. – Denis Murashko Apr 08 '22 at 13:49
-
Could you paste the entire error message here? Are you using git on linux natively, or via mingw (or similar) on Windows? If you're working with a local copy of the git repo, the .git directory and all checked-out files should by owned by your username and group. You can check that with `ls -la`. – Andy J Apr 09 '22 at 01:30
-
I changed cmd on Git Bash - `$ git diff b3c5a1032b07bfb3f3e61023c793c579089223e5 HEAD > "..\repob\folder_name" bash: …\folder_name: Is a directory ` It seems to me that this is not the behavior that should be – Denis Murashko Apr 09 '22 at 08:42
-
The syntax is `git diff b3c5a1032b0 HEAD > name.patch` where name.patch is the patch file you are creating from `repo_b`. – Andy J Apr 09 '22 at 11:00
-
`$ patch -p1 < "C:\ name.patch" can't find file to patch at input line 5 Perhaps you used the wrong -p or --strip option? The text leading up to this was: -------------------------- |diff --git a/…/Myclass.java b/MySecondClass.java |index 071c8d950..15eb7f763 100644 |--- a/ Myclass.java.java |+++ b/ MySecondClass.java -------------------------- File to patch: ` I don't understand what should I do next? – Denis Murashko Apr 10 '22 at 08:32
-
It sounds like you're missing a bit of background on git patches. I think [this blog post](https://www.thegeekstuff.com/2014/03/git-patch-create-and-apply) has a pretty good summary. The only difference is they're using `git patch` instead of `patch`, but the result is the same. By the time you're done reading it you should be good generating and applying patches. – Andy J Apr 10 '22 at 12:06