LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-10-2022, 06:28 PM   #1
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Rep: Reputation: 43
[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?

Code:
$ git log -- ansible/x/vars/certificates.yml
commit 72dcc68fb842fadef67eb23dea28c36213a14a7c
Author: me
Date:   Fri Sep 9 14:02:08 2022 +0200

commit f052f75edce59ed8b2e3d8fbec08f843edf86ad8
Author: me
Date:   Fri Sep 9 14:00:37 2022 +0200

commit bb097a152b437c82c3a06d1903b4782e30db4425
Author: me
Date:   Fri Sep 9 13:43:15 2022 +0200

commit 25a8877965f2086ed21d6af7a2a0310f035edabc
Author: me
Date:   Fri Sep 9 13:38:04 2022 +0200

commit 7eb5c6bae3cff39f0f1a89faca336ba1ce480316
Author: me
Date:   Thu Sep 1 14:30:12 2022 +0200
Note: I tried to do git rm ansible/x/vars/certificates.yml and then push correct version of the file. That sadly restores its all it's history.

Cheers
czezz
 
Old 09-10-2022, 06:54 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,242

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
"git rebase -i" is one option that came up in my searches ("removing revision from git history").
 
Old 09-11-2022, 03:29 AM   #3
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
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.

Code:
$ git rebase -i 7eb5c6bae3cff39f0f1a89faca336ba1ce480316

pick 04e7b1c monitoring XN
pick 3ce2f20 XN
pick 7f4d85d removing ansible.tar
pick 324fddf X_HOST
pick f242a4f CangeLog
pick 4a4893e ChangeLog # empty
pick 0d4bec0 Make vi behave ;)
pick 77978da query1
pick 1f9e477 query1.sh
pick d896fb0 rm tar
pick fd9d6a5 deployment.yaml edited online with Bitbucket
pick 6b91a6e values_1s.yaml edited online with Bitbucket
pick dc0445c values_2s.yaml edited online with Bitbucket
pick 8a0ee3a values_1p.yaml edited online with Bitbucket
pick b408246 values_2p.yaml edited online with Bitbucket
pick 060fdd6 monitoring
pick 8dc5084 monitoring cronjob
pick f6c5e21 monitoring
pick a0c2fdf Dockerfile update
pick 0cc6d7f customization.sh
pick 25c5811 inst-key
pick 2dc024e cleanup tar file
pick 53c8493 monitoring update
pick 72ce7d3 update
pick c4c940f query
pick 8fc27c6 ha
pick 9acd1e4 template
pick f727141 templates
pick 2528877 del
pick bb297a1 ssl
pick f054f75
pick 72dc468
pick 5a10043 readme
pick 817a35d readme
pick 9220b63 update
pick 3b4599e rm
pick f3158f9 certificates.yml

# Rebase 7eb5c6b..f3158f9 onto 7eb5c6b (37 commands)

Last edited by czezz; 09-11-2022 at 03:30 AM.
 
Old 09-11-2022, 03:44 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,962

Rep: Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331
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)?
 
Old 09-11-2022, 04:03 AM   #5
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
To remove sensitive data.
Its there only int that specific commit in that file

It would be still fine to completely remove that file and start over with new one. But doing so the history is restored (see my first post).

Last edited by czezz; 09-11-2022 at 04:11 AM.
 
Old 09-11-2022, 07:20 AM   #6
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,616

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555

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.)


Last edited by boughtonp; 09-11-2022 at 07:22 AM.
 
Old 09-11-2022, 09:52 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,962

Rep: Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331
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).
 
Old 09-11-2022, 10:34 AM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by czezz View Post
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
 
Old 09-11-2022, 10:45 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,962

Rep: Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331
Quote:
Originally Posted by ntubski View Post
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).
 
Old 09-11-2022, 03:57 PM   #10
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
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.

I will also take a look a the link from: Boughtonp
https://www.git-scm.com/book/en/v2/G...riting-History

Additionally, maybe there is a way to completely remove repository and start over?
 
Old 09-12-2022, 12:16 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,962

Rep: Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331Reputation: 7331
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).
 
Old 09-14-2022, 06:09 PM   #12
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,676
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
@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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[GIT] delete file and its commits history czezz Programming 3 05-19-2020 09:53 AM
cvs [commit aborted]: 'root' is not allowed to commit files rakesh@linuxforum Linux - Newbie 7 09-26-2013 03:53 PM
[SOLVED] History utility error in Linux Mint 14; cannot delete history mintyninja41 Linux - Newbie 3 03-22-2013 06:36 AM
How to delete a commit from git tree phyyu Linux - Software 1 11-11-2009 06:48 AM
cvs [commit aborted]: 'root' is not allowed to commit files r_ramya06 Linux - Newbie 5 07-08-2008 09:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration