needleinthehay.de

Best Practice for Feature Branches

Prepare Feature Branch

  1. Make sure you local Main copy matches the latest remote version:
git checkout main
git fetch origin
git reset --hard origin/main
  1. Create new branch prefixed with feature/<ticket-id>. Push to remote, if you want (e.g. to trigger remote tests or if you collaborate with others on that feature):
git checkout feature/ticket-123-improve-sth
git push -u origin feature/ticket-123-improve-sth

Work on the Feature

  1. Make you code changes and test locally.
  2. In case you want gather feedback before the actual pull request, you should be able to generate a link to a git-diff between the feature branch and the main via the git servers Web-UI. Send this link to fellow devs.

File Pull Request

  1. Push most recent commits from feature-branch to remote:
git push
  1. File pull request in the Web-UI of the git server
  2. Explain your changes in the description text

Clean up commits

Do not make changes to the code in this step!

After incorporating comments/feedback from fellow developers into the code, and after the pull requests got approved, it’s good practice to clean up the commit history before merging.

  1. Fetch latest changes on remote:
git fetch origin
  1. Rebase your feature-branch to main to put your commits to the head of the latest main:
git rebase main

or, if you have a commit history with unnecessary commits, clean up by rebasing interactively:

git rebase -i main
  1. After rebase is done, push feature branch with rewritten history to remote:
git push -f

Merging to Main

Usually this is done in the Web-UI of git server.

  1. Checkout and Main and pull latest remote commits:
git checkout main
git pull
  1. Merge without fast forwarding to keep you (cleaned) commits:
git merge --no-ff feature/ticket-123-improve-sth`
  1. Delete Feature Branch locally and remotely:
git branch -D feature/ticket-123-improve-sth`
git push origin :feature/ticket-123-improve-sth`