Git: Create a New Repository

When starting a new project, one of the first things you'll find yourself needing to do is creating a new Git repository. This not only helps you share the project with coworkers, or publicly, but it's also a great way to track updates to a young project that is bound to go through significant changes in its early life. With a full history of those changes, and helpful features like branches, you can more easily test out different designs/implementations for the project without losing previous work.

As with anything in Git, there are a few ways to do what we want, depending on what you need. In this article we'll show a few ways to create a new repository.

Creating a Repository from Scratch

In order to start this new repo you'll want to use the init command. To do this, go to your project directory and type the following:

$ git init
Initialized empty Git repository in /Users/scott/projects/my-new-project/.git/

This can be done at any time in your project, whether there are already files there or not. Once you've added some files and feel like you're at a point in which you should commit your code, you can use the add command to add your files to the new repo:

$ git add .

This will stage the files in your current working directory for the next commit. After staging our files, we can verify that they are ready to commit by using the status command:

$ git status
On branch master

Initial commit

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

    new file:   index.js
    new file:   package.json

Finally, you can commit the files to the new repo using commit:

$ git commit -am "Initial commit"
[master (root-commit) 37a82f5] Initial commit
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 index.js
 create mode 100644 package.json

Here we also use the flags -a and -m, which do the following:

  • -a: Commit all staged files
  • -m: Use the following string as the commit message

To verify

Creating a Repository for an Existing Project

Creating a repository for an existing project is virtually the same process as creating a repository for a new project. Just as in the last section, you'll want to execute the same general commands:

$ git init
Initialized empty Git repository in /Users/scott/projects/my-new-project/.git/
$ git add .
$ git commit -am "Initial commit"
[master (root-commit) 37a82f5] Initial commit
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 index.js
 create mode 100644 package.json

Note that the git add . command will add all files in the current working directory, including those in subdirectories. However, it will also add some files or directories that you might not want, like node_modules for a Node.js project. In order to avoid including unwanted files/directories like this, you should create and commit a .gitignore file.

Backing up a little, our commands to create a repository for an existing project may look more like this:

$ git init
Initialized empty Git repository in /Users/scott/projects/my-new-project/.git/
$ echo "node_modules/" > .gitignore
$ echo "package-lock.json" >> .gitignore
$ git add .

Now when these "ignored" items are present in your project you don't need to worry about accidentally staging and committing them. For example:

$ npm install
$ ls -l
total 40
-rw-r--r--   1 scott  staff      0 Sep 16 15:15 index.js
drwxr-xr-x   4 scott  staff    136 Sep 16 15:27 lib
drwxr-xr-x  52 scott  staff   1768 Sep 16 15:32 node_modules
-rw-r--r--   1 scott  staff  14233 Sep 16 15:32 package-lock.json
-rw-r--r--   1 scott  staff    362 Sep 16 15:32 package.json
$ git add .
$ git status
On branch master

Initial commit

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

    new file:   .gitignore
    new file:   index.js
    new file:   lib/api.js
    new file:   lib/db.js
    new file:   package.json

$ git commit -am "Initial commit"
[master (root-commit) 9646a88] Initial commit
 5 files changed, 19 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 index.js
 create mode 100644 lib/api.js
 create mode 100644 lib/db.js
 create mode 100644 package.json

Notice that our ignored file (package-lock.json) and directory (node_modules) are not included in staging, even after executing git add ., or the commit.

Note: Using .gitignore is not only recommended when creating a repo for an existing project, but we reference it here because you're more likely to need it for an existing project, as more mature projects tend to accumulate files that shouldn't be tracked or shared between users.

Cloning an Existing Project

Although you aren't technically creating a new repo, in this use-case you can clone an existing one, preventing you from needing to git init one yourself. This is very common for when you're needing to make updates to an existing project, or maybe if you want to clone a skeleton project to help get you started.

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

In order to clone a repo, you'll want to use the clone command:

$ git clone <repo-url>

This will copy all of the contents of the repository to your current working directory under the name of the repository. For example:

$ git clone [email protected]:scottwrobinson/twentyjs.git
Cloning into 'twentyjs'...
remote: Enumerating objects: 48, done.
remote: Total 48 (delta 0), reused 0 (delta 0), pack-reused 48
Receiving objects: 100% (48/48), 9.35 KiB | 0 bytes/s, done.
Resolving deltas: 100% (22/22), done.
$ ls -l
total 0
drwxr-xr-x  9 scott  staff  306 Sep 16 15:55 twentyjs

There are a number of other options and use-cases for this command, but they aren't in the scope of this article, so we won't cover them here. For more info, check out the article Git: Clone a Repository.

Last Updated: August 28th, 2023
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms