问题
When I go to VCS>Commit changes
, I see the following screen:
I have probably pressed on some button or something by mistake, and since then I can't commit the whole project, just one file (the .iml file).
Why can't I see any other files that are in my project?.
回答1:
First Things First
As mentioned in the answer you’ve got before, you are not able to commit the whole project as you said. You are only able to commit changes that haven’t been recorded yet.
Bearing that in mind, if you see that there are no files in the “commit changes” screen there is no need to panic. The changes to your files are safe and sound, they have been recorded by git and you won’t lose those changes.
Nonetheless, disagree with the previous answer. From what I understood, you have made few changes to the project but now you can’t locate them. As you said in your question - you recall pressing on something by accident and now the files are “gone” from the list of files you are able to commit.
Possible Causes for the Problem
In that case, let’s try to figure out together where those changes can be.
There are number of possibilities:
- Your might have selected not to add the file to git
- You might have added the files to ignore list
- You might have stashed those changes
- You might have committed the changes but haven't pushed them to your remote repository
First, let’s cover the way git is recording changes -
Recording Changes to the Repository
Git is tracking your files in order to see if there were any changes to the files. If so, git will notice the changes and you will be able to commit them. All tracked files start as “unmodified”. Then, when you edit them they are marked as “modified”.
When you chose to commit the files, git will prepare them for the commit, asking you which file you wish to commit and those will be marked as “staged”.
After a commit is done, all the files are back to the “unmodified” status.
You can read the explanation from the “Git Basics” book for further information:
Remember that each file in your working directory can be in one of two states: tracked or untracked. Tracked files are files that were in the last snapshot; they can be unmodified, modified, or staged. Untracked files are everything else – any files in your working directory that were not in your last snapshot and are not in your staging area. When you first clone a repository, all of your files will be tracked and unmodified because Git just checked them out and you haven’t edited anything.
As you edit files, Git sees them as modified, because you’ve changed them since your last commit. You stage these modified files and then commit all your staged changes, and the cycle repeats.
Adding File to Git
By rule, Android Studio will mark the files in your directory according to their status in git.
- When the name appears in blue, that means the file is modified.
- When the name appears in green, that means the file is a new one and was added to the set of files tracked by git.
- When the name appears in red, that means the file is untracked.
- When the name appears in white/gray, that means the file is unmodified.
If you see that some of the filenames in your repository appear in red, you can:Right Click => Git => Add
and they will be tracked by git and you will be able to see them when you are trying to commit.
Git Ignore List
Gitignore list will let you specify intentionally untracked files to ignore.
The ignore list is basically a file containing the locations of the files in your working directory that you would like git to untrack. This list will prevent untracked files from being added to the set of files tracked by git (bearing in mind that git will continue to track any files that are already being tracked).
If this is your case, adding a file to the gitignore list will make it “disappear” from the list of files you are able to commit.
So, in order to get it back and commit it again, you will need to remove it from the gitignore list.
In order to do that, you will need to type git add -A
command into the git command line. This command will staged all files in your directory (new, modified, deleted) and it will solve your problem.
In addition, you should take a look at the Ignored Files list which is configured on Android Studio and make sure that files you are looking to commit aren’t there.
To access the Ignored Files list, go to Project => Settings => Version Control => Ignored Files
(Windows and Linux) or Android Studio menu => Preferences => Version Control => Ignored Files
(MacOS). In there, you will able to remove it from the “ignored files” list.
Stashed Changes
From the “Git Basics” book:
Often, when you’ve been working on part of your project, things are in a messy state and you want to switch branches for a bit to work on something else. The problem is, you don’t want to do a commit of half-done work just so you can get back to this point later. The answer to this issue is the git stash command.
Stashing takes the dirty state of your working directory — that is, your modified tracked files and staged changes — and saves it on a stack of unfinished changes that you can reapply at any time.
You might have stashed your files, and now they appear to be unmodified and you aren’t able to commit them.
In order to un-stash them (clicking on the main folder of your project):Right Click => Git => Repository => UnStash Changes…
, then select the most recent stash from the list and click on “Apply Stash”
.
If you see nothing on the list, or if the list does not contain the latest changes you have made, then this is not your case.
Committing Changes but Not Pushing Them
The last possible option is that you have already committed the changes but you haven’t pushed them to your remote repository so you don’t see them on the cloud/other computers.
As mentioned above, the git commit command will record changes to your file, but the most important thing in here is that is will be recorded on your local repository.
In order to update the remote repository as well you will need to push those changes too, using the git push command.
Android Studio offers two changes: commit changes and commit and push changes.
You might have chosen to commit the changes, and in that case:
- From the local repository perspective:
the changes are recorded, the files are back to the “unmodified” mode and you can’t see them anymore on the commit screen until the next changes you will make. - From the remote repository perspective:
there was no update since you didn’t execute the push command.
Here is a nice picture from Oliver Steele, that explains the git model and the commands:
In order to solve this, all you have to do is to push the changes to your remote repository by:Right click => Git => Repository => Push
. That simple.
Further Reading
Here are some helpful links:
- Git Basics - Working with Remotes
- Git Basics - Recording Changes to the Repository
- Git Tools - Stashing and Cleaning
Hope my answer has helped you in solving your problem :)
回答2:
Git only commits changes to files, so unless you change every file in your project, you won't be able to stage all files for a commit. The commit snapshot will contain the changed files as well as every other file, so don't worry about losing them.
回答3:
In my case, what happened was that my Android Studio preferences for Version Control had two "git roots", and one of them was wrong. That messed up it's algorithm for finding the modified files, I guess. See this StackOverflow post for more details.
Other things to check include the command line. Do a git status
, and do you see the modified files? In fact, this should be one of the first things to check. If you do see the files with git status
, the problem is probably a misconfiguration of your Android Studio settings for Version Control and / or for Version Control->Git.
Other things to check can also be found in the same Stack Overflow post I linked.
来源:https://stackoverflow.com/questions/38183734/i-cant-see-changed-files-on-commit-changes-screen-in-android-studio