Home / Companies / GitHub / Blog / Post Details
Content Deep Dive

How to undo (almost) anything with Git

Blog post from GitHub

Post Details
Company
Date Published
Author
Joshua Wehner
Word Count
2,026
Language
English
Hacker News Points
-
Summary

Git, a powerful version control system, provides various methods to "undo" changes, catering to different scenarios that users might encounter. For reversing a public commit already pushed to a repository, one can use the `git revert` command, which creates a new commit to negate the changes of a previous one without altering the commit history. If a commit message needs correction before pushing, `git commit --amend` allows users to modify the most recent commit, including its message. Local uncommitted changes can be reverted using `git checkout -- <filename>`, but this permanently discards the changes. For undoing recent local commits, `git reset` can rewind the repository's history, with the `--hard` option also discarding changes in the working directory. If users wish to recover changes after a hard reset, `git reflog` helps navigate the project's history to restore commits. Branch management and rebase functions allow users to reorganize commit history effectively, such as moving commits to a new branch or syncing with an updated base branch. Interactive rebasing (`git rebase -i`) offers granular control over the commit history, enabling users to squash, reorder, or omit commits. Lastly, to stop tracking a file mistakenly added to the repository, `git rm --cached` removes it from version control while keeping the local file intact.