git commit -am "message"
(Add all tracked files and commit)
git commit --amend --no-edit
(Amend the last commit with new changes, keeping
the same message)
git push --force-with-lease
(A safer force push; it won't overwrite work if the
remote has new commits)
git switch -c <new-branch-name>
(The modern, clearer way to create and switch to a
new branch)
git pull --rebase
(Fetch and apply remote changes *before* your local
commits, avoiding merge commits)
git rebase -i HEAD~3
(Interactively review and edit the last 3 commits:
squash, reword, drop, etc.)
git cherry-pick <commit-hash>
(Apply the changes from a single, specific commit
onto your current branch)
git reflog
(Shows a log of all actions; your safety net to
find "lost" commits or branches)
git reset --hard
(DANGER! Discard all uncommitted
changes in the Working Directory and Staging Area.
Reverts to the last commit.)
git clean -fd
(Remove untracked files and directories. Use
-n for a dry-run first!)
Git Stash:
git stash list
# List stash entries
git stash push -m "changes"
# Add changes to stash
git stash apply stash@{0}
# Apply a stash entry
git stash drop stash@{0}
# Delete a stash entry
Git Worktree:
Lets you check out multiple branches at once, each
in its own directory, all linked to the same
repository. Perfect for working on a feature while
fixing a bug on main without stashing.
git worktree add ../hotfix main
# Create a new worktree in a folder named
'hotfix'
git worktree list
# Show all linked worktrees
git worktree remove ../hotfix
# Remove the worktree
git worktree prune
# Clean up deleted worktree data from .git
folder