Advanced Git Commands
Let’s explore more powerful commands that help with advanced workflows and recovery operations.
📦 Temporarily Save Changes: git stash
Use when you want to switch branches but aren’t ready to commit changes.
git stashReapply stashed changes:
git stash applyList stashes:
git stash listDrop a stash:
git stash drop stash@{0}🔍 Recover Lost Commits: git reflog
Shows a log of where HEAD and branches have moved — useful for recovery!
git reflogYou can reset to a previous state using the hash:
git reset --hard <reflog_hash>🧹 Clean Untracked Files: git clean
⚠️ Be careful! Deletes untracked files.
Dry run (see what will be deleted):
git clean -nActually delete:
git clean -f📥 Fetch & Pull from Remote
Download new commits and branches without merging:
git fetchFetch + merge = pull:
git pullUse
git pull --rebaseto rebase instead of merge.
🚀 Push to Remote Repository
Push current branch to origin:
git pushPush to a specific branch:
git push origin branch-name🏷️ Git Tags (Mark a Commit)
Tag a commit (e.g. for releases):
git tag v1.0Push tags to remote:
git push origin --tagsList all tags:
git tag🍒 Cherry-Pick Commits
Copy a single commit from one branch to another:
git cherry-pick <commit_hash>Use this if you want to bring in one feature without merging the whole branch.
📂 Rename a File
git mv old_name.txt new_name.txtThen commit:
git commit -m "Renamed file"🔍 Blame for a Line of Code
See who last modified each line of a file:
git blame filename.py🧪 Try It Yourself
Problem: Stash Changes and Switch Branches
Show Code
git stash
git checkout other-branch
# work here
git checkout main
git stash applyProblem: Clean Untracked Files
Show Code
git clean -n # See what will be deleted
git clean -f # Actually delete