Cheat Sheet - Git

Configuration

Edit via editor

# <project\.git\config 
git config --edit

# C:\Users\%username%\.gitconfig
git config --global --edit

# C:\Program Files\Git\etc\gitconfig
git config --system --edit 

Alias

Edit ~/.gitconfig ($env:userprofile.gitconfig on windows) or use e.g., git config --global alias.st 'status'

[alias]
    st = status
    ci = commit
    co = checkout
    br = branch
    unstage = reset HEAD --
    last = log -1 HEAD --oneline

Branches

Check out a forked remote branch on GitHub

Requires the GitHub CLI to be installed

# List open pull requests
gh pr list

# Check out locally 
gh pr checkout <int>

Delete local and remote branch

git branch -d feature/login 
git push origin --delete feature/login

Delete local references to remote branches that don't exist anymore

git remote prune origin

Update the local list of remote branches

git remote update origin --prune

Automatically set remote branches on push

git config --global --add --bool push.autoSetupRemote true

Remotes

Show remotes

git remote -v 

Set remote URL

git remote set-url origin <url>

Merging

Abort a merge process

git merge --abort

Tags

Checkout Git Tag

git checkout tags/<tag> -b <branch>

Delete single tag

# delete remotely 
git push --delete origin v2.0.1
# delete locally 
git tag --delete v2.0.1

Delete all tags

# First delete remote tags
git tag -l | xargs git push --delete origin

# Then delete local tags 
git tag | xargs git tag -d

Misc

Globally disable paging

git config --global core.pager cat

Push an existing repository

git remote add origin https://github.com/gittower/example.git  
git push -u origin --all  

Deleting the entire commit history

# Checkout
git checkout --orphan latest_branch 

# Add all files
git add -A 

# Commit changes
git commit -am "initial commit" 

# Delete branch
git branch -D main 

# Rename current branch 
git branch -m main 

# Force update repository
git push -f origin main
Sync a forked repository with the remote source
Sync a forked repository with the remote source. GitHub Gist: instantly share code, notes, and snippets.