Fundamentals 25 min read

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.

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

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

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

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

Staging (add to previous commit)

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

Stage part of a new file

$ git add --patch filename.x

If the file is new, use:

$ git add -N filename.x

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

Move unstaged changes to a new branch

$ git checkout -b my-branch

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

Discard specific unstaged files

$ git checkout -p

Delete a local branch

$ git branch -D my-branch

Delete a remote branch

$ git push origin --delete my-branch

Checkout a remote branch locally

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

Undo a rebase or merge

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

Combine 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~2

Stash all changes

$ git stash

Stash 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.git

If already cloned:

$ git submodule update --init --recursive

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

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