GitHub Workflow

This doc is lifted from Kubernetes github-workflow.

Git workflow

1 Fork in the cloud

  1. Visit https://github.com/karmada-io/karmada
  2. Click Fork button (top right) to establish a cloud-based fork.

2 Clone fork to local storage

Per Go’s workspace instructions, place Karmada’ code on your GOPATH using the following cloning procedure.

Define a local working directory:

  1. # If your GOPATH has multiple paths, pick
  2. # just one and use it instead of $GOPATH here.
  3. # You must follow exactly this pattern,
  4. # neither `$GOPATH/src/github.com/${your github profile name/`
  5. # nor any other pattern will work.
  6. export working_dir="$(go env GOPATH)/src/github.com/karmada-io"

Set user to match your github profile name:

  1. export user={your github profile name}

Both $working_dir and $user are mentioned in the figure above.

Create your clone:

  1. mkdir -p $working_dir
  2. cd $working_dir
  3. git clone https://github.com/$user/karmada.git
  4. # or: git clone git@github.com:$user/karmada.git
  5. cd $working_dir/karmada
  6. git remote add upstream https://github.com/karmada-io/karmada.git
  7. # or: git remote add upstream git@github.com:karmada-io/karmada.git
  8. # Never push to upstream master
  9. git remote set-url --push upstream no_push
  10. # Confirm that your remotes make sense:
  11. git remote -v

3 Branch

Get your local master up to date:

  1. # Depending on which repository you are working from,
  2. # the default branch may be called 'main' instead of 'master'.
  3. cd $working_dir/karmada
  4. git fetch upstream
  5. git checkout master
  6. git rebase upstream/master

Branch from it:

  1. git checkout -b myfeature

Then edit code on the myfeature branch.

4 Keep your branch in sync

  1. # Depending on which repository you are working from,
  2. # the default branch may be called 'main' instead of 'master'.
  3. # While on your myfeature branch
  4. git fetch upstream
  5. git rebase upstream/master

Please don’t use git pull instead of the above fetch / rebase. git pull does a merge, which leaves merge commits. These make the commit history messy and violate the principle that commits ought to be individually understandable and useful (see below). You can also consider changing your .git/config file via git config branch.autoSetupRebase always to change the behavior of git pull, or another non-merge option such as git pull --rebase.

5 Commit

Commit your changes.

  1. git commit --signoff

Likely you go back and edit/build/test some more then commit --amend in a few cycles.

6 Push

When ready to review (or just to establish an offsite backup of your work), push your branch to your fork on github.com:

  1. git push -f ${your_remote_name} myfeature

7 Create a pull request

  1. Visit your fork at https://github.com/$user/karmada
  2. Click the Compare & Pull Request button next to your myfeature branch.

If you have upstream write access, please refrain from using the GitHub UI for creating PRs, because GitHub will create the PR branch inside the main repository rather than inside your fork.

Get a code review

Once your pull request has been opened it will be assigned to one or more reviewers. Those reviewers will do a thorough code review, looking for correctness, bugs, opportunities for improvement, documentation and comments, and style.

Commit changes made in response to review comments to the same branch on your fork.

Very small PRs are easy to review. Very large PRs are very difficult to review.

Squash commits

After a review, prepare your PR for merging by squashing your commits.

All commits left on your branch after a review should represent meaningful milestones or units of work. Use commits to add clarity to the development and review process.

Before merging a PR, squash the following kinds of commits:

  • Fixes/review feedback
  • Typos
  • Merges and rebases
  • Work in progress

Aim to have every commit in a PR compile and pass tests independently if you can, but it’s not a requirement. In particular, merge commits must be removed, as they will not pass tests.

To squash your commits, perform an interactive rebase:

  1. Check your git branch:

    1. git status

Output is similar to:

  1. On branch your-contribution
  2. Your branch is up to date with 'origin/your-contribution'.
  1. Start an interactive rebase using a specific commit hash, or count backwards from your last commit using HEAD~<n>, where <n> represents the number of commits to include in the rebase.

    1. git rebase -i HEAD~3

Output is similar to:

  1. pick 2ebe926 Original commit
  2. pick 31f33e9 Address feedback
  3. pick b0315fe Second unit of work
  4. # Rebase 7c34fc9..b0315ff onto 7c34fc9 (3 commands)
  5. #
  6. # Commands:
  7. # p, pick <commit> = use commit
  8. # r, reword <commit> = use commit, but edit the commit message
  9. # e, edit <commit> = use commit, but stop for amending
  10. # s, squash <commit> = use commit, but meld into previous commit
  11. # f, fixup <commit> = like "squash", but discard this commit's log message
  12. ...
  1. Use a command line text editor to change the word pick to squash for the commits you want to squash, then save your changes and continue the rebase:

    1. pick 2ebe926 Original commit
    2. squash 31f33e9 Address feedback
    3. pick b0315fe Second unit of work
    4. ...

Output (after saving changes) is similar to:

  1. [detached HEAD 61fdded] Second unit of work
  2. Date: Thu Mar 5 19:01:32 2020 +0100
  3. 2 files changed, 15 insertions(+), 1 deletion(-)
  4. ...
  5. Successfully rebased and updated refs/heads/master.
  1. Force push your changes to your remote branch:

    1. git push --force

For mass automated fixups (e.g. automated doc formatting), use one or more commits for the changes to tooling and a final commit to apply the fixup en masse. This makes reviews easier.

Merging a commit

Once you’ve received review and approval, your commits are squashed, your PR is ready for merging.

Merging happens automatically after both a Reviewer and Approver have approved the PR. If you haven’t squashed your commits, they may ask you to do so before approving a PR.

Reverting a commit

In case you wish to revert a commit, use the following instructions.

If you have upstream write access, please refrain from using the Revert button in the GitHub UI for creating the PR, because GitHub will create the PR branch inside the main repository rather than inside your fork.

  • Create a branch and sync it with upstream.

    1. # Depending on which repository you are working from,
    2. # the default branch may be called 'main' instead of 'master'.
    3. # create a branch
    4. git checkout -b myrevert
    5. # sync the branch with upstream
    6. git fetch upstream
    7. git rebase upstream/master
  • If the commit you wish to revert is a:

    • merge commit:

      1. # SHA is the hash of the merge commit you wish to revert
      2. git revert -m 1 SHA
    • single commit:

      1. # SHA is the hash of the single commit you wish to revert
      2. git revert SHA
  • This will create a new commit reverting the changes. Push this new commit to your remote.

  1. git push ${your_remote_name} myrevert