Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

edit a file in all commits in git

Tags:

git

I made a mistake and one file on a project in a git repository contains a password. It is not a problem because is not a public repo but I would like to get rid of that password in the repo.

The repository history is very simple as I'm the only developer so it only has 12 commits and one of the commits is tagged.

The change in this case will not affect anything on the history (the diff will remain the same) as the password I want to delete is present just from the very first commit. I would like to remove that string as if it hasn't been there anytime.

Is there some kind of command to do this or I must regenerate the history just from the beginning with a new starting point without the password and applying the diffs on top of that?

like image 653
Pedro Reyes Avatar asked Dec 23 '12 12:12

Pedro Reyes


People also ask

How do you edit a file in a commit?

Find the commit you want, change pick to e ( edit ), and save and close the file. Git will rewind to that commit, allowing you to either: use git commit --amend to make changes, or.

How do I edit all commit messages?

On the command line, navigate to the repository that contains the commit you want to amend. Type git commit --amend and press Enter. In your text editor, edit the commit message, and save the commit.

How do I amend multiple commits?

To change the most recent commit message, use the git commit --amend command. To change older or multiple commit messages, use git rebase -i HEAD~N .


2 Answers

See https://help.github.com/articles/remove-sensitive-data. the file will be present in all commits, if you haven't deleted it, as all commits contain the state of the whole repo.

Basically, what you need to do is run a filter-branch across all your commits and then force push that into repo. The command is destructive and will change all commits where the file has been present at the repo, so be careful.

like image 57
eis Avatar answered Oct 21 '22 17:10

eis


Thanks that was exactly the command I was looking for and reading that article and the filter-branch documentation I got the exactly syntax I wanted making use of sed to remove the string from the file.

git filter-branch --tree-filter "sed -i 's/\<password\>//g' ./path_to_file/filename" --prune-empty --tag-name-filter cat -- --all

where:

"password" -> Is the string that needs to be removed

"./path_to_file/filename" -> Is the path to the file inside the repo that needs to be modified

runned in the repo root did the trick like a charm.

After the change has been made it must be pushed to remote server. This need to be forced so the server accepts the totally new rewritten refs and tags.

So a forced push of the repo to the remote server (origin in most cases)

git push origin --all --force

And a forced push of tags to the remote server

git push origin --tags --force
like image 45
Pedro Reyes Avatar answered Oct 21 '22 17:10

Pedro Reyes