Git Cheat Sheet

1 minute read

This cheat sheet summarizes commonly used Git command, I will update sometimes this list.

Initial Git configuration:

1git config --global user.name "Name Username"
2git config --global user.email name@example.com

Check git settings:

1git config --list

Basic Git commands:

 1git init
 2git add .
 3git status
 4git commit -m "My commit"
 5git push # Send files ot remote server
 6git pull - # Get data from remote server
 7git remote add origin https://github.com/per-ivan/Ubuntu-scripts.git
 8git push -u origin master
 9git rm -f <filename
10git status
11git diff
12git diff --staged
13git log

Show file N-commits ahead

1git show HEAD~4:path/to/my/directory/script.js

Pretty log in terminal without external tools

1git log --pretty=format:"%h %s" --graph
2git log --stat 
3git log --pretty=oneline
4git log --pretty=format:"%h - %an, %ar : %s"

Remove and replace the last commit before and after push

 1# Let say you made and saved changes locally, but you want undo them.
 2git restore <filename>
 3
 4# Same scenario like before but you added changes by git add
 5git restore --staged <filename>
 6
 7# undo last local commit
 8git reset --soft HEAD~1
 9
10# Undo pushed changes
11git add .  
12git commit --amend
13git push -f origin main
14git revert <hash-commit>
15