Git Get One File From Another Repo

Git Get One File From Another Repo

Welcome to Issue #6 of Git Weekly! This week we're going to talk file and moving them around. Let's assume you have a file in one branch and you want to bring it to another 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 switch 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 [email protected]: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

要查看或添加评论,请登录

Mohammad-Ali A'R?BI的更多文章

  • Spring into Markdown

    Spring into Markdown

    So, at this very moment, the Sun has crossed the celestial equator, marking the beginning of Spring in the northern…

    7 条评论
  • 7 Noob Git Tips

    7 Noob Git Tips

    We will start 2025 one month into the year with seven basic git tips. This issue is based on a Twitter series that I…

  • 7 Basic Git Commands

    7 Basic Git Commands

    In this Git Weekly issue, we'll cover what basic commands are used when committing directly to the main branch. As a…

  • Git Submodule Update

    Git Submodule Update

    Halloween is here and we're back with Git Weekly #8, another piece on the spine-chilling git submodules. ?? Submodules…

  • Git Submodules

    Git Submodules

    Welcome to Git Weekly #7. This week, we will discuss having git repos within git repos.

  • How to Squash Git Commits Manually

    How to Squash Git Commits Manually

    Welcome to Git Weekly! Each week, we explore advanced git features to help you fine-tune your workflow. Today’s topic…

  • Git Merge vs Rebase and Where to Use Them

    Git Merge vs Rebase and Where to Use Them

    There are two workflows for merging a feature branch into the master branch, namely 'rebase and fast-forward' and…

  • Git Merge vs Rebase: The Three Types of Merge

    Git Merge vs Rebase: The Three Types of Merge

    Merging is adding the work done in one branch to another. There are three ways one could merge one branch into another:…

    2 条评论
  • Change a Commit's Author on Git

    Change a Commit's Author on Git

    I usually commit to my work and to my private git projects, and I use two different email addresses for them. But it…

    4 条评论
  • Determine the Git Branch You're On

    Determine the Git Branch You're On

    Branching is one of the most used features of git. People usually change branches all the time and need to determine…