Bind
[[meta bind study]]Git Cheat Sheet
This cheat sheet provides a quick reference for common Git commands.
Setup and Configuration
Configure user information for all local repositories.
- Set your user name:
git config --global user.name "Your Name" - Set your email address:
git config --global user.email "your.email@example.com" - Set default editor:
git config --global core.editor "nano" - Set default branch name (optional, common practice):
git config --global init.defaultBranch main - List all configurations:
git config --list - List repository-specific configurations:
git config --local --list
Creating Repositories
Initialize a new repository or clone an existing one.
- Initialize a new local repository:
git init [project-name] - Clone an existing repository (local or remote):
git clone <repository_url> - Clone an existing repository into a specific directory:
git clone <repository_url> <directory_name>
Making Changes (The Basic Workflow)
Track changes in your project files.
- Check the status of your working directory and staging area:
git status - Add a specific file to the staging area:
git add <file_name> - Add all changed files in the current directory and subdirectories to the staging area:
git add . - Add changes interactively:
git add -p - Commit staged changes with a message:
git commit -m "Your commit message" - Stage all tracked, modified files and commit in one step:
git commit -a -m "Your commit message" - Amend the last commit (change message or add more changes):
- Make changes,
git addthem if needed.
git commit --amend- To only change the last commit message:
git commit --amend -m "New commit message"- Caution: Avoid amending commits that have already been pushed to a shared remote.
- Make changes,
Inspecting Changes and History
View differences and project history.
- Show unstaged changes (working directory vs. staging area):
git diff - Show staged changes (staging area vs. last commit):
git diff --staged # or git diff --cached - Show changes between two commits:
git diff <commit_hash_1> <commit_hash_2> - Show changes in a specific file:
git diff <file_name> - View the commit history:
git log - View commit history with patches (changes):
git log -p - View commit history with stats (files changed, lines added/deleted):
git log --stat - View commit history in a compact, graphical format:
git log --oneline --graph --decorate --all - View history for a specific file or directory:
git log -- <file_or_directory_path> - Show metadata and content changes of a specific commit:
git show <commit_hash>
Branching
Manage different lines of development using branches.
- List all local branches (current branch marked with *):
git branch - List all remote branches:
git branch -r - List all local and remote branches:
git branch -a - Create a new branch:
git branch <branch_name> - Switch to an existing branch:
git checkout <branch_name> # Or (newer command, preferred for switching branches): git switch <branch_name> - Create a new branch and switch to it:
git checkout -b <new_branch_name> # Or (newer command): git switch -c <new_branch_name> - Rename the current branch:
git branch -m <new_branch_name> - Rename another branch:
git branch -m <old_branch_name> <new_branch_name> - Delete a local branch (only if merged):
git branch -d <branch_name> - Force delete a local branch (even if unmerged):
git branch -D <branch_name> - Delete a remote branch:
git push <remote_name> --delete <branch_name> # Example: git push origin --delete feature-branch
Merging and Rebasing
Combine changes from different branches.
- Switch to the target branch (e.g.,
main):git switch main - Merge another branch into the current branch:
git merge <branch_to_merge>- Git will create a merge commit if it’s not a fast-forward merge.
- Resolve any merge conflicts if they occur, then
git addthe resolved files andgit commit.
- Abort a merge (if conflicts occurred and you want to stop):
git merge --abort - Rebase the current branch onto another base branch (e.g.,
main):- Switch to your feature branch first:
git switch feature-branch
git rebase <base_branch_name> # Example: git rebase main- Rebase reapplies commits from your current branch onto the tip of the base branch, creating a linear history.
- Resolve conflicts if they occur,
git addresolved files, thengit rebase --continue. - Caution: Avoid rebasing commits that have already been pushed to a shared remote, as it rewrites history.
- Switch to your feature branch first:
- Abort a rebase:
git rebase --abort
Remote Repositories
Work with repositories hosted elsewhere (e.g., GitHub, GitLab).
- List configured remote repositories:
git remote -v - Add a new remote repository:
git remote add <remote_name> <repository_url> # Example: git remote add origin [https://github.com/user/repo.git](https://github.com/user/repo.git) - Remove a remote repository:
git remote remove <remote_name> - Rename a remote repository:
git remote rename <old_name> <new_name> - Fetch changes from a remote repository (doesn’t merge):
git fetch <remote_name> # Example: git fetch origin - Fetch changes from all remotes:
git fetch --all - Fetch changes and merge the remote branch into the current local branch (combo of fetch + merge):
git pull <remote_name> <branch_name> # Example: git pull origin main # If tracking is set up, 'git pull' often suffices. - Push local commits to a remote repository:
git push <remote_name> <branch_name> # Example: git push origin main - Push and set the upstream branch for the current local branch:
git push -u <remote_name> <branch_name> # Example: git push -u origin feature-branch # After this, 'git push' and 'git pull' can be used without specifying remote/branch.
Undoing Changes
Revert or reset changes.
- Discard changes in the working directory for a specific file:
git checkout -- <file_name> # Or (newer command): git restore <file_name> - Discard all changes in the working directory:
git checkout . # Or (newer command): git restore . - Unstage a file (remove from staging area, keep changes in working directory):
git reset HEAD <file_name> # Or (newer command): git restore --staged <file_name> - Unstage all files:
git reset HEAD . # Or (newer command): git restore --staged . - Create a new commit that undoes the changes from a specific commit:
git revert <commit_hash>- This is safer for shared history as it doesn’t rewrite history.
- Reset the current branch HEAD to a specific commit:
- Soft reset (move HEAD, keep changes staged):
git reset --soft <commit_hash> - Mixed reset (move HEAD, unstage changes, keep changes in working directory - DEFAULT):
git reset <commit_hash> # or git reset --mixed <commit_hash> - Hard reset (move HEAD, discard all changes since that commit - DANGEROUS):
git reset --hard <commit_hash> - Caution:
reset --harddiscards work permanently. Use with extreme care, especially on shared branches.
- Soft reset (move HEAD, keep changes staged):
- Remove untracked files from the working directory (Dry Run):
git clean -n - Remove untracked files from the working directory (Force - DANGEROUS):
git clean -f - Remove untracked files and directories (Force - DANGEROUS):
git clean -fd
Stashing
Temporarily save uncommitted changes.
- Stash current changes (staged and unstaged):
git stash push -m "Optional stash message" # Shorter version: git stash - List all stashes:
git stash list - Apply the most recent stash and keep it in the stash list:
git stash apply # Defaults to stash@{0} git stash apply stash@{n} # Apply specific stash - Apply the most recent stash and remove it from the stash list:
git stash pop # Defaults to stash@{0} git stash pop stash@{n} # Pop specific stash - Show changes stored in a stash:
git stash show stash@{n} # Show file names git stash show -p stash@{n} # Show diff - Discard a specific stash:
git stash drop stash@{n} - Discard all stashes:
git stash clear
Tagging
Mark specific points in history as important (e.g., releases).
- List all tags:
git tag - List tags matching a pattern:
git tag -l "v1.8.*" - Create a lightweight tag (just a pointer):
git tag <tag_name> # Example: git tag v1.0.0 - Create an annotated tag (stores metadata, recommended):
git tag -a <tag_name> -m "Tag message" # Example: git tag -a v1.0.0 -m "Version 1.0.0 release" - Tag a specific past commit:
git tag <tag_name> <commit_hash> # Example: git tag v0.9.0 abc1234 - Show info for a tag:
git show <tag_name> - Push a specific tag to the remote:
git push <remote_name> <tag_name> # Example: git push origin v1.0.0 - Push all local tags to the remote:
git push <remote_name> --tags # Example: git push origin --tags - Delete a local tag:
git tag -d <tag_name> - Delete a remote tag:
git push <remote_name> --delete <tag_name> # Example: git push origin --delete v1.0.0
Ignoring Files (.gitignore)
Specify intentionally untracked files that Git should ignore.
Create a file named
.gitignorein the root of your repository (or subdirectories).Add patterns for files or directories to ignore (one per line).
- Blank lines or lines starting with
#are ignored (comments). - Standard glob patterns work (
*matches zero or more characters,?matches one character,[abc]matches one character in the set). - End patterns with a forward slash
/to specify a directory. - Negate a pattern by starting it with an exclamation mark
!.
Example
.gitignore:# Ignore build artifacts build/ dist/ # Ignore IDE files .idea/ *.iml .vscode/ # Ignore dependency directories node_modules/ vendor/ # Ignore log files *.log logs/ # Ignore environment variables file .env # But track a specific file within an ignored directory !node_modules/important-module/README.md- Blank lines or lines starting with