Git Command-Line Tricks
Interactive Add
This command allows you to stage parts of a file instead of the entire file. This is super helpful when you only want to commit certain changes in a file while leaving the rest for later.
Use this when you're working on multiple features in one file but want to commit them separately!
git add -p
Undo the Last Commit
Made a mistake in your last commit? No problem. This command undoes your last commit but keeps the changes in your working directory, so you can easily fix the issue.
Use --soft if you want to keep the changes; use --hard if you want to undo everything, including your local changes.
git reset --soft HEAD~1
Check Your Branch's Upstream Status
This command fetches all updates from the remote and prunes (deletes) references to branches that have been deleted from the remote.
Run this periodically to avoid clutter in your branch list, especially when working in large teams.
git fetch --all --prune
Quick Commit Fix
Forgot to add a file or made a typo in your commit message? git commit --amend lets you update the last commit without creating a new one.
This is great for squashing small mistakes without polluting your Git log with unnecessary commits.
git commit --amend
Stash Your Work
Got to switch branches but don't want to lose your current changes? Stashing lets you save your work without committing it, allowing you to return to it later.
Use git stash save "description" to add a description so you can quickly identify your stashes later.
git stash
Pop Your Stash
When you're ready to get back to your stashed work, you can "pop" it back into your working directory.
Use git stash apply if you want to apply the stash without removing it.
git stash pop
Cherry-Picking Commits
Need a specific commit from another branch? Cherry-picking lets you apply it to your current branch without merging the entire branch.
This is especially useful when you need to backport bug fixes or small features.
git cherry-pick
Clean Up Local Branches
Once you're done with a feature, don't let old branches linger. Clean them up with this simple command.
Use git branch -D if you need to force-delete a branch that hasn't been merged yet.
git branch -d
View File History
Track the evolution of a specific file with git log -- . This shows you all the commits that affected that file.
Add --stat to see more detailed information about changes.
git log --
Blame a Line of Code
Want to know who wrote a specific line of code? git blame gives you a line-by-line history of who changed what in a file.
Combine this with git log -- to get a more detailed history of changes.
git blame
Find the Source of a Bug
This powerful tool performs a binary search through your commit history to find the commit that introduced a bug.
Use it in complex projects where you can't easily pinpoint when things broke.
git bisect start git bisect bad git bisect good
Abort a Merge
If you've started a merge and things aren't going as planned, this command will abort the merge and return you to your previous state.
Always make sure your working directory is clean before attempting a merge.
git merge --abort
Search Commit Messages
Looking for a specific commit message? Use git log --grep to search through commit messages.
Combine this with git log --author to find commits by specific developers.
git log --grep="search term"
Tagging a Commit
Tags are useful for marking specific points in your Git history, such as releases.
Use lightweight tags (git tag ) when you don't need additional metadata.
git tag -a v1.0 -m "Version 1.0 release"
Hard Reset to Clean the Workspace
Need to get rid of untracked files and directories quickly? This command wipes them out, leaving only files under version control.
Use with caution — make sure you won't need those untracked files later!
git clean -fd
View All Git Operations
If you've ever messed up your Git history, git reflog is your safety net. It shows a log of all operations on your repository, allowing you to recover lost changes.
Use this when you think all hope is lost after a bad reset or rebase!
git reflog
Squash Commits
Want to clean up your commit history before pushing? Squashing commits lets you combine several into one for a neater history.
This is ideal for combining multiple small fixes into one clear commit before pushing.
git rebase -i HEAD~
Revert a Commit
Need to undo a specific commit without affecting your entire history? git revert creates a new commit that undoes the changes from the specified commit.
git revert
See a Graph of Your Branches
This command gives you a visual overview of your branch history, making it easier to see merges, branches, and commits.
git log --graph --oneline --all