Blog A.Wolf

Blog Posts

No results for 'undefined'
Powered by Algolia
Git - Working with Remote Forks

Git - Working with Remote Forks

July 1st, 2019 - 3 min read

If you're not a collaborator of a repository the only way to contribute is by forking the repo and creating pull requests to merge changes back.

That's OK but here I'd like to mention some commands that are useful for working in this situation.

If you're new to Git, I'd recommend getting the basics before reading this as I think it's a more advanced post.

For learning Git the following pages may be interesting:

Add an upstream repo

After forking the original repository and cloning it to your computer it's useful to have the remote upstream available so you can update master if it changed remotely.

Use the command

git remote add upstream git@github.com:<username>/<upstream-reponame>.git

to add the repository that is the base of your fork as upstream.

Next run git pull upstream master to pull every remote change on master. If nothing changed you'll get the prompt Already up to date..

Adding a new remote fork

Sometimes you'd like to access a remote fork so you can have a look at a specific branch. To enable this we have to add the fork by adding it as a remote by running the command git remote add fork-username git@github.com:<username

You can name the remote however you like but I'm usually prefixing it with fork- but just the username would be also OK.

Now as we're having the new remote we can run git fetch fork-username. This will load all details from the remote so we can check out the branches. The branches will be listed during fetch.

So if you'd like to work on a branch from the remote you can create a new local branch by running git checkout -b local-branch fork-username/branch-name.

After this, the checked-out branch local-branch will track the remote branch fork-username/branch-name. Updating the branch will work with git pull but git push may not work because that depends on the access right to the fork.

If you'd like to push the new branch to your fork you can call git push -u origin. That will change tracking to origin and you can now create a PR from this remote branch to the branch of the fork.

Reviewing PR branch with-out adding the remote fork

If you're doing many PR reviews it can become cumbersome to add every fork to your local repo.

You can also check out the changes with the PR number with the following command

git fetch upstream pull/<PR-no>/head:<new-branch>

If you like you can add the following alias to your global git configuration:

git config --global alias.checkout-pr '!sh -c "git fetch upstream pull/$1/head:$2 && git checkout $2" -'

And with the command git checkout-pr it's possible to create a new branch based on a PR number - e.g. git checkout-pr 1234 your-local-branch-name (here 1234 is just an example)

Command overview

git remote
show - to list every remote
add - to add a remote
git fetch upstream pull/PR-no/head:new-branch
Fetch PR from upstream & create a new branch based on a PR number.
git checkout -b new-branch fork-name/branch
Checkout new branch and track remote branch

Note

I think I'm extending this post if I'm having other useful commands to list here. If you think I've missed a command please let me know in the comments below.

©2019 Alexander Wolf