This content originally appeared on DEV Community and was authored by Mohammad-Ali A'RÂBI
Let's assume you have a file in one branch and you want to bring it to the other one. That's tricky enough. Even trickier is this: You and your friend have forked the same repo and pushed different changes to each one. Now you want to have the changes your friend made to their fork, in your fork.
In this article, we will answer both questions:
- Get a file from another branch on the same repo
- Get a file from another repo
Get One File From Another Branch
Assume you want to bring a file from the branch feature-source
into the branch feature-dest
. The file is called package.json
.
First, you need to switch to the branch feature-dest
:
git checkout feature-dest
And then bring the file from the other branch:
git checkout feature-source -- package.json
Get One File From Another Repo
Now assume that I have forked the repo rxjsx/rxjsx
into aerabi/rxjsx
. Then I cloned my fork to work on it locally. My amigo also forked the original repo into amigo/rxjsx
and made a few changes and pushed them into his master branch.
Now I want to get one file from his master branch into my local repo. This is how it's done:
First copy the URL of the remote repo you want to get the file from, in this case, the amigo/rxjsx
.
Then fetch it:
git fetch git@github.com:amigo/rxjsx.git
Git will show a message like this:
From github.com:amigo/rxjsx * branch HEAD -> FETCH_HEAD
It means the remote repo is fetched and its HEAD
is now named FETCH_HEAD
. Next, get the file (or directory) you want from the FETCH_HEAD
:
git checkout FETCH_HEAD -- package.json
It's similar to the former case, the only difference is the git label you are checking out from.
Final Words
Because checkout is used both for switching branches and moving files between branches, git introduced a new command called
switch
that we used for switching. I did a full article on the newly introduced commands to use instead ofcheckout
.Subscribe to my Medium publishes to get notified when a new Git Weekly issue is published.
Follow me on Twitter for weekly articles and daily tweets on git.
This content originally appeared on DEV Community and was authored by Mohammad-Ali A'RÂBI
Mohammad-Ali A'RÂBI | Sciencx (2024-09-11T19:16:13+00:00) Git Get One File From Another Repo. Retrieved from https://www.scien.cx/2024/09/11/git-get-one-file-from-another-repo/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.