chtefi
3/13/2015 - 9:39 PM

Transform a classic node project folder into a git repo

Transform a classic node project folder into a git repo

## Project in ./project (with its classic packages.json and subfolders node_modules etc.) non-git

# The simpler solution would be to
# - create a remote repo on github.com
# - clone it
# - add your files into
# - commit/push
#
# clone inject already all the necessary config to work

# If you want to do it manually, here is a guide that explains how

```shell
$ pwd                               # in the project folder
/project
```
```shell
$ git init                          # create the .git
Initialized empty Git repository in c:/test-manual/.git/
```
```shell
$ git status                        # we can check each step what's going on
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        app.js
        node_modules/

nothing added to commit but untracked files present (use "git add" to track)
```

```shell
$ git add .                         # add every files
```
```shell
$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   app.js
        new file:   node_modules/timeago/.npmignore
        new file:   node_modules/timeago/README.md
        new file:   node_modules/timeago/package.json
        new file:   node_modules/timeago/timeago.js
```
```shell
$ echo "node_modules" > .gitignore  # All the files in node_modules, you have to ignore them
$ git reset                         # restart from the beginning
$ git add .                         # now it's good
$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   .gitignore
        new file:   app.js
        
```
```shell
$ git commit -m 'Initial commit'    # commit the adds
[master (root-commit) 16a3a5d] Initial commit
The file will have its original line endings in you
The file will have its original line endings in you
 2 files changed, 2 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 app.js
 
# then you realise you don't have a github.com url for you repo
# you need to create it on github.com itself.
# when it's done, we need to associate (and merge) the remote repo and our local repo
```

```shell
$ git remote add origin https://github.com/chtefi/test-manual.git     # let's connect to the remote repo
$ git push --set-upstream origin master                               # and push!
To https://github.com/chtefi/test-manual.git                          # it doesn't work, github created files on my remote repo
 ! [rejected]        master -> master (fetch first)                   # I need to rebase my local repo
error: failed to push some refs to 'https://github.com/chtefi/test-manual.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

$ git pull                          # hint: git pull
warning: no common commits
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
From https://github.com/chtefi/test-manual
 * [new branch]      master     -> origin/master
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> master

$ git branch --set-upstream-to=origin/master master   # I'm on master sir
Branch master set up to track remote branch master from origin.

$ git rebase                        # Now, I can rebase  if I want
First, rewinding head to replay your work on top of it...
Applying: Initial commit

```shell
$ npm init                          # add "repository": { ... } config into package.json (it does not override, it will just add this section
...
git repository: (https://github.com/chtefi/test-manual.git)
...
$ git add .
$ git commit -m 'More files'        # add the new files from github
$ git push                          # push to the remote repository
```