0

I want to commit only a .java file to an already existing repository. I am using github for a class, and we are each given access to a private repo containing project information. I am supposed to add only a .java file to this repo, but I can't figure out how to do this. I have come close: I committed only the .java file, but it is inside a directory with the same name as my eclipse project.

Is there a way to commit only the .java file, without any of the project information, while keeping the project usable?

Thanks.

edit: My goal is to have \git\repoName\MyFileName.java as well as keeping the files already in \repoName\ and keeping the MyFileName.java in a usable eclipse project.

packapotatoes
  • 33
  • 1
  • 7

3 Answers3

0

Is there a way to commit only the .java file, without any of the project information, while keeping the project usable?

yes, you will need to create a .gitignore file, and put the rest of your project into the gitignore file, (and also gitignore itself), that way, the only files that are tracked by git are the .java files.

As for the files being inside the directory with the same name as your project, take a look at where you cloned the git repo to, and make sure it is on the same level as your project files.

Take a look at this documentation http://git-scm.com/docs/gitignore https://help.github.com/articles/ignoring-files/

Andy N
  • 392
  • 3
  • 8
  • 24
  • Ok so here's where I'm at: I have the repo pulled from github and opened in 'Git Repositories' window of eclipse. It has a .gitignore and a few .txt folders for the project. That folder is called 'Assignment1'. Then I have my project open in 'Project Explorer', I right click->Team->Share Project then add it to the 'Assignment1' repo. The problem is now my .java file is in Assignment1/ProjectName/src/packageName/file.java What I would like is 'Assignment1/file.java' and still be able to use it with my project properly. – packapotatoes Sep 23 '15 at 00:13
0

I think its much simpler than what you are expecting :)

You can do, git add, docs:

 git add yourFileName.java

Now you can commit the .java file

git commit -m "I am updating only .java"

You can either reset or let it be as it is!!

git reset

reset will not have any history. So, be careful if you want to reset. In this scenario I think you dont have to reset. See here for some help.

See my answer on how to push/pull changes from eclipse into github. Let me know for any help on how to interact with github on eclipse.

You want to remove a subtree and create a new repo? This is definnitely possible but you have to be careful. Creating a subtree only for file like yourFileName.java surprizes me a bit, but why?

For whatever reasons, see this for an easy way, and the original post is this Detach (move) subdirectory into separate Git repository

This blog might also help you.

Community
  • 1
  • 1
  • 1
    I'm confused, where does the yourFileName.java have to be when I use add? – packapotatoes Sep 23 '15 at 00:51
  • It has to be in your repo, what ever repo you are currently dealing with. Don't hesitate for any questions. I have gone through this, its confusing, I spent whole day to figure this. So, ask me, I can help you. –  Sep 23 '15 at 02:13
  • 1
    So I put the entire project into the repo, then I add only the `fileName.java` file. I can commit it fine, but it retains the file structure of the project. So I end up with something like this: `repoName/eclipseProjectName/src/fileName.java` What I would like is to end up with just `repoName/fileName.java` Is this possible? Thanks for your help so far! – packapotatoes Sep 23 '15 at 02:43
  • @packpotatoes Yes its possible. Updated the post :) –  Sep 23 '15 at 14:47
0

I figured it out!!

The key was to add a source. You can do it when you create a project, one of the options will say "Link Source" or something along those lines.

You can also do it after a project is created:

Right click the project in Project Explorer. Go to Properties -> Java Build Path (on the left) -> Source (the first tab) -> "Link Source..."(button on the right)

Then just browse for the git repo folder that you already have and you're good to go! Just create new class files within /repoName in Project Explorer.

If anyone needs clarification or anything, let me know.

packapotatoes
  • 33
  • 1
  • 7