Skip to content

Instantly share code, notes, and snippets.

@dlaehnemann
Last active August 11, 2024 18:55
Show Gist options
  • Save dlaehnemann/e6c8f46eee80112267af70ae92a3df13 to your computer and use it in GitHub Desktop.
Save dlaehnemann/e6c8f46eee80112267af70ae92a3df13 to your computer and use it in GitHub Desktop.
contributing to github repo without write access: pull from origin repo, push to your own fork

Whenever I want to create pull requests to a repo that I don't have write access to, I:

  1. Fork the original repo to my account.
  2. Clone the original repo to my local machine.
  3. Add my fork as an additional remote and make it the push default.
  4. Make changes in a new branch locally.
  5. Push this branch to my fork.
  6. Create a pull request from there.
git clone https://github.com/<origin_org>/<repo_name>.git
cd <repo_name>
git remote add fork [email protected]:<your_name>/<your_repo>.git
git config remote.pushDefault fork

This way, at a later time point I can easily:

  1. Pull changes from origin into the local master branch.
  2. Create another contribution branch.
  3. Push that to my fork.
  4. Create a new pull request.
cd <repo_name>
git checkout master
git pull
git push # update the fork, which can be relevant for continuous integration checking against the master branch
git checkout -b new-contribution-branch
# make changes and commit
git push

Sources:

@SiddheshKukade
Copy link

Thank u for this appreciate your work

@brianjbayer
Copy link

😍 Thank you, this is great and was very helpful.

However with GitHub removing the password authentication for https, I did need to use my SSH access to push the fork, so to "Add my fork as an additional remote and make it the push default.", I used the command...

git remote add fork [email protected]:<your_name>/<your_repo>.git

@dlaehnemann
Copy link
Author

Cheers @brianjbayer, I've updated the gist accordingly.

Although, to be honest, I rarely use this setup nowadays. I usually just use throw-away forks for such contributions, as this is so quick and easy with GitHub:

  1. Create a fork of the repo on GitHub.
  2. Optional: Clone the fork onto my local machine.
  3. Introduce changes on a new branch (for small changes, this is easily done on the fork directly through the GitHub interface).
  4. Create a pull request.

Once the pull request is resolved, GitHub nowadays offers direct buttons to:

  1. Delete the branch you used for it.
  2. Delete the fork you used for it.

And if I contribute to the same repo again, I just repeat this easier workflow. Also, this avoids cluttering my personal GitHub profile with lots of long-forgotten forks...

So I'd reserve the original gist for projects where I don't have write access, but contribute regularly -- which is very rare.

@brianjbayer
Copy link

🖖

@simonlrostron
Copy link

Can confirm this works -- thank you!

For those using Sourcetree:

  1. Clone the original repository.
  2. Click Settings.
  3. On the Remotes pane, click Add.
  4. For the URL/Path, enter the value for your fork. Configure the account settings appropriately.
  5. On the Advanced tab, you may wish to specify different values for the name and email address used when committing.
  6. Create a local branch and commit your changes.
  7. When ready to push, be sure to select your personal remote as the target.
  8. Create a pull request to merge the working branch from your fork into the default branch from the original repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment