Skip to content

Instantly share code, notes, and snippets.

@danawoodman
Last active September 30, 2018 19:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danawoodman/9493416 to your computer and use it in GitHub Desktop.
Save danawoodman/9493416 to your computer and use it in GitHub Desktop.
Git Workflow

Git Workflow

Below outlines the general git workflow I find works well in most situations/teams/projects.

Feature Branches

Make sure master is up to date before starting a feature branch.

git checkout master
git pull origin master

Add a Feature

Checkout a new feature branch, prefixed with the case/issue number and a brief description of the feature.

git checkout -b feature/123-my-branch

Then, implement the feature/fix the bug. Commit early and often.

Stay Up To Date

Rebase against the upstream frequently to prevent your branch from diverging significantly:

git fetch
git rebase origin/master

Rebase and Merge Your Feature

Once work on the feature is complete, you will have a branch with a lot of small commits like

  • "adding a model and a migration"
  • "adding a controller and some views"
  • "oh crap - adding tests"... and so on.

If you need to push your local branch to remote, use: git push -u origin feature/123-my-branch ... but try to avoid doing so unless necessary.

We want the rebase to affect only the commits we’ve made to this branch, not the commits that exist on the upstream. To ensure that we only deal with the “local” commits, use:

git fetch
git rebase -i origin/master

Merge your changes back into master

git checkout master
git pull
git merge feature/123-my-branch

Finally, push your changes:

git push origin master

Dealing with Conflicts

Often when you're working with other developers, someone will be working in areas that you're referencing in your branch and when you go to rebase, you'll come across merge conflicts.

When you rebase, you'll see a list of conflicts and then you can review each one and manually resolve the conflict. Once you resolve the conflict locally, do a git add path/to/conflict-file and then type git rebase --continue

If at any point during a rebase you want to start over, type git rebase --abort.

Cleanup

Delete your local feature branch now that you're done with it

git branch -d feature/123-my-branch

If you've pushed your feature branch to origin, then you can remove you remote branch it by running:

git push origin --delete feature/123-my-branch

You can also delete the branch within Github or in the Pull Request.

Note: this is a dangerous command as it destroys history on origin, use wisely!

Going Further

Check out my list of useful git commands.

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