AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Reset head to commit12/6/2023 Use the -soft option to roll back to a previous commit, while preserving file changes in the working directory and staging area. The reset command comes with three flags that define how the command deals with changed files in the working directory and staging area. This alias stands in for the ID of the previous commit, providing easy access when trying to revert to the last commit. With the reset command, you have access to the HEAD~1 alias. Essentially, reset “rewinds” you to a previous commit, eliminating later commits and history along the way. The command is more radical than revert and works by removing commits entirely from the repository’s commit history. Git’s reset command can also be used to revert the last commit. In this case, that is example-file-2.txt: cat example-file-2.txt You can even check the modified file to see that the changes have been reversed. Use the log command again, and you can see that there is now a new commit to revert the previous commit: git log -onelineĮ86542a (HEAD -> master) Revert "Added text to second file." When you are satisfied with the description, exit the text editor to complete the reversion. It may present you with a text editor allowing you to edit the description for the new commit. Git starts a new commit to revert the changes. The line at the top of the screen, with an ID of f4391b2 in this example, represents the last commit.įrom there, you can revert that commit using the revert command with that commit’s ID, as in: git revert f4391b2 Here, the command is used with the -oneline option to make each commit display on a single line: git log -oneline Thus, to use revert to undo the last commit, you first need the ID for that commit. The command then creates a new commit that reverts the changes. Git’s revert command undoes a commit by comparing the changes made in that commit to the repository’s previous state. You now have a Git repository with a couple of files and several commits, which you can see listed with: git log -onelineį4391b2 (HEAD -> master) Added text to second file. Git commit -m "Added text to second file." Git commit -m "Added text to first file."ĭo the same for the second file: echo "Some example text for the second file." > example-file-2.txt Then stage and commit those changes: echo "Some example text for the first file." > example-file-1.txt Make some changes to the first file, adding some content to it. Use the touch command to create some new empty files: touch example-file-1.txtĪdd the files to the Git staging area, then commit the staged changes: git add. Initialize the new Git repository: git init Here, the new directory, git-example is created in the current user’s home directory: mkdir ~/git-exampleįrom here on, you should execute the given commands while working in this directory. If you have not done this yet, you can learn how in our guide How to Install Git and Clone a GitHub Repository.Ĭreate a new directory for your Git repository, and change into that directory. The steps presume you have already installed Git and done basic configuration (e.g. The commit IDs may be different, but the contents of the repository should otherwise be the same. These next few steps set you up with an example Git repository similar to the one used for the examples in this tutorial. To get the best effect, you can follow along using a similar Git repository. This guide gives example output for the commands it describes. Learn more about Git generally in our guide Git vs SVN: Pros and Cons of Each Version Control System.įor a more general, and thorough, coverage of reverting Git commits, take a look at our guide on How to Undo a Git Commit. It covers methods using both the revert and reset commands, and explains the differences. Git has the ability to revert the last commit, and this tutorial shows you exactly how. You might need that recoverability after an inadvertent commit, or to undo the most recent commit for any reason. It also keeps track of all the committed changes along the way, so things are readily recoverable. Git utilizes local clones of central repositories to bring more effective collaboration. Git is a widely used Version Control System (VCS) known for its versatility.
0 Comments
Read More
Leave a Reply. |