Skip to content

Instantly share code, notes, and snippets.

@jmackie
Last active March 21, 2018 00:13
Show Gist options
  • Select an option

  • Save jmackie/09f72acff48e24491a224a96c7d22061 to your computer and use it in GitHub Desktop.

Select an option

Save jmackie/09f72acff48e24491a224a96c7d22061 to your computer and use it in GitHub Desktop.
Helpful Git tips

Git Tips

A friendly cheatsheet that answers the questions I find myself asking embarrassingly often...

"I should probably work today, how do I sync up with everyone?"

git fetch origin               # update everything you're not working on
git diff master origin/master  # see what's changed
git merge origin/master        # update what you're working on
#   ^ There might be merge conflicts to deal with...

NOTE: git pull conveniently wraps the above commands, but best to know what's going on.

"What other branches can I work on?"

git branch -r   # r(emote) branches

"That branch looks funner than my branch, how do I work on that?"

git checkout --track origin/feature      # now on branch 'feature'
git checkout -b difficult origin/feature # now on branch 'difficult'

"Whats the difference between a local and remote-tracking branch?"

Local branches are what you work on. But a local branch can and often will (confusingily) track a remote-tracking branch. Basically, the remote branch is not on your machine, so all you can really do with it is:

  • Update from it with git fetch
  • Merge from it into your current branch
  • Create new local branches based on it

"How do I branch off a branch?"

git checkout plzwork
git checkout -b hotfix plzwork

"How do I merge this code what I did?"

git checkout target           # or master, or whatever
git merge --squash --ff-only  # flags are optional, but good defaults

NOTE: with both merge and rebase you invoke them from the target branch. That is, the branch you're merging to or the branch you're rebasing.

"I merged, woot. Now what?"

If you want to keep the merged branch going...

git checkout spinoff
git rebase target

Or if you're done with it.

git branch -d spinoff  # d(elete)

"How do I make my branch into a PR?"

git checkout -b hotfix
# ...commits n stuff
git push origin hotfix
# ...mange in the repo UI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment