Fundamentals 7 min read

Common Git Commands: Branch, Stash, Reset, Tag, and General Operations

This article provides a concise reference of essential Git commands covering branch management, stashing changes, resetting revisions, tagging commits, and various routine operations such as pushing, ignoring files, and configuring password‑less authentication.

Java Captain
Java Captain
Java Captain
Common Git Commands: Branch, Stash, Reset, Tag, and General Operations

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 all branches.

git branch -v – show the last commit on each branch.

git branch -vv – view detailed information of the current branch.

git branch -b <branch> origin/<branch> – create a local tracking branch from a remote branch.

git branch --merged – list branches that have been merged into the current branch.

git branch --no-merged – list branches that have not been merged.

git branch -d <branch> – delete a local branch.

git branch -D <branch> – force‑delete a local branch.

git branch origin :<branch> – delete a remote branch.

git merge <branch> – merge the specified branch into the current one.

Stash Operations

git stash – stash current changes.

git stash apply – apply the most recent stash without removing it.

git stash pop – apply the most recent stash and delete it.

git stash list – list all stashes.

git stash drop <stash@{n}> – remove a specific stash.

git stash clear – clear all stashes.

Reset (Rollback) 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 (restores from index or last commit).

git reset HEAD <file> – unstage a file, moving it back to the working directory.

Tag Operations

git tag <tagname> – create a lightweight 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.

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 all actions.

git log --graph – display a visual commit graph.

git merge --no-ff -m "merge message" <branch> – perform a non‑fast‑forward merge with a custom message.

git check-ignore -v <file> – show which ignore rule applies to a file.

git add -f <file> – force‑add a file that would otherwise be ignored.

Creating a Project Repository

git init – initialize a new repository.

git remote add origin <url> – link the local repo to a remote.

git pull – fetch and merge from the remote.

git fetch – retrieve all remote branches locally.

Ignoring Files Already Tracked

git update-index --assume-unchanged <file> – ignore changes to a specific tracked file.

git rm -r --cached <path> – remove a file or directory from tracking (useful for ignoring whole directories).

Un‑ignoring Files

git update-index --no-assume-unchanged <file> – stop ignoring a previously ignored file.

Password‑less Pull/Push

git config --global credential.helper store – store credentials to avoid repeated password prompts.

gitversion controltagBranchingresetstash
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.