Git Commands to Keep a Fork Up to Date
Git Commands to Keep a Fork Up to Date
If using Git intimidates you, just look at these commands to understand how to keep a fork up-to-date.
Join the DZone community and get the full member experience.Join For Free
Open source vulnerabilities are on the rise. Read here how to tackle them effectively.
I've seen the following tweet about git making its way around Twitter recently:
And it's true, you can do most of your work with those commands.
What if you want to fork and contribute to an open source project on GitHub, GitLab or BitBucket though? You're going to need a few more commands so that you can keep your fork up to date, namely
merge. Let's see how they are used.
When you fork a project, you make a copy of it under your own namespace. To work with the project you then clone the repository to your own machine. Let's use a repo I have forked as an example: Twilio's Node.js package. After forking, we clone the repo:
We can now see the first use case for
git remote in the
twilio-node directory and we see the following:
OK, that's not too useful, it just shows we have one remote repo called "origin". Running it again with the
--verbose flag shows a bit more information:
That's better, this shows that the remote repository is the one we cloned and that we can fetch from and push to it.
When contributing something back to the repo, it is easiest to do so on a branch. That keeps the master branch clean and makes it easy to keep up to date. Setting up your changes is covered by the six commands Cory listed in his tweet; you create a new branch with
checkout the branch, make the changes you want,
commit as many times as necessary and finally
push the branch to the origin, your fork. Then you can create your pull request and hope it gets accepted.
Sometime later down the line you find you want to make another pull request, but the original repo has moved on. You need to update your fork so that you're working with the latest code. You could delete your fork and go through the process of forking and cloning the repo again, but that's a lot of unnecessary work. Instead, add the upstream repository as another remote for the repo and work with it from the command line.
Adding the Upstream
To do this, we use the
remote command again, this time to
add a new remote. Find the original repo's URL and add it as a new remote. By convention this remote is called "upstream" though you can call it whatever you want.
Now if we inspect the repo's remotes again, we will see both the origin and the upstream. You can use
-v as a shortcut for
Fetching the Latest
To bring the repo up to date, we can now
fetch the latest from the upstream repo. It looks like this:
git fetch downloads objects and refs from the repository, but doesn't apply it to the branch we are working on. We want to apply the updates to the master branch, so make sure it is checked out.
Merging It All Together
To bring the master branch up to date with the remote
merge the remote's master branch into our own, like so:
If you have kept your work off the master branch this will go ahead smoothly. Finally
push these updates to the fork so that it is up to date too.
Now everything on the master branch of the fork is up to date. If you need to update a different branch, substitute the branch name master for the branch you are working with.
If you have been working with
git pull then you may have already seen a potential shortcut.
pull is the combination of
merge, so to perform these two actions in one command you can run:
That's all you need to know for keeping your fork up to date when contributing to open source. Add the upstream as a new
fetch the upstream repo and
merge the branch you want to update.
For more detail on the various commands here, take a look at "Working with Remotes" from the Pro Git book.
And remember, if you get stuck with something with git, check out Oh shit, git! for all your expletive based ways to save yourself.
Published at DZone with permission of Phil Nash , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.