Skip to content

Instantly share code, notes, and snippets.

@QNimbus
Last active January 10, 2024 11:33
Show Gist options
  • Save QNimbus/7c2aec2fbaaf67823223380d9f794be2 to your computer and use it in GitHub Desktop.
Save QNimbus/7c2aec2fbaaf67823223380d9f794be2 to your computer and use it in GitHub Desktop.
Git cheat sheet #git #cheatsheet

Git cheat sheet

Useful links

GitHub 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>"

Add remote repository:

git remote add origin <url>

Branches

Show branches:

git branch

Create branch:

git branch <branch>

Create and checkout branch:

git checkout -b <branch>

Checkout branch:

git checkout <branch>

Rename branch:

git branch -m <from> <to>

Delete branch:

git branch -d <branch>

Delete remote branch:

git push origin :<branch>

Review branch changes:

git diff <branch>

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>

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 with submodules:

git clone --recursive <url>

Submodules

Add submodule to repository:

git submodule add <url>

Update submodule:

git submodule update

Stash

Stash changes:

git stash

Show stashes:

git stash list

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>

Special

Remove last commit (not pushed):

git reset --hard HEAD~1

Misc

Get the number of commits in the current branch:

git log --pretty=oneline | wc -l

Configuration

Set name:

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

Set email:

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

Set editor (e.g. TextMate):

git config --global core.editor "mate -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