This is my personal Git workflow, basically applicable with GitHub, GitLab and Bitbucket, but probably not Gerrit.
Let's assume there is a cool project on GitHub you will work with and collaborate with other contributors, which is called cool with the git URL like:
git@github.com:CoolProject/cool.git
.
- Fork the repo to your own account using online GUI, usually just click the Fork button from your browser.
- Then you have two options to clone the repo:
$ git clone https://github.com/<username>/cool.git
or
$ git clone git@github.com:<username>/cool.git
Either way will work, but with the first way (via HTTPS), you will have to input your password every time you push, which is a bit tedious. Therefore, the second way (via SSH) is recommended, but we need to upload the public SSH key first.
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# This will generate a new ssh key for you
$ sudo apt-get install xclip
# Downloads and installs xclip. If you don't have `apt-get`, you might need to use another installer (like `yum`)
$ xclip -sel clip < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard
Then add your new SSH key to the account on the git web frontend. A long guide (for GitHub) can be found here: https://help.github.com/articles/connecting-to-github-with-ssh/
- After clone the repo, let's add a remote for the upstream to the repo:
$ git remote add upstream git@github.com:CoolProject/cool.git
Now if you run
$ git remote -v
You should see something like
origin git@github.com:<username>/cool.git (fetch)
origin git@github.com:<username>/cool.git (push)
upstream git@github.com:CoolProject/cool.git (fetch)
upstream git@github.com:CoolProject/cool.git (push)
- Make sure to start from master branch, otherwise we need to switch to master
$ git checkout master
- Sync with upstream to have the latest version, and push to your own origin remote if there is any changes from upstream
$ git pull upstream master
$ git push origin master
- Check out a new branch to work on
$ git checkout -b some_branch_name
- Do some work, and commit it
$ git add .
$ git commit -m "some nice commit message"
- Make sure your branch is up-to-date with master
$ git pull upstream master --rebase
Or in more verbose way
$ git checkout master
$ git pull upstream master
$ git push origin master
$ git checkout some_branch_name
$ git rebase master
This is basically to sync with remote upstream, if there's new changes merged to upstream during the time we work on the new branch, we will fetch it and rebase on top of it.
- Push to the forked remote
$ git push origin some_branch_name
- Create a pull request in whatever web frontend for git is being used. If you get some code review feedback from others, do the update and push again. Either amend the existing commit and force-push, or create new commits on top of it, which can be possibly squashed in later steps.
$ git add .
$ git commit --amend
$ git push -f origin some_branch_name
Whenever the team is satisfied with the changes, the pull request will be merged.
- Clean up after the pull request get merged (optional)
$ git checkout master
$ git branch -D some_branch_name
$ git push origin :some_branch_name
$ git remote prune origin (removes objects that are no longer being referenced)