https://learngitbranching.js.org/

Stashing Changes

When to use: You need to switch branches but have uncommitted changes

*# Stash specific file*
git stash push <file_name>

*# Stash all changes*
git stash

*# Stash with a message*
git stash push -m "work in progress on login feature"

*# Pull latest changes*
git pull

*# Apply and remove latest stash*
git stash pop

*# Apply stash but keep it in stash list*
git stash apply

*# List all stashes*
git stash list

*# Apply specific stash*
git stash apply stash@{2}

*# Delete specific stash*
git stash drop stash@{0}

*# Clear all stashes*
git stash clear

Most common way:

git stash push .
git pull
git stash pop

Branching Scenarios

Scenario: Student created a branch → asked tutor to review, but student needs to work further on another feature that requires current implementation

Solution: Create a branch from current branch. When current branch is merged and deleted, new branch will stay consistent and can be merged to main afterwards

*# While on feature-1 branch*
git checkout -b feature-2
# -b flags creates new branch and checkouts to it

*# Now feature-2 is based on feature-1
# When feature-1 gets merged and deleted, feature-2 remains intact*
# List all branches
git branch

# List all branches with remote
git branch -a

# Delete local branch (safe)
git branch -d <branch-name>

# Force delete local branch
git branch -D <branch-name>

# Delete remote branch
git push origin --delete <branch-name>

# Rename current branch
git branch -m <new-name>

# Show which branches are merged
git branch --merged

# Show which branches are NOT merged
git branch --no-merged

Undoing Commits

*# Undo last commit, KEEP changes in working directory (staged)*
git reset --soft HEAD~1

*# Undo last commit, KEEP changes in working directory (unstaged)*
git reset HEAD~1
*# or*
git reset --mixed HEAD~1

*# Undo last commit, DELETE all changes permanently ⚠️*
git reset --hard HEAD~1

*# Undo last 3 commits*
git reset --soft HEAD~3

Key difference:

Essential Flags Explained