The “git reset” command can be seen as the opposite of the “git add” command, essentially adding files to the Git index. If you are not familiar with this notation, “HEAD~1” means that you want to reset the HEAD (the last commit) to one commit before in the log history. The last commit will be removed from your Git history. You have to specify the commit to undo which is “HEAD~1” in this case. However, if the branch is shared on remote repositories, resetting can confuse other users sharing the branch.The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. Any “accidents” can usually be recovered from by just resetting again with the desired commit. Resets on local branches are generally safe. Commits that are unreferenced remain in the repository until the system runs the garbage collection software. If we hadn’t tagged them, they would still be in the repository, but there would be no way to reference them other than using their hash names. It’s just that they are no longer listed in the master branch. Here we see that the bad commits haven’t disappeared. * b083abb | Revert "Oops, we didn't want this commit" (tag: oops) Execute: git hist -all Output: $ git hist -all Remember that at the beginning of this lab we tagged the reverting commit with the tag “oops”. Nothing is Ever Lostīut what happened to the bad commits? It turns out that the commits are still in the repository. The -hard parameter indicates that the working directory should be updated to be consistent with the new branch head. Our master branch now points to the v1 commit and the Oops commit and the Revert Oops commit are no longer in the branch. * 4254c94 | Added a comment (HEAD -> master, tag: v1) Since that branch is tagged, we can use the tag name in the reset command (if it wasn’t tagged, we could just use the hash value). Looking at the log history (above), we see that the commit tagged ‘v1’ is the commit right before the bad commit. Execute: git tag oops Reset to Before Oops First, Mark this Branchīut before we remove the commits, let’s mark the latest commit with a tag so we can find it again. We see that we have an “Oops” commit and a “Revert Oops” commit as the last two commits made in this branch. * c8b3af1 | Added a default value (tag: v1-beta) * 7a4110f | Oops, we didn't want this commit * b083abb | Revert "Oops, we didn't want this commit" (HEAD -> master) Let’s do a quick check of our commit history. Optionally reset the working directory to match the specified commit.Optionally reset the staging area to match the specified commit.Rewrite the current branch to point to the specified commit.a hash, branch or tag name), the reset command will … We’ve already seen the reset command and have used it to set the staging area to be consistent with a given commit (we used the HEAD commit in our previous lab). It would be as if the bad commit never happened. The “take back” command would even prevent the bad commit from showing up the git log history. It would be nice to have a “take back” command that would allow us to pretend that the incorrect commit never happened. Often we make a commit and immediately realize that it was a mistake. However, both the original commit and the “undoing” commit are visible in the branch history (using the git log command). The revert command of the previous section is a powerful command that lets us undo the effects of any commit in the repository. Learn how to remove the most recent commits from a branch.Lab 17 Removing Commits from a Branch Goals Git Internals: Working directly with Git Objects.
0 Comments
Leave a Reply. |