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:
Terminal window git config --global user.name "Your Name" - Set your email address:
Terminal window git config --global user.email "your.email@example.com" - Set default editor:
Terminal window git config --global core.editor "nano" - Set default branch name (optional, common practice):
Terminal window git config --global init.defaultBranch main - List all configurations:
Terminal window git config --list - List repository-specific configurations:
Terminal window git config --local --list
Creating Repositories
Initialize a new repository or clone an existing one.
- Initialize a new local repository:
Terminal window git init [project-name] - Clone an existing repository (local or remote):
Terminal window git clone <repository_url> - Clone an existing repository into a specific directory:
Terminal window 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:
Terminal window git status - Add a specific file to the staging area:
Terminal window git add <file_name> - Add all changed files in the current directory and subdirectories to the staging area:
Terminal window git add . - Add changes interactively:
Terminal window git add -p - Commit staged changes with a message:
Terminal window git commit -m "Your commit message" - Stage all tracked, modified files and commit in one step:
Terminal window git commit -a -m "Your commit message" - Amend the last commit (change message or add more changes):
- Make changes,
git addthem if needed.
Terminal window git commit --amend- To only change the last commit message:
Terminal window 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):
Terminal window git diff - Show staged changes (staging area vs. last commit):
Terminal window git diff --staged# orgit diff --cached - Show changes between two commits:
Terminal window git diff <commit_hash_1> <commit_hash_2> - Show changes in a specific file:
Terminal window git diff <file_name> - View the commit history:
Terminal window git log - View commit history with patches (changes):
Terminal window git log -p - View commit history with stats (files changed, lines added/deleted):
Terminal window git log --stat - View commit history in a compact, graphical format:
Terminal window git log --oneline --graph --decorate --all - View history for a specific file or directory:
Terminal window git log -- <file_or_directory_path> - Show metadata and content changes of a specific commit:
Terminal window git show <commit_hash>
Branching
Manage different lines of development using branches.
- List all local branches (current branch marked with *):
Terminal window git branch - List all remote branches:
Terminal window git branch -r - List all local and remote branches:
Terminal window git branch -a - Create a new branch:
Terminal window git branch <branch_name> - Switch to an existing branch:
Terminal window git checkout <branch_name># Or (newer command, preferred for switching branches):git switch <branch_name> - Create a new branch and switch to it:
Terminal window git checkout -b <new_branch_name># Or (newer command):git switch -c <new_branch_name> - Rename the current branch:
Terminal window git branch -m <new_branch_name> - Rename another branch:
Terminal window git branch -m <old_branch_name> <new_branch_name> - Delete a local branch (only if merged):
Terminal window git branch -d <branch_name> - Force delete a local branch (even if unmerged):
Terminal window git branch -D <branch_name> - Delete a remote branch:
Terminal window 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):Terminal window git switch main - Merge another branch into the current branch:
Terminal window 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):
Terminal window git merge --abort - Rebase the current branch onto another base branch (e.g.,
main):- Switch to your feature branch first:
git switch feature-branch
Terminal window 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:
Terminal window git rebase --abort
Remote Repositories
Work with repositories hosted elsewhere (e.g., GitHub, GitLab).
- List configured remote repositories:
Terminal window git remote -v - Add a new remote repository:
Terminal window 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:
Terminal window git remote remove <remote_name> - Rename a remote repository:
Terminal window git remote rename <old_name> <new_name> - Fetch changes from a remote repository (doesn’t merge):
Terminal window git fetch <remote_name># Example: git fetch origin - Fetch changes from all remotes:
Terminal window git fetch --all - Fetch changes and merge the remote branch into the current local branch (combo of fetch + merge):
Terminal window 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:
Terminal window git push <remote_name> <branch_name># Example: git push origin main - Push and set the upstream branch for the current local branch:
Terminal window 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:
Terminal window git checkout -- <file_name># Or (newer command):git restore <file_name> - Discard all changes in the working directory:
Terminal window git checkout .# Or (newer command):git restore . - Unstage a file (remove from staging area, keep changes in working directory):
Terminal window git reset HEAD <file_name># Or (newer command):git restore --staged <file_name> - Unstage all files:
Terminal window git reset HEAD .# Or (newer command):git restore --staged . - Create a new commit that undoes the changes from a specific commit:
Terminal window 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):
Terminal window git reset --soft <commit_hash> - Mixed reset (move HEAD, unstage changes, keep changes in working directory - DEFAULT):
Terminal window git reset <commit_hash># orgit reset --mixed <commit_hash> - Hard reset (move HEAD, discard all changes since that commit - DANGEROUS):
Terminal window 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):
Terminal window git clean -n - Remove untracked files from the working directory (Force - DANGEROUS):
Terminal window git clean -f - Remove untracked files and directories (Force - DANGEROUS):
Terminal window git clean -fd
Stashing
Temporarily save uncommitted changes.
- Stash current changes (staged and unstaged):
Terminal window git stash push -m "Optional stash message"# Shorter version:git stash - List all stashes:
Terminal window git stash list - Apply the most recent stash and keep it in the stash list:
Terminal window 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:
Terminal window git stash pop # Defaults to stash@{0}git stash pop stash@{n} # Pop specific stash - Show changes stored in a stash:
Terminal window git stash show stash@{n} # Show file namesgit stash show -p stash@{n} # Show diff - Discard a specific stash:
Terminal window git stash drop stash@{n} - Discard all stashes:
Terminal window git stash clear
Tagging
Mark specific points in history as important (e.g., releases).
- List all tags:
Terminal window git tag - List tags matching a pattern:
Terminal window git tag -l "v1.8.*" - Create a lightweight tag (just a pointer):
Terminal window git tag <tag_name># Example: git tag v1.0.0 - Create an annotated tag (stores metadata, recommended):
Terminal window 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:
Terminal window git tag <tag_name> <commit_hash># Example: git tag v0.9.0 abc1234 - Show info for a tag:
Terminal window git show <tag_name> - Push a specific tag to the remote:
Terminal window git push <remote_name> <tag_name># Example: git push origin v1.0.0 - Push all local tags to the remote:
Terminal window git push <remote_name> --tags# Example: git push origin --tags - Delete a local tag:
Terminal window git tag -d <tag_name> - Delete a remote tag:
Terminal window 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 artifactsbuild/dist/# Ignore IDE files.idea/*.iml.vscode/# Ignore dependency directoriesnode_modules/vendor/# Ignore log files*.loglogs/# 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