Skip to content

Instantly share code, notes, and snippets.

@morganprecision
Last active October 31, 2022 09:59
Show Gist options
  • Save morganprecision/4120873 to your computer and use it in GitHub Desktop.
Save morganprecision/4120873 to your computer and use it in GitHub Desktop.
Git cheatsheet

Add files ...

only changed files: git add -u

only changed and new (but not deleted) git add .

changed, new, and deleted git add -A

Pull remote changes

git fetch origin

Merge remote changes into local branch

i.e., if you are in the local development branch:
git merge origin/development

Show diffs without regard to line endings

git diff -b

Undo uncommitted changes to a file:

git checkout <file>

Undo last commit (if you haven't pushed):

  • If you just want to change the commit message:
    git commit --amend -m "New commit message"

  • You can also add more files and then use --amend to have those files included in the commit.

  • If you want to rewind to the point before you did "git add":
    git reset HEAD^

  • If you want to rewind to the point before you did "git commit" (still have changes staged):
    git reset --soft HEAD^

See this Stack Overflow answer for more details.

Get history ...

  • Show diffs in addition to commit messages:
    git log -p

  • Find a changeset of interest for a particular file:
    git log -- path/to/file
    (use dashes if file has been deleted; put quotes around dashes if in PowerShell)

  • Show what was changed at a specific revision:
    git show [type the first few characters of the commit identifier]

  • Search for a commit based on content of commit message:
    git log --grep=pattern

  • Search for a commit based on something that was changed (i.e., the diff):
    git log -i -S string_to_search -- the -i makes it case-insensitive

  • Search for a commit based on diff content, and show the diff, along with other files changed:
    git log -i -S string_to_search -p --pickaxe-all

  • Show filenames that were changed at a specific revision:
    git show --name-only [commit identifier] -- shows only filenames

  • Show the entire file as it looked at a specific revision:
    git show [commit identifier]:filename.ext

Forget a file that has already been committed to the repository:

git rm --cached <file>

Show only modified files

git ls-files -m

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