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?
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.
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.
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
git reset --soft "HEAD^"
this will remove the latest commit.
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.
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.
Related
Hi i'm here to asking you, how to push my commit/chage(s) when i have another commit and i forgot to pull the commit from github.
This is the chronology: when i'm no have another work, i decide to make own project and i use git to be my vcs, and then i make my project to be repository.
A few day's later, i decide to remoting my github.
Everything is alright until the problem come. In one day, i'm edit my README.md and save the change, in git same to but a different is i updating my source code and save the change. After that i'm push my commit and this is happent.
Screen shoted the problem
But in here, i'm not realize i'm forgot to pull a commit from github until i'm realized that.
So please help me! I stuck in this condition! Please!
And i'm sorry if my English is bad
So, basically your solution might be to pull the changes from GitHub (that is, if I understand correctly you have did some changes to you remote Github - updated your README.md - not from your local - now you want that change to be reflected in your local and you need to push your new changes in the local to the remote).
The best solution would be to first add all your changes to staging and then committing by doing:
git add .
git commit -m "Your commit message (One-liner of your change)"
and then do a rebase as follows:
git pull --rebase
Rebasing will pull the changes from the remote, and re-apply your local changes on top of it, thus your local repo is now in sync with the remote repo (they are essentially in the same state). Now you can do the push:
git push <REMOTENAME> <BRANCHNAME>
After updating I am getting this notification.
I understand your problem follow these steps to recover your local deleted file and code:
Go to toolbar: VCS->Git->UnStash Changes
From UnStash Changes pick recent Uncommitted changes
Click to view button
You will get a list of all files which get affected
Now you can merge manually those files which are affected and one thing you have to do manually add deleted files again.
hope you are having a good day.
I want to know a small thing about pull/push in git repo.
Below is the representation of my code/project.
Blue: This is my code which is worked on and without commit i pulled the red code from git.
Red: This is the older version of blue code.
Yellow: this is the code which i have now
So i have worked on blue code but without commit or push i pulled red code, so it over writes my blue updated code and merged as yellow code. So now i want to get back my blue code, is it possible? i searched in my project folder but i cant find it. i search on my git branch code too but it wasn't there too.
Thanks for the read, if you know how to revert that pull request it will save my couple of hours. Thanks again
And by the way this is an android project and code is in bitbucket.
[UPDATE] - Solved-
Thanks for the response. I got my blue code back using Android Studio Local History tool. Reverted all the changes that i have made.
Try git lola9 (if you've set up the alias, or whatever way you prefer to view your commit history), and you should see the commits you pulled. Alternatively, the console output of the git pull should also include the commits you pulled.
Once you have that, what I would do is:
Note the number of commits you want to git rid of
git stash your changes,
git reset head~n, where n is the number of commits we figured out earlier. This will 'un-commit' the changes you pulled
git co . & git reset as needed to 'lose' all those now un-commit-ed changes that you've pulled down
git stash apply to retrieve your changes (rather than pop in case somethings gone wrong, you don't want to dirty your changes)
Some times when I try to undo changes in Android Studio I get this:
Can't Undo
Following files affected by this action have already been changed.
That's nice but I don't care about the files affected. I only care about the current file. I will deal with those files later. Is there a way to force Android Studio to undo my textual changes in the current file and worry less about what's in other files? This is one of those situations when devs think they know better than the user. Makes me scared of using that undo button tbh.
The reason for this is
You have first done some action(A) which has affected some set of files including some file called F.
Then you have done some changes which has affected another set of files which including some files on previous set but not F.
Now you go to file F & try to undo. The action(last) on the top of the action stack for F is A. But when you request it to revert back, it has to revert back with all the other files because you are reverting the action not just text. But since some set of other files has over changed with your later actions, your request cannot bypass them. So you are getting message.
Can't Undo
Following files affected by this action have already been changed.
So you want only to revert your changes locally to F(or some folder). For that purpose you have a tool called Local History.
You can go to your file and then VCS -> Local History -> Show History. Or Right click your file or folder -> Local History -> Show History.
You can see your multiple revision histories and restore anyone you want. :))
I have a problem with merging branches in Android Studio.
If I want to merge my local master with my remote master, the Android Studio alerts me that there were changes between the remote and the local, and asks me to commit and push the changes. but when I try to push the changes it says that I need to save or stash the changes.
Is there an easy way to work with the git in Android Studio?
It sounds like you have unstaged changes. I don't know about how to do this from android studio, git status should show you what state things are in. If you have unstaged changes, first you need to 'stage' or add your changes (git add .) then create a commit from the changes you added (git commit).
Next, it sounds like there are changes in the remote master that you want to incorporate into your local state. Before you do, if it's been a while since you fetched/pulled, you might want to get the latest master from the server using git fetch. Then you either need to combine your changes with the changes from the remote master (git merge origin/master), or re-apply your changes on top of the remote master (git rebase origin/master).
Alternatively, if you haven't made any commits yet, and all your work is in the form of unstaged changes, you can stash them (git stash) to temporarily store them away, then use git pull to update to the latest version of master, followed by git stash pop to apply the changes you previous stored.