How to delete file from git
How to delete file from git
Deleting files in a repository
In this article
You can delete an individual file or an entire directory in your repository on GitHub.
People with write permissions can delete files or directories in a repository.
About file and directory deletion
You can delete an individual file in your repository or an entire directory, including all the files in the directory.
If you try to delete a file or directory in a repository that you don’t have write permissions to, we’ll fork the project to your personal account and help you send a pull request to the original repository after you commit your change. For more information, see «About pull requests.»
If the file or directory you deleted contains sensitive data, the data will still be available in the repository’s Git history. To completely remove the file from GitHub, you must remove the file from your repository’s history. For more information, see «Removing sensitive data from a repository.»
Deleting a file
Browse to the file in your repository that you want to delete.
At the top of the file, click
At the bottom of the page, type a short, meaningful commit message that describes the change you made to the file. You can attribute the commit to more than one author in the commit message. For more information, see «Creating a commit with multiple co-authors.»
If you have more than one email address associated with your account on GitHub.com, click the email address drop-down menu and select the email address to use as the Git author email address. Only verified email addresses appear in this drop-down menu. If you enabled email address privacy, then @users.noreply.github.com is the default commit author email address. For more information, see «Setting your commit email address.»
Below the commit message fields, decide whether to add your commit to the current branch or to a new branch. If your current branch is the default branch, you should choose to create a new branch for your commit and then create a pull request. For more information, see «Creating a new pull request.»
Click Propose file change.
Deleting a directory
Browse to the directory in your repository that you want to delete.
In the top-right corner, click
Review the files you will delete.
At the bottom of the page, type a short, meaningful commit message that describes the change you made to the file. You can attribute the commit to more than one author in the commit message. For more information, see «Creating a commit with multiple co-authors.»
If you have more than one email address associated with your account on GitHub.com, click the email address drop-down menu and select the email address to use as the Git author email address. Only verified email addresses appear in this drop-down menu. If you enabled email address privacy, then @users.noreply.github.com is the default commit author email address. For more information, see «Setting your commit email address.»
Below the commit message fields, decide whether to add your commit to the current branch or to a new branch. If your current branch is the default branch, you should choose to create a new branch for your commit and then create a pull request. For more information, see «Creating a new pull request.»
Click Propose file change.
How to completely remove a file from a Git repository
Have you already committed an SSH private key, a password file or a config file with sensitive data to your repository before?
Share this article
Intro
Have you already committed an SSH private key, a password file or a config file with sensitive data to your repository before? In case you did not, I would recommend to first try this out before you continue reading this blogpost.
For the rest of us: DON’T PANIC! Take a deep breath, get up from your desk, walk around for a few minutes. Ready? Okay, let’s get started!
The goal is to completely wipe a file out of existence in a Git repository, to cover all tracks of your horrible mistake. Do you want to be the person who committed AWS keys to a public GitHub repository, only to find out 24 hours later that
USD2000 has been spent mining bitcoins?
Several methods
Simply git rm passwords.txt won’t do it, as the file will still be there in all previous commits. Depending on where the file is, you can use several methods. Hereby an overview per scenario in increasing order of complexity.
All of these methods assume that you are familiar with console commands.
They are written for Linux, but should work on OS X and even on Windows if you use Git Bash.
If you have already pushed your changes, then things might become complicated.
If you’re a single developer, just go for it. But if you work in a team then first talk it over with them.
If your code (with unwanted file) is already out in the open (GitHub, BitBucket. ) then you might be out of luck. But keep reading until the end.
Scenario 1: the file is in the last commit and you have not yet pushed
1. You want to keep the file locally
The git reflog expire and git gc commands force a garbage collection, to keep the file from dangling somewhere in your repository.
2. You do not want to keep the file locally
Just amend the last commit.
Scenario 2: the file is further down in the history and you have not yet pushed
Solution 1: BFG Repo-Cleaner
Download ‘BFG Repo-Cleaner’ here. This tool claims to work 10-720x faster than any other method, but you cannot specify a subdirectory, it will delete all files with the same name in any directory.
Solution 2: interactive rebase
An interactive rebase lets you go back in history and redo commits, as if they were correct in the first place. Sounds like cheating? Maybe. But you want to get rid of that file, and you have the power to do it.
Edit the git-rebase-todo file: change the first command from pick to edit.
Then save and close the editor. The rebase will roll back the commits to the commit that added the unwanted file.
Solution 3: Git filter-branch
If you want to do it for the entire repository (you really shouldn’t!):
Scenario 3: you have already pushed
I can’t repeat this enough: first consult with the rest of your team. You will make their life miserable if you don’t.
Secondly, make a backup of the repository before you do anything else.
Then: MAKE A BACKUP.
I don’t know if I have mentioned this already, but are you really, really sure that you have made a backup?
Use one of the methods described above to remove the file.
Scenario 4: the commits are already on GitHub
In this case, the risk exists that someone can still access the unwanted file, even after a force push.
There are two ways to do that:
if they made a fork or a clone of the repository: a force push will only update our repository, it will not update forks/clones.
if they happen to know the exact hash of the commit that added the file (maybe they wrote it down or the web page is still in their browser cache).
GitHub may garbage collect cached views after some time, but this is not something to rely on.
The best thing to do, is to contact GitHub support and tell them the repository and the offending commit, they will then manually delete the cached views.
What to expect next?
In the follow-up blogpost, I will explain some best practices used at iText Software to handle sensitive files needed in our projects.
scy / delete-from-repo.md
How to delete a file from a Git repository, but not other users’ working copies
Of course, you only realize that two days after the fact and have already pushed it, and your colleagues have already pulled it. They use the same IDE as you do, so whenever they change a setting or fix paths, they can either
Deleting the files from the repository
However, how do you delete it just from the repository? Because you don’t want to lose your IDE settings of course. The folder should still be available on your machine after, like, «un-committing» it.
(Of course you could just copy it somewhere else, tell Git to delete it, commit that and than put the copy back. But that would be cheating.)
How not to be hated by your colleagues
When others pull this «pseudo-deletion» you just committed, their local Git doesn’t know that you kept a copy of the folder and didn’t actually delete it. So what Git on their machines is going to do is to happily delete the folder in their working copies, causing their IDE to become somewhat unhappy. How can you keep Git from doing that?
Well, to be honest, you can’t. The change you committed will cause Git to delete the folder on each and every colleague’s machine.
Except … well, except if you delete it before Git does!
Pseudo-delete it before Git deletes it
Just restore it
If the folder gets deleted on their machine, they can simply restore it again. After all, this is version control!
But wait! There is one more absolutely crucial step!
How can I completely remove a file from a git repository?
I noticed recently that the project files my text editors use (along with some other junk) got added the git repository for the project. Since they aren’t actually part of the project, I’d like to remove them, but git rm doesnt remove the old versions from the repository, and I couldnt find anything else that looks promising.
3 Answers 3
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
will remove «my_file» from every commit.
Notice that this rewrites every commit, so if you push into a remote repository, you have to (a) force the update, and (b) everyone else who pulled from you will now have duplicate commits (since you rewrote the history), as described on the git rebase man page.
This is what git filter-branch is for, but beware that your repo history will change, and the commit hashes will be different after history rewrite.
git forget-blob main.c.swp
You can get more information here
It’s now recommended to use git-filter-repo instead. Running git-filter-branch actually prints:
Not the answer you’re looking for? Browse other questions tagged git or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Completely remove files from Git repo and remote on GitHub
Due to this, my repo size has increased a lot. How can I remove those blobs completely? And I want to remove it from my remote GitHub repo too.
3 Answers 3
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
This is what you’re looking for: ignoring doesn’t remove a file. I suggest you read that page, but here’s the specific command to use:
Also, to remove all the deleted files from caches git creates, use:
You can find more info about the last command, as well as a script that does everything you want in one single action, here: git: forever remove files or folders from history.
Another links with lots of explanation: Remove sensitive data.
(Commands copied from natacado ‘s answer in the question linked above.) If you have already removed the files from the working copy, the following should work. Find out the hash for the commit that added the unwanted files. Then do:
You could just rebase your whole branch and remove both the commit that added the images and the commit that removed them.
You will be presented with a list of commits. Delete the lines that have the rogue commits you want to remove. («dd» deletes a line in vim, the default editor. Then save with ZZ)
The dangling commits will then be cleaned up in the course of natural git garbage collection, a process you can force with the command given in Darhuuk’s answer.
Note that this will be very annoying to anyone who has pulled from your branch. They should consult «recovering from upstream rebase».
Источники информации:
- http://itextpdf.com/blog/technical-notes/how-completely-remove-file-git-repository
- http://gist.github.com/scy/6636390
- http://stackoverflow.com/questions/3458685/how-can-i-completely-remove-a-file-from-a-git-repository
- http://stackoverflow.com/questions/5563564/completely-remove-files-from-git-repo-and-remote-on-github