To be able to travel back and forth in time, we need a way to know where we are. Touch page3.txt & echo "Content for page3" > page3.txt git add page3.txt git commit -m "create page3" Checking Git History Touch page2.txt & echo "Content for page2" > page2.txt git add page2.txt git commit -m "create page2" Let's add a few more files with separate commits so we can have more commits to work with. Now, we have a repo set up with one file added and one commit in the git history. Mkdir git-undo & cd git-undo git init touch page1.txt & echo "Let us put something here" > page1.txt git add page.txt git commit -m "create page1" Setting Things Upįrom my terminal, I'll run the following commands to create the repo folder, add some files and create some commits to work with: We'll be doing everything from the terminal, so get ready to do some typing. I advise you do the same so you can easily follow along. To make this post practical and relatable, I'll be creating a new git folder to use as a demo. We'll be taking advantage of this timeline to move back and forth as we please. As you know, Git stores snapshots of a repo at different points and then creates a timeline history. So, next time you think you've made a commit in error, you'll have no reason at all to panic. In this post, we'll explore ways to undo changes in Git. For example, maybe you're working on a new feature that isn't ready yet, and then you need to rollback to a previous commit. There are many other valid reasons you'd want to travel in time between different versions of a project. Lucky us! Undoing things in Git isn't just for mistakes. While life might not always present us with a chance to undo our mistakes, Git provides us ample opportunities to do just that. In life, undoing our mistakes is something we've always wished was possible. Removing the oops tag 14.Time Travel in Your Project: Undo Changes with Git However, other users sharing the branch can be confused if the branch is shared on remote repositories. The consequences of any "accident" can be reverted by using the proper commit. Resets on local branches are usually harmless. Unreferenced commits remain in the repository until the garbage collection software is run by system. They would still be in the repository if we did not tag them, but then we could reference them only by their hashes. They are not listed in the main branch anymore but still remain in the repository. We can see that the wrong commits are not gone. $ git log -all b7614c1 | Added HTML header (HEAD -> main, tag: v1) Ĩ6364a1 | Revert "Oops, we didn't want this commit" (tag: oops) Ĥ6afaff | Added standard HTML page tags (tag: v1-beta) Ħa44bec | Oops, we didn't want this commit ħ8433de | Added h1 tag ĥ836970 | Initial commit Let us do a quick scan of our commit history. Optionally reset the working directory so it will match the specified commit.Optionally reset the staging area so it will comply with the specified commit.Point the current branch to the specified commit.When you run the reset command along with a commit reference ( HEAD, branch or tag name, commit hash, etc.), the command will. We have 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 lesson). This command would prevent the appearance of one or more unwanted commits in the git log history. It would be nice to have an undo command which allows the incorrect commit(s) to be immediately deleted. Often after a commit is already made, we realize it was a mistake. However, both original and cancelled commits are seen in the history of the branch (when using git log command). Revert is a powerful command of the previous section that allows you to cancel any commits to the repository. Learn to delete the branch's latest commits.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |