---
title Git commands to work with upstream on Github
author Charles Shih <[email protected]>
date 2018-09-29
updated 2018-10-22
tags git,github,pull request,upstream,downstream
---
Create downstream repo
1. Fork the repo
It would be easy, just fork the repo in github.com.
For example: fork https://github.com/intel/lkp-tests to https://github.com/schen2015/lkp-tests
2. Set upstream
$ git clone [email protected]:SCHEN2015/lkp-tests.git
$ cd lkp-tests
$ git remote add intel https://github.com/intel/lkp-tests.git
Ideas about the branch
There are 3 kinds of branch:
- master → keep up-to-date with upstream
- devel → code development
- pr → contribute to upstream
Code development
1. Setup devel branch
Create a new devel branch
$ git branch rhel-dev --set-upstream origin rhel-dev
$ git checkout rhel-dev
Use the existed devel branch
$ git checkout -b rhel-dev origin/rhel-dev
2. Commit to devel branch
Do some code change in this branch…
$ git add .
$ git commit -m "<description here>"
$ git push
Update downstream repo
1. Rebase master branch
$ git checkout master
$ git fetch intel
$ git rebase intel/master
Since master branch only be used to follow the upstream, there would be no code conflict.
2. Rebase devel branch
$ git checkout rhel-dev
$ git rebase master
You many need to solve the conflicts, just edit the checked out file, then git add
and git rebase --continue
$ git show
3. Update the remote branches
$ git push origin master
$ git push origin rhel-dev
You may need to use --force
to update the remote branch, if the local one diverged from it after rebasing.
Contribute to the upstream
1. Commit in devel branch
2. Make the downstream repo up-to-date (recommended)
$ git checkout master
$ git rebase intel/master
3. Create PR branch
$ git checkout master
$ git branch pr-20181018-1
4. Cherry-pick commits from devel branch
$ git checkout pr-20181018-1
$ git cherry-pick <commit id>
(solve conflict and execute git commit
)
$ git log
$ git push --set-upstream origin pr-20181018-1
5. Create PR to upstream
Go to the homepage of the upstream repo: https://github.com/intel/lkp-tests
Click the “Compare & pull request” button in the banner which says “pr-20181018-1 (less than a minute ago)”
Then follow the instruction on the webpage to create this PR.
6. Delete the PR branch (optional)
After upstream merge this PR, you can delete the PR branch in downstream safely.
In the PR page, click “Delete Branch” button followed “Pull request successfully merged and closed”.
Or you can also delete this PR branch in CLI:
$ git checkout master
$ git branch -d pr-20181018-1
$ git push origin :pr-20181018-1
(this command will delete the remote branch)
In this project, I prefer to keep the remote branch and delete the local one.
7. Rebase master branch
$ git checkout master
$ git fetch intel
$ git rebase intel/master
Since master branch only be used to follow the upstream, there would be no code conflict.
After that you will be able to see your commits by searching comments in git log.
8. Rebase devel branch
$ git checkout rhel-dev
$ git rebase master
You may need to git rebase --skip
for the commits you have contributed to the upstream.
9. Update the remote branches
$ git push origin master
$ git push origin rhel-dev
You may need to use --force
to update the remote branch, if the local one diverged from it after rebasing.