Reputation: 3537
I know Git has tons of documentation and there are tons of posts on SO about it (I've looked at How to delete a 'git commit' and this, for example) but nothing has worked yet and I'm not sure what to do here.
I have a repo that so far only I'm committing to, but I recently started working from two different laptops and somehow I accidentally pushed some nonsense that Git saw as a merge and then I pushed the latest changes as well.
I've been trying to figure out how to delete those last 2 commits completely, rewinding the repo to the state before that fateful day (as I now have a bunch of new commits I need to get into that repo). If you're willing to give me any pointers, note that the repo is hosted on github here.
Upvotes: 2
Views: 198
Reputation: 3537
Thanks to an answer in the question Shahbaz posted, I initially tried
git push -f origin HEAD^^:master
which appeared to remove the last two commits perfectly. But as soon as I made some changes to the local files and did a push, the old commits showed back up! :(
After trying it again, this time with HEAD^^^
(since I now needed to remove the last 3 commits), I looked at git log
and saw that everything was still there locally. Ignorant as to how to resolve this, I just wiped out the local directory, re-cloned the repo, and manually copied in my newly-modified files. Finally, adds and pushes didn't bring back the old unwanted commits.
Obviously, I'm sure there's a better way, but I wasn't able to make it happen by following you lovely peoples' suggestions. (Example: I tried doing git rebase -i HEAD~4
but I didn't understand the output I got in the editor -- it gave way too many lines (9) and I didn't know what to do.)
I'll leave it to someone more experienced to decide if this question should be deleted as a duplicate.
Upvotes: 0
Reputation: 129526
for someone new, the easiest thing to do instead of git reset --hard HEAD^
, is to use gitk --all.
This way you see all your history. The current branch is in bold. You can right-click the commit that you were on before and select "reset current branch here". You will be prompted for what kind of reset to perform. Select "hard" and you should be back to where you want to be.
Upvotes: 2
Reputation: 18243
This worked for me (by memory):
git reset --hard HEAD^^
git push --force HEAD:master
git pull
Upvotes: 0
Reputation: 185653
In order to delete the most recent commit, just use git reset --hard HEAD^
. If the most recent commit is a merge, this will revert back to the first parent (i.e. the state before the merge
/pull
was run).
If you can identify the exact commit that you wish to be the tip of the branch, you can use git reset --hard $SHA
where $SHA
is the commit in question.
Upvotes: 1