huggie
huggie

Reputation: 18237

Git reverting old version as new commit across file name changes

I want to revert to an old version and commit it as a new version:

So I do:

git checkout 1778b26 .

However, if there was a renamed file involved between the latest version and the version I am checking out, both file names would exist. git clean -fdx does not fix this.

How do I make sure that , when I check out that old version as new, the files and directories are exactly as the old version?

Upvotes: 2

Views: 135

Answers (2)

VonC
VonC

Reputation: 1323203

You can:

  • mark your current state: git branch tmp
  • reset index and working tree to your old commit: git reset --hard oldSHA1
    However, this also moves HEAD back and lose the history of the commits after oldSHA1.
  • reset HEAD to yourCurrentBranch previous HEAD (marked tmp):
    git reset --soft tmp

From there, you can add and commit: that will create a new commit which will have the exact same content as the old SHA1.

Upvotes: 2

Sam Varshavchik
Sam Varshavchik

Reputation: 118292

Use

git diff 1778b26..HEAD | git apply -R --index

This generates a diff between that other commit and your current HEAD, then reverses the diff, applies it to HEAD, and then records the changes in the index.

This should work as long as you do not have any binary files in your repository.

Upvotes: 1

Related Questions