DEV Community

Can Cellek
Can Cellek

Posted on • Updated on

Git Cheat Sheet

Hello there!
Here I would like to share my most commonly used git commands and a few aliases to keep your console simple and beautiful by formatting.

EDIT:
I have started a new project for common cli tool cheatsheets. You can find git cheatsheets there!
App: https://dev-cheats.vercel.app
Article: https://dev.to/excalith/dev-cheats-5b3o

Create

# Create a new local repository
git init

# Clone an existing repository
git clone ssh://user@domain.com/repo.git
Enter fullscreen mode Exit fullscreen mode

Check Changes

# Check all changes
git fetch

# Check status
git status

# Changes to tracked files
git diff

# Log
git log --oneline

# Diff
git log --stat

# Shortlog
git shortlog

# Graphs
git log --graph --oneline --decorate

# Show Log By Author
git log --author="John"
git log --author="John\|Mary"
Enter fullscreen mode Exit fullscreen mode

Stash

# Add To Stash
git stash

# Show Stash List
git stash show

# Pop Stash
git stash pop

# Apply Spesific Stash
git stash apply stash@{index}

# Drop Spesific Stash
git stash drop stash@{index}
Enter fullscreen mode Exit fullscreen mode

Stage & Commit

# Add all changes to next commit
git add .

# Add file changes to next commit
git add

# Commit staged changes
git commit -m "Commit Message"

# Change the last commit
git commit --ammend

# Change the last commit name
git commit --amend -m "New commit message"
Enter fullscreen mode Exit fullscreen mode

Branches & Tags

# List all existing branches
git branch -av

# Switch HEAD branch
git checkout

# Checkout to a remote branch
git checkout --track origin/branch-name

# Create branch
git branch

# Delete local branch
git branch -D

# Delete origin branch
git branch -dr origin/branch

# Tag current commit
git tag
Enter fullscreen mode Exit fullscreen mode

Update & Publish

# Download all changes
git pull

# Upload all changes
git push

# Publish tags
git push --tags
Enter fullscreen mode Exit fullscreen mode

Merge & Rebase

# Merge into current HEAD
git merge

# Rebase your current HEAD (Don't rebase on public branches!)
git rebase

# Abort rebase
git rebase --abort

# Continue rebase
git rebase --continue

# Use your configured merge tool for conflicts
git mergetool

# After solving conflicts mark as resolved
git add
git rm
Enter fullscreen mode Exit fullscreen mode

UNDO

#Discard all tracked changes
git checkout .

#Discard all local changes on working directory
git reset --hard HEAD

#Discard all local changes in file
git checkout HEAD

#Reset HEAD pointer into a previous commit
git reset --hard
Enter fullscreen mode Exit fullscreen mode

Advanced Stuff

# Local Garbage Collection

# finds and removes all references of unused commits in reflog
git reflog expire --expire="1 hour" --all

# finds remove all references of unreachable commits in reflog
git reflog expire --expire-unreachable="1 hour" --all

# prune all unreachable objects from database
git prune --expire="1 hour" -v

# clean-up all unnecessary files and optimize the local repo
git gc --aggressive --prune="1 hour"

# count latest objects
count-objects -vH
Enter fullscreen mode Exit fullscreen mode
# Delete Old History (link: https://stackoverflow.com/a/41953383)
# checkout to the status of the git repo at commit f; creating a branch named "temp"
git checkout --orphan temp

# create a new commit that is to be the new root commit
git commit -m "new root commit"

# now rebase the part of history from  to master on the temp branch
git rebase --onto temp  master

# we don't need the temp branch anymore
git branch -D temp

# this is a destructive operation
git push -f
Enter fullscreen mode Exit fullscreen mode
# Check LFS
git lfs ls-files # It will show all tracked files.
git lfs push origin master --all # Push any missing files
git count-objects -vH # Check the real size of the repository.
Enter fullscreen mode Exit fullscreen mode
# Editing Global Config File
git config --global -e
Enter fullscreen mode Exit fullscreen mode
# Setting An Editor For Config File
[core]
    editor = 'Path/To/Your/Editor.exe'
Enter fullscreen mode Exit fullscreen mode

Git Alias

# Shortcut Git Status: Use 'git st' to show git status -sb
git config --global alias.st "status -sb"

# Formatting Git Log: Use 'git ls' to show log with colored format
git config --global alias.ls "log -n 15 --pretty=format:'%C(Yellow) %h %Cred%ad %Cblue%an %Creset%s%Cgreen%d' --date=short --all"

# Formatting Git Graph: Use 'git graph' to show log with colored format
git config --global alias.graph "log -n 25 --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all"

# Show only modified and deleted files including your extension

# instead of ls-cs and *.cs, write your own extension
git config --global alias.ls-cs "ls-files -zmd *.cs"

# Ignoring Files Locally
# use 'git ignore filePath' to ignore a file locally
git config --global alias.ignore "update-index --assume-unchanged"

# use 'git unignore filePath' to unignore a file locally
git config --global alias.unignore "update-index --no-assume-unchanged"

# use 'git ignored' to see your ignored files
git config --global alias.ignored "!git ls-files -v | grep "^[[:lower:]]""
Enter fullscreen mode Exit fullscreen mode

Top comments (0)