Skip to content

Instantly share code, notes, and snippets.

@hugoalvarado
Forked from betawax/gist:1053967
Last active November 1, 2022 06:04
Show Gist options
  • Save hugoalvarado/123d73a41b53b29bc4d5c59607a68047 to your computer and use it in GitHub Desktop.
Save hugoalvarado/123d73a41b53b29bc4d5c59607a68047 to your computer and use it in GitHub Desktop.
Git cheat sheet

Git cheat sheet

Common commands

General

Create repository:

git init

Add file:

git add <file>

Remove file:

git rm <file>

Move or rename file:

git mv <from> <to>

Commit changes:

git commit

Show changes:

git status

Show log:

git log

Show log with tags:

git log --decorate

Search thru commit messages:

git log --grep="<search>"

Branches

Show branches:

git branch

Create branch:

git branch <branch>

Create and checkout branch:

git checkout -b <branch>

Checkout branch:

git checkout <branch>

Checkout remote branch:

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

Rename branch:

git branch -m <from> <to>

Delete branch:

git branch -d <branch>

Delete remote branch:

git push origin :<branch>

or

git push <remote> --delete <branch>

Review branch changes:

git diff <branch>

Review staged changes:

git diff --staged

Merge branch into current:

git merge <branch>

Resolve merge conflicts:

mate <file>
git add <file>
git commit

Discard branch changes:

git checkout -f master

Tags

Show tags:

git tag

Create tag:

git tag -a <tag>

Create tag for specific commit:

git tag -a <tag> <commit>

Show tag data:

git show <tag>

Update tag:

git tag -f <tag> <tag> -m "<message>"

Delete tag:

git tag -d <tag>

Delete remote tag:

git push origin :refs/tags/<tag>

Push

Push to master:

git push origin master

Push with tags:

git push origin master --tags

Pull

Fetch from remote repository:

git fetch origin

Merge remote branch into current:

git merge origin/master

Fetch and merge into current branch:

git pull

Clone

Clone repository:

git clone <url>

Clone repository into directory:

git clone <url> <directory>

Clone specific branch:

git clone <url> -b <branch> <directory>

Clone with submodules:

git clone --recursive <url>

Submodules

Add submodule to repository:

git submodule add <url> <path>

Update submodule:

git submodule update

Stash

Stash changes:

git stash

Show stashes:

git stash list

Show stash diff:

git stash show -p <stash>

Restore stash:

git stash apply

Restore stash and restage files:

git stash apply --index

Restore specific stash:

git stash apply <stash>

Remove stash:

git stash drop <stash>

Restore and remove stash:

git stash pop

Create branch from stash:

git stash branch <branch>

Remote

Show remotes:

git remote -v

Add remote:

git remote add origin <url>

Remove remote:

git remote rm origin

Reset

Clear the working tree:

git reset --hard HEAD

Undo a commit:

git reset --hard HEAD~1

Rollback to a specific commit:

git reset --hard <commit>

Misc

Get the number of commits in the current branch:

git log --pretty=oneline | wc -l

Search a Git repository by commit message

git log --grep='changed foo'

Configuration

Set name:

git config --global user.name "<name>"

Set email:

git config --global user.email "<email>"

Set editor (e.g. TextMate or Sublime Text):

git config --global core.editor "mate -w"
git config --global core.editor "subl -w"

Use colors:

git config --global color.ui true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment