Search code examples
gitversion-controlgit-push

Cancel part of a single line from specific commit, after git push


Someone pushed the following line to our repository:

git = ['a', 'w', 'e', 's', 'o', 'm', 'e', BugSyntaxErrorBug]

But it actually should be:

git = ['a', 'w', 'e', 's', 'o', 'm', 'e']

Since than many pushes were made, and I want to rewrite it from this specific push. The idea behind this is that if someone will pull an older version of this code, the code will work.

Thanks :)


Solution

  • This is how I would do it.

    tag these so you can restore if something goes wrong.

    git tag MyOldCommit BADCOMMIT
    git tag MyOldMaster master
    

    if you need to you can recover master unchanged with

    git branch -f master MyOldMaster
    

    now checkout the bad commit to a new branch

    git checkout -b MyFixedCommit BADCOMMIT
    

    after removing the offending bytes, replace the old commmit with new one.

    git commit --amend -m"my commit message"
    

    go back to your old branch master branch

    git checkout master
    

    rebase master (and only master!!!) onto MyFixedCommit

    git rebase --onto MyFixedCommit BADCOMMIT master
    

    git will re-play (commit by commit) the changes from BADCOMMIT to tip of master on to MyfixedCommit. You will have to resolve any conflicts that Git finds on the way.

    !!Since you are re-writing history, the commit chain of master will now be different other user's tags and branches that referenced the old master commit chain!!