Reputation: 836
I got a Github repository from client where there is no code. I cloned this repository and pasted my code folder to this repository and pushed the code to git. But now I need to revert back my commit. For this I did the following steps:
git revert xxxxxxxxxxxxxxxx(commit)
git add .
git commit -a -m "reverted back"
git push origin master
After this I went to my github page, there I find project folder with app/views, config, db, log, public, tmp folders where revert is not properly done.
How to undo my last 2 commits (both commit and revert) and get back to the main repository as it is?
Please help me, I am worried.
Upvotes: 0
Views: 1001
Reputation: 12547
The simplest way is just to hard reset to commit preceding the bad one:
git reset --hard xxxxxxxxxxxxxxxx^
git push -f origin <your-branch-name>
There xxxxxxxxxxxxxxxx
is your sha, that you reverted in the first time.
Note the ^
before xxxxxxxxxxxxxxxx
. It means that you reset to the commit before xxxxxxxxxxxxxxxx
.
Or you can specify exact hash to reset to, i.e.
git reset --hard A
Before resetting your branch is looking like:
A -> xxxxxxxxxxxxxxxx -> B(bad)
After git reset --hard xxxxxxxxxxxxxxxx^
A
git push -f origin <your-branch-name>
pushes your local changes made on the previous step to the Github server
Be careful with force pushing though, because you force your local repository state on the server. So you lose server state forever
Upvotes: 3