Git Keep Trying to Upload Large File That Was Deleted
Every developer has deleted the incorrect file from their project at least once. It can either be a hastily executed`rm -rf`
control, or an absent-minded select and delete, or maybe the effect of an erroneous script. Any the reason, deleting an important file tin can be troublesome if not stock-still immediately. When working with a squad, 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 direct away, or in the worst instance, the error volition pop upwardly somewhere down the line—maybe in some not-so-obvious identify—at which point, it might be difficult to effigy out the exact cause.
And then, at present that y'all have accidentally deleted a file, or files, how do you lot recover them? Since Git is a version command arrangement, it has features to roll back a single file to a previous version, including deleted files.
In this tutorial, we'll look at 3 means to recover a deleted file: using the Git command line, using GitHub's web and app UI, and using a total-calibration backup solution with BackHub.
Yous can follow along with this tutorial past cloning the demo repository.
Recovering Deleted Files with the Command Line
Recovering a deleted file using the Git command line involves the `git restore
` or `git checkout
`command. Whenever you change files in Git—including creating new files, editing, or deleting existing files—the changes kickoff as unstaged. Then you phase the changes with the `git add`
command, and finally, you commit the changes using the `git commit
` control. Git provides ways to recover a deleted file at whatever bespeak in this life cycle of changes.
If you take not staged the deletion yet, simply run`git restore <filename>`
and the file volition be restored from the alphabetize.
If y'all have staged the changes, however, running `git restore`
volition throw an fault, since the file does not exist in the index anymore.
In this case, you need to run`git restore --staged --worktree <filename>`
. 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.
If you lot accept deleted the file and already committed the changes, y'all need to use the `git checkout`
command to restore the file. Outset, you need to notice out the checksum of the commit that deleted the file, and then bank check out the file from the previous commit.
In the demo repo,`file1.txt`
has already been deleted and committed. Let's recover that file. To figure out which commit deleted`file1.txt`
, you need to employ the`git rev-list`
control:
```
git rev-list HEAD -due north 1 -- file1.txt
```
This command tells `git
` to list all commits, which can be reached from the HEAD, that changed the file`file1.txt
`. The`-north 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 cheque that this is the offending commit by using the `git show
` control with the checksum –
The commit before this one is the last commit where this file was present. You can restore the file from that particular commit by running the following command. The`^`
at the end of the commit hash tells Git to fetch the commit earlier this one:
"`
git checkout 3d5210ddd8632d539ed3f5e362dc047ed508a510^ file1.txt
"`
Pros and Cons of Using the Command Line
This method is the quickest to perform since you lot just need access to the command line. However, it requires you to run unlike commands depending on your state of affairs. As well, it might not exist the easiest to master, and some developers may adopt a more visual arroyo.
Using the GitHub Desktop App
If you are more comfortable with a graphical interface, yous tin use the GitHub Desktop, which is available for macOS and Windows. Similar to the previous example, in that location are two scenarios: one where you have non committed the deletion, and i where you take.
Any changes you lot make in your repository volition prove up in the staging surface area in the left sidebar of the app. In that location you tin discard the changes, which works similar to the`git restore`
command. If you take not yet committed the deletion, you tin can utilize this characteristic to quickly recover the deleted file.
Go ahead and delete`file5.txt`
from the repository and come dorsum to GitHub Desktop. Y'all should see the deletion in the staging surface area.
You tin right-click on the change and click onDiscard changes.
You lot will be asked for confirmation. One time confirmed, the change will be discarded and the deleted file will be back in its place.
If you lot have already committed the modify, yous need to know the commit hash of the offending commit. There is no way to do that from the GitHub desktop app, so you demand to utilise the command line and run the`git rev-list`
control nosotros discussed earlier.
Merely like earlier, let'due south restore the already deleted`file1.txt`
. Start, you demand to know the hash of the commit that deleted the file:
```
$ git rev-list HEAD -n one -- file1.txt
3d5210ddd8632d539ed3f5e362dc047ed508a510
```
In the app, the commits are listed with their names, not hashes. In lodge to know the name of the commit, you need to run`git show`
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 meliorate choice if you're comfortable with graphical interfaces. However, it has the following disadvantages:
- The desktop app is only available for Windows and macOS. If you lot're using Linux then y'all won't be able to utilize this method.
- If y'all have already committed the deletion, this method becomes cumbersome since y'all need to utilise the command line to find the commit name and and then search through the history in the app to locate the commit.
- Information technology is not possible to check out only the required file from the commit using this method. You need to revert the entire commit, which means whatever other changes made past the commit will exist reverted likewise.
Using the GitHub Web UI
If you have committed the deletion and pushed information technology to GitHub, it is possible to recover a deleted file using the GitHub Web UI. GitHub lets you browse the commit history and explore the projection at whatever indicate in history, which so allows you lot to view and download any file.
Let'due south recover the already deleted`file1.txt`
in the repository. Like to the previous arroyo, you demand to know which commit deleted the file, using the`git rev-list`
command explained earlier. In our case, the commit hash is `3d5210ddd8632d539ed3f5e362dc047ed508a510`
.
Open a browser and visit the URL –`https://github.com/<username>/<repo-proper noun>/commits/3d5210ddd8632d539ed3f5e362dc047ed508a510^`
. Make sure to replace`<username>`
and `<repo-name>
` to point to your repository. This volition open up the commit before the offending commit.
Click onBrowse files and you will exist presented with the project structure of that particular commit.
Detect the file you want to restore. In this case`file1.txt`
. Open it past clicking on it.
Click on theRaw button and you will exist presented with a raw text version of the file. You can right-click on the page and selectSave As to download the file and save information technology to your project. Now you tin can add it back to your local repo and commit –
```shell
git add file1.txt
git commit -m "Reintroduce file1.txt"
```
Pros and Cons of Using GitHub Web UI
Similar to using the app, this method is easier to apply than the CLI method because of its graphical interface. You lot can also visually browse the repository at any bespeak 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 one file. So if you accept accidentally deleted more than one file, you lot have to download them ane by one—a time-consuming chore. Additionally, this method somewhat relies on the command line since y'all need the command line to figure out the commit hash.
Using a Full Backup
Recovering a deleted file with Git is a comparatively complex process. Not only practise you lot have to dabble with the control line to effort and figure out the commit hashes, but you besides have to brand sure that the commit you're restoring the file from is indeed the right ane. If yous have accidentally deleted more one file, restoring them from dissimilar commits can innovate inconsistency into your project. Using a full backup solution similar BackHub can save y'all some trouble by backing upward the unabridged repository, thus ensuring a consistent country.
BackHub offers powerful features like nightly snapshots, Amazon S3 sync, bankroll up repository metadata (bug, pull requests, wiki, etc.), and the ability to clone any snapshot directly from the BackHub server. Using their full backup solution volition ensure y'all do not have to fiddle with Git when you accidentally delete one or more files. Instead, you can just restore from a nightly backup. Fifty-fifty better, you can restore a deleted repository in only a few clicks if you happen to accidentally delete one.
Let's learn the process of installing BackHub and restoring files from the backup.
Installing BackHub
BackHub plans outset from $12 per calendar month. You can sign up for a free trial from the installation page. Later signing upwards, yous'll be directed to an install screen that sets upward permissions with your GitHub account. Here you lot can select which repositories yous want to back up using BackHub. You can either chooseAll repositories to back up all of your projects or individually select as many as you lot like. If yous cullAll repositories, any new repository you create on GitHub will automatically be backed up.
From there, you need to sign in to GitHub and qualify BackHub to cease the installation. Once y'all return to your BackHub dashboard, yous should encounter a list of backed-up repositories.
From this point onward, BackHub will keep nightly backups of the repositories. The snapshots are stored for thirty days, but up to one yr (365 days) of storage is bachelor for enterprise plans. You can also connect an S3 bucket in the settings if you lot'd similar to preserve the snapshots indefinitely.
Restoring a Full Backup
Let's become over the steps of restoring a deleted file with BackHub.
First, let's delete a file and push.
```
rm file5.txt
git commit -am "Delete file5.txt"
git button origin master
```
In the BackHub dashboard, select the repository y'all'd like to restore.
On the left-hand side, you tin run across the time of the latest backup and the number of snapshots BackHub has created. If this is a recently added repository, you will take simply i snapshot of the current version. If the repository was added some time agone, you lot will have daily snapshots and you can choose the snapshot of any day to restore.
Once the snapshot is chosen, click on theDownload Files button. The download will contain a Zippo with all the files from the head of the main branch. From there, you can hands re-create over the deleted file `file5.txt`
to your local repository and welcome information technology back with a commit:
```
git add together file5.txt
git commit -m "Reintroduce file5.txt"
```
Determination
Accidentally deleting important files is every developer's nightmare. With Git not having an intuitiveundo control, information technology tin can exist difficult to efficiently restore a deleted file.
With a total fill-in solution ready, however, you tin can rest bodacious y'all're able to combat any issues that might occur because of deleted files. You'll too be able to restore deleted files without issue. Having a full backup solution can show to be useful in the long run, particularly in critical concern software or team-based environments. Cheque out BackHub for one of the best complete backup and restore solutions. See information technology in action yourself: start your free trial of BackHub (now part of Rewind).
desrosiersgristordenty.blogspot.com
Source: https://rewind.com/blog/recovering-deleted-files-in-github/
0 Response to "Git Keep Trying to Upload Large File That Was Deleted"
Post a Comment