Keep the main branch tidy and avoid surprise breakage
If you treat master like a wild animal it will bite you at the worst possible time. This practical git tutorial walks through merging feature branches into master using local git and the GitHub pull request flow. You will learn how to update local master, merge locally or with a PR, handle conflict resolution, and clean up branches without creating a history mess.
Prep work for less drama
Before you even think about merging make sure your local master is current. That single habit prevents 90 percent of the tiny tragedies that happen in version control.
git checkout master
git pull origin master
Now your master matches remote origin. Congrats, future you will thank you.
Local merge for small solo changes
If this is a tiny fix do the work locally then merge and run tests. It is fast and avoids ping pong between branches.
- Checkout your feature branch and run your tests
- Merge into master locally with
git merge feature-branch
- Run tests again and confirm everything behaves
Use a pull request for collaboration and CI
For anything team facing push the branch and open a GitHub pull request. This gives your teammates a chance to roast your code gently and lets CI run automated checks.
Push the feature branch and open a PR to initiate code review and continuous integration. If you update the branch push again and the PR updates automatically.
Conflict resolution without panic
If git complains about conflicts run git status
to see which files need attention. Open the files, pick the correct code, then mark them resolved.
git status
# edit conflicting files to choose the desired code
git add path/to/file
git commit
If you hate merge commits prefer rebasing. Rebase your feature branch on top of master then resolve conflicts during the rebase.
git checkout feature-branch
git fetch origin
git rebase origin/master
# resolve conflicts then
git add path/to/file
git rebase --continue
# finally force push with care
git push --force-with-lease origin feature-branch
Push and update the pull request
After a successful local merge push master or push the feature branch to update the pull request. CI will usually run and report success or failure on the PR page. If tests fail fix the issues and push again.
Merge on GitHub with good judgment
On the PR page click the green merge button when everyone is happy. Choose the merge method that fits your repo policy. A merge commit preserves history while squash keeps the main branch linear and easy to read. Fast forward may be fine for trivial workflows.
Clean up after the merge
Delete the remote feature branch once the PR is merged to avoid clutter. Use the GitHub button or run this command locally.
git push origin --delete feature-branch
# update your local master too
git checkout master
git pull origin master
Quick checklist for human survival
- Update local master with
git pull origin master
before merging - Run tests before and after merging
- Open a pull request for team work to get reviews and CI feedback
- Resolve conflicts by editing files then
git add
andgit commit
- Prefer rebase if you need a tidy linear history and know how to force push safely
- Delete the remote feature branch when it is merged
Following this workflow keeps your version control sane and your teammates slightly less likely to send angry messages. Git and GitHub are powerful tools. Use them with respect and a little sarcasm and you will survive another release.