34

How do I add an existing java project in eclipse to git?

I noticed when creating the project there was an option to add to source control but I can't find this option for an existing project. Is this possible from the git plugin in eclipse or must it be done from the command line? (I am using a Mac)

1

2 Answers 2

48

Follow these steps

  1. Right click on the project
  2. Select Team -> Share
  3. You will be prompted to select the tool you prefer
  4. Click Create -> Browseif you already have one
  5. Select git and go on
3
  • 2
    too easy! i like to punish myself on the command line... :P git init; git commit -a -m "blah"; git remote add origin [repo_url]; git push origin master; Aug 26, 2014 at 21:31
  • 3
    personally I prefer git gui + command line to egit ... it's not punishing yourself, it's beeing in control..:)
    – vratojr
    Aug 27, 2014 at 12:51
  • @coffeeaddict can you add your comment as an answer?
    – dmeehan
    Jul 13, 2016 at 21:22
29

An alternative route (I have found the built-in Eclipse tool to be finicky at times):

  1. Open up your terminal
  2. Navigate to your project directory
  3. Type git init to create a repository
  4. Assuming you already have files in that folder, type git add --all to add all your files to the repository (Note: if you skip this step, you will have an empty repository. You can also use git add filename to add only specific files/folders)
  5. Type git commit -m "your message here" to perform your first commit

At this point, you have a brand new local repository containing your files! The following steps will hook it up to a remote repository.

  1. Create your remote repository (using GitHub as an example, simply click the New button, and follow the prompts.
  2. Open up your terminal and navigate to your project directory
  3. On the page for your repository, you should see an HTTPS link ending in your-repository-name.git
  4. Paste git remote add origin into your terminal, followed by that HTTPS link
  5. Paste git push -u origin master into your terminal (Note: these last two steps are shown on the GitHub new project page as well, for easy copy-and-pasting into your terminal)

Now you have a local repository connected to a remote repository, ready to use! All Eclipse projects exist somewhere in your file system, and can easily be accessed just like any other folder you might want to turn into a repository.

I do realize you asked to avoid the command line, but this is a relatively simple command line task, and learning to be somewhat familiar with how to use your command line can pay big dividends later on.

4
  • 2
    Thanks for the excellent answer, it served me very well to understand the process!! May 2, 2018 at 14:55
  • 4
    What this doesn't (appear to) do is solve the problem of "what should be in gitignore for an eclipse java project?" So this is a generic "how do I create a git repo" answer rather than an answer to the question, which was "how do I set up an existing eclipse java project". Jan 4, 2019 at 21:56
  • 1
    Just a small update on to the existing steps. In case, if one has already created the git repo and has some files in it like ReadMe.txt or .gitIgnore. Then the "git push -u origin master" will fail. To resolve that, one should run the below commands first : 1) git pull origin master --allow-unrelated-histories. (which will pull all the history from the repo) now your local/remote are in sync. 2) Now run : git push --set-upstream origin master Aug 2, 2021 at 10:15
  • 1
    Note: GitHub recently changed its Authentication policy. Users have to use Personal Access Token (P.A.T) instead of password while doing pull or push or clone etc. Take a look at: docs.github.com/en/github/authenticating-to-github/…
    – Shadyar
    Aug 14, 2021 at 23:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.