Git in Action : Quick & Essential Commands

 

Git Command Cheat Sheet

Setup & Configuration

COMMANDDESCRIPTION
git --versionCheck Git version
git config --global user.name "Name"Set username globally
git config --global user.email "email@example.com"Set email globally
git config --listList all configurations
git config --global core.editor "editor"Set default editor
git config --global init.defaultBranch mainSet default branch name
git config --global alias.<alias-name> "<git-command>"Create command alias

Repository Initialization

COMMANDDESCRIPTION
git initInitialize a new Git repository
git init --bareInitialize a bare repository
git clone <repository-url>Clone a repository
git clone --depth <depth> <repository-url>Clone with limited history
git clone --branch <branch-name> <repository-url>Clone a specific branch

Basic Operations

COMMANDDESCRIPTION
git statusShow working tree status
git add <file>Add file to staging area
git add .Add all files to staging area
git add -pInteractively add changes
git commit -m "message"Commit staged changes
git commit -a -m "message"Add and commit all changes
git commit --amendAmend the last commit
git commit --amend --no-editAmend last commit without changing message

Viewing Changes

COMMANDDESCRIPTION
git diffShow unstaged changes
git diff --stagedShow staged changes
git diff <commit1> <commit2>Compare two commits
git diff <branch1> <branch2>Compare two branches
git show <commit>Show a specific commit
git logShow commit history
git log --onelineShow commit history in one line format
git log --graphShow commit history as a graph
git log -pShow commit history with diffs
git log --statShow commit history with stats
git log --author="name"Filter commits by author
git log --since="date"Filter commits since date
git log --until="date"Filter commits until date
git log --grep="pattern"Filter commits by pattern
git blame <file>Show who changed each line in a file
git reflogShow reference logs

Undoing Changes

COMMANDDESCRIPTION
git restore <file>Discard changes in working directory
git restore --staged <file>Unstage changes
git reset <commit>Reset to a specific commit (keep changes)
git reset --hard <commit>Reset to a specific commit (discard changes)
git reset --soft <commit>Reset to a specific commit (keep staged)
git revert <commit>Create a new commit that undoes changes
git rm <file>Remove file from working directory and staging
git rm --cached <file>Remove file from staging area only
git clean -nShow what files would be removed
git clean -fRemove untracked files
git clean -fdRemove untracked files and directories

Branching & Merging

COMMANDDESCRIPTION
git branchList local branches
git branch -aList all branches (local and remote)
git branch -rList remote branches
git branch <branch-name>Create a new branch
git branch -d <branch-name>Delete a branch
git branch -D <branch-name>Force delete a branch
git branch -m <new-name>Rename current branch
git branch -m <old-name> <new-name>Rename specific branch
git switch <branch-name>Switch to a branch
git switch -c <branch-name>Create and switch to a new branch
git checkout <branch-name>Switch to a branch (older method)
git checkout -b <branch-name>Create and switch to a new branch (older method)
git checkout -Switch to the previous branch
git checkout <commit> <file>Checkout file from specific commit
git merge <branch-name>Merge a branch into current branch
git merge --no-ff <branch-name>Merge a branch creating a merge commit
git merge --abortAbort a merge in case of conflicts
git mergetoolLaunch merge conflict resolution tool

Rebasing

COMMANDDESCRIPTION
git rebase <branch-name>Rebase current branch onto another
git rebase -i <commit>Interactive rebase
git rebase --abortAbort a rebase
git rebase --continueContinue rebase after resolving conflicts
git rebase --skipSkip current patch and continue

Stashing

COMMANDDESCRIPTION
git stashStash changes
git stash push -m "message"Stash changes with a message
git stash listList stashes
git stash show <stash@{n}>Show stash changes
git stash apply <stash@{n}>Apply stash without removing
git stash pop <stash@{n}>Apply stash and remove it
git stash drop <stash@{n}>Remove a stash
git stash clearRemove all stashes

