The git command

the stupid content tracker 49 examples from the lessons of the IVski Linux Playground — try them in the terminal next to them.

Subcommands in the examples

git add
Add file contents to the index
git commit
Record changes to the repository
git log
Show commit logs
git branch
List, create, or delete branches
git diff
Show changes between commits, commit and working tree, etc
git push
Update remote refs along with associated objects
git status
Show the working tree status
git switch
Switch branches
git clone
Clone a repository into a new directory
git config
Get and set repository or global options
git pull
Fetch from and integrate with another repository or a local branch
git remote
Manage set of tracked repositories
git blame
git checkout
Switch branches or restore working tree files

Examples

git --version
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git init
git status
git add file.txt

Stage one file

git add file1.txt file2.txt
git add .

Stage everything in current directory

git add -A

Stage everything in the whole repo

git commit -m "Add initial project files"
git commit -am "Fix typo in README"
git restore --staged file.txt

Unstage, keep changes

git commit -m "stuff"
git commit -m "Fix login redirect bug for expired sessions"
git log
git log --oneline

Compact, one line per commit

git log --oneline --graph

ASCII graph of branches

git log -n 5

Last 5 commits

git log --author="Ana"

Filter by author

git diff

Unstaged changes vs last commit

git diff --staged

Staged changes vs last commit

git diff HEAD~1 HEAD

Compare two commits

git diff file.txt

Changes in one file

git show a1b2c3d
git blame file.txt
git branch feature-login

Create

git switch feature-login

Switch (modern)

git checkout feature-login

Switch (classic)

git switch -c feature-login

Create + switch in one step

git branch

Local branches

git branch -a

All, including remote

git switch main
git merge feature-login
git commit
git branch -d feature-login

Safe delete (merged only)

git branch -D feature-login

Force delete

git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git my-folder
git remote add origin https://github.com/user/repo.git
git remote -v

List configured remotes

git push origin main
git push -u origin main

-u remembers this pairing for future plain `git push`

git pull origin main

fetch + merge in one step

git fetch origin

Just download, don't merge yet

git pull

Get latest changes

git commit -m "Add feature X"
git push
git commit -m "Ship feature: user dashboard"
git log --oneline -n 3

Lessons that use git