Fundamentals 29 min read

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.

Programmer DD
Programmer DD
Programmer DD
45 Essential Git Commands for Everyday Code Merging and Management

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 --amend

This 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 SHA1234

Staging (暂存)

Add staged changes to the previous commit

git commit --amend

Stage part of a new file

git add --patch filename.x

Split 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 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 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 -f

Or 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 c5bc55a

Discard local commits to match remote

git status   # shows how many commits ahead
git reset --hard origin/my-branch

Commit was made to main instead of a new branch

git branch my-branch   # create branch without switching
git reset --hard HEAD^   # move main back

Move a file from another ref to current branch

git checkout solution -- file1.txt

Split 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 5ea5173

Delete a local branch that was removed upstream

git fetch -p

Recover a deleted branch

Use reflog to find the commit hash and recreate the branch:

git checkout -b my-branch-help
git reset --hard 4e3cd85

Delete a branch

# Remote
git push origin --delete my-branch
# or
git push origin :my-branch
# Local
git branch -D my-branch

Checkout a remote branch

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

Rebasing and Merging

Undo a rebase/merge

git reset --hard ORIG_HEAD

Rebase 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-branch

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 to squash or fixup commits.

Safe merge strategies

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

Squash a branch into a single commit

git merge --squash my-branch

Combine 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-merges

Interactive 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 --continue

Abort if needed:

git rebase --abort

Stash

Stash all changes

git stash

Stash specific files

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

Stash 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_SHA1

Miscellaneous

Clone all submodules

git clone --recursive git://github.com/foo/bar.git
# If already cloned
git submodule update --init --recursive

Delete 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 MyFile

Remove from Git but keep locally

git rm --cached log.txt

Configuration

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 -p

Cache 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 main

Reset to a previous commit using its hash:

git reset --hard 0254ea7
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.

branchingrebasingstash
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

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.