![]() ![]() First, you need to know the hash of the commit that deleted the file: Just like before, let’s restore the already deleted `file1.txt`. There is no way to do that from the GitHub desktop app, so you need to use the command line and run the `git rev-list` command we discussed earlier. If you have already committed the change, you need to know the commit hash of the offending commit. Once confirmed, the change will be discarded and the deleted file will be back in its place. You can right-click on the change and click on Discard changes. You should see the deletion in the staging area. ![]() Go ahead and delete `file5.txt` from the repository and come back to GitHub Desktop. If you have not yet committed the deletion, you can use this feature to quickly recover the deleted file. There you can discard the changes, which works similar to the `git restore` command. Similar to the previous case, there are two scenarios: one where you have not committed the deletion, and one where you have.Īny changes you make in your repository will show up in the staging area in the left sidebar of the app. If you are more comfortable with a graphical interface, you can use the GitHub Desktop, which is available for macOS and Windows. Also, it might not be the easiest to master, and some developers may prefer a more visual approach. However, it requires you to run different commands depending on your situation. This method is the quickest to perform since you only need access to the command line. To figure out which commit deleted `file1.txt`, you need to use the `git rev-list` command: In the demo repo, `file1.txt` has already been deleted and committed. First, you need to find out the checksum of the commit that deleted the file, and then check out the file from the previous commit. If you have deleted the file and already committed the changes, you need to use the ` git checkout` command to restore the file. The `-staged` argument tells `git` to restore the file in the index from HEAD, and the `-worktree` argument tells Git to restore the working tree as well. ![]() In this case, you need to run `git restore -staged -worktree `. If you have staged the changes, however, running ` git restore` will throw an error, since the file does not exist in the index anymore. If you have not staged the deletion yet, simply run `git restore ` and the file will be restored from the index. Git provides ways to recover a deleted file at any point in this life cycle of changes. Then you stage the changes with the ` git add` command, and finally, you commit the changes using the ` git commit` command. Whenever you modify files in Git-including creating new files, editing, or deleting existing files-the changes start as unstaged. Recovering a deleted file using the Git command line involves the ` git restore` or ` git checkout`command. Recovering Deleted Files with the Command Line You can follow along with this tutorial by cloning the demo repository. In this tutorial, we’ll look at three ways to recover a deleted file: using the Git command line, using GitHub’s web and app UI, and using a full-scale backup solution with BackHub. So, now that you have accidentally deleted a file, or files, how do you recover them? Since Git is a version control system, it has features to roll back a single file to a previous version, including deleted files. Depending on the file, either they’ll get an error straight away, or in the worst case, the error will pop up somewhere down the line-maybe in some not-so-obvious place-at which point, it might be difficult to figure out the exact cause. When working with a team, accidentally deleting a file and then pushing it upstream can be catastrophic for other team members who pull the changes. Whatever the reason, deleting an important file can be troublesome if not fixed immediately. It can either be a hastily executed `rm -rf` command, or an absent-minded select and delete, or maybe the result of an erroneous script. Recovering Deleted Files with the Command LineĮvery developer has deleted the wrong file from their project at least once. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |