You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First of all, fork the upstream repo that you want to collaborate with, through GitHub UI.
Let's assume the upstream is: https://github.com/the-org/the-repo.git.
And let's assume I'm the one who forks it so this repo will be forked into my namespace, which is brightzheng100, as:
# Let's export some variables to make later commands genericexport NAMESPACE_UPSTREAM=the-org
export NAMESPACE_MINE=brightzheng100
export REPO_NAME=the-repo
Clone it back to our local workspace
Remember to clone it back by using git protocol, instead of https, so that it's updatable.
Whenever you want to update your fork with the latest upstream changes:
# Fetch from upstream
git fetch upstream
# View all branches, including those from upstream
git branch -va
# Checkout your master branch and merge upstream
git checkout master
git merge upstream/master
Working on it
Create a branch when trying to contribute
Whenever you begin work on a new feature or bugfix, it's important that you create a new branch.
Not only is it proper git workflow, but it also keeps your changes organized and separated from the master branch so that you can easily submit and manage multiple pull requests for every task you complete.
To create a new branch and start working on it:
# Checkout the master branch - you want your new branch to come from master
git checkout master
# Create a new branch named say add-xxx-feature -- please be informative enough, and check out to it
git branch -b add-xxx-feature
Now start hacking away and making whatever changes you want to in this branch
Rebase before submitting a Pull Request
Always rebase your development branch so that merging it will be a simple fast-forward that won't require any conflict resolution work.
# Fetch upstream master
git fetch upstream
# Merge with your repo's master branch
git checkout master
git merge upstream/master
# Rebase your development branch
git checkout add-xxx-feature
git rebase master
Squash your commits if required
It may be desirable to squash some of your smaller commits down into a small number of larger more cohesive commits.
You can do this with an interactive rebase process:
# Rebase all commits on your development branch
git checkout add-xxx-feature
git rebase -i master
This will open up a text editor where you can specify which commits to squash.
Tips: to perform an interactive rebase of only the last 3 commits: git rebase -i HEAD~3
There are a couple of options that we can perform, but please remember some important ones:
pick is to continue to pick the commit as is
reword is to use the commit but change the commit message
squash is to squash the commit so the content will be merged into previous one but the commit will be deleted
fixup is like squash but discard the commit's log message
Once you've committed and pushed all of your changes to your GitHub repo, go to the page for your fork on GitHub, select your development branch, and click the pull request button.
If you need to make any adjustments to your pull request, just push the updates to GitHub: Your pull request will automatically track the changes on your development branch and update.
Continue to Squash your commits if required step if you want to further squash the commits.
Upstream Owner: accepting and merging a Pull Request
Check out and test the PRs
Open the .git/config file and add a new line under [remote "origin"], like this:
Now you can fetch and checkout any pull request so that you can test them:
# Fetch all pull request branches
git fetch origin
# Checkout out a given pull request branch based on its PR number
git checkout -b pr-123 pull/origin/123
Keep in mind that these branches will be read only and you won't be able to push any changes.
Automatically merge a Pull Request
In cases where the merge would be a simple fast-forward, you can automatically do the merge by just clicking the button on the pull request page on GitHub.
Manually merge a Pull Request
To do the merge manually, you'll need to checkout the target branch in the source repo, pull directly from the fork, and then merge and push.
# Checkout the branch you're merging to in the target/upstream repo
git checkout master
# Pull the development branch from the fork repo where the pull request development was done
git pull https://github.com/$NAMESPACE_MINE/$REPO_NAME.git add-xxx-feature
# Merge the development branch
git merge add-xxx-feature
Do take care of the conflicts if there are any
Push it to upstream repo
# Push master with the new feature merged into it
git push origin master
Clean up
Now that you're done with the development branch, you're free to delete it.