Github Trying to Upload Already Deleted File
Every developer has deleted the wrong file from their project at least once. Information technology tin can either be a hastily executed`rm -rf`
command, or an absent-minded select and delete, or maybe the result of an erroneous script. Whatever the reason, deleting an important file can be troublesome if not fixed immediately. When working with a team, accidentally deleting a file and and so pushing it upstream can be catastrophic for other squad members who pull the changes. Depending on the file, either they'll get an error straight away, or in the worst instance, the fault will pop up somewhere down the line—maybe in some non-so-obvious identify—at which betoken, information technology might be difficult to effigy out the exact cause.
So, now that you have accidentally deleted a file, or files, how practise you recover them? Since Git is a version command system, it has features to roll back a single file to a previous version, including deleted files.
In this tutorial, nosotros'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-calibration fill-in solution with BackHub.
Y'all can follow along with this tutorial by cloning the demo repository.
Recovering Deleted Files with the Control Line
Recovering a deleted file using the Git command line involves the `git restore
` or `git checkout
`command. Whenever y'all alter files in Git—including creating new files, editing, or deleting existing files—the changes outset as unstaged. Then you stage the changes with the `git add`
command, and finally, you commit the changes using the `git commit
` command. Git provides means to recover a deleted file at any point in this life cycle of changes.
If yous have not staged the deletion yet, just run`git restore <filename>`
and the file will exist restored from the index.

If yous have staged the changes, yet, running `git restore`
will throw an error, since the file does not exist in the index anymore.

In this instance, you demand to run`git restore --staged --worktree <filename>`
. The`--staged`
statement tells`git`
to restore the file in the index from Head, and the`--worktree`
argument tells Git to restore the working tree every bit well.

If you have deleted the file and already committed the changes, y'all need to use the `git checkout`
command to restore the file. Start, y'all need to find out the checksum of the commit that deleted the file, and then check out the file from the previous commit.
In the demo repo,`file1.txt`
has already been deleted and committed. Permit's recover that file. To effigy out which commit deleted`file1.txt`
, you need to utilize the`git rev-list`
command:
```
git rev-list HEAD -n i -- file1.txt
```

This command tells `git
` to list all commits, which can exist reached from the Caput, that changed the file`file1.txt
`. The`-n 1`
option tells`git`
to limit the result to only one commit. The output is the checksum of the commit that deleted the file. You can bank check that this is the offending commit past using the `git show
` control with the checksum –

The commit before this one is the last commit where this file was nowadays. You lot tin can restore the file from that item commit by running the following command. The`^`
at the end of the commit hash tells Git to fetch the commit before this ane:
"`
git checkout 3d5210ddd8632d539ed3f5e362dc047ed508a510^ file1.txt
"`

Pros and Cons of Using the Control Line
This method is the quickest to perform since you lot only need admission to the command line. However, it requires you to run unlike commands depending on your situation. Too, it might not be the easiest to master, and some developers may prefer a more visual arroyo.
Using the GitHub Desktop App
If y'all are more comfortable with a graphical interface, you tin can use the GitHub Desktop, which is available for macOS and Windows. Like to the previous case, there are two scenarios: one where you accept not committed the deletion, and one where you accept.
Whatsoever changes you make in your repository volition show up in the staging area in the left sidebar of the app. There you can discard the changes, which works similar to the`git restore`
command. If you take not all the same committed the deletion, you tin can employ this feature to rapidly recover the deleted file.
Go ahead and delete`file5.txt`
from the repository and come up dorsum to GitHub Desktop. You should run into the deletion in the staging area.

You tin correct-click on the change and click onDiscard changes.

Yous volition be asked for confirmation. Once confirmed, the change will be discarded and the deleted file volition be back in its identify.

If you take already committed the change, y'all demand to know the commit hash of the offending commit. There is no way to do that from the GitHub desktop app, and then yous need to use the control line and run the`git rev-list`
command nosotros discussed earlier.
Just like earlier, let's restore the already deleted`file1.txt`
. First, yous demand to know the hash of the commit that deleted the file:
```
$ git rev-list HEAD -northward i -- file1.txt
3d5210ddd8632d539ed3f5e362dc047ed508a510
```
In the app, the commits are listed with their names, not hashes. In order to know the proper noun of the commit, yous need to run`git bear witness`
command with the commit hash:
```
git show 3d5210ddd8632d539ed3f5e362dc047ed508a510
```

The name of the commit is "Add file4." Next, locate this commit in theHistory tab in the app.

Right-click on the commit and selectRevert changes in commit.

This will revert the offending commit and create a new commit.

