Master Essential Git Commands for Efficient Version Control
This guide presents a comprehensive collection of essential Git commands—from initial configuration and repository initialization to advanced operations like rebasing, cherry‑picking, and signing commits—helping developers manage local and remote codebases effectively.
1. Basic Git Commands
Configure your identity for the first use of Git:
git config --global user.name "Your Name"
git config --global user.email [email protected]Check the installed Git version:
git version
git -vInitialize a new repository: git init Clone an existing remote repository: git clone <url> Add files to the staging area (examples):
git add *
git add .
git add -u .
git add -A .
git add *.html
git add *.txt
git add index/
git add index/index.htmlCommit changes:
# Open editor for commit message
git commit
# Commit specific files
git commit file1 file2
# Commit with message
git commit -m "message"
# Commit all tracked changes without editor
git commit -am "message"
# Amend previous commit
git commit --amend -m "new message"Show repository status: git status [options] [--] [path...] Branch management:
git branch # list local branches
git branch -v # list with last commit
git branch -vv # list with upstream info
git branch -a # list all branches
git branch -d branchName # delete local branch
git push <remote> -d branchName # delete remote branch
git checkout branchName # switch to branch
git checkout -b branchName # create and switch
git push <remote> <local>:<remote> # create remote branchCheckout explanation:
git checkout branchName
git checkout -b branchName2. Remote Repository Operations
Manage remote connections:
git remote
git remote -v
git remote add <name> <url>
git remote rm <name>
git remote rename old_name new_namePush changes to remote:
git push <remote> <localBranch>:<remoteBranch>
# same name can omit remote branch
git push origin master
# Force push
git push --force origin master
# Delete remote branch
git push origin --delete master
# Set upstream
git push --set-upstream <remote> <branch>Fetch and pull updates:
git fetch <remote>
# fetch specific branch
git fetch <remote> <branch>
git pull [options] [<remote> [<branch>]]
# Example: pull remote branch into local master
git pull origin next:master
# Pull with rebase
git pull --rebase <remote> <branch>Stash changes:
git stash save "test-cmd-stash"
git stash list
git stash pop
git stash apply
git stash drop
git stash show3. Log and History Commands
View commit history:
git log [options] [<since>..<until>] [--] [<path>...]Common log options: -p: show patches --stat: file change statistics --shortstat: summary of stats --name-only: list changed files --name-status: show added/modified/deleted files --abbrev-commit: shorten SHA‑1 --relative-date: human‑readable dates --graph: ASCII graph of branches --pretty: custom format (oneline, short, full, fuller, format)
Summarize logs:
git shortlog [options] [revision range] [--] [path...]4. Advanced Git Commands
Rebase to rewrite history: git rebase -i <startpoint> <endpoint> Bisect to find bad commits: git bisect <subcommand> <options> Cherry‑pick specific commits:
git cherry-pick <commitHash>
# multiple commits
git cherry-pick <HashA> <HashB>Blame to trace line changes: git blame <filename> Tag management for releases:
git tag <tagName>
git push origin <tagName>
git push origin --tags
# Annotated tag on specific commit
git tag -a <tagName> <commitId>
git show <tagName>
# Delete tag
git tag -d <tagName>
git push origin :refs/tags/<tagName>Verify signatures:
git verify-commit <commit>
git verify-tag [--format=<format>] <tag>…Diff to compare changes:
# Working tree vs. index
git diff
# Working tree vs. HEAD
git diff HEAD
# Index vs. HEAD
git diff --cached
# Between two commits
git diff <commit1> <commit2>
# Specific file or directory
git diff <commit1> <commit2> -- path/Graphical commit tool:
git citoolSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
