image.png

Environment Setup

Command Description
git config --global user.name "Your Name" Set global username
git config --global user.email "[email protected]" Set global email
git config --global core.editor "vim" Set default Git editor
git config --list --show-origin List all Git config and sources
git config --global -l List global Git configuration
git config --local -l List local repository configuration
ssh-keygen -t ed25519 -C "[email protected]" Generate an SSH key for authentication
ssh -T [email protected] Test SSH connection to GitHub

Repository Setup

Command Description
git init Initialize a new Git repository
git clone <url> Clone a remote repository
git clone --depth=1 -b main <url> Shallow clone only the latest commit from the main branch
git remote add origin <url> Adds a remote called origin to your local repository
git remote -v Lists configured remote repositories
git remote remove origin Removes the remote named origin
git remote rename <old> <new> Rename a remote
git remote show origin Shows detailed info about the origin remote

Working with Changes

Command Description
git status Show current status
git add <file> Stage a file
git add . Stage all changes
git restore <file> Discard changes (Git 2.23+)
git checkout -- <file> Discard changes (older versions)

Committing Changes

Command Description
git commit -m "message" Commit staged changes
git commit --amend -m "new message" Edit last commit message
git commit --amend --no-edit Add changes to last commit without changing message

Pushing & Pulling

Command Description
git push Push commits (tracking branch)
git push origin <branch> Push specific branch
git push -u origin <branch> Push and set upstream
git push --force Force-push (dangerous!)
git push --force-with-lease Safe force-push (checks remote changes)
git pull origin <branch> Fetch and merge from remote
git fetch origin Fetch changes only

Branching & Merging

Command Description
git branch <branch> Create new branch
git branch -d <branch> Delete a local branch
git checkout <branch> Switch branch
git checkout -b <branch> Create and switch branch
git merge <branch> Merge a branch into current
git rebase <branch> Reapply commits on another branch
git cherry-pick <commit> Apply a specific commit
git push origin --delete <branch> Delete remote branch
git branch --set-upstream-to=origin/main main Set branch upstream manually

Viewing Branches

Command Description
git branch List branches
git branch -v Show branch list with their latest commits
git branch -vv Show branch list with tracking and latest commit info
git branch -r List remote branches
git branch -a List all (local + remote) branches

Undoing Commits

Command Description
git reset <file> Unstage a file
git reset --soft HEAD~1 Undo commit, keep changes staged
git reset --hard HEAD~1 Undo commit and discard changes
git revert <commit> Create a new commit that reverts a commit
git reset --hard Reset working directory and staging area

Stashing

Command Description
git stash Save uncommitted changes
git stash list List stashed changes
git stash pop Apply and remove latest stash
git stash drop Remove latest stash without applying

Logs & History