2

I usually add an existing project to Github using the following documentation.. https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/

The documentation also says.. To avoid errors, do not initialize the new repository with README, license, or gitignore files. You can add these files after your project has been pushed to GitHub.

So now when I push the changes for the first time all the unnecessary jar files/target directory/ide files get pushed. I then manually exclude those files by adding .gitignore like this..

$ echo '.idea' >> .gitignore
$ git rm -r --cached .idea
$ git add .gitignore
$ git commit -m '(some message stating you added .idea to ignored entries)'
$ git push
  1. Can I prevent adding all these unneeded files first time itself?
  2. After deleting the unneeded files (target,jar,ide files) and adding them to .gitignore, I have verified that they no longer exist in github, but when I clone the url its still 100MB, the actual download size is however less than 1MB? Can someone clarify this.
iltempo
  • 15,718
  • 8
  • 61
  • 72
Himalay Majumdar
  • 3,883
  • 14
  • 65
  • 94
  • I would suggest you read at least the first few chapters of an introductory Git text (such as the excellent one on the Git website). Your issue is that you blindly followed the steps in your link (specifically `git add .`) without realising you could be selective about what you add. – JBentley Dec 31 '15 at 15:42
  • What JBentley says is true and I generally don't recommend `git add .`, but IMO it doesn't answer the right question. There is _nothing_ wrong with including a `.gitignore` file in your initial push to GitHub. What _is_ important is that you don't let _GitHub_ create a `.gitignore` when you create your repository. See below for details. – ChrisGPT was on strike Dec 31 '15 at 16:44
  • 1
    @JBentley Dint really follow the steps blindly and I have been using git for a while. I am aware what `git add .` can do, but there are so many different kinds of files in my project and I don't want to add them manually one by one thru command line (I prefer command line). Others have suggested its ok to add .gitignore to my local and use it during the push, which I like. – Himalay Majumdar Jan 04 '16 at 12:38

3 Answers3

4

Do not include the files you will not need in repository when you make an initial commit locally. I.e.:

  1. cd project_directory
  2. git init
  3. git add <everything_except what you do not need>. You can move/delete the files that should not be in repository if you don't want to use the .gitignore.
  4. commit and push.

See also this answer if you want to wipe out the files from repository completely.

Community
  • 1
  • 1
Zloj
  • 2,235
  • 2
  • 18
  • 28
  • I did try 'git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch target'. I verified in github they don't exist anymore. When I download my repo as zip in github its less than 1MB, however when I do a git clone, its still 100MB. After cloning I found `MyRepo/.git/objects/pack/pack-b6b2b82ecd58c551c3648b9ca97e4f8b29rrt3c2.pack` is 99.8 MB. How can I get rid of this? – Himalay Majumdar Jan 04 '16 at 12:46
4
  1. One reason not to add a .gitignore file in the first place is to make sure all the files are tracked. That way you cannot accidentally leave out an important directory for example. If you know what you are doing -- add a corresponding .gitignore template before committing.

  2. The .idea directory is still existing in the git history. There is an extensive documentation about removing sensitive data from your repository on Github.

iltempo
  • 15,718
  • 8
  • 61
  • 72
  • 1
    "The only reason not to add a `.gitignore` file in the first place is to make sure all the files are tracked." I strongly disagree with this. There's nothing wrong with some files being untracked, and no reason to ensure that they are. This recommendation applies to the process of creating a repository _on GitHub_. If you use the "initialize project with README" option and you also have an existing local repo you'll have to force push or otherwise deal with two conflicting histories later. However, having a `.gitignore` locally and pushing it is perfectly fine. – ChrisGPT was on strike Dec 31 '15 at 16:09
  • @Chris That's what I'm saying. – iltempo Dec 31 '15 at 16:10
  • Not really. Saying the "only reason not to add a `.gitignore` file in the first place is to make sure all the files are tracked" ignores the much more important reason of avoiding an unnecessary merge conflict. – ChrisGPT was on strike Dec 31 '15 at 16:13
  • @Chris How would you avoid a merge conflict by not adding a `.gitignore` file? – iltempo Dec 31 '15 at 16:18
  • Explaining in enough detail in a comment is difficult, but they key is _where_ that file gets created. _Don't_ create it directly in GitHub using the new repository wizard, but feel free to create it locally. I have added my own answer. Hopefully that will make it clear. – ChrisGPT was on strike Dec 31 '15 at 16:31
  • This is very Github specific which wasn't reflected in the question. I updated it accordingly. – iltempo Dec 31 '15 at 16:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99418/discussion-between-iltempo-and-chris). – iltempo Dec 31 '15 at 16:48
  • Thanks for your answer, I still have one concern though. I did follow the steps to remove files from my repo. I verified in github they don't exist anymore. When I download my repo as zip in github its less than 1MB, however when I do a git clone, its still 100MB. After cloning I found `MyRepo/.git/objects/pack/pack-b6b2b82ecd58c551c3648b9ca97e4f8b29rrt3c2.pack` is 99.8 MB. How can I get rid of this? – Himalay Majumdar Jan 04 '16 at 12:45
  • @HimalayMajumdar May you add this issue as a new question please? That way you will get help from others too. – iltempo Jan 04 '16 at 12:53
  • @iltempo Sure, its here http://stackoverflow.com/questions/34592913/removing-unwanted-files-from-git-github – Himalay Majumdar Jan 04 '16 at 14:05
  • For the sake of clarification - files in `.idea` directory exist, but directories in git are not tracked explicitly. They are rather track implicitly, meaning that directory exists in repository as long as some file is stored in it. – Zloj Jan 05 '16 at 15:31
4

To avoid errors, do not initialize the new repository with README, license, or gitignore files. You can add these files after your project has been pushed to GitHub.

This is somewhat misleading. There is nothing whatsoever wrong with having these files in your local repository and including them when you push, even the first time.

Here is a slightly clearer recommendation:

When creating your repository on GitHub, do not use the "Initialize this repository with a README" option, shown below:

GitHub's "Initialize this repository with a README" option

This option is meant for brand new repositories, but will give you a headache if you use it when you're planning to push an existing repository. Here's why:

  1. When you use this option you create a commit directly in GitHub. This commit does not exist anywhere in your local project.
  2. When you push your existing project Git will not be able to resolve its history with the lone commit you created when initializing. The two repositories share no commits, and therefore cannot be automatically resolved against each other.

However, if you wish to include a README, .gitignore, or license file in your local repository and include these files when you push please feel free to do so.

Community
  • 1
  • 1
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257