I recently forked a project and applied several fixes. I then created a pull request which was then accepted.
A few days later another change was made by another contributor. So my fork doesn't contain that change... How can I get that change into my fork?
Do I need to delete and re-create my fork when I have further changes to contribute? Or is there an update button?gitgithub
In your local clone of your forked repository, you can add the original GitHub repository as a "remote". ("Remotes" are like nicknames for the URLs of repositories -
origin is one, for example.) Then you can fetch all the branches from that upstream repository, and rebase your work to continue working on the upstream version. In terms of commands that might look like:
# Add the remote, call it "upstream": git remote add upstream https://github.com/whoever/whatever.git # Fetch all the branches of that remote into remote-tracking branches, # such as upstream/master: git fetch upstream # Make sure that you're on your master branch: git checkout master # Rewrite your master branch so that any commits of yours that # aren't already in upstream/master are replayed on top of that # other branch: git rebase upstream/master
If you don't want to rewrite the history of your master branch, (for example because other people may have cloned it) then you should replace the last command with
git merge upstream/master. However, for making further pull requests that are as clean as possible, it's probably better to rebase.
If you've rebased your branch onto
upstream/master you may need to force the push in order to push it to your own forked repository on GitHub. You'd do that with:
git push -f origin master
You only need to use the
-f the first time after you've rebased.
Here is GitHub's official document on Syncing a fork:
Syncing a fork
Before you can sync, you need to add a remote that points to the upstream repository. You may have done this when you originally forked.
Tip: Syncing your fork only updates your local copy of the repository; it does not update your repository on GitHub.
$ git remote -v # List the current remotes origin https://github.com/user/repo.git (fetch) origin https://github.com/user/repo.git (push) $ git remote add upstream https://github.com/otheruser/repo.git # Set a new remote $ git remote -v # Verify new remote origin https://github.com/user/repo.git (fetch) origin https://github.com/user/repo.git (push) upstream https://github.com/otheruser/repo.git (fetch) upstream https://github.com/otheruser/repo.git (push)
There are two steps required to sync your repository with the upstream: first you must fetch from the remote, then you must merge the desired branch into your local branch.
Fetching from the remote repository will bring in its branches and their respective commits. These are stored in your local repository under special branches.
$ git fetch upstream # Grab the upstream remote's branches remote: Counting objects: 75, done. remote: Compressing objects: 100% (53/53), done. remote: Total 62 (delta 27), reused 44 (delta 9) Unpacking objects: 100% (62/62), done. From https://github.com/otheruser/repo * [new branch] master -> upstream/master
We now have the upstream's master branch stored in a local branch, upstream/master
$ git branch -va # List all local and remote-tracking branches * master a422352 My local commit remotes/origin/HEAD -> origin/master remotes/origin/master a422352 My local commit remotes/upstream/master 5fdff0f Some upstream commit
Now that we have fetched the upstream repository, we want to merge its changes into our local branch. This will bring that branch into sync with the upstream, without losing our local changes.
$ git checkout master # Check out our local master branch Switched to branch 'master' $ git merge upstream/master # Merge upstream's master into our own Updating a422352..5fdff0f Fast-forward README | 9 ------- README.md | 7 ++++++ 2 files changed, 7 insertions(+), 9 deletions(-) delete mode 100644 README create mode 100644 README.md
If your local branch didn't have any unique commits, git will instead perform a "fast-forward":
$ git merge upstream/master Updating 34e91da..16c56ad Fast-forward README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Tip: If you want to update your repository on GitHub, follow the instructions here
Starting in May 2014, it is possible to update a fork directly from GitHub. This still works as of September 2017, BUT it will lead to a dirty commit history.
Update from original).
Now you have three options, but each will lead to a less-than-clean commit history.
This branch is X commits ahead, Y commits behind <original fork>.
So yes, you can keep your repo updated with its upstream using the GitHub web UI, but doing so will sully your commit history. Stick to the command line instead - it's easy.
A lot of answers end up moving your fork one commit ahead of the parent repository. This answer summarizes the steps found here which will move your fork to the same commit as the parent.
Change directory to your local repository.
git checkout master
Add the parent as a remote repository,
git remote add upstream <repo-location>
git fetch upstream
git rebase upstream/master
git push origin master
For more information about these commands, refer to step 3.
Follow the below steps. I tried them and it helped me.
Checkout to your branch
Syntax: git branch yourDevelopmentBranch
Example: git checkout master
Pull source repository branch for getting the latest code
Syntax: git pull https://github.com/tastejs/awesome-app-ideas master
Example: git pull https://github.com/ORIGINAL_OWNER/ORIGINAL_REPO.git BRANCH_NAME
Since November 2013 there has been an unofficial feature request open with GitHub to ask them to add a very simple and intuitive method to keep a local fork in sync with upstream:
Note: Since the feature request is unofficial it is also advisable to contact
[email protected] to add your support for a feature like this to be implemented. The unofficial feature request above could be used as evidence of the amount of interest in this being implemented.
Foreword: Your fork is the "origin" and the repository you forked from is the "upstream".
Let's assume that you cloned already your fork to your computer with a command like this:
git clone [email protected]:your_name/project_name.git cd project_name
If that is given then you need to continue in this order:
Add the "upstream" to your cloned repository ("origin"):
git remote add upstream [email protected]:original_author/project_name.git
Fetch the commits (and branches) from the "upstream":
git fetch upstream
Switch to the "master" branch of your fork ("origin"):
git checkout master
Stash the changes of your "master" branch:
Merge the changes from the "master" branch of the "upstream" into your the "master" branch of your "origin":
git merge upstream/master
Resolve merge conflicts if any and commit your merge
git commit -am "Merged from upstream"
Push the changes to your fork
Get back your stashed changes (if any)
git stash pop
You're done! Congratulations!
GitHub also provides instructions for this topic: Syncing a fork
If you are using GitHub for Windows then now they have a one-click feature to update forks:
In the meantime, GitHub user bardiharborow has created a tool to do just this: https://upriver.github.io/
Source is here: https://github.com/upriver/upriver.github.io
If, like me, you never commit anything directly to master, which you should really, you can do the following.
From the local clone of your fork create your upstream remote. You only need to do that once:
git remote add upstream https://github.com/whoever/whatever.git
Then whenever you want to catch up with the upstream repository master branch you need to:
git checkout master git pull upstream master
Assuming you never committed anything on master yourself you should be done already. Now you can push your local master to your origin remote GitHub fork. You could also rebase your development branch on your now up-to-date local master.
So past the initial upstream setup and master checkout, all you need to do is run the following command to sync your master with upstream: git pull upstream master.
Actually, it is possible to create a branch in your fork from any commit of the upstream in the browser:
https://github.com/<repo>/commits/<hash>, where repo is your fork, and hash is full hash of commit which you can find in the upstream web interface. For example, I can open https://github.com/max630/linux/commits/0aa0313f9d576affd7747cc3f179feb097d28990, which points to
masteras time of writing.
You can then fetch that branch to your local clone, and you won't have to push all that data back to GitHub when you push edits on top of that commit. Or use the web interface to change something in that branch.
How it works (it is a guess, I don't know how exactly GitHub does it): forks share object storage and use namespaces to separate users' references. So you can access all commits through your fork, even if they did not exist by the time of forking.
That depends on the size of your repository and how you forked it.
If it's quite a big repository you may have wanted to manage it in a special way (e.g. drop history). Basically you can get differences between current and upstream versions, commit them and then cherry pick back to master.
Try reading this one. It describes how to handle big Git repositories and how to upstream them with latest changes.
As a complement to this answer, I was looking for a way to update all remote tracking branches of my cloned repo from upstream in one go. This is how I did it.
This assumes you have already configured an
upstream remote pointing at the source repository and have synced it with
git fetch upstream.
for branch in $(git ls-remote --heads upstream|sed 's#^.*refs/heads/##'); do git push origin refs/remotes/upstream/$branch:refs/heads/$branch; done
The first part of this command will list all heads in the remote repo
upstream and remove the SHA-1 and
Then for each of these branches, it will push the local copy of the remote branch (
refs/remotes/upstream/<branch>) directly to the remote branch on
Any of these branch sync commands may fail for one of two reasons: either the
upstream branch have been rewritten or you have pushed commits on that branch to your fork. In the first case where you haven't committed anything to the branch on your fork it is safe to push forcefully (
git push -f). In the other case this is normal as your fork branch have diverged and you can't expect the sync command to work until your commits have been merged back into
I update my forked repos with this one line:
git pull https://github.com/forkuser/forkedrepo.git branch
Use this if you dont want to add another remote endpoint to your project, as other solutions posted here.
Android Studio now has learned to work with GitHub fork repositories (you don't even have to add "upstream" remote repository by console command).
Open menu VCS → Git
And pay attention to the two last popup menu items:
Rebase my GitHub fork
Create Pull Request
Try them. I use the first one to synchronize my local repository. Anyway the branches from the parent remote repository ("upstream") will be accessible in Android Studio after you click "Rebase my GitHub fork", and you will be able to operate with them easily.
(I use Android Studio 3.0 with "Git integration" and "GitHub" plugins.)
When you have cloned your forked repository, go to the directory path where your clone resides and the few lines in your Git Bash Terminal.
$ cd project-name $ git remote add upstream https://github.com/user-name/project-name.git # Adding the upstream -> the main repo with which you wanna sync $ git remote -v # you will see the upstream here $ git checkout master # see if you are already on master branch $ git fetch upstream
And there you are good to go. All updated changes in the main repository will be pushed into your fork repository.
The "fetch" command is indispensable for staying up-to-date in a project: only when performing a "git fetch" will you be informed about the changes your colleagues pushed to the remote server.
You can still visit here for further queries