Fundamentals 28 min read

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.

Architecture Digest
Architecture Digest
Architecture Digest
Comprehensive Guide to Common Git Commands and Scenarios

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 show

or

$ git log -n1 -p

I wrote the wrong commit message

If the commit hasn't been pushed, amend it with:

$ git commit --amend --only

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

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

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

Staging

Add staged changes to the previous commit

(my-branch*)$ git commit --amend

Stage part of a new file

Use the patch mode:

$ git add --patch filename.x

For 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 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 or a specific file:

# one commit back
(my-branch)$ git reset --hard HEAD^
# reset a file
$ git reset filename

Discard only part of the unstaged changes

Checkout the unwanted parts interactively:

$ git checkout -p

Or stash the parts you want to keep and reset:

$ git stash -p
$ git reset --hard
$ git stash pop

Branches

Accidentally pulled into the wrong branch

Use git reflog to find the previous HEAD, then reset:

$ git reset --hard c5bc55a

Delete a local branch after it has been merged upstream

$ git push origin --delete my-branch
$ git branch -D my-branch

Checkout a remote branch someone else is working on

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

Rebasing and Merging

Undo a rebase or merge

(my-branch)$ git reset --hard ORIG_HEAD

Combine 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/branch

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}"

Miscellaneous

Clone a repository with submodules

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

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

Remove a file from the repository but keep it locally

(main)$ git rm --cached log.txt

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

Cache 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 0254ea7

Reflog provides a safety net for accidental resets, rebases, or other destructive actions.

gitmergerebaseversion controlbranchcommitstash
Architecture Digest
Written by

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.

0 followers
Reader feedback

How this landed with the community

login 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.