6

Git: How to pull a branch from the upstream to the local repo in 5 steps

 2 years ago
source link: https://dev.to/adiatiayu/git-how-to-pull-a-branch-from-the-upstream-to-the-local-repo-in-5-steps-1ili
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Open-Source (2 Part Series)

Hello Fellow CodeNewbies 👋,

I am collaborating with a friend to create a project in React.
I created a repo for the project, and my teammate forked this repo.

Recently, I created a branch to made some changes. Then I pushed this branch to the remote repo and created a pull request.

I asked my teammate to pull this branch and test things out locally before we merge it into the main branch.
After making sure that we don't have anything to fetch and merge from the remote repo, and after several attempts, we still couldn't pull the branch from the remote repo.
We mostly got the error of fatal: couldn't find remote ref <branch-name>.

Then we tried to figure things out.
Looking from my teammate's side, my repo is the upstream repo. He then forked this repo, and it automatically becomes his origin repo.
For him to be able to fetch and pull a branch — that hasn't been merged to main — from the upstream repo, his origin repo should have access to the upstream.
So, we need to set the origin repo to point to the upstream repo.

Pull a particular branch from the upstream repo

  1. Check our current configured remote repo for our fork.

      git remote -v
    

    If we haven't configured a remote that points to the upstream repo, we will get:

     origin <fork-repo-url> (fetch)
     origin <fork-repo-url> (push)
    
  2. Add a new remote upstream repo that will be synced with the
    origin repo.

    git remote add upstream <original-repo-url>
    

    original-repo-url is the HTTPS URL of the repo that we fork.

    We can copy this link by going to the repo on GitHub, clicking the green button with "Code" written on it, and copying the HTTPS link.

  3. Check if the new upstream has now been added.

    git remote -v
    

    Now we should get:

    origin <fork-repo-url> (fetch)
    origin <fork-repo-url> (push)
    upstream <original-repo-url> (fetch)
    upstream <original-repo-url> (push)
    
  4. Fetch data from the upstream.

    git fetch upstream
    

    By now, you will see in the command line that we have fetched the branches on the upstream repo, including the target branch.

  5. Navigate to the branch.

    git checkout branch-name
    

    Running this command will also automatically create a branch with the same name in our local repo.

Now you have the branch on your local repo and you can test out your teammates' changes locally! 😄


Thank you for reading!
Last but not least, you can find me on Twitter. Let's connect! 😊


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK