Git is a system that encourages you to not be afraid of making mistakes. You can even say that it's the only way to learn how to use it. In git, there is almost always a way to restore or change any commits. As a last resort, another git clone
will save the day.
What if a commit has already been made, but we don't like it for some reason? There can be many potential situations, and they all arise regularly, even for professional developers:
- Maybe you forgot to add some necessary files to the commit
- Changes need to be rolled back in order to be refined
- The changes are no longer required and need to be deleted
- The changes were made by mistake and need to be reverted
Git is, for the most part, a "forward only" system. The right way to work with git is to create a new one, not change an old one. All of the situations described above can be solved by a new commit with required code changes. It's both safe and convenient. Changing the commit history is a dangerous operation that may cause synchronization problems with remote repositories. We'll talk about that later.
Despite what we said above, there are special commands within git to make it easy to undo or change a commit. With their help, you can make the history of commits clear and make rolling back fast.
Git Revert
The easiest thing to do is to undo the changes. In fact, it boils down to creating another commit that performs the opposite changes to the commit being canceled. It's hard to create this kind of commit by hand, so they added a command to git to automate the rollback. This command is called git revert
:
# This command needs a commit identifier
# This is the commit that deleted the PEOPLE.md file
git revert aa600a43cb164408e4ad87d216bc679d097f1a6c
# After this command, the editor will open, waiting to enter the commit description
# Usually the revert message doesn't get changed, so just close the editor
[main 65a8ef7] Revert "remove PEOPLE.md"
1 file changed, 1 insertion(+)
create mode 100644 PEOPLE.md
# the PEOPLE.md file returns to the project
git log -p
commit 65a8ef7fd56c7356dcee35c2d05b4400f4467ca8
Author: tirion <tirion@got.com>
Date: Sat Sep 26 15:32:46 2020 -0400
Revert "remove PEOPLE.md"
This reverts commit aa600a43cb164408e4ad87d216bc679d097f1a6c.
diff --git a/PEOPLE.md b/PEOPLE.md
new file mode 100644
index 0000000..4b34ba8
--- /dev/null
+++ b/PEOPLE.md
@@ -0,0 +1 @@
+Haskell Curry
The revert command can "undo" not only the last commit, but also any other commit from the project history. You have to agree, it's very cool. Without the version control system, you couldn't even dream of something like this.
Git Reset
Sometimes you need to delete a commit you only just made by mistake. Of course, in this case, git revert
is also useful, but it doesn't do much good to the system if you do that. If the commit was just made now and has not yet been submitted to GitHub, it's better to make it look as if the commit didn't exist at all.
Git allows you to delete commits. This is a dangerous operation that you should only do if it's a new commit that nobody else has except you.
If a commit has been sent to an external repository, such as GitHub, you should never change the history, it will break work done by other people working on your project.
To delete a commit, use the git reset
command. This is how it's done:
# adding a new commit, which we will immediately delete
echo 'test' >> INFO.md
git add INFO.md
git commit -m 'update INFO.md'
[main 17a77cb] update INFO.md
1 file changed, 1 insertion(+)
# It is important not to do a git push
git reset --hard HEAD~
HEAD is now at 65a8ef7 Revert "remove PEOPLE.md"
# If you look at the git log, the last commit is no longer there
git reset
is a powerful command with many different flags and ways of working. It can be used to delete or undo (without deleting) commits, restore files from the history, and so on. This is an advanced git tool, but here we're only talking about the basics.
The --hard
flag means complete deletion. Without it, git reset
will undo a commit, but it will not delete it, it will put all the changes to that commit into the working directory so you can continue working with it. HEAD~
means "one commit from the last commit". If we wanted to delete the last two commits, we could write HEAD~2
.
HEAD means the last commit made. We'll cover this terminology in more detail in the lesson on the inner workings of git.
If the --hard
flag is not specified, the --mixed
flag is implied by default. In this case, reset sends the changes of the last commit to the working directory. They can then be corrected or undone and a new commit made.
echo 'no code no pain' > README.md
git add README.md
git commit -m 'update README.md'
[main f85e3a6] update README.md
1 file changed, 1 insertion(+)
# Now roll back the last commit
git reset HEAD~
Unstaged changes after reset:
M README.md
git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
The last commit no longer exists, but the changes made to it are still there. They are in the working directory for further modification.
Do it yourself
- Follow all of the steps in the lesson
- Change the added text to No code No pain and commit it with a message reading "update README.md"
- Upload the changes to GitHub