Aditya Mukherji
Aditya Mukherji

Reputation: 9256

Making git forget I did anything to a file

Theres a file I may have accidentally changed in my branch, which I have committed to and rebased several times. Before I hand over this branch to somebody else, I want to make git forget I have ever done anything to this file.

How do I ask git to get the latest version of that file from the master branch (which other people have been updating), and forget the fact that my branch ever touched that file?

Upvotes: 4

Views: 481

Answers (2)

svick
svick

Reputation: 244837

When on your branch, you can checkout the file from master:

git checkout master the_file

Commit that change and then find the sha1 of the commit where you accidentally changed the file. And use

git rebase -i that_sha1

You will see a list of commits from your branch, from oldest to newest. Move the last line (the fixing commit you just made) after the first line (the incorrect commit) and change pick to fixup. This will have the same effect as amending the incorrect commit and then rebasing the rest of commits on top of that.

This all assumes you didn't publish your branch. If you did and somebody else has it now too, you are in trouble.

Upvotes: 3

Sean
Sean

Reputation: 681

It may be easiest to checkout the file from master, to overwrite your changes. After you commit the file that you just checked out, you may wish to rebase/squash the branch to remove any commits made to that file. The syntax to checkout would be:

git checkout origin/master <path_to_file>

A more advanced option may be to use git-filter-branch

Upvotes: 1

Related Questions