Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I update an outdated fork?

Tags:

git

github

Months ago, I forked a repository, made changes and submitted a pull request, which was eventually accepted into master. The code of my fork has been untouched on my hard drive since. Now, I'd like to contribute again to the same repository. The code from my fork is out of sync with master, because it has changes that were never pulled into master, and because master has moved on. Should I delete my fork and refork, or is there a better way?

I thought this might have been asked before. While searching, I found Pull new updates from original GitHub repository into forked GitHub repository, but the StackOverflower in that question wants to keep local changes, whereas I want to discard all local changes and take whatever is currently in master.

like image 376
smockle Avatar asked Dec 07 '22 04:12

smockle


1 Answers

If there's nothing in your fork that you want to keep, deleting and re-forking is certainly an option. If you don't want to do that though, there are plenty of other ways to update your repo.

I'd probably start by adding the upstream repo as a remote, if you haven't already. Do this with:

git remote add upstream <clone-url>

Then, fetch all the changes from the upstream remote:

git fetch upstream

Now, you can do whatever you want with those newly fetched commits. For example, to reset your local master branch to the one in the upstream repo, run this:

git checkout master
git reset --hard upstream/master

You can push that change up to your GitHub fork like this:

git push --force origin master

For more on working with remotes, I'd recommend this chapter on working with remotes from the Pro Git book: 2.5 Git Basics - Working with Remotes

like image 80
Ajedi32 Avatar answered Dec 11 '22 12:12

Ajedi32