Data report"State of code review 2024" is now liveRead the full report

How to add a repository in Git

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


In this guide, we will delve into the process of adding repositories in Git, specifically focusing on creating new repositories and connecting them to remote platforms like GitHub.

A Git repository is a storage location where a project's files and revision history are stored, allowing multiple contributors to collaborate on the same codebase. It enables version control, tracking changes made to files over time, and facilitates collaboration by providing tools for branching, merging, and managing different versions of the codebase. Git repositories can be hosted locally or on remote servers like GitHub, GitLab, or Bitbucket.

  • Local repository: The local repository refers to the collection of code files, metadata and other assets that are stored directly on your local machine. This includes the .git directory inside your project, where Git tracks the changes and stores the history of your project.

  • Working directory: The working directory is just one aspect of the local repository, and refers to the directory on your local machine where you are currently making changes to your project's files. It contains the files you are actively working on and allows you to modify, add, or delete files as needed. Changes made in the working directory can be staged and committed to the Git repository to track the progress of your project.

To add a new repository in Git first you must initialize it. This creates the .git subdirectory and configures the repository with all of the necessary Git metadata.

  1. Open your terminal or Git Bash.
  2. Navigate to your project directory:
    Terminal
    cd path/to/your/project
  3. Initialize the repository:
    Terminal
    git init
    This command creates a new subdirectory named .git that houses all necessary repository files – a Git repository skeleton. Your project is now officially a Git repository, but it only exists locally.

After creating a local repository, you'll next want to push your project to a remote server, such as GitHub, to collaborate with others or back up your work.

  1. Create a new repository on GitHub.

    • Go to GitHub.com, sign in, and click on the "New repository" button.
    • Name your repository and choose the visibility.
    • Click "Create repository."

    GitHub create repository screenshot

  2. Link your local repository to the GitHub repository:

    Terminal
    git remote add origin https://github.com/yourusername/newrepository.git

    Replace https://github.com/yourusername/newrepository.git with the URL provided by GitHub after creating your new repository.

    This establishes a connection between your local Git repository and the corresponding remote repository on GitHub, enabling synchronization between the two.

If you have pre-existing code that you want to add to this newly created repository:

  1. Add the files to the staging area with:

    Terminal
    git add .

    This command stages all your current directory changes for commit. The staging area in Git acts as a middle ground between your working directory and the commit history. When you use git add ., you place all your modified and new local files in the staging area, preparing them for inclusion in the next commit. This allows you to selectively choose which changes to commit, providing a level of control over your version history.

  2. Commit the changes:

    Terminal
    git commit -m "Initial commit"

    This command creates a snapshot of the changes staged in the previous step and saves them permanently to the local Git repository. The -m flag allows you to include a short message describing the changes made in this commit, which aids in understanding the purpose of the commit at a glance. In this example, "Initial commit" is the commit message, typically used for the first commit in a repository to signify the project's initial state.

  3. Push the changes to GitHub:

    Terminal
    git push -u origin main

    This command sends your commits from your local repo to GitHub. It also sets your local main branch to track the remote main branch.

  • Commit often: Make small, frequent commits that capture logical changes. This makes it easier to identify issues and roll back changes if something goes wrong.

  • Write clear commit messages: Your commit messages should be clear and descriptive to help other developers understand the history of the project.

  • Ignore unnecessary files: Use a .gitignore file to exclude temporary files and other non-essential files from being tracked by Git.

  • Review changes before committing: Use git status and git diff to review your changes before staging them.

For further reading on adding repositories in Git, see the official Git documentation.

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2