# Bulletproof Git Workflow ## start working ```bash git checkout master git pull git checkout -b feature/my-work # edit your files git add changed-file-a.js git commit -m 'added my work' ``` Do some more work: ```bash git add changed-file-b.js git commit -m 'fixed something related to my work' git add changed-file-c.js git commit -m 'added the last bit to complete my work' ``` ## tidy up Tidy up your commits by interactively (`-i`) rebasing your branch. During the rebase you will: - `reword` the top commit - `fixup` other commits as appropriate (`` is the number of commits) ```bash git rebase -i HEAD~ # eg. git rebase -i HEAD~3 ``` ## sync with master Get latest from master: ```bash git checkout master git pull ``` Rebase your work on latest master: ```bash git checkout feature/my-work git rebase -i master ``` Resolve conflicts and: ```bash git add my/resolved/file.js ... git rebase --continue ``` ...until done. If you get stuck, `git rebase --abort` and phone a friend. ## code review If working on an NPM package, use the [Bulletproof NPM Workflow](https://gist.github.com/db/065107eea4633d63dd88). Share your branch: ```bash git push origin feature/my-work ``` Go to Github/Bitbucket and create your pull request. In the pull request provide links to passing automation tests that have been executed on your branch. ## review feedback Use all of the previous workflow to apply updates to your branch from feedback in the pull request. As you've already pushed your branch to the remote, and you've re-tidied up, you will need to **force push** a newly rebased & modified history to the remote. **Force pushing is dangerous** as it will completely replace the remote copy of your branch with your local copy of your branch. This means that if others have pushed to your remote branch (like to add automation tests for your feature), their commits will be lost. Before force-pushing, fetch and rebase on your remote to collect any updates, just in case. ```bash git pull --rebase origin feature/my-work # just in case git push --force origin feature/my-work ``` ## done Use the merge button in the pull request to merge to master, squash and rename commits as necessary, and delete the feature branch.