Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a file from a older commit without affecting any other commits in gitlab

Tags:

git

gitlab

I am using the gitlab repository for uploading my code files. I have made more than 500 commits. My repo size was upto 540 mb. I want to minimize the size of the repo by deleting unwanted files in the repo.

Somehow i managed and deleted the files in my local. Now my current file size is 10 mb in the local. I pushed it and it was good. Eventhough it shows 540mb. I came to know the previous commit files holds the storage. There is a zip file with a size of 400 mb which is in one of my older commit.

I found the commit by using the shell script. I want to delete the zip file in the commit. But it shows "You can only delete files when you are on the branch" in the gitlab webui. In the terminal, I found the command git rm <file_name> to remove the file. I got the file path from the commit. But I don't know how to use it on a particular commit. It holds some important files. I don't wanna play with it.

My question is: how do I remove a certain file in a particular commit in gitlab without affecting any other commits? I tried Stack Overflow and found some answers to delete the file in previous commit but not in the older commit. Help me with some solutions.

like image 391
Steven Avatar asked Oct 30 '25 22:10

Steven


2 Answers

The short answer is grim : you can't.

That is, you could modify a commit in the past, but it would modify every commit down the line so it doesn't check the "without affecting anyother commits" part.

Take a look at git filter-branch to have an idea of what tools are at your disposal to change history, but be aware that's quite a heavy operation.

like image 161
Romain Valeri Avatar answered Nov 01 '25 13:11

Romain Valeri


It's no possible to delete the file without affect commit history.

To delete the file in the history, you can use git built-in git-filter-branch or third party bfg-repo-cleaner

Refer to i-want-to-remove-a-large-file-from-ever-existing-in-repo-history of Github: git-flight-rules

recommended-technique-use-third-party-bfg

(master)$ git rm path/to/filetoremove
(master)$ git commit -m "Commit removing filetoremove"
(master)$ java -jar ~/Downloads/bfg.jar --delete-files filetoremove

built-in-technique-use-git-filter-branch

(master)$ git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch filepattern' --prune-empty --tag-name-filter cat -- --all
like image 45
LF00 Avatar answered Nov 01 '25 11:11

LF00



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!