Home > OS >  How to link .gitignore to local project when generated through project creation on github?
How to link .gitignore to local project when generated through project creation on github?

Time:11-10

I need to commit an Android project to git.

I've created a new project on Github and selected the selections for adding .gitignore with all the Android files to be ignored.

Now I need to commit the Android project for the first time into that repo:

Step 1. go to the project location and do git add .
Step 2. git remote add origin https://github.com/projectlocation
Step 3. git commit - m "My First Commit"
Step 4. git push -u origin master

Following these steps question is how do I link the .gitignore file so those files don't get pushed? Or does it automatically not push those files?

CodePudding user response:

If you created a new Android project, then a .gitignore file would have been generated. Just make sure that the .gitignore that is in your local project is the same as the one that is on your GitHub repo.

If the project is old, and there is no .gitignore file, you can create a new one in the root directory of the project and then you can do it two ways:

  • Manually copy the content from .gitignore that is on GitHub and put it into local .gitignore (not recommended)

  • (Recommended) Set your remote with

git remote add origin <url>

and then do

git pull

which will automatically import the .gitignore, LICENSE or README.md file that is there on GitHub, to your local project.

Then you can commit and push the normal way, and the unwanted files will be ignored.

CodePudding user response:

Although @Gourav's answer is good, I'd like to give a more thourough explanation:

If the .gitignore is created before an ignored file is committed for the first time, it is done automatically (that should be the case for your new project).

If you first do a git add someFileThatShouldBeIgnored.xxx (and presumably also a git commit), your file will be added to Git even if it appears in the .gitignore as the file was versionned before it was told not to.

To remove a committed file that should not have been:

  1. Add that file to .gitignore.
  2. Use git rm --cached someFileThatShouldBeIgnored.xxx to un-version it (--cached keeps the file in your project, if ommitted, the file will be deleted from your local directory as well).
  3. Commit those two changes together.
  •  Tags:  
  • git
  • Related