A revert undoes changes made in a particular commit, without touching other commits that may have come later. A checkout restores a file to a previous version, removing all commits after that point. That question (despite the title) pertains to git-checkout.This is not a duplicate of Reverting a single file to a previous version in git. My working theory is that it is not possible to revert only some files, and that the Git error message is misleading. Save the state of your current branch in another branch, named my-backup ,in case something goes wrong: git commit -a -m Backup. Still, you have to remember that even the official Git documentation (can be. Another exciting command that we can use is git restore. We must also remember that the git checkout command is not the only built-in tool in Git that will allow us to restore changes, files, etc. Use '-' to separate paths from revisionsīut that is precisely what I did! (And yes, my/path/to/revert is in my working tree.) Listing 1.Finding and restoring files with the git checkout command. I tried this git revert -no-commit abcdef123456 - my/path/to/revertĪnd I got this error fatal: ambiguous argument 'my/path/to/revert': unknown revision or path not in the working tree. To understand how git revert latest commit functions, we will add some files n-file.css and newfile.js in our local repository bug-fix branch. Different git revert methods Method-1: Perform git revert to the latest commit. We can use the ls command to verify that the file does not exist: ls. We have a local repository git-sample set up in git version 2.32.0.windows.2 and ready to use for practising different git revert methods as follows. With the repo in this state, we are ready to initiate a git revert. At the end of the repo setup procedure, we invoke git log to display the commit history, showing a total of 3 commits. This file contained some standard configuration settings for our project. Here we have initialized a repo in a newly created directory named gitreverttest.We have made 3 commits to the repo in which we have added a file demofile and modified its content twice. To unstage a file use: git restore -staged path/to/file. To undo a change to the repository: git restore -source commit id path/to/file. For the simple cases we’ve discussed here, you can use the following equivalents. Our repository once stored a file called config.py which we now want to retrieve. Later versions of Git have a newer command: restore. Revert file to master If you want to revert the file to its state in master bash git checkout origin/master thefile.
If you are unfamiliar with the difference, keep reading.) We’re working on a repository called ck-git. I want to do a revert of a commit, but only for some files.