Below outlines the general git workflow I find works well in most situations/teams/projects.
Make sure master is up to date before starting a feature branch.
git checkout master
git pull origin master
Checkout a new feature branch, prefixed with the case/issue number and a brief description of the feature.
git checkout -b feature/123-my-branch
Then, implement the feature/fix the bug. Commit early and often.
Rebase against the upstream frequently to prevent your branch from diverging significantly:
git fetch
git rebase origin/master
Once work on the feature is complete, you will have a branch with a lot of small commits like
- "adding a model and a migration"
- "adding a controller and some views"
- "oh crap - adding tests"... and so on.
If you need to push your local branch to remote, use: git push -u origin feature/123-my-branch
... but try to avoid doing so unless necessary.
We want the rebase to affect only the commits we’ve made to this branch, not the commits that exist on the upstream. To ensure that we only deal with the “local” commits, use:
git fetch
git rebase -i origin/master
Merge your changes back into master
git checkout master
git pull
git merge feature/123-my-branch
Finally, push your changes:
git push origin master
Often when you're working with other developers, someone will be working in areas that you're referencing in your branch and when you go to rebase, you'll come across merge conflicts.
When you rebase, you'll see a list of conflicts and then you can review each one and manually resolve the conflict. Once you resolve the conflict locally, do a git add path/to/conflict-file
and then type git rebase --continue
If at any point during a rebase you want to start over, type git rebase --abort
.
Delete your local feature branch now that you're done with it
git branch -d feature/123-my-branch
If you've pushed your feature branch to origin, then you can remove you remote branch it by running:
git push origin --delete feature/123-my-branch
You can also delete the branch within Github or in the Pull Request.
Note: this is a dangerous command as it destroys history on origin, use wisely!
Check out my list of useful git commands.