Quick Git Reference Guide - northern-bites/nbites GitHub Wiki

Quick references to frequently used git commands. For more information about any of them, click on their title and it will take you to the section further down on the page with a description.

Add

  • Add all files to tracked staging area

    git add -A

Commit with a message

  • Commits all files tracked in the staging area with

    git commit -m <message>

  • Commit all tracked and untracked files

    git commit -a

Checkout

  • Checkout a specific branch.

    git checkout <branch name>

  • Checkout a previous version of a file. Turns file in working directory into exact copy of file from previous commit, then adds the file to the staging area.

    git checkout <commit> <file>

  • Checkout all files from a previous commit

    git checkout <commit>

Cherry-Pick (full description)

git cherry-pick <commit>

  • Save all untracked .

    git checkout <branch name>

  • Checkout a previous version of a file. Turns file in working directory into exact copy of file from previous commit, then adds the file to the staging area.

    git checkout <commit> <file>

  • Checkout all files from a previous commit

    git checkout <commit>

Log

To view entire commit history, with each commit listed on one line each:

``` git log --oneline```
  • Show the last commits

    git log -n <number>

  • Search through commits for specific author

    git log --author="<author name>"

  • Search through commits for specific commit message

    git log --grep="<commit message>"

Revert

Creates a new commit that undoes all changes from <commit> only:

```git revert <commit>```

Reset

  • Remove a file from the staging area.

    git reset <file name>

  • Unstage all files.

    git reset

  • Current head of the branch goes back to , but does not make any changes to code. All changes that have been made since will appear in the staging area. Useful when you forgot to add a file to your last commit. Reset to one below the head, then add missing file to staging area and re-commit.

    git reset <commit>

  • Sets head of branch to , and removes all changes that have been taken since then - including uncommitted changes and committed changes. WARNING: USE CAUTION! YOU COULD LOSE YOUR LATEST WORK!

    git reset --hard <commit>

Clean

Used to remove files from the staging area (does not remove folders / files in .gitignore).

  • Show files that will be removed:

    git clean -n

  • Remove untracked files from the current directory

    git clean -f

  • Remove untracked files from specified path

    git clean -f <path>

  • Remove untracked files and untracked directories

    git clean -df

  • Remove untracked files from current directory, and any files in .gitignore

    git clean -xf

QUICK JUMP TO EXAMPLES AT BOTTOM OF PAGE
  1. Cherry-Pick a Commit
  2. Your guide to Git Stash
  3. Resolving merge conflicts
  4. Git Reset
  5. Git aliases

Setting up the scenario:

  • User Beyonce has branch "dance"
    • branch dance has most recent commit "move hips" and second most recent commit "stretch"
  • User Batman has branch "flying"
    • branch flying has most recent commit "flap arms" and second most recent commit "untangle cape"

Cherry-Pick a Commit

Let's say that you are Batman on your flying branch, and you want to add the code changes that are in the commit "stretch", on Beyonce's dance branch.

  1. Checkout Beyonce's dance branch.

  2. gitk in terminal

  3. Click on the commit you want to cherry-pick, i.e. the one with the comment "stretch"

  4. Copy the SHA1 ID (with ctrl-c only)

  5. KEEP THE gitk terminal window open!!!

  6. In a new terminal window, checkout your own flying branch - i.e. where you want to cherry-pick to.

  7. Type git cherry-pick <SHA1 ID>, which you get the from ctrl-shift-v

  8. Check out the cherry-pick with gitk to make sure it is correct!

  9. You are done! You may close all terminal windows now.

⚠️ **GitHub.com Fallback** ⚠️