-1

While learning something (e.g. Spring framework etc.), I code in my local machine, usually using Eclipse IDE.

I want to transfer all the code in git, is it possible to do so? The existing code which I have, aren't any repository, they are simple Java projects which I created manually while learning a new concept.

Can anyone help how to put all these code in git so that it can be accessible from any machine.

CuriousMind
  • 8,301
  • 22
  • 65
  • 134
  • Possible duplicate of [Git for beginners: The definitive practical guide](https://stackoverflow.com/questions/315911/git-for-beginners-the-definitive-practical-guide) – phd Jan 14 '18 at 23:52

2 Answers2

2

Open the terminal and go into the root directory of the project you want to transfer.

  • First type git init.
  • Then add everthing to git with git add -A
  • Create the commit git commit -m "Transfer code"
  • Add the repository git remote add origin THIS_SHOULD_BE_YOUR_REPO_URL
  • Then push it git push -u origin master
Robert D. Mogos
  • 900
  • 7
  • 14
  • Thanks for your answer. I want to ask one more thing, consider that I have "maven" based Java projects in which all the dependencies are present in .m2 folder, is there a way to "push" all those dependencies as well? – CuriousMind Jan 14 '18 at 20:57
  • 1
    Indeed. By doing git add -A you’ll add everything to git – Robert D. Mogos Jan 14 '18 at 21:00
  • But the dependencies are present elsewhere in .m2 folder (which isn't in source directory) e.g. source code is C:\JavaStuff\Proj1 and maven downloads all jars in C:\Users\XYX\.m2 ; so if I push the code of Proj1, I have to be in Proj1, it isn't possible to push the related dependencies which are present in different directory altogether. I hope you got what I am asking for. – CuriousMind Jan 14 '18 at 21:05
  • 1
    Gotcha. You can’t push it together in this case. But why would you need to push them? When you checkout the project somewhere else, you’ll just install again the dependencies – Robert D. Mogos Jan 14 '18 at 21:07
  • Hmm, so we can push code in this case ; and when we check out the code, all these dependencies need to be downloaded as part of the build process. But at least code is centrally present :-) – CuriousMind Jan 14 '18 at 21:08
  • I wanted to have "portable" code which I have written (one code snippet at a time) so that I can use it in any machine, and having all the dependencies as part of the repo. But at least code would be readily available – CuriousMind Jan 14 '18 at 21:10
  • 1
    @CuriousMind Man, do not push dependencies to git... That's why they are dependencies - simply push `pom.xml` in your project and do a mvn package on a new machine to download the new dependencies. That's similar to pushing .class files... Do not transfer anything that you can generate/download from a better and faster source is the rule of thumb. Edit: Alternatively, you could set up your local maven nexus and download the dependencies from one computer to another. – halpdoge Jan 14 '18 at 21:18
1

It sounds like you'll want to create Github repos for each of these existing projects and then pushing them up. Github actually has a pretty great guide on doing just that :)

https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/

Gonzodamus
  • 11
  • 3