Asked  7 Months ago    Answers:  5   Viewed   35 times

I forked someone's repository on GitHub and would like to update my version with commits and updates made in the original repository. These were made after I forked my copy.

How can I pull in the changes that were made in the origin and incorporate them into my repository?

 Answers

70

You have to add the original repository (the one you forked) as a remote.

From the GitHub documentation on forking a repository:

Screenshot of the old GitHub interface with a rectangular lens around the "Fork" button

Once the clone is complete your repo will have a remote named “origin” that points to your fork on GitHub.
Don’t let the name confuse you, this does not point to the original repo you forked from. To help you keep track of that repo we will add another remote named “upstream”:

$ cd PROJECT_NAME
$ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
$ git fetch upstream

# then: (like "git pull" which is fetch + merge)
$ git merge upstream/master master

# or, better, replay your local work on top of the fetched branch
# like a "git pull --rebase"
$ git rebase upstream/master

There's also a command-line tool (hub) which can facilitate the operations above.

Here's a visual of how it works:

Flowchart on the result after the commands are executed

See also "Are Git forks actually Git clones?".

Tuesday, June 1, 2021
 
Kevin_Kinsey
answered 7 Months ago
76
git remote add {name} {Public Clone URL}
git pull {name} master
git push

Example:

git remote add bret git://github.com/bret/watir.git
git pull bret master
git push
Wednesday, June 9, 2021
 
juananrey
answered 6 Months ago
100

Your origin repository is ahead of your local repository. You'll need to pull down changes from the origin repository as follows before you can push. This can be executed between your commit and push.

git pull origin development

development refers to the branch you want to pull from. If you want to pull from master branch then type this one.

git pull origin master
Sunday, August 8, 2021
 
Pradip
answered 4 Months ago
92

My usual workflow is to create a dedicated branch from which to track the upstream project. You can cherry pick what you want onto that branch and create a pull request without muddying the template with your project specifics.


First thing, go ahead and fork aurelia/skeleton-navigation so you can easily issue a pull request through Github's GUI.

Clone your fork of the project with remote named upstream in a new folder called skeleton-typescript

git clone -o upstream git@github.com:YOUR_GITHUB_USERNAME/skeleton-navigation.git skeleton-typescript

cd skeleton-typescript

Rename master branch.

git branch -m asmaster

Create a new repository for skeleton-typescript

Tip: You can do this right from the command line using Github's API with something like curl

curl --data '{"name":"skeleton-typescript"}' -u YOUR_GITHUB_USERNAME https://api.github.com/user/repos

Add the remote.

git remote add origin git@github.com:YOUR_GITHUB_USERNAME/skeleton-typescript.git

Split the repo into a subtree (see source code for man page) which will contain a new tree with all the commit history of files in the prefix directory.

git subtree split --prefix=skeleton-typescript

This will print out a single commit ID which is the HEAD of the subtree.

539d913a8cf9b34b644273b5cdb480359553247c

Create your master branch from that commit.

git checkout -b master 539d913a8cf9b34b644273b5cdb480359553247c

Push to and track your new repo.

git push -u origin master

Backporting

Commit some work on skeleton-typescript

echo "notable contribution" >> file.txt
git add .
git commit -m "backport test commit"
git push origin master

Checkout the upstream super-project branch, and cherry-pick the subtree commits.

git checkout asmaster
git cherry-pick -x --strategy=subtree master
git push upstream asmaster:master

Now you can issue a pull request from your upstream fork YOUR_GITHUB_USERNAME/skeleton-navigation:master branch to their aurelia/skeleton-navigation:master branch.

Updating

Now there's going to be updates no doubt to your upstream's upstream (aurelia/skeleton-navigation:master) which will include updates to your subtree's skeleton-typescript folder.

Add another remote to track the original project.

git remote add upupstream git@github.com:aurelia/skeleton-navigation.git

Note that you now have 3 remotes in your local repository.

git remote -v
origin    git@github.com:YOUR_GITHUB_USERNAME/skeleton-typescript.git (fetch)
origin    git@github.com:YOUR_GITHUB_USERNAME/skeleton-typescript.git (push)
upstream  git@github.com:YOUR_GITHUB_USERNAME/skeleton-navigation.git (fetch)
upstream  git@github.com:YOUR_GITHUB_USERNAME/skeleton-navigation.git (push)
upupstream    git@github.com:aurelia/skeleton-navigation.git (fetch)
upupstream    git@github.com:aurelia/skeleton-navigation.git (push)

Pull down updates.

git checkout asmaster
git pull upupstream master

Split off the subtree again and grab the HEAD commit.

git subtree split --prefix=skeleton-typescript

095c0c9f7ed06726e9413030eca4050a969ad0af

Switch back to subproject.

git checkout master

If you have never backported changes, then a notable attribute of git subtree split is that you'll have the exact same hash commit history, so you can fast forward merges without rewriting history. From the docs:

Repeated splits of exactly the same history are guaranteed to be identical (i.e. to produce the same commit ids). Because of this, if you add new commits and then re-split, the new commits will be attached as commits on top of the history you generated last time, so git merge and friends will work as expected.

git merge 095c0c9f7ed06726e9413030eca4050a969ad0af

However, if you have already backported cherry-picked updates, or any other changes to the subtree history, then you'll want to rebase changes, otherwise you'll have duplicate commits.

git rebase 095c0c9f7ed06726e9413030eca4050a969ad0af
Saturday, August 21, 2021
 
shin
answered 4 Months ago
33

What worked for me was loading up and logging into GitLab in your browser. Navigating to the repository you want, highlighting and drag/drop the https url for your repo into the GitHub for Windows app. It will clone locally, and ask you to login.

Wednesday, November 10, 2021
 
jezrael
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share