Comprehensive Guide to Common Git Commands and Scenarios
This article presents a detailed collection of 45 everyday Git operations covering commits, amendments, branch management, staging, rebasing, merging, stashing, and recovery techniques, providing clear command examples and explanations to help developers handle typical version‑control tasks efficiently.
git is a fundamental skill for developers, and while graphical tools like Sourcetree simplify merging, many interview and personal‑branding situations still require solid command‑line knowledge.
Below we list 45 classic Git scenarios that cover most daily needs.
What did I just commit?
If you used git commit -a and want to see what was included, display the latest commit on HEAD :
(main)$ git showor
$ git log -n1 -pI wrote the wrong commit message
If the commit hasn't been pushed, amend it with:
$ git commit --amend --onlyThis opens your editor; alternatively do it in one line:
$ git commit --amend --only -m 'xxxxxxx'If the commit was already pushed, you can amend and force‑push (not recommended).
The author name/email in my commit is wrong
For a single commit, fix it with:
$ git commit --amend --author "New Authorname
"To rewrite history for many commits, see the git filter-branch guide.
Remove a file from a specific commit
Use the following steps:
$ git checkout HEAD^ myfile
$ git add -A
$ git commit --amendThis is useful when an unwanted file was added to an open patch.
Delete my last commit
If the commit was pushed, you can reset the branch (dangerous) or create a new revert commit:
$ git reset HEAD^ --hard
$ git push -f [remote] [branch]If not pushed, a soft reset suffices:
(my-branch*)$ git reset --soft HEAD@{1}Otherwise use git revert SHAofBadCommit or force‑push if the branch is safe.
Delete an arbitrary commit
Use rebase to drop a commit:
$ git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT
$ git push -f [remote] [branch]Or perform an interactive rebase and delete the line.
Push an amended commit but get a non‑fast‑forward error
The error indicates the remote tip is ahead; you need to integrate remote changes first or force‑push:
(my-branch)$ git push origin mybranch -fGenerally avoid force‑push; create a new commit instead.
Recover after an accidental hard reset
Run git reflog to see recent HEAD positions, then reset to the desired SHA:
(main)$ git reflog
(main)$ git reset --hard SHA1234Staging
Add staged changes to the previous commit
(my-branch*)$ git commit --amendStage part of a new file
Use the patch mode:
$ git add --patch filename.xFor a brand‑new file, first add it with -N then edit the patch.
Split changes in one file across two commits
Use git add -p to interactively select hunks for each commit.
Swap staged and unstaged changes
Create a temporary commit, stash the unstaged work, reset the temporary commit, then pop the stash:
$ git commit -m "WIP"
$ git add .
$ git stash
$ git reset HEAD^
$ git stash pop --index 0Unstaged Changes
Move unstaged changes to a new branch
$ git checkout -b my-branchMove unstaged changes to an existing branch
$ git stash
$ git checkout my-branch
$ git stash popDiscard local uncommitted changes
Reset the working tree or a specific file:
# one commit back
(my-branch)$ git reset --hard HEAD^
# reset a file
$ git reset filenameDiscard only part of the unstaged changes
Checkout the unwanted parts interactively:
$ git checkout -pOr stash the parts you want to keep and reset:
$ git stash -p
$ git reset --hard
$ git stash popBranches
Accidentally pulled into the wrong branch
Use git reflog to find the previous HEAD, then reset:
$ git reset --hard c5bc55aDelete a local branch after it has been merged upstream
$ git push origin --delete my-branch
$ git branch -D my-branchCheckout a remote branch someone else is working on
$ git fetch --all
$ git checkout --track origin/davesRebasing and Merging
Undo a rebase or merge
(my-branch)$ git reset --hard ORIG_HEADCombine several commits
Soft‑reset to the base and recommit:
(my-branch)$ git reset --soft main
(my-branch)$ git commit -am "New awesome feature"Or use interactive rebase ( git rebase -i ) to squash, fixup, or reorder commits.
Check whether all commits on a branch have been merged
$ git log --graph --left-right --cherry-pick --oneline HEAD...feature/branchStash
Stash all changes
$ git stashStash specific files
$ git stash push path/to/file1.ext path/to/file2.extStash with a message
$ git stash save "my message"
# or
$ git stash push -m "my message"Apply a specific stash
$ git stash list
$ git stash apply "stash@{n}"Miscellaneous
Clone a repository with submodules
$ git clone --recursive git://github.com/foo/bar.git
# If already cloned
$ git submodule update --init --recursiveDelete a tag
$ git tag -d
$ git push
:refs/tags/Recover a deleted tag
Find the unreachable tag SHA and recreate it:
$ git fsck --unreachable | grep tag
$ git update-ref refs/tags/Rename a file only by case
(main)$ git mv --force myfile MyFileRemove a file from the repository but keep it locally
(main)$ git rm --cached log.txtAdd command aliases
Example ~/.gitconfig snippet:
[alias]
a = add
amend = commit --amend
c = commit
ca = commit --amend
ci = commit -a
co = checkout
d = diff
dc = diff --changed
ds = diff --staged
f = fetch
loll = log --graph --decorate --pretty=oneline --abbrev-commit
m = merge
one = log --pretty=oneline
outstanding = rebase -i @{u}
s = status
unpushed = log @{u}
wc = whatchanged
wip = rebase -i @{u}
zap = fetch -pCache repository credentials
$ git config --global credential.helper cache
# Cache for 1 hour
$ git config --global credential.helper 'cache --timeout=3600'Recover lost work with reflog
Use git reflog to view recent HEAD moves and reset to a previous SHA:
$ git reset --hard 0254ea7Reflog provides a safety net for accidental resets, rebases, or other destructive actions.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.