[GIT] Permanently delete specific version / specific commit of the file in git history.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
[GIT] Permanently delete specific version / specific commit of the file in git history.
Hi everyone,
Im struggling to remove specific revision of one file in GIT history.
I want to remove commit: 7eb5c6bae3cff39f0f1a89faca336ba1ce480316 (first from the bottom).
Is there a way to do that?
Hi Dugan,
Thanks for reply.
I tried that already but I do not see the file/commit on the list.
Should I maybe add it to the list, eg: drop 7eb5c6 ?
Note: I do want to remove 1 commit of specific file: ansible/x/vars/certificates.yml and nothing else.
just curious. Why on earth do you want to do that? In general git (as any other version control system) will save all the versions of the files. There is no need to alter the history. You ought to create a new commit if you want to change something (you can do a reverse commit too).
Additionally git can be tricky, so we need to know if that commit contains other files too and if you want to revert that change (only for the given file)?
If you need to change a single commit, simply make the relevant changes, stage them as normal, then use "git commit --amend" option to modify the last commit.
If it's not the most recent commit and/or you have more complex needs, see Rewriting History section in the Pro Git book (which covers amending, rebasing and related techniques).
Remember that amending commits and rewriting history is a tool for organizing local repositories - before they are shared with others. If you have already pushed to a shared/public repo, it's safer to treat that sensitive data as compromised.
(There is a --force option which can overwrite when pushing - useful if you have multiple local repositories to deal with, but also capable of deleting unrelated data that you wanted to keep, so you need to fully understand what it does and use with care.)
You need to remove the file completely, after that the history cannot be restored (if deleted properly). But actually you can change only your local repo and other clones will/may still contain that file (as it was mentioned).
I tried that already but I do not see the file/commit on the list.
Should I maybe add it to the list, eg: drop 7eb5c6 ?
Note: I do want to remove 1 commit of specific file: ansible/x/vars/certificates.yml and nothing else.
Code:
$ git rebase -i 7eb5c6bae3cff39f0f1a89faca336ba1ce480316
I think you need to start the rebase at one commit prior to the one you want to change, e.g., git rebase -i 7eb5c6bae3cff39f0f1a89faca336ba1ce480316~1
I think you need to start the rebase at one commit prior to the one you want to change, e.g., git rebase -i 7eb5c6bae3cff39f0f1a89faca336ba1ce480316~1
rebase will not solve this issue if the file is visible/available on several branches. Better to remove the file completely. But again, other clones cannot be easily managed (not to speak about the fact: actually we don't know the number and location of clones).
I will try git rebase -i 7eb5c6bae3cff39f0f1a89faca336ba1ce480316~1 tomorrow.
There is only master branch of this repository and there is very small group of people who use it for the moment.
yes, you can just remove the directory with all the files within and clone again. And you can also clone again into another dir (and keep both the old and new one if you wish).
@czezz: I would cordially recommend that, before you proceed any further with your present quest, that you escalate your plans to your management. As they used to say, "this is probably above your pay grade." (And I mean nothing against you by saying that in public.)
The actions which you now appear to contemplate could potentially have far-reaching implications, even if you actually succeed in doing them. And, you might not be in a position to know what those implications might be. Therefore, other parties must now become involved.
I consider this whole thing to now be an "XY Problem" which appropriately calls for [senior ...] management review before you proceed any further.
Present the issue to your manager: "This is what I think that the problem is. This is why I think we need to do something about it. This is what I propose to do. I await your instructions." Do not expect an immediate response. Do not act until and unless you get one.
Last edited by sundialsvcs; 09-14-2022 at 06:16 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.