Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: change one line in file for the complete history

When I started my git repo I commited a few files as initial commit to it. Now, many commits later, I noticed that I included in those files a line with information which I do not want to publish (unlike the rest of the code). So I want to remove/change this one line and keep the rest of the code.

Searching around I found this solution: Insert an empty commit as initial commit (described here: Insert a commit before the root commit in Git?), do a rebase on it and then edit the old first commit via amend. Unfortunately, many cruel merge conflicts arise during rebase (as described here: git: solving conflicts caused by rebase).

Is there a different way to solve my problem or do have to rebase and edit all conflicts by hand?

Thanks in advance :)

like image 602
tbolender Avatar asked Aug 25 '11 17:08

tbolender


People also ask

How do I rewrite git history?

Git doesn't have a modify-history tool, but you can use the rebase tool to rebase a series of commits onto the HEAD that they were originally based on instead of moving them to another one.

Can git history be rewritten?

There are many ways to rewrite history with git. Use git commit --amend to change your latest log message. Use git commit --amend to make modifications to the most recent commit. Use git rebase to combine commits and modify history of a branch.

Which command is used to view the history of all the changes to a file?

Using git log --follow -p bar will show the file's entire history, including any changes to the file when it was known as foo .

How do I amend a previous commit?

You can modify the most recent commit in the same branch by running git commit --amend. This command is convenient for adding new or updated files to the previous commit. It is also a simple way to edit or add comments to the previous commit. Use git commit --amend to modify the most recent commit.


2 Answers

Here's a command that will remove an offending line from a file's history in all your branches:

git filter-branch --tree-filter 'sed -i "/sensitive information/ d" filename' -- --all 

This checks out every revision, runs the sed command on it, then commits that revision back.

The sed command in this case matches any lines containing the pattern sensitive information in the file named filename and deletes those lines.

Note: it's good if you have a backup, and try the sed script on its own first to make sure it's doing what you want, because it can take quite a while to run on a long history.

like image 105
Karl Bielefeldt Avatar answered Oct 09 '22 04:10

Karl Bielefeldt


I made this command that doesn't error out when a file doesn't exist:

 filename=./path/to/your/filename  filter=your_regex_here   git filter-branch --tree-filter 'test -f $filename && sed -i.bak "/$filter/d" $filename  || echo “skipping file“' -- --all 
like image 22
Ted Yavuzkurt Avatar answered Oct 09 '22 04:10

Ted Yavuzkurt