0

I have a Git repository where I have added, committed and pushed some files a month ago, which, I was now informed, should not have been on the root level and not in the repository at all.

Note that the questions that pop-up as similar here are about undoing "most recent local commits" and unfortunately do not help me as eversince last month, when I committed them, there have been many commits to that branch named 32-nd-branch. The instruction which I received is

"Please drop them by reverting the commits."

My first thought when reading this was that reverting to the < commit id >, which I found by git log --diff-filter=A -- <file name> in order to delete (not sure if that is meant by "drop") the files will leave a month of progress burried somewhere deep in the git repo history, and will set the current state of the repository to the one back then.

I want instead, to just go back to that commit which I added that particular file, delete the file and then, most importantly, push this change to that commit without affecting any other files, if possible, without changing the hash of the commit. But then this file I delete in this old commit would be there in all subsequent ones, right? So how do I delete this unwanted file from all of the commits following it's creation in the repository?

apingaway
  • 33
  • 1
  • 1
  • 17
  • 1
    You can't _modify_ a commit and _not_ change the sha1. Actually, you _never_ modify a commit. It's written in stone. You can generate a _new_ commit with something different and that will produce a _new_ commit with a different sha1. – eftshift0 Oct 27 '22 at 14:29
  • 2
    Then.... probably the best tool for the job of modifying history to remove something is `git filter-repo`, but we are talking about _the big guns_. If all you are working with is a single branch (or multiple but not that separate from a main branch) then you could pull it off with a rebase interactive.... but you would be rewriting history. As I said in the first comment. You _cannot_ modify something from a commit and _not_ modify its ID – eftshift0 Oct 27 '22 at 14:36
  • Why is it important for you to save sha? This goes against the idea of a version control system. You want to make changes to a commit. This will create a new commit with a new SHA. This is the correct behavior of a version control system. Otherwise, it is impossible to ensure the integrity of the repository. – Ivan Yuzafatau Oct 27 '22 at 15:22

0 Answers0