20 Essential Git Command-Line Tricks Every Developer Should Know
Git is a powerful version control system that every developer should master. Whether youβre a beginner or an experienced developer, knowing a few handy Git command-line tricks can save you time and improve your workflow. Here are 20 essential Git tips and tricks to boost your efficiency.
1. Undo the Last Commit (Without Losing Changes)
git reset --soft HEAD~1
If you made a commit but want to undo it while keeping your changes, this command resets the last commit but retains the modified files in your staging area.
This is useful when you realize you need to make more changes before committing.
If you also want to remove the changes from the staging area but keep them in your working directory, use,
git reset HEAD~1
2. Discard Unstaged Changes
git checkout -- <file>
Use this to discard local changes in a file before staging. Be careful, as this cannot be undone! If you want to discard all unstaged changes in your working directory, use,
git reset --hard HEAD
3. Delete a Local Branch
git branch -d branch-name
Removes a local branch safely if itβs already merged. If itβs not merged and you still want to delete it, use -D
git branch -D branch-name
4. Delete a Remote Branch
git push origin --delete branch-name
Deletes a branch from the remote repository, useful for cleaning up old feature branches. If you mistakenly deleted the branch and want to restore it, you can use
git checkout -b branch-name origin/branch-name
if it still exists remotely.
5. Rename a Local Branch
git branch -m old-name new-name
Useful when you want to rename a branch locally without affecting the remote repository. To update the remote reference after renaming, push the renamed branch and delete the old one,
git push origin -u new-name git push origin --delete old-name
6. See the Commit History in a Compact Format
git log --oneline --graph --decorate --all
A clean and structured way to view Git history, showing branches and commits in a visual format. If you want to see a detailed history with diffs, use
git log -p
7. Stash Your Changes Temporarily
git stash
If you need to switch branches but donβt want to commit yet, stash your changes and retrieve them later with
git stash pop
To see all stashed changes
git stash list
8. Find the Author of a Line in a File
git blame file-name
Shows who made changes to each line in a file. Helpful for debugging or reviewing historical changes. If you want to ignore whitespace changes
git blame -w file-name
9. View a File from a Previous Commit
git show commit-hash:path/to/file
Useful for checking an older version of a file without switching branches. If you want to restore the file from an old commit
git checkout commit-hash -- path/to/file
10. Reset a File to the Last Committed Version
git checkout HEAD -- file-name
Restores the file to the last committed state, removing any local changes. If you want to reset all files
git reset --hard HEAD
11. Clone a Specific Branch
git clone -b branch-name --single-branch repository-url
Instead of cloning the entire repository, this fetches only the specified branch, saving time and space. If you want all branches but donβt want to check them out initially:
git clone --mirror repository-url
12. Change the Last Commit Message
git commit --amend -m "New message"
Use this to correct a typo in your last commit message before pushing. Be cautiousβif youβve already pushed, use
git push --force-with-lease
13. See the List of Tracked Files
git ls-files
Displays all files being tracked by Git, which is useful for auditing your repository. To see ignored files
git ls-files --others --ignored --exclude-standard
14. Check the Difference Between Two Branches
git diff branch-1..branch-2
Compares changes between two branches, helping you understand what has been modified. To see only file names that changed
git diff --name-only branch-1..branch-2
15. Add a Remote Repository
git remote add origin repository-url
Links a remote repository to your local project, enabling push and pull operations. To verify remote repositories
git remote -v
16. Remove a Remote Repository
git remote remove origin
Unlinks your repository from a remote source, useful when switching remotes.
17. View the Last Commit Details
git show HEAD
Shows detailed information about the most recent commit, including the changes made. To see only the commit message
git log -1 --pretty=%B
18. Check Whatβs Staged for Commit
git diff --staged
Displays changes that are staged for commit, helping you review before finalizing a commit.
19. Fetch and Rebase from a Remote Branch
git pull --rebase origin main
Combines fetching and rebasing in one step, keeping your branch up-to-date cleanly. If conflicts arise, resolve them manually and continue with
git rebase --continue
20. View All Git Aliases
git config --global --list | grep alias
If youβve set up aliases, this command helps you see them all. Aliases can make your Git workflow faster by shortening common commands. For example
git config --global alias.co checkout
allows you to use git co
instead of git checkout
.
Try these tricks in your daily development to level up your Git skills!