Master Essential Git Commands: Branching, Stashing, Merging, Tagging & More
This guide compiles the most commonly used Git commands for branch management, stashing changes, reverting commits, handling tags, performing routine operations, initializing repositories, ignoring files, and configuring password‑less pushes, providing a concise reference for developers to streamline version control workflows.
Branch Operations:
git branch create a new branch
git branch -b create and switch to a new branch
git checkout switch branches
git branch list branches
git branch -v view the last commit of each branch
git branch -vv view the current branch
git branch -b <branch> origin/<branch> create a local tracking branch from a remote branch
git branch --merged list branches merged into the current branch
git branch --no-merged list branches not merged into the current branch
git branch -d <branch> delete a local branch
git branch -D <branch> force‑delete a branch
git push origin :<branch> delete a remote branch
git merge <branch> merge a branch into the current branch
Stash Operations:
git stash stash current changes
git stash apply apply the most recent stash
git stash pop apply and drop the most recent stash
git stash list list all stashes
git stash drop stash@{0} remove a specific stash
git stash clear clear all stashes
Revert Operations:
git reset --hard HEAD^ revert to the previous commit
git reset --hard <commit_id> revert to a specific commit
git checkout -- <file> discard changes in a file (from index or working tree)
git reset HEAD <file> unstage a file, moving it back to the working directory
Tag Operations:
git tag <tagname> create a tag on the current commit
git tag <tagname> <commit_id> tag a specific commit
git tag -a <tagname> -m "description" create an annotated tag with a message
git tag list all tags
git show <tagname> display tag details
git tag -d <tagname> delete a local tag
git push origin <tagname> push a tag to the remote repository
git push origin --tags push all tags to the remote repository
git push origin :refs/tags/<tagname> delete a remote tag
General Operations:
git push origin <branch> push a local branch to the remote
git rm -r --cached <path> stop tracking a file or directory
git reflog view the reference log of commands
git log --graph visualize branch merges
git merge --no-ff -m "merge message" <branch> merge without fast‑forward to retain a merge commit
git check-ignore -v <file> see which ignore rule applies
git add -f <file> force‑add a file
Initialize a Project Repository:
git init initialize a new repository
git remote add origin <url> add a remote origin
git pull fetch and merge from the remote
git fetch retrieve all remote branches locally
Ignore Files Already in the Repository:
git update-index --assume-unchanged <file> ignore a single file
git rm -r --cached <path> stop tracking a file or directory (ignore all files in that path)
Cancel Ignored Files:
git update-index --no-assume-unchanged <file> stop ignoring a file
Password‑less Pull/Push:
git config --global credential.helper store enable credential caching for password‑less operations
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.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
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.
