Skip to content

Instantly share code, notes, and snippets.

@ltfschoen
Forked from db/bulletproof-git-workflow.md
Last active January 18, 2017 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ltfschoen/3c7a085f132baf4aff13c9d561b35d03 to your computer and use it in GitHub Desktop.
Save ltfschoen/3c7a085f132baf4aff13c9d561b35d03 to your computer and use it in GitHub Desktop.
bulletproof git workflow

Bulletproof Git Workflow

start working

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:

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

(<n> is the number of commits)

git rebase -i HEAD~<n>
# eg. git rebase -i HEAD~3

sync with master

Get latest from master:

git checkout master
git pull

Rebase your work on latest master:

git checkout feature/my-work
git rebase -i master

Resolve conflicts and:

git add my/resolved/file.js
...
git rebase --continue

...until done.

If you get stuck, git rebase --abort and phone a friend.

code review

View changes to a specific branch of a repository:

  • Clone repository
  • Show remote branches
  • Map a remote branch to a local branch
  • Checkout the local branch
  • Rebase against the remote branch in case another user adds commits to it (i.e. if get error hint Updates were rejected because the remote contains work that you do hint: not have locally)
  • Push changes to remote repo:
$ git clone https://github.com/<username_or_org_name>/<repository_link>
$ git branch
  * master
$ git branch -r
  origin/HEAD -> origin/master
  origin/feature/my-feature1
  origin/feature/my-feature2
  origin/master
$ git fetch origin feature/my-feature1:feature/my-feature1
$ git branch
  feature/my-feature1
  * master
$ git checkout feature/my-feature1
$ git branch --set-upstream-to=origin/feature/my-feature1 feature/my-feature1
$ git pull --rebase origin feature/my-feature1
$ git checkout master

$ git pull --rebase origin master
$ git checkout feature/my-feature1

# interactively (with optionally squashing) rebase latest changes in master with local branch
$ git rebase -i master

# force push to remote branch overwriting history with latest from master and the remote branch both included
$ git push -f origin feature/my-feature1

If working on an NPM package, use the Bulletproof NPM Workflow.

Share your branch:

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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment