Fundamentals 28 min read

45 Essential Git Commands Every Developer Must Master

This comprehensive guide covers 45 everyday Git scenarios—from viewing commits and amending messages to advanced branching, rebasing, merging, and stash techniques—providing clear command examples and explanations for developers of all levels.

Open Source Linux
Open Source Linux
Open Source Linux
45 Essential Git Commands Every Developer Must Master

Git Basics

Git is a fundamental skill for developers, and while GUI tools like Sourcetree simplify merging, mastering the command line is essential for interviews and showcasing personal expertise.

What did I just commit?

Use git show or git log -n1 -p to display the latest commit on HEAD.

Incorrect commit message (not pushed)

Amend the message with git commit --amend --only or directly with git commit --amend --only -m 'new message'.

Wrong author/email in a single commit

Rewrite the commit using

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

. To change the author for all history, refer to git filter-branch.

Remove a file from a commit

Execute:

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

This is useful when an unwanted file was added in an open patch.

Delete the most recent commit

If the commit has not been pushed, reset with git reset --hard HEAD^. If it has been pushed, use git revert SHAofBadCommit or force‑push with git push -f (cautiously).

Delete any commit

Use interactive rebase:

git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT
git push -f [remote] [branch]

Or perform an interactive rebase and drop the unwanted lines.

Push an amended commit (error handling)

When pushing an amended commit, Git may reject the push because the remote tip is behind. Resolve by force‑pushing:

git push origin mybranch -f

Prefer creating a new commit instead of force‑pushing.

Recover after a hard reset

Run git reflog to list recent HEAD positions, find the desired SHA, then reset:

git reset --hard SHA1234

Staging (Staging Area)

Add staged changes to the previous commit

Use git commit --amend.

Stage part of a new file

Run git add --patch filename (or -p) to interactively select hunks. For a brand‑new file, first add with git add -N filename, then use git diff --cached to review.

Split changes in one file across two commits

Use git add for the first part, commit, then repeat with git add -p for the remaining changes.

Swap staged and unstaged changes

Create a temporary commit, stash the rest, reset, 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 hard to a previous commit or to the current HEAD:

# one commit
git reset --hard HEAD^
# two commits
git reset --hard HEAD^^
# four commits
git reset --hard HEAD~4
# or discard all changes
git checkout -f

To discard a specific file, use git reset filename.

Discard only part of the working copy

Use git checkout -p and answer y for the hunks you want to drop, or stash the parts you want to keep and reset the rest.

Branches

Wrong branch pull

Find the previous HEAD with git reflog, then reset:

git reset --hard <em>desired_commit_sha</em>

Discard local commits to match remote

Ensure you have no unpushed commits, then reset to the remote:

git reset --hard origin/my-branch

Accidentally deleted a branch

Recover using reflog:

git reflog
git checkout -b my-branch-help <em>sha_of_deleted_branch</em>

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

Fetch all and track the remote branch:

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

Rebasing and Merging

Undo a rebase or merge

Reset to ORIG_HEAD:

git reset --hard ORIG_HEAD

Rebase without force‑pushing

Rebase locally, then fast‑forward merge:

git checkout my-branch
git rebase -i main
git checkout main
git merge --ff-only my-branch

Combine several commits

Soft‑reset to the base and commit once:

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

For finer control, use interactive rebase ( git rebase -i) and replace pick with squash, fixup, or reword as needed.

Safe merge strategies

Merge without auto‑commit: git merge --no-ff --no-commit my-branch. Squash a branch into a single commit: git merge --squash my-branch.

Combine only unpushed commits

Interactive rebase against the upstream reference:

git rebase -i @{u}

Check if all commits on a branch are merged

Use:

git log --graph --left-right --cherry-pick --oneline HEAD...feature/branch

Or:

git log main ^feature/branch --no-merges

Interactive rebase troubleshooting

If the editor shows noop, the branches are already aligned; try rebasing onto an earlier commit (e.g., HEAD~2). For conflicts, resolve the files, add them, then continue with git rebase --continue. Use git rebase --abort to cancel.

Stash

Stash all changes

git stash

(add -u to include untracked files).

Stash specific files

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

Save a stash with a message

git stash push -m "my message"

Apply a specific stash

List stashes with git stash list, then apply:

git stash apply "stash@{n}"

Stash while keeping untracked changes

Create a stash commit and store it:

git stash create
git stash store -m "commit-message" <em>created_sha1</em>

Miscellaneous

Clone with submodules

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

or after cloning git submodule update --init --recursive.

Delete a tag

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

Recover a deleted tag

Find the unreachable tag SHA with git fsck --unreachable | grep tag, then restore:

git update-ref refs/tags/<tag_name> <sha>

Track file name case changes only

git mv --force myfile MyFile

Remove a file from Git but keep it locally

git rm --cached log.txt

Configure Git aliases

Add shortcuts in ~/.gitconfig under the [alias] section, e.g., a = add, amend = commit --amend, etc.

Cache credentials

Enable the credential cache:

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

Recover from mistakes with reflog

git reflog

shows all HEAD movements. Reset to a previous state using the desired SHA:

git reset --hard <em>sha</em>
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.

Gitcommand-linebranchingrebasingversion-control
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.