45 Essential Git Commands for Everyday Code Merging and Management
This guide compiles 45 practical Git commands covering commit inspection, message amendment, author correction, file removal, branch manipulation, rebasing, merging, stash usage, tag handling, and configuration tricks, providing developers with a comprehensive reference for everyday version‑control tasks.
Git Basics
Git is a fundamental skill for developers; while GUI tools like Sourcetree simplify merging, mastering command‑line Git is essential for interviews and showcasing personal expertise.
What did I just commit?
Use git show or git log -n1 -p to view the latest commit on HEAD.
Incorrect commit message
If the commit has not been pushed, amend it with git commit --amend --only or directly with git commit --amend --only -m 'new message'. After pushing, rewriting requires a force push, which is discouraged.
Wrong author information
Amend a single commit with
git commit --amend --author "New Author <[email protected]>". To rewrite history, see git filter-branch.
Remove a file from a commit
Run:
git checkout HEAD^ myfile
git add -A
git commit --amendThis is useful for correcting an accidental file addition before force‑pushing.
Delete the last commit
If the commit is already pushed, use:
git reset HEAD^ --hard
git push -f [remote] [branch]If not pushed, a soft reset restores the state: git reset --soft HEAD@{1} Alternatively, git revert SHAofBadCommit creates a new commit that undoes the changes.
Delete any commit
Use rebasing:
git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT
git push -f [remote] [branch]Or interactive rebase to drop specific lines.
Force‑push after an amended commit
When a push is rejected because the remote is ahead, you must force‑push: git push origin mybranch -f Avoid force‑pushing when possible; create a new commit instead.
Recover after a hard reset
Use reflog to find the lost commit and reset:
git reflog
git reset --hard SHA1234Staging (暂存)
Add staged changes to the previous commit
git commit --amendStage part of a new file
git add --patch filename.xSplit changes into two commits
Use git add for the whole file and git add -p for interactive selection.
Swap staged and unstaged changes
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:
# one commit
git reset --hard HEAD^
# two commits
git reset --hard HEAD^^
# four commits
git reset --hard HEAD~4
# or
git checkout -fOr reset a specific file with git reset filename.
Branches
Wrong branch pull
Use git reflog to find the previous HEAD, then reset:
git reset --hard c5bc55aDiscard local commits to match remote
git status # shows how many commits ahead
git reset --hard origin/my-branchCommit was made to main instead of a new branch
git branch my-branch # create branch without switching
git reset --hard HEAD^ # move main backMove a file from another ref to current branch
git checkout solution -- file1.txtSplit commits across branches
Reset main to a clean commit, then create branches and cherry‑pick the relevant commits:
git reset --hard a13b85e
git checkout -b 21
git cherry-pick e3851e8
git checkout main
git checkout -b 14
git cherry-pick 5ea5173Delete a local branch that was removed upstream
git fetch -pRecover a deleted branch
Use reflog to find the commit hash and recreate the branch:
git checkout -b my-branch-help
git reset --hard 4e3cd85Delete a branch
# Remote
git push origin --delete my-branch
# or
git push origin :my-branch
# Local
git branch -D my-branchCheckout a remote branch
git fetch --all
git checkout --track origin/davesRebasing and Merging
Undo a rebase/merge
git reset --hard ORIG_HEADRebase without force‑push
After rebasing, push with fast‑forward only or merge instead of pushing directly.
git checkout my-branch
git rebase -i main
git checkout main
git merge --ff-only my-branchCombine several commits
Soft‑reset onto the base branch and recommit:
git reset --soft main
git commit -am "New awesome feature"Or use interactive rebase to squash or fixup commits.
Safe merge strategies
git merge --no-ff --no-commit my-branchSquash a branch into a single commit
git merge --squash my-branchCombine only unpushed commits
git rebase -i @{u}Check if all commits on a branch are merged
git log --graph --left-right --cherry-pick --oneline HEAD...feature/branch
# or
git log main ^feature/branch --no-mergesInteractive rebase troubleshooting
If the rebase editor shows noop, the branches are already aligned; try rebasing onto an earlier commit.
Resolve rebase conflicts
Identify conflicted files with git status, edit the conflict markers, then continue:
git add README.md
git rebase --continueAbort if needed:
git rebase --abortStash
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}"
# or by time
git stash apply "stash@{2.hours.ago}"Store a stash without popping
git stash create
git stash store -m "commit-message" CREATED_SHA1Miscellaneous
Clone all submodules
git clone --recursive git://github.com/foo/bar.git
# If already cloned
git submodule update --init --recursiveDelete a tag
git tag -d <tag_name>
git push <remote> :refs/tags/<tag_name>Recover a deleted tag
git fsck --unreachable | grep tag # find hash
git update-ref refs/tags/<tag_name> <hash>Recover a lost patch
Manually copy the commit content to a new branch, commit, adjust the author, and create a new pull request.
Track files
Rename case‑only
git mv --force myfile MyFileRemove from Git but keep locally
git rm --cached log.txtConfiguration
Add aliases
In ~/.gitconfig under [alias] you can define shortcuts such as:
[alias]
a = add
amend = commit --amend
c = commit
co = checkout
d = diff
s = status
zap = fetch -pCache credentials
git config --global credential.helper cache
git config --global credential.helper 'cache --timeout=3600'Use reflog to recover lost work
Reflog records every change to HEAD locally, even if the commit is no longer referenced.
git reflog
# Example output
0a2e358 HEAD@{0}: reset: moving to HEAD~2
0254ea7 HEAD@{1}: checkout: moving from 2.2 to mainReset to a previous commit using its hash:
git reset --hard 0254ea7Signed-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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
