Git is an essential tool for developers, and mastering it makes you faster, more efficient, and less prone to errors. Here's a categorized list of commonly used Git commands with concise descriptions to help you navigate Git like a pro.
Great! Here’s a structured and categorized blog post based on your list of Git commands, each with a clear one-line description for easy reference. You can use this as a handy guide or publish it as a comprehensive Git cheatsheet on your blog.
Git Configuration
git config --global user.name "Your Name"
Set your global Git username.
git config --global user.email "you@example.com"
Set your global Git email address.
git config --get user.name
View your currently configured Git username.
git config --get user.email
View your currently configured Git email.
git config --list
List all Git configuration settings.
git config --system
Show system-level Git configuration.
git config --global credential.helper cache
Cache Git credentials temporarily in memory.
git help <command>
Display help for a specific Git command.
📝 All configurations are stored in ~/.gitconfig
.
Creating & Cloning Repositories
git init
Initialize a new Git repository.
git clone <repo-url>
Clone an existing repository.
Working with Branches
git branch
List all local branches.
git branch -a
List all local and remote branches.
git branch -r
List all remote branches.
git checkout -b <branchname>
Create and switch to a new branch.
git checkout <branchname>
Switch to an existing branch.
git branch -d <branchname>
Delete a local branch.
git branch --move old new
Rename a local branch.
git push origin <branchname>
Push branch to remote.
git push -u origin <branchname>
Push new local branch and track remote.
git push origin :<branchname>
Delete remote branch.
Staging & Committing
git add <filename>
Stage a file.
git add .
Stage all files in the current directory.
git commit -m "Message"
Commit staged changes with a message.
git commit -a -m "Message"
Stage and commit tracked files.
git commit --amend
Amend the previous commit.
git revert HEAD
Revert the last commit.
git revert --no-commit HEAD~3
Revert the last 3 commits without committing.
Merging & Rebasing
git merge <branchname>
Merge a branch into the current branch.
git merge --squash <branchname>
Squash and merge a branch into the current branch.
git pull --rebase
Pull from remote and rebase instead of merge.
git rebase -i HEAD~5
Interactively rebase the last 5 commits.
git merge
Merge current branch with another.
Pushing & Pulling
git push origin master
Push commits to the master branch on remote.
git fetch origin
Download changes from remote without merging.
git pull
Fetch and merge changes from remote.
git pull --tags -f
Force fetch all tags from remote.
Resetting, Reverting, & Stashing
git reset --hard HEAD~1
Undo the last commit permanently.
git reset --hard origin/master
Reset local branch to match remote branch exactly.
git stash
Stash all local changes.
git stash list
List all stashed changes.
git stash apply
Apply the latest stash.
git clean -n
Preview which untracked files will be deleted.
git clean -f
Delete untracked files.
git clean -fd
Delete untracked files and directories.
Viewing Status & Logs
git status
Show the working tree status.
git diff
Show unstaged differences.
git diff --staged
Show staged differences.
git diff <branch1> <branch2>
Compare two branches.
git log
View commit history.
git log --oneline
Show log in compact form.
git log -p -2
Show patch for last 2 commits.
git log --since=2.weeks
Limit log to recent commits.
git log --graph --oneline --all
View commit history as a graph.
git blame -L 1137,1180 filename
Show who changed specific lines.
tig
Visual interface for Git log (requires installation).
Tags
git tag
List local tags.
git tag -a v1.0 -m "Version 1.0"
Create an annotated tag.
git tag v1.0 <commitID>
Tag a specific commit.
git push origin tagname
Push a tag to the remote.
git push --tags origin
Push all local tags to the remote.
git push --delete origin tagname
Delete a remote tag.
git ls-remote --tags origin
List remote tags.
Miscellaneous
git show HEAD --check
Check for whitespace errors.
git show HEAD --color
Show colored diff output.
git remote -v
Show all remotes with URLs.
git remote add <name> <url>
Add a new remote with a shortname.
open .git/config
Manually edit the Git config file.
gitk
Launch Git GUI for history viewing.
git config --global push.default current
Set default push to current branch only.
Fixing Merge Conflicts
- Open conflicted files in editor.
- Manually resolve conflicts.
- Stage the files with
git add
. - Commit the changes with
git commit
.