2.5 KiB
2.5 KiB
Git Cheatsheet
This is a quick reference for common Git commands.
Configuration
git config --global user.name "Your Name": Set your usernamegit config --global user.email "your.email@example.com": Set your emailgit config --list: View configuration
Creating Repositories
git init: Initialize a new Git repository in the current directorygit clone <repository-url>: Clone an existing repository
Basic Snapshotting
git status: Show the working tree statusgit add <file>: Add a file to the staging areagit add .: Add all changes in the current directory to the staging areagit commit -m "commit message": Commit staged changes with a messagegit commit -a -m "commit message": Commit all tracked changes with a message (skips staging)
Branching and Merging
git branch: List all branchesgit branch <branch-name>: Create a new branchgit checkout <branch-name>: Switch to a branchgit checkout -b <branch-name>: Create and switch to a new branchgit merge <branch-name>: Merge a branch into the current branchgit branch -d <branch-name>: Delete a branch
Remote Repositories
git remote add <name> <url>: Add a remote repository (e.g., origin)git push <remote> <branch>: Push changes to a remote branchgit pull <remote> <branch>: Pull changes from a remote branchgit fetch: Fetch changes from remote without merging
Inspection and Comparison
git log: Show commit historygit log --oneline: Show commit history in one line per commitgit diff: Show changes between commits, commit and working tree, etc.git show <commit>: Show details of a commit
Undoing Changes
git reset --hard: Reset to the last commit, discarding changesgit revert <commit>: Create a new commit that undoes a previous commitgit checkout -- <file>: Discard changes in a file
Stashing
git stash: Stash changes in a dirty working directorygit stash apply: Apply stashed changesgit stash list: List stashed changes
Advanced Commands
git rebase <branch>: Rebase current branch onto anothergit cherry-pick <commit>: Apply a commit from another branchgit reflog: Show reference loggit bisect: Binary search for bug introduction
Submodules
git submodule add <repo> <path>: Add submodulegit submodule update --init: Initialize submodules
For more details, refer to the official Git documentation: https://git-scm.com/docs and https://www.geeksforgeeks.org/git-cheat-sheet/"