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 it possible to make a git commit non-amendable?
  • How to change a commit content after the commit has been pushed to the remote branch?
  • How to undo “git commit --amend” done instead of “git commit”
  • Git - do amended commits persist?
  • git mirroring to GitHub and filtering private files
  • How to push to repo after doing 'git commit --amend'
  • Is there a different way to solve my problem or do have to rebase and edit all conflicts by hand?

    Thanks in advance 🙂


  • Git reset not working
  • why does the gitk and sourcetree commit graph look different for the same repository
  • Can't clone repo using git clone git://… - ok with git clone http://
  • GitFlow: How do I know which feature I am working on?
  • Tagging a GitHub project through Jenkins
  • Clone and set local repository config in one command
  • 3 Solutions collect form web for “git: change one line in file for the complete history”

    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.

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

     git filter-branch --tree-filter 'test -f $filename && sed -i.bak "/$filter/d" $filename  || echo “skipping file“' -- --all

    You can have a look at git filter-branch

    The examples in the link should get you going:

    Git Baby is a git and github fan, let's start git clone.