btse
btse

Reputation: 8021

Git merge single file from another repository into my own

Say I have my own git repo with a bunch of text files in it. There is another different git repo that someone else owns with a bunch of text files that all differ from my own except for one file.

I am continuously making changes to the different text files in my repo, but every now and then I want to merge any changes of that single file from the other repo into my own.

Is there any easy way of going about doing this? I've searched around and found some similar questions but none that we're for my exact scenario.

Upvotes: 16

Views: 11676

Answers (5)

ZohrehBayramalizadeh
ZohrehBayramalizadeh

Reputation: 11

Clone Both Repositories Locally: First, clone both repositories to your local machine if you haven't already.

git clone <repository_url_1>
git clone <repository_url_2>

Add Remote for the Second Repository: In one of the cloned repositories, add the second repository as a remote.

cd <repository_1_directory>
git remote add <remote_name> <repository_url_2>

Fetch Remote Branches: Fetch the branches from the second repository.

git fetch <remote_name>

Merge Branches: Merge the branches from the second repository into the first repository. You can either merge specific branches or merge all branches.

git merge <remote_name>/<branch_name>

git merge --allow-unrelated-histories <remote_name>/<branch_name>

Resolve Conflicts: If there are any conflicts, resolve them during the merge process. Push Changes: Finally, push the merged changes to the remote repository.

git push origin <branch_name>

Upvotes: 1

Ghanshyam Nakiya
Ghanshyam Nakiya

Reputation: 1712

Go to your current directory, This way I'm doing currently, I hope you will help too.

cd your_current_git_Directory

Set your username and repository of another repository/second(from want to copy)

git remote add -f repo-b [email protected]:<username>/<repository>.git

Now, Merge the filename you looking for(if you want file merge subfolder)

git checkout -p repo-b/master  <folder_name>/<filename>

Or merge filename only

git checkout -p repo-b/master  <filename>

And Remove repository

git remote rm repo-b

Upvotes: 1

tmarwen
tmarwen

Reputation: 16354

Try to add the other-repo then bring its branches including your target-branch

git remote add other [email protected]:username/other-repo.git
git fetch other

Switch to the branch you want to merge in the target-file.ext:

git checkout your-branch

Merge the target file into yours:

git checkout -p other/target-branch target-file.ext

Upvotes: 12

khano
khano

Reputation: 1

If you are using any kind of packaging (artifacts) results in the original repo, then you can ask the owner of the repo to publish the sources as well. Then in your own repo you consume and unpack such artifact and just copy the file you need. E.g. If using Java-Maven, make sure the build on the original repo publishes also the sources, then in your own repo, you put a pre-step (before compile) in which you download the sources dependency, unpack the jar file and copy the one you need to your proper directory. There are maven plugins for that

Upvotes: 0

jthill
jthill

Reputation: 60275

You can merge arbitrary files or repo objects. Simplest might be:

git merge-file file.txt <(git show last-merged-file.txt) /path/to/their/file.txt
# resolve any conflicts, then
git tag last-merged-file.txt `git hash-object-w file.txt`

Upvotes: 6

Related Questions