KiZo
KiZo

Reputation: 543

How to reset to my last commit in Android Studio

I commit changes to git from Android Studio, after that I have made some changes in my project that gives me errors, and now I want to get back that commited version that has no errors. How can I do that?

Upvotes: 49

Views: 58136

Answers (5)

MMG
MMG

Reputation: 3268

Suppose you want to commit project changes. When you want to commit files Android Studio will show you which files have been changed. See them and you can convert every file you want to last commit.

Upvotes: 0

Amin Keshavarzian
Amin Keshavarzian

Reputation: 3943

According to this great article on jetbrains webiste here:

Reset a branch to a specific commit If you notice an error in a set of recent commits and want to redo that part, you can roll back your repository to a specific state. This is done by resetting the current branch HEAD to a specified commit (and optionally resetting the index and working tree if you prefer not to reflect the undo in the history).

  • Open the Version Control tool window Alt+9 and switch to the Log tab.
  • Select the commit that you want to move HEAD onto and select Reset Current Branch to Here from the context menu.
  • In the Git Reset dialog that opens, select how you want your working tree and the index to be updated and click Reset:

Soft: all changes from commits that were made after the selected commit will be staged (that means they will be moved to the Local Changes view so that you can review them and commit later if necessary).

Mixed: changes made after the selected commit will be preserved but will not be staged for commit.

Hard: all changes made after the selected commit will be discarded (both staged and committed).

Keep: committed changes made after the selected commit will be discarded, but local changes will be kept intact.

Upvotes: 1

minato
minato

Reputation: 2332

git reset --soft "HEAD^"

this will remove the latest commit.

Upvotes: 8

Tim
Tim

Reputation: 43314

To undo your latest changes and reset to the most recent commit:

Go to VCS -> Git -> Reset HEAD..

Change Reset type to hard to remove those changes.

It will look like this. You can validate the reset before you do it if you want.

enter image description here


What happens if you click Validate?
A screen will pop up that shows the changes that were made in the commit you are about to reset to. You can view diffs per file that show what the commit changed in that file. It's more or less equal to what $ git show in a terminal would do.

Contrary to what I assumed before, it does not show what files will be affected when you perform the reset.

Upvotes: 84

DavidL
DavidL

Reputation: 1150

Just get your commit id using git log. Then you can use (with 0d1d7fc32 for example):

# This will detach your HEAD, that is, leave you with no branch checked out:
git checkout 0d1d7fc32

or if you don't want to save your changes (hard reset):

# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32

If you want to go back to the last commit (without saving changes), then no need to get the id, go for:

git reset --hard HEAD

From this post

Upvotes: 18

Related Questions