Mark charlton
Mark charlton

Reputation: 377

New to Git. Made a big mistake with git commit and ended up at an older commit

I'm new to Git and I've made a huge mistake. Git kept prompting me with

git - rejected master -> master (non-fast-forward).

But, I still committed by using:

--force

This was disastrous, the whole project changed back to the stage it was at about a week ago. I've lost so many changes. I seem to have been pushed back to an earlier commit. Is there anyway I can get back to one of my newer commits? As I have made an enormous amount of changes and need to get them back.

Upvotes: 1

Views: 207

Answers (2)

Tanzeel Kazi
Tanzeel Kazi

Reputation: 3827

From your question it appears you did a git push --force. This is very bad. I am not sure if this will work but it is worth a try. Go in order of suggestions and only if one suggestion doesn't work skip to the next one:

  • Try reverting the commit hash that you forced onto the master branch with git revert {commit_hash}. If you feel your changes have been restored then do another git commit and git push.
  • If the above did not work and this was the last commit you did you can try to go back one commit with git checkout HEAD~1. This will take the HEAD back one commit. The easiest way to know how many commits you need to go back is using git reflog and see which commit was the last stable one. If this state is what you feel is correct then do a git commit and git push.

Please don't ignore git warnings. In my experience git only warns you about the absolutely necessary stuff.

Upvotes: 0

Karthik Bose
Karthik Bose

Reputation: 36104

Use git reflog command. The reflog command will give you a good history of what's been happening on the head of your branches. Run it, then find the line that refers to the state that you want to get back to.

Once you found the commit, reset your branch to point to the specific commit.

git reset --hard <commit_id>

The --hard options sets the HEAD of the current branch back to the commit that you specify.

Upvotes: 4

Related Questions