17. Removing a commit from a branch
Goals
- To learn to delete the branch's latest commits
Revert
is a powerful command of the previous section that allows you to cancel any commits to the repository. However, both original and cancelled commits are seen in the history of the branch (when using git log
command).
Often after a commit is already made, we realize it was a mistake. It would be nice to have an undo command which allows the incorrect commit(s) to be immediately deleted. This command would prevent the appearance of one or more unwanted commits in the git log
history.
01 The reset
command
We have already used the reset
command to match the buffer zone and the selected commit (HEAD commit was used in the previous lesson).
When a commit reference is given (ie, a branch, hash, or tag name), the reset
command will...
- Overwrite the current branch so it will point to the correct commit
- Optionally reset the buffer zone so it will comply with the specified commit
- Optionally reset the working directory so it will match the specified commit
02 Check our history
Let us do a quick scan of our commit history.
Run:
git hist
Result:
$ git hist * 45fa96b 2011-03-09 | Revert "Oops, we didn't want this commit" (HEAD, master) [Alexander Shvets] * 846b90c 2011-03-09 | Oops, we didn't want this commit [Alexander Shvets] * fa3c141 2011-03-09 | Added HTML header (v1) [Alexander Shvets] * 8c32287 2011-03-09 | Added standard HTML page tags (v1-beta) [Alexander Shvets] * 43628f7 2011-03-09 | Added h1 tag [Alexander Shvets] * 911e8c9 2011-03-09 | First Commit [Alexander Shvets]
We see the last two commits in this branch are "Oops" and "Revert Oops". Let us remove them with the reset
command.
03 Mark this branch first
Let us mark the last commit with tag
, so you can find it after removing a commit(s).
Run:
git tag oops
04 Reset commit to previous Oops
In the history log above, the commit tagged "v1" is before the "Oops" and "Revert Oops" commits. Let us reset the branch to that point. As the branch has a tag, we can use the tag name in the reset command (if it does not have a tag, we can use the hash value).
Run:
git reset --hard v1 git hist
Result:
$ git reset --hard v1 HEAD is now at fa3c141 Added HTML header $ git hist * fa3c141 2011-03-09 | Added HTML header (HEAD, v1, master) [Alexander Shvets] * 8c32287 2011-03-09 | Added standard HTML page tags (v1-beta) [Alexander Shvets] * 43628f7 2011-03-09 | Added h1 tag [Alexander Shvets] * 911e8c9 2011-03-09 | First Commit [Alexander Shvets]
Our master branch is pointing at commit v1 and the "Revert Oops" and "Oops" commits no longer exist in the branch. The --hard
parameter makes the working directory reflect the new branch head.
05 Nothing is ever lost
What happened to the wrong commits? They are still in the repository. Actually, we can still refer to them. At the beginning of the lesson, we created the "oops" tag for the canceled commit. Let us take a look at all commits.
Run:
git hist --all
Result:
$ git hist --all * 45fa96b 2011-03-09 | Revert "Oops, we didn't want this commit" (oops) [Alexander Shvets] * 846b90c 2011-03-09 | Oops, we didn't want this commit [Alexander Shvets] * fa3c141 2011-03-09 | Added HTML header (HEAD, v1, master) [Alexander Shvets] * 8c32287 2011-03-09 | Added standard HTML page tags (v1-beta) [Alexander Shvets] * 43628f7 2011-03-09 | Added h1 tag [Alexander Shvets] * 911e8c9 2011-03-09 | First Commit [Alexander Shvets]
We can see that the wrong commits are not gone. They are not listed in the master branch anymore but still remain in the repository. They would still be in the repository if we did not tag them, but then we could reference them only by their hash names. Unreferenced commits remain in the repository until the garbage collection software is run by system.
06 Reset dangers
Resets on local branches are usually harmless. The consequences of any "accident" can be reverted by using the proper commit.
However, other users sharing the branch can be confused if the branch is shared on remote repositories.