Master Essential Git Commands: Clone, Merge, Rebase, Cherry‑Pick and More
This guide walks you through the most useful Git commands—from cloning a repository and basic workflow operations to advanced techniques like rebasing, cherry‑picking, and resetting—explaining when and how to use each command, illustrating differences with visual diagrams, and warning about pitfalls when rewriting history.
This article introduces a collection of essential Git commands for developers who are new to version control or want to deepen their workflow knowledge.
Basic Commands
git clone [email protected]:nohosts/nohost.git– clone a remote repository. git pull origin master – fetch and merge the remote master branch. git branch -a – list all local and remote branches. git checkout -b bugFix – create and switch to a new branch. git status – show the current working tree status. git add . – stage all changes. git commit -m 'xxx' – commit staged changes with a message. git push – push local commits to the remote. git tag v1.0 <commit-id> – create a tag on a specific commit.
Merge vs. Rebase
Both merge and rebase integrate changes from one branch into another, but they produce different histories. merge creates a special commit with two parents, preserving the original branch topology, while rebase rewrites commits onto the tip of the target branch, yielding a linear history. git checkout master; git merge bugFix The article includes diagrams (not shown here) that illustrate the commit graph before and after each operation.
Rebase Example
git checkout bugFix; git rebase masterAfter rebasing, the bugFix commits appear directly on top of master, creating new commit IDs (e.g., C3') while the original commits remain in the graph.
Extended Rebase Usage
Rebase without checking out: git rebase targetBranch originBranch Interactive rebase to edit, reorder, squash, or drop commits: git rebase -i <commit-id> Continue after conflicts: git rebase --continue Abort a rebase:
git rebase --abortCherry‑Pick
git cherry-pick <commit-id>copies a specific commit onto the current HEAD, useful for applying isolated changes without merging the whole branch.
git checkout master; git cherry-pick C2Reset vs. Revert
git reset --hard HEAD~1moves the branch pointer backward, discarding commits as if they never existed (local only). git revert HEAD creates a new commit that undoes the changes of the specified commit, preserving history and avoiding push conflicts.
HEAD^n and HEAD~n References
These symbols navigate the commit graph: HEAD^ moves one parent up, HEAD~3 moves three commits back, and they can be chained (e.g., HEAD^2~3^).
git branch -f master HEAD~3Best Practices
Use rebase on private branches only; avoid rewriting history that has been shared. Before pushing, fetch the latest remote changes and rebase if needed. Prefer git revert over git reset for public branches to prevent conflicts.
Conclusion
The commands covered constitute the 80 % of Git operations you’ll use daily. Mastering them and understanding when to apply merge, rebase, cherry‑pick, reset, or revert will make your version‑control workflow more efficient and less error‑prone.
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.
