Undo last commit but keep changes
This command undoes the last commit but keeps all the changes in your working directory, so you can make adjustments before committing again.
git reset --soft HEAD~1
View commit history as a graph
Displays the commit history as a text-based graphical representation, showing branches and merges.
git log --graph --oneline --all
Example:
* a1b2c3d (HEAD -> main) Add new feature
* e4f5g6h Fix bug in login
|\
| * i7j8k9l (feature) Add feature implementation
|/
* m0n1o2p Initial commit
Stash only unstaged changes
Stashes only the changes that haven't been staged yet, keeping your staged changes intact.
git stash --keep-index
Find which commit deleted a line
Uses git blame with the -w option to find which commit deleted a specific line of code.
git log -S "<deleted-text>" -- <file-path>
Example:
git log -S "function getUserData" -- src/utils.js
Create an empty commit
Creates a commit with no changes, which can be useful for triggering CI/CD pipelines or marking points in history.
git commit --allow-empty -m "Empty commit message"
Find common ancestor of two branches
Finds the common ancestor commit between two branches, useful for understanding where branches diverged.
git merge-base <branch1> <branch2>
Example:
git merge-base main feature-branch
Interactive rebase
Allows you to modify commits in many ways such as editing, deleting, and squashing as you move them to the new base.
git rebase -i HEAD~<number-of-commits>
Example:
git rebase -i HEAD~3
Temporarily switch to another commit
Temporarily switches to another commit without creating a branch, useful for quick inspection.
git checkout <commit-hash>
Example:
git checkout a1b2c3d
Find which commit introduced a bug
Uses binary search to find which commit introduced a bug by marking commits as good or bad.
git bisect start
git bisect bad
git bisect good <commit-hash>
# Git will checkout commits for you to test
# Mark each as good or bad: git bisect good/bad
# When done: git bisect reset
Show changes between two commits
Displays the differences between two commit references.
git diff <commit1>..<commit2>
Example:
git diff HEAD~3..HEAD
Rename local and remote branch
Renames both local and remote branches to a new name.
# Rename local branch
git branch -m <old-name> <new-name>
# Delete old remote branch and push new one
git push origin :<old-name>
git push origin <new-name>
Recover deleted branch
Recovers a branch that was deleted, as long as the garbage collection hasn't run yet.
git reflog
# Find the hash of the commit at the tip of deleted branch
git checkout -b <branch-name> <commit-hash>
Commit only part of a file
Allows you to stage and commit only specific parts of a file, rather than the entire file.
git add -p <file-path>
Find who changed a line
Shows who last modified each line of a file and in which commit.
git blame <file-path>
Example:
git blame src/components/Button.jsx
Clean untracked files and directories
Removes all untracked files and directories, useful for cleaning up your working directory.
git clean -fd