Git Configuration

git config --global user.name "Your Name" Sets your global Git username
git config --global user.email "[email protected]" Sets your global Git email
git config --global core.editor "vim" Sets your default text editor for Git
git config --list --show-origin Lists all Git configurations (system, global, local), plus file sources
git config --global -l Shows global Git configuration
git config --local -l Shows repository-specific (local) Git configuration
ssh-keygen -t ed25519 -C "[email protected]" Generates an SSH key for authentication
ssh -T [email protected] Tests SSH connection to GitHub

Git Repository Setup

git init Initializes a new Git repository in the current directory
git clone <repository-url> Clones a remote repository
git clone --depth=1 -b main <repository-url> Shallow clone only the latest commit from the main branch
git remote add origin <repository-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 show origin Shows detailed info about the origin remote
git remote rename <old-name> <new-name> Renames a remote
git fetch Fetches updates from the remote repository without merging
git push origin <branch> Pushes your local branch to the remote repository (origin)
git pull origin <branch> Fetches and merges changes from the specified remote branch

Branching and Merging

git branch Lists all local branches
git branch -v Lists branches with their latest commit info
git branch -vv Lists branches with detailed tracking info (which remote branch they’re connected to)
git branch <branch-name> Creates a new branch
git branch -d <branch-name> Deletes a local branch
git branch -r Lists remote branches
git branch -a Lists all branches (local + remote)
git checkout <branch-name> Switches to another branch
git checkout -b <branch-name> Creates and switches to a new branch
git push origin --delete <branch-name> Deletes a remote branch from the server
git merge <branch-name> Merges <branch-name> into the current branch
git rebase <branch-name> Reapplies commits on top of <branch-name> (useful for a cleaner history)
git cherry-pick <commit-hash> Applies a single commit from another branch into your current branch
git branch --set-upstream-to=origin/main main Sets the local main branch to track origin/main

Staging & Committing

git add <file> Adds a specific file to the staging area
git add . Adds all modified files to the staging area
git commit -m "Commit message" Commits staged changes with a message
git commit --amend -m "New commit message" Modifies the last commit message (or add changes to the last commit if you also staged new files)
git commit --amend --no-edit Adds staged changes to the last commit without changing its message

Pulling & Pushing

git pull origin <branch> Fetches and merges changes from <branch> on origin
git fetch origin Fetches remote changes without merging them
git push Pushes all committed changes to the remote (if already set)
git push origin <branch> Pushes local <branch> to the remote repository
git push -u origin main Pushes main and sets it to track origin/main
git push --force Force-pushes changes (potentially overwriting remote history, use with caution)
git push --set-upstream origin <branch> Sets a local branch to track a remote branch

Checking Status & Logs

git status Shows the working directory and staging area status
git log Displays the commit history
git log --oneline --graph --decorate --all Shows a concise, graphical commit history—very handy for visualizing branches
git show <commit-hash> Shows details of a specific commit
git diff Compares the working directory with the staging area
git diff --staged Compares the staging area with the last commit

Undoing Changes

git restore <file> Discards changes in the working directory for <file> (Git 2.23+)
git checkout -- <file> Restores <file> to the last committed state
git reset <file> Unstages a file (moves changes from staging back to working directory)
git reset --hard Discards all local changes and resets to the last commit
git reset --soft HEAD~1 Undoes the last commit but keeps changes staged (allows you to re-commit)
git reset --hard HEAD~1 Undoes the last commit and discards all related changes
git revert <commit-hash> Creates a new commit that undoes changes introduced by <commit-hash>

Stashing Changes

git stash Saves your uncommitted changes
git stash list Shows all saved stashes
git stash pop Applies the most recent stash and removes it from the stash list
git stash drop Removes the most recent stash without applying it

Tagging

git tag Lists all tags
git tag <tag-name> Creates a lightweight tag
git tag -a <tag-name> -m "Tag message" Creates an annotated tag
git push origin <tag-name> Pushes a single tag to the remote repository
git push origin --tags Pushes all local tags to the remote repository

Cleaning Untracked Files

git clean -n Shows which untracked files would be deleted (preview)
git clean -f Deletes untracked files
git clean -fd Deletes untracked files and directories