Git Tutorial
Removing a commit from a branch in git
Let's 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.
Step1: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
Step2:Check our history
Let us do a quick scan of our commit history.
RUN:
git hist
RESULT:
$ git hist
* 45fa96b 2020-01-09 | Revert "Oops, we didn't want this commit" (HEAD, master) [kwikl3arn]
* 846b90c 2020-01-09 | Oops, we didn't want this commit [kwikl3arn]
* fa3c141 2020-01-09 | Added HTML header (v1) [kwikl3arn]
* 8c32287 2020-01-09 | Added standard HTML page tags (v1-beta) [kwikl3arn]
* 43628f7 2020-01-09 | Added h1 tag [kwikl3arn]
* 911e8c9 2020-01-09 | First Commit [kwikl3arn]
We see the last two commits in this branch are "Oops" and "Revert Oops". Let us remove them with the reset
command.
Step3: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
Step4:
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 2020-01-09 | Added HTML header (HEAD, v1, master) [kwikl3arn]
* 8c32287 2020-01-09 | Added standard HTML page tags (v1-beta) [kwikl3arn]
* 43628f7 2020-01-09 | Added h1 tag [kwikl3arn]
* 911e8c9 2020-01-09 | First Commit [kwikl3arn]
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.
Step5:
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 2020-01-09 | Revert "Oops, we didn't want this commit" (oops) [kwikl3arn]
* 846b90c 2020-01-09 | Oops, we didn't want this commit [kwikl3arn]
* fa3c141 2020-01-09 | Added HTML header (HEAD, v1, master) [kwikl3arn]
* 8c32287 2020-01-09 | Added standard HTML page tags (v1-beta) [kwikl3arn]
* 43628f7 2020-01-09 | Added h1 tag [kwikl3arn]
* 911e8c9 2020-01-09 | First Commit [kwikl3arn]
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.
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.