Essential Git Commands Every Developer Should Master
This guide compiles the most frequently used Git commands, explaining their purpose, common options, and practical scenarios such as logging, branching, committing, rebasing, merging, stashing, and reverting, to help developers work efficiently with version control in real-world projects.
Introduction
The article gathers high‑frequency Git commands that the author uses in daily projects, focusing on practical usage rather than exhaustive documentation. It assumes Git version 2.24.0 and targets developers who need concrete examples for real business scenarios.
Viewing History
git log displays a concise one‑line history:
# Output a compact log
# Equivalent to: git log --pretty=oneline --abbrev-commit
git log --oneline
# Show the last 5 commits
git log --oneline -5
# Graphical view similar to GUI tools
git log --graph --date=relative --pretty=tformat:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%an %ad)%Creset'Working Directory Status
git status shows the current state of the working tree. Short format is available via git status -s. The command can also display stash information with --show-stash.
Branch and Commit Navigation
git checkout switches to a specific commit, branch, or tag. Common forms include:
git checkout dev
git checkout origin/test
git checkout --track origin/feature-test
git checkout -b testbranch
git checkout -- file
git checkout .
git checkout -Creating Commits
Typical commit commands:
git commit --amend --no-edit
git commit --no-verify -m "xxx"
git commit -m "xxx"
git commit -t templateFile
git commit -FUndoing Changes
git reset moves the HEAD pointer. Frequently used options:
git reset --hard commit_sha1
git reset --soft commit_sha1
git reset --soft HEAD~1
git reset --mixed commit_sha1
git reset --merge commit_sha1
git reset --keep commit_sha1git revert creates a new commit that undoes a previous change while preserving history:
git revert commit-sha1Rebasing
Interactive rebasing cleans up commit history. Common commands:
git rebase -i git-sha1|branch(HEAD)
git rebase --continue
git rebase --skip
git rebase --abortRebase action keywords:
pick – keep the commit
edit – amend the commit after checking it out
reword – change the commit message
squash – combine with the previous commit
fixup – combine without keeping the message
drop – discard the commit
Merging and Pulling
git merge integrates changes without fast‑forward: git merge --no-ff branchName git pull fetches and integrates remote changes. Using --rebase (or -r) keeps a linear history, while the default performs a fast‑forward merge that creates a new commit.
Pushing Changes
Common push variations:
git push origin localbranch
git push -d origin branchName
git push --tags
git push --follow-tags
git push -f origin branchName
git push --force-with-leaseRemote Management
Managing multiple repository URLs:
git remote add origin url
git remote add github url
git remote set-url origin new_urlBranch Operations
Typical branch commands focus on deletion, renaming, and upstream tracking:
git branch -d branchName
git branch -M oldBranch newNameBranch
git branch --set-upstream-to=origin/xxx
git branch --set-upstream-to origin xxxStashing Work
Stash saves unfinished work. Adding a descriptive message is recommended.
git stash save stashName
git stash -u save stashName
git stash push -m "changed xx"
git stash apply stash@{0}
git stash pop stash@{0}
git stash list
git stash clear
git stash drop stash@{0}
git stash show stash@{0}Reflog
git reflog records all actions (rebase, merge, reset, etc.) allowing recovery of lost commits:
git reflog -5Cherry‑Pick
Selective commit copying from other branches:
git cherry-pick commit-sha1
git cherry-pick master~4 master~2
git cherry-pick startGitSha1..endGitSha1Removing Files
Common git rm usage for updating .gitignore:
git rm --cache -- file
git rm -r --cached .
git add .
git commit -m "xxx"Repository Information
git rev-parse extracts repository details useful in scripts:
git rev-parse --short HEAD --verify
git rev-parse --show-toplevel
git rev-parse --git-dir
git rev-parse --allDiffing Changes
git diff shows file differences; the author prefers GUI tools for larger changes.
Conclusion
Mastering these high‑frequency Git commands equips developers with a solid workflow for version control, while many commands echo familiar Linux patterns. The list is not exhaustive, and readers are encouraged to share additional tips or corrections.
Signed-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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
