There are a lot of git workflows out there. This is mine, based on working with Git for 10-something years now. It makes use of
git rebase, so turn away now if you're scared of rebasing (and then go read thisÂ and realize it's just another tool). BONUS: be sure to check out my handy git helpers too!
- Find your â€˜origin’ repo
- Fork the origin repo to create your own repo
- If using Bitbucket, it will automatically setup your repo to â€˜sync’ with â€˜origin’ unless you uncheck 'keep in sync'
- Clone your fork to your local machine, where â€˜origin’ will refer to your fork.
- Create a feature branch off of your local master
git checkout –b feature-branch-namereplacing "feature-branch-name" with whatever name you want to give your branch
- Good practice: make your branch names unique and specific to the task or feature you're working on. "branch-1" is not a useful branch name, "time-travel-feature" is better, "adding-unlimited-undo-in-cms" is best.
- Optional: immediately push the new, empty feature branch to your fork’s remote endpoint using
git push –u origin feature-branch-name. This sets up â€˜tracking’ so that every time you push going forward you don’t have to specify where, you just do
git pushand it will already know where to push to. Same with
- DO YOUR WORK
- Commit early and often. I recommend committing every time you get something to â€˜done’ or at least working, but WIP (work in progress) commits are recommended anytime you need to switch tasks or just want a waypoint for what you’re working on.
- Make a â€˜final’ commit, if you haven’t already.
- Switch to your local master branch and pull down the latest changes to it.
git checkout master
- Switch back to your feature branch and rebase it onto the latest master.
git checkout feature-branch-name
git rebase master
- Fix any merge conflicts, if any.
- FIX FIX FIX
git add .Â orÂ
git add /file(s)/that/were/changed
git rebase –continue
- Having resolved any conflicts, push up your feature branch again, forcing your remote to overwrite whatever is already there (because rebasing will change the branch’s history and thus change its SHA checksum).
git push –f
- Create a pull request from your feature branch back up to the original â€˜origin’ repo’s master branch.
- i.e. fork/feature-branch-name -> origin/master
- Rest, reflect on what you have accomplished, and then start over again from step 4.
The cycle can be illustrated like this:
OriginMaster -> ForkMaster -> LocalMaster -> LocalFeatureBranch -> ForkFeatureBranch -> OriginMaster
Bitbucket will auto-sync OriginMaster and your ForkMaster if you're using Bitbucket. Then you have to pull down any updates from ForkMaster to your LocalMaster. Then, you switch to your LocalFeatureBranch and rebase it onto the latest changes in LocalMaster. Then you push up to ForkFeatureBranch, where you create a Pull Request from there to the OriginMaster, and the cycle starts over again.
Good practice: any time you hear that a significant amount of new work has been merged into the â€˜origin’ repo’s master branch, it’s a good idea to update your local master and rebase your feature branch. The more you keep your feature branch up-to-date with the latest changes, the easier merge conflicts will be to solve, and the less new stuff you’ll be surprised by as you work.
If you haven't checked it out yet, go here for my handy git helpers that make this workflow super easy, fast, and intuitive (from the command line).