Best Practice for Feature Branches
Prepare Feature Branch
- Make sure you local Main copy matches the latest remote version:
git checkout main
git fetch origin
git reset --hard origin/main
- 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
- Make you code changes and test locally.
- 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
- Push most recent commits from feature-branch to remote:
git push
- File pull request in the Web-UI of the git server
- 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.
- Fetch latest changes on remote:
git fetch origin
- 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
- 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.
- Checkout and Main and pull latest remote commits:
git checkout main
git pull
- Merge without fast forwarding to keep you (cleaned) commits:
git merge --no-ff feature/ticket-123-improve-sth`
- Delete Feature Branch locally and remotely:
git branch -D feature/ticket-123-improve-sth`
git push origin :feature/ticket-123-improve-sth`