'Git pull from someone else's fork

We are two students working on our online repository (different repo) that is forked from a common upstream repo.

Let's say other student made Changes, Commits and Pushed to his repo on a specific branch.

  1. How do I pull these changes into my own local repository?

  2. Do I need to commit and push those changes to my staged area?

Thank you!



Solution 1:[1]

Add a new remote (say, other) in your own repo. Pull other/<branch> changes into your local branch (say, add-other-changes). Push to your own forked repo (origin/add-other-changes). Now, when're you done with add-other-changes branch, create a Pull request & merge it with origin/master.

  • Pull other repo's changes into your own repo:

      # go into your own repo
      $ git remote add other <other-student-repo-url>  # add a new remote with other's repo URL
    
      $ git fetch other         # sync/update local with other's repo
    
      $ git checkout -b add-other-changes       # create a new branch named 'add-other-changes'                    
      $ git pull other <specific-branch-name>   # pull other/<branch> changes           
    
      $ git push origin HEAD    # push changes to your own(origin) forked repo `add-other-changes` branch
    

Solution 2:[2]

If you both want to work on the same project, then you shouldn't have forked the the repository twice. You or you friend (not both) should fork the repository, then both of you should clone the forked one in local (permissions need to be granted by the one who forked repository).

Once this is done, when members of the project want to know if there are new changes on the remote, they can do git remote update or more commonly git fetch origin.

If you're working on the same branch and you want to update your local branch with the remote one git pull origin <branh_name>

If one have made changes that should get shared:

git add file_path_1 file_path_2 directory_path1 ...
git commit -m "<your brief message>"
git push origin <branch_name>

Solution 3:[3]

commit and push your working branch. Then do a merge from the main branch to your branch. Ensure all build and resolve any conflicts. commit and push your branch. Now merge your branch into the main branch and it should work.

Solution 4:[4]

For GitHub users:

If you want to pull changes from a Pull Request where the source branch is in a fork, you can use GitHub CLI without adding a remote:

gh pr checkout {<number> | <url> | <branch>}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 smarber
Solution 3 bilpor
Solution 4 victorlin