Since working on our group project a couple of weeks ago, I’ve gotten more familiar with using Git. After suffering through some painful merging of branches into master, I have learned some important lessons of what to do (e.g., make sure your local master is up to date before rebasing and merging branches!) and what not to do (e.g., panic and type arbitrary git commands during a rebase!)
Here are 10 steps for Git success (for branching and merging):
1) Before branching, make sure your local master is up to date with remote master.
git pull origin master
2) Create and check out a new branch.
git checkout -b [NAME_OF_FEATURE_BRANCH]
3) Do all your work on this branch. Once your branch is complete, commit all of your changes and push them to the branch.
git commit -am "helpful commit message!"
4) Check out master and make sure that your local master is up to date with the latest changes on master.
git pull origin master
5) Check out your feature branch.
git co [FEATURE_BRANCH]
6) From your feature branch, rebase master onto the feature branch.
git rebase master
7) Resolve all conflicts on the feature branch. After resolving conflicts in your files, add your changes and continue the rebase.
git add .
git rebase --continue
8) Once the rebase is complete, check out master.
git co master
9) Merge feature branch into master.
git merge [FEATURE_BRANCH]
10) Double check that all conflicts have been resolved and that your code is working properly. If everything looks good, push the merge to remote master.
git push origin master
Some additional thoughts on good practices:
- Keep remote master clean. Write any new code on branches.
- Don’t branch off a feature branch. You should generally branch off master.
- Don’t merge a branch into master until the feature is fully complete.
- Keep your changes small so that you don’t spend too much time working on the same branch.
- Write succinct but descriptive commit messages.
- Once your branch is complete and merged, any new work should be done on a new branch.