Git opens up the VIM editor by default. Change it to use the editor of your choice.
- Pull master
git checkout -b my-feature-branch
- Commit as you go
- Push work up
- Integrate latest changes from master with
git merge master
orgit pull origin master
This workflow leads to polluted master branch commit history that makes it more difficult to...
- Understand project history
- Cherry-pick changes into releases
- Deploy or rollback to past versions
Keep your master branch commit history clean by:
- Squashing commits before merge
- Rebasing to integrate changes into feature branches
Warning: only squash and rebase commits that exist exclusively in your feature branch and not in the mainline branch. Rewriting commits that are already master will lead to misery for your team.
Rebasing accomplishes roughly the same thing as merging, but goes about it in a different way. Rebasing takes all the commits in your feature branch and replays them over the latest commits from master.
To rebase, use the command git rebase master
instead of git merge master
, and git pull --rebase origin master
instead of git pull origin master
. You may need to resolve conflicts just like when you merge.
For more information, see Atlassian's article on Merging vs Rebasing.
Suppose you are in a feature branch that has 4 commits ahead of master. To squash the commits, you'd run the following command:
git rebase -i HEAD~4
The -i
opens an editor to let you rewrite commits. The HEAD~4
loads your last 4 commits for editing.
After your editor opens, follow the instructions presented to squash commits and then force push to upload your branch.
For more information, see Git's documentation on Rewriting History.
As mentioned earlier, never rebase or squash commits that exist outside of the feature branch you are working on. Rewriting commits that are already master will lead to misery for your team.
The basic workflow looks like:
- Check out a feature branch
- Do work in your feature branch, committing early and often, and pushing up to a "WIP" pull request
- Rebase frequently to incorporate upstream changes on master
- When you're done with your feature, interactive rebase and squash your commits
- Force push your cleaned up feature branch to have it reviewed and merged