My most used Git commands on open source projects.

The basic step when committing to open source projects is to fork the project.
Then the process is easy you create your branch and you make a pull request. However from time to time you need to adjust you branch based on the latest changes.

Sync fork

This is how you sync your fork to the original one.

#set remote if it does not exist. This is the project you forked from
git remote add upstream
git fetch upstream
git checkout master
git merge upstream/master

Synchronize from original Repo

This is pretty easy but you might want something more than just synchronizing with the original repository.

For example there might be a pull request which never got merged for various reasons and you wan’t to pick up from where it was left.

The first step is to add the repository needed

git remote add $remote_repo_identifier $remote_repo_url

So we just added another remote to our repository.

The next step is to fetch the branches from the remote.

git fetch $remote_repo_identifier

Then you can switch to the branch of your choice, continue make a new branch and continue with a pull request.

git fetch $remote_branch

Remove the upstream

 git remote remove $remote_repo_identifier

Set upstream to the current repo

git branch -u $remote_repo_identifier/$remote_branch $remote_branch
git branch --set-upstream-to=$remote_repo_identifier/$remote_branch $remote_branch

For example change the upstream to the origin one

git push --set-upstream origin $remote_branch

Continue on an abandoned Pull Request

It is very common to see PRs getting left behind for various reasons. If the value, the PR was to deliver, is what you seek, it makes sense to pick up where it was left improve it and make it up to date. Obviously in those cases chances are you just pick up the changes from the original fork to your own fork and continue from there (it can be difficult to contribute to the original forked PR).

#Add the fork of the PR as a remote
git remote add $remote_repo_identifier $remote_repo_url
#Fetch the branch from the fork
git fetch $remote_repo_identifier
#checkout to a new branch taken from the original PR
git checkout -b $remote_branch $remote_repo_identifier/$remote_branch
#set upstream to your fork
git branch $remote_branch --set-upstream-to origin/$remote_branch

Rebase from original repo

Now that we got the upstream commands in place let’s see how to rebase from the origin fork.
The rebase is a very common thing.
Branch starts from fork code is added, then a merge is getting closed and as expected a rebase is asked because master had some changes.

First let’s add and fetch from the upstream

git remote add upstream http://the-repo
git fetch upstream

Then checkout to the branch of your choice and rebase

git checkout branch-under-pr
git rebase upstream/master

Once your rebase is successful proceed on pushing

git push -f origin branch-under-pr

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.