Is there an easy way to the following with git?
Basically I want to create a new commit at the top of my commit history that is equivalent to a previous com
To create a new commit, restoring the content of an old commit, you can:
First, mark the current HEAD of your branch (assuming master
here): we will need to move that HEAD without modifying master
, so let's create a new temporary branch called 'tmp
' where master
is.
git checkout master
git checkout -b tmp
(yes, that is against the "without branching" of the question, but you will delete that tmp branch soon)
Then we go back to an older commit with the right content.
git reset --hard
That resets the index (and working tree) to the right content, but that also moves HEAD. However, that moves tmp
HEAD, not master
HEAD.
move back tmp
HEAD to where master
is, but without modifying the index or the working tree (which are representing what we need for a new commit)
git reset --soft master
make a new commit, on top of master
/tmp
HEAD, which represents the right content (the old commit).
git commit -m "new commit, image of an old one"
Finally, force master
to be where tmp
is: one new commit later.
git branch -M tmp master
git checkout master
git branch -d tmp
Now a regular git push
is enough, any collaborator can simply pull as usual, and still get the old reset content.
git push