Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git Workflow Cheatsheet

Git Workflow CheatSheet

By Ira Herman


Use this workflow when working with teams to take advantage of Pull Requests and keep the master branch clean.

Creating and using your own branch locally:

Create a new branch:

git checkout -b ira

In the future, you don't need the -b for this git repo, that is just to create a NEW branch if it doesn't exist in this repo.

Switch branches:

  • git checkout master Switches to the master branch
  • git checkout ira Switches to the ira branch
  • etc

How to work:

Make updates on your branch and some git commits. When you're ready to merge into the master, do this:

Push your branch up to github

git push origin ira

In github's web interface, Create a PR (Pull Request).

NOTE: If you don't know how to do this, google create pull request github

Anyone on your team can create and submit pull requests, but be sure the Release Manager (Repo master) manages and merges the PR's. In the case of any conflicts that need to be merged via github's PR web interface, the Release Manager and anyone involved in the code conflicts work together to help merge changes and closes pull request.

What to do after a Pull Request(s) has been merged

All team members need to pull updated master to their local computers. Here's how to do it:

  • git checkout master
  • git pull (pulls down the latest master from github, with all the changes from merged in PR's included)
  • Then each team member merges the updated master code into their named branch to get the latest updates.
    • Method 1:

      • Make sure you have your named branch checked out: git checkout ira
      • git merge master to merge all the latest master changes into your branch
    • Method 2:

      • Use this method if you are using a git visualizer and want the branch history to look cleaner (optional):
      • git checkout master
      • git branch -D ira (deletes your branch locally)
      • git checkout -b ira (recreates your branch as a brand new branch using the updated master as a base)

That's the main workflow for teams.


Handling problems - Manual merge (locally)

If you’re not seeing the latest master changes in your pull request, you might need to do a local merge then push your branch back up to github again. Then create a new pull request and you’ll see the correct baseline and changes.

Here’s how to do that:

Pull the latest master:

  • git checkout master
  • git pull

Change to your branch:

  • git checkout ira (or whatever your branch name is 😄)

Manually merge in the updated master branch:

  • git merge master

You’ll get an error about merge conflicts. Read it to see which files have conflicts you need to resolve.

Resolve ALL conflicts in the affected files:

Let’s say you get a conflict in App.js.

  • Open App.js in your code editor.

Any spot where there’s a conflict, you are going to see something like this:

<<<<<<<
One person’s code
=======
Another person’s code
>>>>>>>

You’ll need to delete all the <<<<’s, =====’s, and >>>>’s and replace that section with just the good code that should be there. Sometimes you need to get creative and merge both sets of code together — but be sure to avoid duplication or unclosed parenthesis, etc.

Once you’ve removed and replaced all the conflicts in your affected file(s), do a

  • git add -A
  • git commit -m “resolved merge conflicts”
  • git push ira (replace ira with your branch name)

Congratulations! You have manually merged your code based on the latest master!

Now create a pull request and it should do what you expect it to :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.