Remote Operations

COMMANDDESCRIPTION
git remoteList remotes
git remote -vList remotes with URLs
git remote add <name> <url>Add a new remote
git remote remove <name>Remove a remote
git remote rename <old-name> <new-name>Rename a remote
git remote set-url <name> <url>Change remote URL
git fetch <remote>Fetch from remote
git fetch --allFetch from all remotes
git pull <remote> <branch>Fetch and merge from remote
git pull --rebase <remote> <branch>Fetch and rebase from remote
git push <remote> <branch>Push to remote
git push -f <remote> <branch>Force push to remote
git push --set-upstream <remote> <branch>Push and set upstream
git push <remote> --delete <branch>Delete remote branch
git push --tagsPush tags to remote

Tagging

COMMANDDESCRIPTION
git tagList tags
git tag <tag-name>Create a lightweight tag
git tag -a <tag-name> -m "message"Create an annotated tag
git tag -d <tag-name>Delete a tag
git push <remote> <tag-name>Push a tag to remote
git push <remote> --tagsPush all tags to remote
git checkout <tag-name>Checkout a tag

Worktrees

COMMANDDESCRIPTION
git worktree listList worktrees
git worktree add <path> <branch>Create a new worktree
git worktree remove <path>Remove a worktree
git worktree prunePrune worktree information

Submodules

COMMANDDESCRIPTION
git submodule add <repository-url> <path>Add a submodule
git submodule initInitialize submodules
git submodule updateUpdate submodules
git submodule update --init --recursiveInitialize and update all submodules recursively
git submodule foreach <command>Execute command in each submodule

Advanced Operations

COMMANDDESCRIPTION
git cherry-pick <commit>Apply changes from a specific commit
git cherry-pick --continueContinue cherry-pick after resolving conflicts
git cherry-pick --abortAbort a cherry-pick
git rebase -i --autosquash <commit>Interactive rebase with autosquash
git bisect startStart binary search for issues
git bisect good <commit>Mark commit as good
git bisect bad <commit>Mark commit as bad
git bisect resetReset bisect state
git grep <pattern>Search for pattern in tracked files
git archive --format=zip HEAD > archive.zipCreate a zip archive of the repository
git shortlogShow commit counts by author
git shortlog -snShow commit counts sorted by number
git rev-parse HEADGet full SHA-1 of current commit
git rev-parse --short HEADGet short SHA-1 of current commit
git fsckCheck repository integrity
git gcGarbage collection
git pruneRemove unreachable objects
git verify-pack -v .git/objects/pack/*.idxVerify packed objects

Gitflow Operations

COMMANDDESCRIPTION
git flow initInitialize gitflow in repository
git flow feature start <name>Start a new feature
git flow feature finish <name>Finish a feature
git flow release start <version>Start a release
git flow release finish <version>Finish a release
git flow hotfix start <version>Start a hotfix
git flow hotfix finish <version>Finish a hotfix

Git Hooks

COMMANDDESCRIPTION
git hook listList available hooks
.git/hooks/pre-commitPre-commit hook
.git/hooks/commit-msgCommit message hook
.git/hooks/post-commitPost-commit hook
.git/hooks/pre-pushPre-push hook

Troubleshooting

COMMANDDESCRIPTION
git fsckCheck repository integrity
git gcGarbage collection
git pruneRemove unreachable objects
git verify-pack -v .git/objects/pack/*.idxVerify packed objects
git count-objects -vCount objects and show size
git show-refList references
git reset --hardReset to HEAD discarding changes
git clean -fdRemove untracked files and directories
git reflog expire --expire=now --allExpire all reflog entries
git gc --prune=nowGarbage collection with pruning
git config --global --unset <config-name>Unset a config value

Comments

Popular posts from this blog

Free Courses - Git & GitHub (DevOps)

6 FREE courses to learn AWS & AWS DevOps (Concepts + Hands-on + Interview)