How do I make an old commit the current master commit with git?
Here are my commits:
A B C
With C being the current commit. I want things to look like this on origin:
A B C A
So A is my current commit, but B and C are retained. I currently have the repo cloned with C being the current commit. What do I need to do next to get origin how I want it?
2 Solutions collect form web for “How do I make an old commit the current master commit with git?”
Commits ‘B’ and ‘C’ are progressive states of the branch’s history. To reset to ‘A’ without losing ‘B’ or ‘C’ locally, just use a new branch on origin:
git checkout -B new-branch master git reset --hard <A_commit_hash> git push origin new-branch
‘B’ and ‘C’ will be retained on ‘master’, so you can
git cherry-pick <commit_of_B_or_C> or
git merge master without trouble.
If you really want ‘B’ and C’, but want to reset ‘master’ to ‘A’, and have thought through the consequences of forcibly overwriting your remote:
git branch archived-master master ;# create 'archived-master' branch git reset --hard A ;# reset master to 'A' state git push -f origin master ;# overwrite remote's master
That’ll save ‘B’ and ‘C’ locally on the ‘archived-master’ branch.
I would avoid
git revert for this. Revert commits make commands like
git bisect and
git rebase more difficult, and if you ever want to reuse ‘B’ and ‘C’ it’s harder for a human to parse the history.
git revert B C
(this will add two commits, but the state of your tree will be as it was at A)