Fundamentals 26 min read

45 Essential Git Commands Every Developer Should Master

This comprehensive guide covers 45 everyday Git scenarios—from viewing commits and fixing messages to advanced branching, rebasing, merging, stashing, and recovering lost work—providing clear command examples and best‑practice tips to help developers manage code efficiently.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
45 Essential Git Commands Every Developer Should Master

What did I just commit?

If you used git commit -a and want to see what was committed, run git show or git log -n1 -p.

My commit message is wrong

If the commit hasn't been pushed, amend it with git commit --amend --only or git commit --amend --only -m 'new message'. If it has been pushed, avoid force‑pushing.

Wrong author/email in a commit

For a single commit, run

git commit --amend --author "New Author <[email protected]>"

. To rewrite history, see git filter-branch.

Remove a file from a commit

Use:

git checkout HEAD^ myfile
git add -A
git commit --amend

Delete the last commit

If not pushed, reset with git reset HEAD^ --hard and push force if needed. If already pushed, use git revert SHAofBadCommit or force‑push with care.

Delete any commit

Use rebasing:

git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT

or interactive rebase to drop lines.

Push an amended commit

After amending, you must force‑push: git push -f. Prefer creating a new commit instead of force‑pushing.

Recover after a hard reset

Run git reflog to find the lost commit SHA, then git reset --hard SHA1234.

Staging

Add staged changes to the previous commit

git commit --amend

Stage part of a new file

Use git add --patch filename (or -p) and for new files git add -N filename then edit with git diff --cached.

Split changes into two commits

Use git add -p to interactively select hunks for each commit.

Swap staged and unstaged changes

git commit -m "WIP"
git add .
git stash
git reset HEAD^
git stash pop --index 0

Unstaged Changes

Move unstaged changes to a new branch

git checkout -b my-branch

Move unstaged changes to an existing branch

git stash
git checkout my-branch
git stash pop

Discard uncommitted changes

Reset hard to a previous commit or use git checkout -f. To discard a specific file: git reset filename. To discard parts, use git checkout -p or git stash -p followed by git reset --hard and git stash pop.

Branches

Wrong branch pull

Find the correct commit with git reflog and reset: git reset --hard c5bc55a.

Discard local commits to match remote

git reset --hard origin/my-branch

Commit to a new branch after committing to main

git branch my-branch
git reset --hard HEAD^
git checkout my-branch

Copy a whole file from another ref

git checkout solution -- file1.txt

Split commits into separate branches

Use git checkout -b 21 then git cherry-pick e3851e8, repeat for other bugs.

Delete a remote branch

git push origin --delete my-branch

Delete a local branch

git branch -D my-branch

Checkout a teammate's remote branch

git fetch --all
git checkout --track origin/daves

Rebasing and Merging

Undo a rebase/merge

git reset --hard ORIG_HEAD

Combine several commits

Soft‑reset onto the base branch and recommit:

git reset --soft main
git commit -am "New awesome feature"

Or use interactive rebase ( git rebase -i) to squash, fixup, or reorder commits.

Safe merge

git merge --no-ff --no-commit my-branch

Squash a branch into one commit

git merge --squash my-branch

Combine 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

Interactive rebase conflict resolution

Use git status to find conflicted files, edit them, then git add and git rebase --continue. Abort with git rebase --abort.

Stash

Stash all changes

git stash

Stash specific files

git stash push path/to/file1 path/to/file2

Stash with a message

git stash push -m "my message"

Apply a specific stash

git stash apply "stash@{0}"

Keep unstaged changes while stashing

git stash create
git stash store -m "commit-message" CREATED_SHA1

Miscellaneous

Clone with submodules

git clone --recursive git://github.com/foo/bar.git

Delete a tag

git tag -d <tag_name>
git push <remote> :refs/tags/<tag_name>

Recover a deleted tag

git fsck --unreachable | grep tag
# note the hash
git update-ref refs/tags/<tag_name> <hash>

Cache credentials

git config --global credential.helper cache
# optional timeout (seconds)
git config --global credential.helper 'cache --timeout=3600'

Use reflog to recover lost work

git reflog
# find the desired HEAD@{n}
git reset --hard HEAD@{n}
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Gitmergerebasecommand-lineVersion Controlbranchingstash
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

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.