Git cheatsheet

See all local branches

git branch

See all remote branches

git branch -r

See all branches

git branch -a

Clone a git repo

git clone [url]

Create a local branch from remote and switch to it

git checkout -b <branch> <remote>/<branch>

List the current configured remote repository for your fork.

git remote -v

List recent commits

git log

Add a new remote upstream repository

git remote add upstream [url]

See what files are changed

git status

See only changes that have already been added to the Staging Area

git diff --staged

Show unstaged changes

git diff

Push to remote

git push <remote> <branch>

Check out pull requests locally

In .git/config add this under the [remote “upstream”] section

fetch = +refs/pull/*/head:refs/remotes/origin/pr/*:

To check out a particular pull request:

git checkout pr/999

Excellent Git resources
https://services.github.com/on-demand/downloads/github-git-cheat-sheet/
https://www.git-tower.com/learn/git/ebook/en/command-line/introduction#start

Finding a bad commit with git-bisect

When working on a project last year, I was introduced to this handy feature in Git – git bisect.  According to https://git-scm.com/docs/git-bisect, it allows you to:

Use binary search to find the commit that introduced a bug

It is pretty straightforward to use it:

  1. Initiate a bisect session
    $ git bisect start
  2. Specify a good commit
    $ git bisect good d89ec
  3. Specify a bad commit
    $ git bisect bad 96cac
  4. At this point, Git will output something like this:
    Bisecting: 6 revisions left to test after this (roughly 3 steps)
  5. Test your site, if it looks good, type:
    $ git bisect good

    Otherwise,

    $ git bisect good
  6. Now git will output something like:Bisecting: 3 revisions left to test after this (roughly 2 steps)
  7. Repeat Step 5 until there’s git says something like this:d89ecffb672c142cadbbaeef5e31597aac6ecf06 is the first bad commit