Video

Watch the full video on YouTube:

NestJS Git Setup

Why Version Control Matters

Before you write another line of code, setting up version control is essential. Git tracks every change you make, allows you to experiment with branches without risk, and enables collaboration with other developers. GitHub hosts your repository online, providing backup, code review tools, and deployment integrations.

This guide walks through installing Git, creating a GitHub repository, configuring SSH authentication, and pushing your first commit.

Step 1: Install and Configure Git

If you have not installed Git yet, download it from the official website:

https://git-scm.com

After installation, configure your identity. This information is attached to every commit you make:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Set a few sensible defaults:

git config --global init.defaultBranch main
git config --global pull.rebase true

Step 2: Initialize the Repository

Navigate to your NestJS project directory and initialize a Git repository:

cd my-nestjs-api
git init

This creates a .git folder that tracks all changes in your project. Git does not automatically track everything. You need to tell it which files and folders to track using the staging area and commits.

Step 3: Create a .gitignore File

A .gitignore file tells Git which files and folders to ignore. The NestJS CLI automatically creates one for you, but it is worth understanding what it contains:

node_modules/       # Dependencies, should never be committed
dist/               # Build output, generated by the compiler
.env                # Environment variables with secrets
*.log               # Log files

Never commit node_modules or build artifacts. They can always be regenerated from package.json and your source code.

Step 4: Create a GitHub Repository

Go to https://github.com and sign in. Click the plus icon in the top right corner and select “New repository”.

Give your repository a name that matches your project. Do not initialize it with a README, .gitignore, or license since your NestJS project already has these files.

After creation, GitHub shows you commands to connect your local repository. Copy the SSH URL, not the HTTPS one.

Step 5: Set Up SSH Authentication

SSH is the recommended way to authenticate with GitHub. It is more secure than HTTPS with a password and eliminates the need to enter credentials every time you push.

Check for Existing SSH Keys

ls -la ~/.ssh

If you see files named id_ed25519 and id_ed25519.pub, you already have an SSH key pair.

Generate a New SSH Key

ssh-keygen -t ed25519 -C "your.email@example.com"

Press Enter to accept the default location. You can optionally set a passphrase for additional security.

Add the Key to the SSH Agent

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Add the Public Key to GitHub

Copy your public key:

cat ~/.ssh/id_ed25519.pub

Go to GitHub Settings > SSH and GPG keys > New SSH key. Give it a descriptive title and paste your public key.

Test the Connection

ssh -T git@github.com

You should see a message confirming that you are authenticated.

Step 6: Stage, Commit, and Push

Now that Git is configured and GitHub is ready, add your files and make the first commit:

# Check the status of your files
git status

# Add all files to the staging area
git add .

# Create the first commit
git commit -m "Initial commit: NestJS API project scaffolded"

Add the remote repository and push:

git remote add origin git@github.com:your-username/your-repo-name.git
git push -u origin main

The -u flag sets the upstream so future pushes can be done with just git push.

Understanding the Basic Git Workflow

Git follows a three step workflow for saving changes:

  1. Working Directory: You modify files as you code
  2. Staging Area: You select which changes to include in the next commit using git add
  3. Repository: You save the staged changes with git commit, creating a permanent snapshot

The basic commands you will use daily:

git status        # See what has changed
git add <file>    # Stage a specific file
git add .         # Stage all changes
git commit -m "description"  # Commit staged changes
git push          # Send commits to GitHub
git pull          # Get latest changes from GitHub

Why SSH Instead of HTTPS?

Using SSH with GitHub has several advantages over HTTPS:

  • No password prompts: Once configured, authentication is seamless
  • More secure: SSH uses public key cryptography instead of passwords
  • No personal access tokens: GitHub deprecated password authentication for Git operations; SSH avoids needing to manage tokens
  • Works in scripts: SSH authentication works non interactively, which is useful for deployment scripts and CI/CD pipelines

What We Accomplished

Your NestJS project is now version controlled and backed up on GitHub. You have SSH authentication configured for secure, password free access. From here, every change you make can be tracked, reviewed, and rolled back if needed.

Recommendations

This post is part of a NestJS API development series. Check out the other entries:

NestJS API Development Guide 03: First ProjectIntroduction to Neovim