Jay Taylor's notes
back to listing indexCompletely remove file from all Git repository commit history
[web search]
I accidentally committed an unwanted file ( |
|||||||||
|
|||||||||
Please don't use this recipe if your situation is not the one described in the question. This recipe is for fixing a bad merge, and replaying your good commits onto a fixed merge. Although Try the following recipe:
(Note that you don't actually need a temporary branch, you can do this with a 'detached HEAD', but you need to take a note of the commit id generated by the |
|||||||||||||||||||||||||||||||||
|
If you haven't committed anything since, just If you have
will go through each change from |
|||||||||||||||||||||||||||||||||
|
Intro: You Have 5 Solutions AvailableThe original poster states:
There are many different ways to remove the history of a file completely from git:
In the case of the original poster, amending the commit isn't really an option by itself, since he made several additional commits afterwards, but for the sake of completeness, I will also explain how to do it, for anyone else who justs wants to amend their previous commit. Note that all of these solutions involve altering/re-writing history/commits in one way another, so anyone with old copies of the commits will have to do extra work to re-sync their history with the new history. Solution 1: Amending CommitsIf you accidentally made a change (such as adding a file) in your previous commit, and you don't want the history of that change to exist anymore, then you can simply amend the previous commit to remove the file from it:
Solution 2: Hard Reset (Possibly Plus a Rebase)Like solution #1, if you just want to get rid of your previous commit, then you also have the option of simply doing a hard reset to its parent:
That command will hard-reset your branch to the previous 1st parent commit. However, if, like the original poster, you've made several commits after the commit you want to undo the change to, you can still use hard resets to modify it, but doing so also involves using a rebase. Here are the steps that you can use to amend a commit further back in history:
Solution 3: Non-interactive RebaseThis will work if you just want to remove a commit from history entirely:
Solution 4: Interactive RebasesThis solution will allow you to accomplish the same things as solutions #2 and #3, i.e. modify or remove commits further back in history than your immediately previous commit, so which solution you choose to use is sort of up to you. Interactive rebases are not well-suited to rebasing hundreds of commits, for performance reasons, so I would use non-interactive rebases or the filter branch solution (see below) in those sort of situations. To begin the interactive rebase, use the following:
This will cause git to rewind the commit history back to the parent of the commit that you want to modify or remove. It will then present you a list of the rewound commits in reverse order in whatever editor git is set to use (this is Vim by default):
The commit that you want to modify or remove will be at the top of this list. To remove it, simply delete its line in the list. Otherwise, replace "pick" with "edit" on the 1st line, like so:
Next, enter
At this point, you can remove the file and amend the commit, then continue the rebase:
That's it. As a final step, whether you modified the commit or removed it completely, it's always a good idea to verify that no other unexpected changes were made to your branch by diffing it with its state before the rebase:
Solution 5: Filtering BranchesFinally, this solution is best if you want to completely wipe out all traces of a file's existence from history, and none of the other solutions are quite up to the task.
That will remove
Again, after the
Filter-Branch Alternative: BFG Repo CleanerI've heard that the BFG Repo Cleaner tool runs faster than
Additional Resources
|
|||||||||||||||
|
This is the best way: Just be sure to backup the copies of the files first. EDIT The edit by Neon got unfortunately rejected during review. E.g. to remove all
That still didn't work for me? (I am currently at git version 1.7.6.1)
Not sure why, since I only had ONE master branch. Anyways, I finally got my git repo truely cleaned up by pushing into a new empty and bare git repository, e.g.
(yes!) Then I clone that to a new directory and moved over it's .git folder into this one. e.g.
(yeah! finally cleaned up!) After verifying that all is well, then you can delete the |
||||
Rewriting Git history demands changing all the affected commit ids, and so everyone who's working on the project will need to delete their old copies of the repo, and do a fresh clone after you've cleaned the history. The more people it inconveniences, the more you need a good reason to do it - your superfluous file isn't really causing a problem, but if only you are working on the project, you might as well clean up the Git history if you want to! To make it as easy as possible, I'd recommend using the BFG Repo-Cleaner, a simpler, faster alternative to You should carefully follow the steps here: http://rtyley.github.com/bfg-repo-cleaner/#usage - but the core bit is just this: download the BFG jar (requires Java 6 or above) and run this command:
Your entire repository history will be scanned, and any file named Full disclosure: I'm the author of the BFG Repo-Cleaner. |
|||||||||
|
This is what |
|||
You can also use:
|
|||||||||
|
Just to add that to Charles Bailey's solution, I just used a git rebase -i to remove unwanted files from an earlier commit and it worked like a charm. The steps:
|
|||
Your Answer
Not the answer you're looking for? Browse other questions tagged git git-filter-branch git-rewrite-history git-rm or ask your own question.
asked |
7 years ago |
viewed |
182143 times |
active |
Get the weekly newsletter! In it, you'll get:
- The week's top questions and answers
- Important community announcements
- Questions that need answers
see an example newsletter
Linked
Related
Hot Network Questions
- How is y’all’dn’t’ve pronounced
- How can I protect my tron deck against Crumble To Dust
- Inserting a \textdegree symbol in tikzpicture path decoration text
- New colleague is a smoker; I can hardly breathe near him
- Turning wine into water
- Excluding subsection from toc but allow proper referencing
- In simple English, what does it mean to be transcendental?
- Is it ok if I can't solve a problem when sitting in front of a computer?
- What is the limit of (Odd Numbers Product) / (Even Number Product)
- A skill that you have, but has little to no benefit for you
- Invoking a function without parentheses
- boolean modifier leaves cutting edge on face
- Disputing charge made with chip card
- Is there an informal term for the "best company in an industry"?
- How should I keep the vegetables from flying off the board when I cut them?
- How to print the content of a macro to the terminal
- Does shooting people make them dislike me?
- Energy required to raise temperature of water flowing through a heater
- Does Deadpool know he is breaking the 4th wall?
- How can I best display a blank space character?
- Why did the original Game Boy have four colours?
- Which computer-made song is this?
- Why are berries so expensive?
- Why was Remus Lupin on the Hogwarts Express?
Technology | Life / Arts | Culture / Recreation | Science | Other | ||
---|---|---|---|---|---|---|