Pros and Cons of Using GitHub Desktop App
This method is insufficiently easier than using the command line and a better selection if yous're comfy with graphical interfaces. Even so, it has the post-obit disadvantages:
- The desktop app is just available for Windows and macOS. If you're using Linux so you won't exist able to use this method.
- If yous have already committed the deletion, this method becomes cumbersome since you need to apply the command line to detect the commit proper noun and so search through the history in the app to locate the commit.
- It is not possible to check out only the required file from the commit using this method. You demand to revert the entire commit, which ways any other changes made past the commit will exist reverted as well.
Using the GitHub Web UI
If you take committed the deletion and pushed it to GitHub, information technology is possible to recover a deleted file using the GitHub Web UI. GitHub lets y'all browse the commit history and explore the project at any point in history, which then allows you lot to view and download whatever file.
Let'due south recover the already deleted`file1.txt`
in the repository. Similar to the previous arroyo, you need to know which commit deleted the file, using the`git rev-list`
command explained before. In our instance, the commit hash is `3d5210ddd8632d539ed3f5e362dc047ed508a510`
.
Open up a browser and visit the URL –`https://github.com/<username>/<repo-name>/commits/3d5210ddd8632d539ed3f5e362dc047ed508a510^`
. Make sure to supersede`<username>`
and `<repo-name>
` to point to your repository. This will open upward the commit earlier the offending commit.

Click onBrowse files and you volition exist presented with the project structure of that particular commit.

Notice the file you desire to restore. In this case`file1.txt`
. Open up it by clicking on it.

Click on theRaw push and yous will exist presented with a raw text version of the file. Yous can right-click on the page and selectRelieve Equally to download the file and relieve information technology to your project. At present you can add together it dorsum to your local repo and commit –
```beat
git add file1.txt
git commit -thousand "Reintroduce file1.txt"
```
Pros and Cons of Using GitHub Web UI
Similar to using the app, this method is easier to use than the CLI method considering of its graphical interface. You lot can also visually scan the repository at any point of your commit history without having to clone or checkout the commit.
The biggest disadvantage of this approach, however, is that there is no way of selecting and downloading more than ane file. So if y'all take accidentally deleted more than ane file, you have to download them 1 past one—a fourth dimension-consuming task. Additionally, this method somewhat relies on the command line since y'all need the control line to figure out the commit hash.
Using a Full Backup
Recovering a deleted file with Git is a comparatively complex process. Not merely practice you lot take to dabble with the command line to endeavor and effigy out the commit hashes, but you lot also have to make certain that the commit you're restoring the file from is indeed the correct i. If you have accidentally deleted more than i file, restoring them from different commits tin can introduce inconsistency into your project. Using a full backup solution like BackHub tin can salve you lot some problem by backing upward the entire repository, thus ensuring a consequent land.
BackHub offers powerful features like nightly snapshots, Amazon S3 sync, bankroll up repository metadata (issues, pull requests, wiki, etc.), and the ability to clone whatsoever snapshot direct from the BackHub server. Using their full fill-in solution will ensure y'all do not take to dabble with Git when you accidentally delete i or more files. Instead, yous tin can only restore from a nightly fill-in. Even better, you can restore a deleted repository in merely a few clicks if you happen to accidentally delete i.
Permit'southward learn the procedure of installing BackHub and restoring files from the fill-in.
Installing BackHub
BackHub plans outset from $12 per month. You lot can sign up for a free trial from the installation page. After signing up, you'll be directed to an install screen that sets up permissions with your GitHub business relationship. Here you can select which repositories you want to back upward using BackHub. You tin can either chooseAll repositories to back up all of your projects or individually select as many as you similar. If you cullAll repositories, any new repository y'all create on GitHub will automatically be backed up.
From there, you need to sign in to GitHub and authorize BackHub to finish the installation. Once y'all return to your BackHub dashboard, you should meet a listing of backed-upwardly repositories.

From this point onward, BackHub will go on nightly backups of the repositories. The snapshots are stored for thirty days, but upwards to 1 year (365 days) of storage is available for enterprise plans. You can also connect an S3 saucepan in the settings if you'd like to preserve the snapshots indefinitely.
Restoring a Full Backup
Let's go over the steps of restoring a deleted file with BackHub.
Beginning, let'south delete a file and push.
```
rm file5.txt
git commit -am "Delete file5.txt"
git push origin main
```
In the BackHub dashboard, select the repository you'd like to restore.

On the left-manus side, you can see the time of the latest backup and the number of snapshots BackHub has created. If this is a recently added repository, you will have only i snapshot of the electric current version. If the repository was added some time ago, you will take daily snapshots and you can cull the snapshot of whatsoever day to restore.

In one case the snapshot is chosen, click on theDownload Files button. The download volition contain a Cypher with all the files from the head of the main co-operative. From at that place, you lot can easily re-create over the deleted file `file5.txt`
to your local repository and welcome it back with a commit:
```
git add file5.txt
git commit -m "Reintroduce file5.txt"
```
Conclusion
Accidentally deleting important files is every developer'southward nightmare. With Git not having an intuitiveundo command, it tin can be hard to efficiently restore a deleted file.
With a full backup solution ready, however, you can rest assured you're able to gainsay whatever issues that might occur because of deleted files. You'll likewise be able to restore deleted files without upshot. Having a full backup solution can show to be useful in the long run, specially in critical business software or team-based environments. Check out BackHub for i of the best consummate fill-in and restore solutions. See it in activity yourself: start your gratuitous trial of BackHub (now part of Rewind).
Source: https://rewind.com/blog/recovering-deleted-files-in-github/
Posting Komentar untuk "Github Trying to Upload Already Deleted File"