git reset --soft HEAD~1
Removes the last commit but leaves all its changes staged in your working tree, so you can recommit them.
Safer approach: If the commit was already pushed, prefer git revert <hash> which adds a new undo commit instead of rewriting history.
git reset --hard HEAD~1
Removes the last commit AND permanently deletes its changes from your working tree.
Destructive: this can permanently delete work. Double-check before running.
Safer approach: Back up first with git stash or git branch backup-branch. If already pushed, use git revert <hash> instead.
git commit --amend -m "new message"
Replaces the most recent commit with a new one that has the updated message (and any newly staged changes).
Safer approach: Only amend commits you have not pushed. If pushed, others may have the old commit — add a new commit instead.
git rebase -i HEAD~N
Opens an interactive rebase where you mark commits as 'squash' or 'fixup' to combine them into one. Replace N with how many commits back to include.
Safer approach: Squash only local, unpushed commits. For a shared branch, coordinate with your team or squash at merge time via a pull request.
git branch -m old-name new-name git push origin -u new-name git push origin --delete old-name
Renames the branch locally, pushes the new name and sets tracking, then deletes the old branch name on the remote.
Safer approach: Tell collaborators to run git fetch --prune and re-checkout, since the old remote branch is removed.
git branch -d branch-name git branch -D branch-name git push origin --delete branch-name
Use -d to delete a fully merged local branch, -D to force-delete an unmerged one, and the push command to delete it on the remote.
Destructive: this can permanently delete work. Double-check before running.
Safer approach: Prefer -d over -D so git refuses to delete unmerged work. Recover a deleted branch via git reflog if needed.
git stash git stash pop
git stash saves your uncommitted changes and cleans the working tree; git stash pop re-applies the most recent stash and removes it from the stash list.
Safer approach: Use git stash list to inspect stashes and git stash apply (instead of pop) to keep the stash entry as a backup.
git revert <hash>
Creates a new commit that undoes the changes from the given commit, without altering existing history — safe for shared branches.
Safer approach: This is already the safe way to undo public commits. Avoid git reset on pushed branches.
git reflog git checkout -b branch-name <hash>
git reflog lists recent HEAD positions; find the commit hash the deleted branch pointed to, then recreate the branch at that hash.
Safer approach: Act soon — reflog entries expire (90 days by default) and may be garbage-collected.
git remote set-url origin <new-url>
Points the 'origin' remote at a new URL, for example when switching from HTTPS to SSH or moving to a new host.
Safer approach: Run git remote -v before and after to confirm the change. No history is affected.
git restore --staged <file>
Removes a file from the staging area while keeping its changes in the working tree. (Older git: git reset HEAD <file>.)
Safer approach: This does not touch your file contents, so it is fully safe to run.
git restore <file> git checkout -- <file>
Reverts a file in your working tree back to the last committed version. Both commands do the same thing (restore is the newer form).
Destructive: this can permanently delete work. Double-check before running.
Safer approach: Stash first with git stash if you might want the changes back — discarding is irreversible.
git cherry-pick <hash>
Applies the changes from a specific commit onto your current branch as a new commit.
Safer approach: If conflicts occur, resolve them then run git cherry-pick --continue, or git cherry-pick --abort to cancel.
# 1. Edit the conflicted files to resolve <<<<<<< markers git add <file> git commit git merge --continue
Manually edit files containing conflict markers, stage the resolved files with git add, then finish the merge with a commit (or git merge --continue).
Safer approach: Run git merge --abort at any point to cancel the merge and return to the pre-merge state.
git remote add upstream <upstream-url> git fetch upstream git merge upstream/main
Adds the original repository as an 'upstream' remote, fetches its latest commits, and merges them into your current branch.
Safer approach: You only need to add the upstream remote once. Use git merge (not rebase) to avoid rewriting shared history.
Reference commands based on the official Git documentation. Always review destructive commands before running them.