45 Essential Git Commands Every Developer Should Master
This comprehensive guide covers 45 everyday Git scenarios—from viewing commits and amending messages to managing branches, rebasing, merging, stashing changes, and configuring aliases—providing clear command examples that help developers handle version‑control tasks safely and efficiently.
Git is a fundamental tool for developers, and mastering its commands is essential for everyday workflow. Below are concise examples for common situations.
View what you just committed
(main)$ git show $ git log -n1 -pFix a wrong commit message (not pushed)
$ git commit --amend --only $ git commit --amend --only -m 'new message'Change author information for the last commit
$ git commit --amend --author "New Author <[email protected]>"To rewrite history for all commits, see git filter-branch.
Remove a file from a specific commit
$ git checkout HEAD^ myfile
$ git add -A
$ git commit --amendDelete the most recent commit
$ git reset HEAD^ --hard
$ git push -f [remote] [branch]If the commit is not pushed, use a soft reset:
(my-branch*)$ git reset --soft HEAD@{1}Delete an arbitrary commit
$ git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT
$ git push -f [remote] [branch]Or use interactive rebase to drop lines.
Push an amended commit
To https://github.com/yourusername/repo.git
! [rejected] mybranch -> mybranch (non-fast-forward)
hint: Updates were rejected because the tip of your current branch is behind its remote counterpart.Force‑push with git push -f only when necessary.
Recover after a hard reset
(main)$ git reflog (main)$ git reset --hard SHA1234Staging (add to previous commit)
(my-branch*)$ git commit --amendStage part of a new file
$ git add --patch filename.xIf the file is new, use:
$ git add -N filename.xSplit changes between two commits
Use git add -p to interactively select hunks.
Swap staged and unstaged changes
$ git commit -m "WIP"
$ git add .
$ git stash
$ git reset HEAD^
$ git stash pop --index 0Move unstaged changes to a new branch
$ git checkout -b my-branchDiscard local uncommitted changes
# one commit
(my-branch)$ git reset --hard HEAD^
# two commits
(my-branch)$ git reset --hard HEAD^^
# four commits
(my-branch)$ git reset --hard HEAD~4
# or
(main)$ git checkout -fDiscard specific unstaged files
$ git checkout -pDelete a local branch
$ git branch -D my-branchDelete a remote branch
$ git push origin --delete my-branchCheckout a remote branch locally
$ git fetch --all
$ git checkout --track origin/davesUndo a rebase or merge
(my-branch)$ git reset --hard ORIG_HEADCombine commits (squash)
(my-branch)$ git reset --soft main
(my-branch)$ git commit -am "New awesome feature"Or use interactive rebase:
(main)$ git rebase -i HEAD~2Stash all changes
$ git stashStash with a message
$ git stash save "work in progress"
$ git stash push -m "work in progress"Apply a specific stash
$ git stash list
$ git stash apply "stash@{0}"Clone with submodules
$ git clone --recursive git://github.com/foo/bar.gitIf already cloned:
$ git submodule update --init --recursiveDelete and recover tags
$ git tag -d <tag_name>
$ git push <remote> :refs/tags/<tag_name>
# Recover
$ git fsck --unreachable | grep tag
$ git update-ref refs/tags/<tag_name> <hash>Configure command aliases
[alias]
a = add
amend = commit --amend
c = commit
co = checkout
d = diff
loll = log --graph --decorate --pretty=oneline --abbrev-commit
...Cache credentials
$ git config --global credential.helper cache
$ git config --global credential.helper 'cache --timeout=3600'Use reflog to recover lost work
(main)$ git reflog
0a2e358 HEAD@{0}: reset: moving to HEAD~2
...Reset to a previous commit using the recorded SHA.
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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
