Master Uncommon Git Commands: From Basics to Advanced Rebase Techniques
This article introduces essential and less‑common Git commands, explains basic operations like clone, pull, branch, and commit, then dives into advanced topics such as merge versus rebase, cherry‑pick, reset versus revert, and HEAD reference syntax, providing clear examples and visual illustrations.
Basic Commands
git clone [email protected]:username/repo.git– clone a remote repository to the local machine.
git pull origin master– fetch the remote master branch and merge it into the current branch.
git branch -a– list all local and remote branches.
git checkout -b bugFix– create a new branch named bugFix and switch to it (omit
-bif the branch already exists).
git status– show the current working tree status.
git add .– stage all changes in the current directory.
git commit -m 'message'– commit staged changes with a message.
git push– push local commits to the remote branch.
git tag v1.0– create a tag v1.0 on the current commit.
Advanced Commands
Merge vs Rebase
Git integrates changes from different branches mainly via merge or rebase . merge creates a special commit with two parents, preserving the full history, while rebase rewrites commits onto another base, producing a linear history.
<code>git checkout master; git merge bugFix</code>After merging, a new commit (e.g.,
C4) appears on
masterthat contains all changes from both branches.
<code>git checkout bugFix; git rebase master</code>Rebase copies the commits from
bugFixonto the tip of
master, resulting in a linear sequence of commits.
Extended Rebase Usage
git rebase targetBranch originBranch– rebase
originBranchonto
targetBranchwithout switching branches.
git rebase -i commitid– interactively edit, reorder, squash, or drop commits starting from the specified
commitid.
Cherry‑Pick
git cherry-pick copies a specific commit from anywhere in the commit graph onto the current HEAD .
<code>git checkout master; git cherry-pick C2</code>Reset vs Revert
git revert HEAD creates a new commit that undoes the changes of the specified commit, preserving history; git reset moves the branch pointer backward, discarding commits as if they never existed.
<code>git reset HEAD~1</code> <code>git revert HEAD</code>Using
resetafter a push can cause conflicts, whereas
revertsafely adds a compensating commit.
HEAD^n and HEAD~n
HEAD^ moves one parent up; HEAD~3 moves three commits back. These operators can be chained (e.g., HEAD^2~3 ).
<code>git branch -f master HEAD~3</code>This forces
masterto point to the third ancestor of the current
HEAD.
Conclusion
The guide summarizes several less‑common Git commands, encouraging regular practice so they become familiar tools for everyday development.
Tencent IMWeb Frontend Team
IMWeb Frontend Community gathering frontend development enthusiasts. Follow us for refined live courses by top experts, cutting‑edge technical posts, and to sharpen your frontend skills.
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.