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. Note: git checkout also has a patch mode that does exactly the same as git restore’s patch mode, but the key here is that using the git restore command to restore a file to its previously committed state is both more memorable and semantically correct.Git is a widely used Version Control System (VCS) known for its versatility. Pretty overwhelming… The best place to start is with y (‘yes, discard this hunk’) and n (‘no, don’t discard this hunk’), leaving things like splitting hunks for another day, once you’re more familiar with it all. S - split the current hunk into smaller hunkse - manually edit the current hunk K - leave this hunk undecided, see previous hunk K - leave this hunk undecided, see previous undecided hunk J - leave this hunk undecided, see next hunk J - leave this hunk undecided, see next undecided hunk search for a hunk matching the given regex Q - quit do not discard this hunk or any of the remaining onesĪ - discard this hunk and all later hunks in the fileĭ - do not discard this hunk or any of the later hunks in the file ![]() N - do not discard this hunk from worktree In case you’re not sure what any of those mean (a situation I find myself in all the time!), typing ? and hitting enter gives you a nice reference: y - discard this hunk from worktree Once we’re in patch mode, we just need to work our way through the changes by typing a letter from the multitude of options: Discard this hunk from worktree ? The following command would enter patch mode for every file we’ve edited since our last commit: git restore -pĪlternatively, we can restore parts of a specific file with: git restore -p path/to/file.html Just like git add’s patch mode, we don’t have to use the full -patch flag we get a handy -p shortcut. I really like git restore, and one of its superpowers is its patch mode, where we can restore parts (or ‘hunks’) of a file, rather than the whole file at once.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |