-2

In the morning I made a commit and pushed it to main branch and it was merged as it was just a little change.

But then i totally screwed up the whole project. Now I want to go back to that last commit which I did in the morning, I don't want my new changes to be stashed or something else. I just want to put my code to the last commit I did.

I'm using android studio terminal for git, which is basically same as normal git terminals.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Tehleel Mir
  • 743
  • 8
  • 27

2 Answers2

2

if it is a private project, you can simply go back in history (is if you never committed that thing ever). That can be easily done with a git reset --hard some-revision. Provide the id of the revision where you want to go back to and the revisions after it will be gone from the branch. BE CAREFUL: This gets rid of any changes that you might have uncommitted in your working tree.

As a last thing, if you have pushed the wrong revision to a remote branch, you will have to do a forced push to the remote to set it to the old revision:

git push -f some-remote some-branch

And that's it.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
2

here is a very good explaining question: How do I revert a Git repository to a previous commit?

Alternatively, you could use git revert HEAD or git reset. I would go for git revert. It's a command to rollback changes but not like the normal git reset. It does not delete the latest commit but determines how changes from your last commit can be undone. git revert expects a Commit-reference. When you use the reference HEAD, the newest commit will be undone.

Like I said, git reset can be used too: git reset [] []

This form resets the current branch head to <commit> and possibly updates the index (resetting it to the tree of <commit>) and the working tree depending on <mode>. If <mode> is omitted, defaults to --mixed. The <mode> must be one of the following:

(https://git-scm.com/docs/git-reset)

Janosch
  • 84
  • 1
  • 6
  • 1
    `revert is for weaklings`. XD Not really. This is a good alternate approach. The only thing that makes me rather go back in time than revert is that this is a private project/branch. Say, what is the point of keeping something that is _wrong_ in a branch if you are the only one that has seen it? If it were a problic branch and people are already using the busted work, well.... you can still reset --hard but it will be painful so it better be worth it. In this case (public branch, people are already using it) I would be rather inclined for reverting. But private stuff? `reset --hard` is the key – eftshift0 Jul 16 '21 at 17:03