Master Over 70 Essential Git Commands and Tricks for Developers
This article compiles more than seventy practical Git commands and techniques, explains core concepts such as working directory, staging area, and commit IDs, and provides ready‑to‑use snippets for everyday version‑control tasks, all backed by the open‑source git‑tips and HelloGitHub projects.
Git is a distributed version‑control system that records every change in a project, enabling retrieval of any past state, collaboration across machines, and efficient history management. The commands below are collected from the open‑source repository https://github.com/521xueweihan/git-tips (6.8K Stars) and the related collection https://github.com/521xueweihan/HelloGitHub (11K Stars).
Fundamental concepts
Working directory – where files are added, edited, or deleted.
Staging area – files added with git add are placed here.
Local repository – created with git commit; each commit represents a version.
Remote repository – updated with git push and fetched with git pull or git fetch.
Commit‑id – the SHA‑1 hash shown by git log that uniquely identifies a commit.
Commonly used commands
Help and information
git help -gLists concept guides and common command groups.
Synchronise with remote
git fetch --all && git reset --hard origin/masterDiscard all local changes and reset the working tree to the state of the remote master branch.
Reset the first commit
git update-ref -d HEADDeletes the current HEAD reference, removing all existing commits so a new initial commit can be created.
Show differences
git diffShows changes between the working directory and the staging area. git diff --cached Shows changes staged for the next commit. git diff HEAD Shows changes between the working directory and the last commit.
Branch operations
git branch -vvDisplays local branches with upstream tracking information. git checkout - Switches quickly to the previously checked‑out branch.
git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -dDeletes branches that have already been merged into master. git branch -r Lists all remote branches. git branch -a Lists both local and remote branches. git checkout -b <branch-name> Creates and switches to a new local branch.
git checkout -b <branch-name> origin/<branch-name>Creates a local branch that tracks a remote branch. git branch -d <local-branch> Deletes a local branch. git push origin --delete <remote-branch> Deletes a remote branch (alternative syntax: git push origin :<remote-branch>). git branch -m <new-branch-name> Renames the current branch.
Tag management
git tagLists all tags. git describe --tags --abbrev=0 Shows the most recent tag on the current branch. git tag -ln Shows detailed information for each tag. git tag <version-number> Creates a lightweight tag on the latest commit.
git tag -a <version-number> -m "v1.0 release" <commit-id>Creates an annotated tag on a specific commit. git push origin <local-version-number> Pushes a single tag to the remote. git push origin --tags Pushes all local tags. git tag -d <tag-name> Deletes a local tag. git push origin :refs/tags/<tag-name> Deletes a remote tag.
git checkout -b <branch-name> <tag-name>Creates a new branch from a tag.
Stash operations
git stashSaves current changes (including staged files) without committing. git stash -u Saves changes and untracked files. git stash list Shows all stash entries. git stash apply <stash@{n}> Applies a specific stash without removing it. git stash pop Applies the latest stash and removes it from the stash list. git stash clear Removes all stashes.
git checkout <stash@{n}> -- <file-path>Restores a specific file from a stash.
Inspecting the repository
git ls-files -tShows all tracked files. git ls-files --others Shows all untracked files. git ls-files --others -i --exclude-standard Shows all ignored files. git status --ignored Displays ignored files in the status output. git log Shows commit history.
git log --pretty=oneline --graph --decorate --allShows a condensed, graphical commit history. git reflog Shows a log of all updates to HEAD, useful for recovering lost commits. git blame <file-name> Shows line‑by‑line author information for a file. git log --show-signature Displays GPG signatures in the commit log.
Amending and resetting
git commit --amendModifies the most recent commit message or adds staged changes to it. git revert <commit-id> Creates a new commit that undoes the changes introduced by the specified commit. git reset <commit-id> Resets HEAD to a specific commit (default --mixed), affecting the staging area. git reset --hard <commit-id> Resets HEAD, index, and working directory to the given commit, discarding all changes. git reset --soft HEAD~3 Moves HEAD back three commits while keeping changes staged.
Configuration
git config --global alias.st statusCreates a shortcut alias git st for git status. git config --global --list Shows global configuration entries. git config --local --list Shows configuration for the current repository. git remote set-url origin <URL> Changes the URL of the remote named origin. git remote add origin <remote-url> Adds a new remote repository. git remote Lists all configured remotes.
Advanced operations
git bundle create <file> <branch-name>Exports a branch into a single bundle file.
git clone repo.bundle <repo-dir> -b <branch-name>Clones a repository from a bundle and checks out the specified branch. git rebase --autostash Automatically stashes local changes before a rebase and reapplies them afterward.
git fetch origin pull/<id>/head:<branch-name>Fetches a specific pull‑request commit from a remote repository. git diff --word-diff Shows word‑level differences. git clean -f <file-name> Force‑removes an untracked file (cannot be recovered). git clean -df <directory-name> Force‑removes an untracked directory. git clean -X -f Removes files ignored by .gitignore. git config core.fileMode false Ignores permission changes when detecting file modifications.
git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/Lists all local branches ordered by most recent commit. git log --all --grep='<given-text>' Searches commit messages for a specific text across all branches. git reset <file-name> Unstages a specific file, moving it back to the working directory.
git push -f <remote-name> <branch-name>Force‑pushes a branch to the remote.
All commands have been tested with git version 2.7.4 (Apple Git-66) . It is recommended to try commands in a safe test repository before applying them to production code.
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.
