git revert
git revert
: revert changes from a specific commit.
git revert
creates a new commit that inverses the changes made by the specified commit, effectively canceling out its effects without altering the commit history.
The purpose of git revert
is to provide a safe and clean way to undo changes in a repository without rewriting its history. This is particularly useful in collaborative environments, where rewriting history (e.g., using git reset
) can cause confusion and conflicts for other developers working on the same project.
When you use git revert
, Git creates a new commit that contains the inverse of the changes made in the original commit. This new commit is then added to the repository's history, leaving a clear record of both the original changes and their reversal.
git revert
is especially helpful when you need to undo a specific change that was introduced in a previous commit without affecting any of the commits that came after it. This allows you to surgically remove a problematic change while preserving the integrity of your commit history.
Examples
Let's say you have a commit history like this:
a1b2c3d (HEAD -> main) Update README
b2c3d4e Fix typo in main code
c3d4e5f Add new feature
d4e5f6g Initial commit
If you want to revert the Fix typo in main code
commit (b2c3d4e
), you can use git revert
:
git revert b2c3d4e
This will open your default text editor, asking you to enter a commit message for the revert commit. After you save and close the editor, Git will create a new commit that inverses the changes made in b2c3d4e
:
a1b2c3d (HEAD -> main) Revert "Fix typo in main code"
b2c3d4e Update README
c3d4e5f Fix typo in main code
d4e5f6g Add new feature
e5f6g7h Initial commit
Now, the effects of the Fix typo in main code
commit have been undone, but the commit itself is still part of the history, and a new commit has been created to record the reversal of its changes.
Hi! I'm Alex, creator of GitByBit.
And this is Gitopedia—a reference that summarizes knowledge in my Git course.
Gitopedia is designed to be a personal knowledge base. When you progress through the main Git course in VS Code, you unlock all of these topics, one by one, bit by bit. This gives you time to digest the information and gradually build the mental model of Git. Here's an example of how it works:
Learning directly in VS Code lets you practice Git exactly as you would in real life, doing real jobs, writing real code. In addition, the course has access to your actual terminal, so it can point out mistakes, suggest workarounds, etc.
The course is FREE, there are no Ads or other bullshit. There are optional premium add-ons you can purchase, mainly to support my work (regrettably, I have to eat every day), but that's totally up to you.