0

I followed this link https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/ to push my project to a repo. I noticed that I don't have a gitignore file and that files such as .idea and .iml have been added to the repo. How do I add a gitignore file and delete these from the repo?

Gerald Tan
  • 91
  • 14
  • Possible duplicate of [Ignore files that have already been committed to a Git repository](https://stackoverflow.com/questions/1139762/ignore-files-that-have-already-been-committed-to-a-git-repository) – phd Jul 19 '18 at 18:08

2 Answers2

4

You need to add .gitignore file to your project root directory

Get .gitignore file for android from Android.gitignore It contains all possible ignore files which should be ignored from android project

Then follow these git commands to remove files from git repository. Local files will not be deleted

 git rm --cached .idea
 git rm --cached project.iml
 git rm --cached <file_name>

Remove all the files you want to ignore Then add the changes and commit it using following commands

 git add .
 git commit -m "ignoreable files ignored"

Then if you want to push those changes to remote just use

git push origin master

Edit 1

In case your remote branch has new commits and updated by other contributor you need to fetch it and rebase it before pushing

git fetch origin master
git rebase origin/master
git push origin master

So it will be synced with local branch

adityakamble49
  • 1,971
  • 18
  • 27
  • What files are not necessary? I know .gradle and .idea folders are not as well as any .iml files. What about the gradle folder or any of the gradle files? – Gerald Tan Jul 19 '18 at 17:35
  • Keep "gradle" folder it contains gradle wrapper. Don't keep ".gradle" folder. Also checkout this file, has list of all unwanted files for android project https://raw.githubusercontent.com/github/gitignore/master/Android.gitignore – adityakamble49 Jul 19 '18 at 17:37
  • Would it be the same to just go to my repo and delete it manually instead of through git commands? If so, do I need to pull in my project before pushing again? – Gerald Tan Jul 19 '18 at 17:41
  • You can delete it directly. It will work. But if you want to delete it from .git repository history you need to use git rm with --cached option – adityakamble49 Jul 19 '18 at 17:42
  • If i delete it from the repository history, and then I pull, will those files get deleted on my machine? – Gerald Tan Jul 19 '18 at 17:46
  • If you need to push changes just use push master it will get reflected on remote repo. If your remote files are changed then first fetch (not pull) the remote and apply rebase and push to remote – adityakamble49 Jul 19 '18 at 17:49
  • @GeraldTan Updated answer with Edit 1 section to show remote sync. BTW Consider upvoting and approving this answer if it helped :) – adityakamble49 Jul 19 '18 at 18:27
  • I'm a little confused as to what fetch and rebase will do. How is this different than pull? Also, will the files that I deleted in the repo get deleted on my local machine if I pull? – Gerald Tan Jul 20 '18 at 15:09
  • @GeraldTan Use `git rm --cached ` if you just want to delete it from repository and not local machine. Pull will directly pull code and merge into local code and may cause extra merge commit if remote is updated by someone else. So its recommended to use fetch and rebase to keep git clean :) – adityakamble49 Jul 20 '18 at 15:16
0

You need to add a .gitignore file at the root of your git repository. In that, you can put as a new-line separated list, the regular expressions of the paths you want git to ignore while looking for changes in your repository.

So for example, your git repo root directory has the following sub directories that you want git to ignore,

{git-root}/project/bin/debug {git-root}/project/bin/release

You can simply add {git-root}/project/bin/ in your .gitignore if these two directories are the only ones in your {git-root}/project/bin sub-directory.

Similarly, if in your {git-root}/project/bin sub directory, you have another directory tests and you do not want to ignore that, then you will have to add
{git-root}/project/bin/debug
{git-root}/project/bin/release

to your .gitignore file.

For an example, this is the .gitignore file of an android project I am working on. You can observe the syntax:

{project}/app/release/
{project}/app/debug/
{project}/api_objects/
{project}/.idea/caches/
{project}/api/
{project}/.idea/vcs.xml
{project}/.idea/inspectionProfiles/Project_Default.xml



After this, don't forget to add the .gitignore to your git repo using git add .gitignore. You can do all this manually or through any of the Git desktop clients like Sourcetree or Git Desktop. It is easier to do this using the UI tools I listed.

Rajan Prasad
  • 1,582
  • 1
  • 16
  • 33
  • How should the asker remove the files that have already been committed to the repository? They're still in the repository even now that they're ignored. – ojchase Jul 20 '18 at 16:33