1

I use a git repository as a temporal storage. I add some directory, push it to remote repo, some time later, on another machine I clone repo, use directory data and after this moment I don't need it anymore. So, my commit history looks like this :

commit dddddddddddddddddddddddddddddddddddddddd
    Add folder D

commit cccccccccccccccccccccccccccccccccccccccc
    Add folder C

commit bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
    Add folder B

commit aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    Add folder A

And directories :

.
├── A
├── B
├── C
├── D
└── README.md

I don't change data in this directories once I pushed them to the repo. I repeat an operation of adding a folder to my repo very often what makes repository to grow very fast.

As a solution I would like to delete a folder, that I don't need. Let it be a directory C. Every folder has only one commit in its commit history (For the directory C it will be commit cccc...). I can delete this folder, commit changes but in .git/objects data from C will remain, but I don't need it. That's why I would like to delete commit cccc.... I can't use git revert because it just replaces for me processes of deletion and submission.

What I want is to completely get rid of data in folder C and from commit cccc.... There is an option : git rebase -i <commit>~ . But it opens an interactive mode to edit history, while I want a command way of a commit deletion, so I can add a script that deletes not-needed data once I finish my work with it.

What are your the solutions for a current problem?

koshachok
  • 460
  • 5
  • 19
  • 3
    From what you describe, Git is not the right tool for you. You seem to be looking for a file synchronization tool. – mkrieger1 Jan 25 '21 at 13:41
  • you could use the SAME commit message and then force push each time which would change the commits history to the latest commit ONLY. – Michael Jan 25 '21 at 13:43
  • Does this answer your question? [How do you remove a specific revision in the git history?](https://stackoverflow.com/questions/37219/how-do-you-remove-a-specific-revision-in-the-git-history) – mkrieger1 Jan 25 '21 at 13:43
  • @mkrieger1 In my case it is the most suitable and fastest tool – koshachok Jan 25 '21 at 13:49
  • @Michael some times I need to delete `B`, so A, C, D folders will remain. It some sort, it looks like thread problem, as I have common resource, and some workers would like to use from it. I could "lock" repository, and when I finish everything I can unlock it and let other workers do their job, but it's not time efficient. – koshachok Jan 25 '21 at 13:51
  • Well whatever you need to delete you can force push onto that commit with your next commit and revise the history by replacing a not needed commit with a new one. – Michael Jan 25 '21 at 13:53
  • https://stackoverflow.com/q/12394166/7976758 – phd Jan 25 '21 at 13:57

1 Answers1

2

First of all, git does not seem like a tool you want to use. It was designed to do a very different thing. I also like both @Michal's and @mkrieger1's answers given in comments more, than my solution, but I have written it already, so posting:

I very much doubt this is the most elegant way, but you can replace the VISUAL environment variable with a command which will act as a filter for your interactive rebase instructions file and make git do the work for you: use something like VISUAL='sed -i -n /c4f1c6e/!p' git rebase -i <commit>~

Example:

(there is a slightly better highlight at pastebin than below)

nikolay@KoLin:~/tmp$ git init
Initialized empty Git repository in /home/nikolay/tmp/.git/
nikolay@KoLin:~/tmp$ touch f g h
nikolay@KoLin:~/tmp$ git add f
nikolay@KoLin:~/tmp$ git commit -m f
[master (root-commit) f9cbe42] f
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 f
nikolay@KoLin:~/tmp$ git add g
nikolay@KoLin:~/tmp$ git commit -m g
[master c4f1c6e] g
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 g
nikolay@KoLin:~/tmp$ git add h
nikolay@KoLin:~/tmp$ git commit -m h
[master bae0beb] h
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 h
nikolay@KoLin:~/tmp$ git log
commit bae0bebe5815e9ca32f43f47eb725ac020b6f0c3 (HEAD -> master)
Author: Nikolay Nechaev <Nikolay_Nechaev@mail.ru>
Date:   Mon Jan 25 16:45:25 2021 +0300

    h

commit c4f1c6e440988233098cc2dcb8ad8bf3cd1803a2
Author: Nikolay Nechaev <Nikolay_Nechaev@mail.ru>
Date:   Mon Jan 25 16:45:19 2021 +0300

    g

commit f9cbe428ff170dc400eaa42dd5eea8ef680b5fe5
Author: Nikolay Nechaev <Nikolay_Nechaev@mail.ru>
Date:   Mon Jan 25 16:45:14 2021 +0300

    f
nikolay@KoLin:~/tmp$ VISUAL='sed -i -n /c4f1c6e/!p' git rebase -i c4f1c6e~
Successfully rebased and updated refs/heads/master.
nikolay@KoLin:~/tmp$ git log
commit 2c868b1c50c1def4c60e122305dd402d29fb9fb9 (HEAD -> master)
Author: Nikolay Nechaev <Nikolay_Nechaev@mail.ru>
Date:   Mon Jan 25 16:45:25 2021 +0300

    h

commit f9cbe428ff170dc400eaa42dd5eea8ef680b5fe5
Author: Nikolay Nechaev <Nikolay_Nechaev@mail.ru>
Date:   Mon Jan 25 16:45:14 2021 +0300

    f
nikolay@KoLin:~/tmp$ 

NOTE, that the commit hashes must be short because git prints them short in the interactive rebase file. The exact number of characters you should use may depend on git version. It is 7 for me (git version 2.25.1).

NOTE: if it opens an interactive text editor instead of executing sed after you update the VISUAL environment variable, try updating the EDITOR variable instead

Kolay.Ne
  • 1,345
  • 1
  • 8
  • 23