Creating a New Git Repository Using PowerShell

Initialize a local repository, make your first commit, and push to a remote — from PowerShell.

For this example, the directory git and subdirectory portfolio will be used — portfolio will become the new repository.

Create the Directory

New-Item -Path '.\Git\Portfolio' -ItemType Directory

Some individuals use the -Force flag to ensure that both directories are created if they don't already exist. This has not been necessary in practice, so it is not included here.

Confirm with ls:

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2/22/2024  10:18 AM                Portfolio

Initialize the Repository

  1. Change into the directory:
    cd Portfolio
  2. Initialize the repository:
    git init

    A .git folder is created containing Git's records and configuration files. Do not edit these files manually.

    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    d--h--         2/22/2024  10:57 AM                .git

Add a Remote

Adding a remote tells Git that your new remote repository is tied to this local folder.

git remote add origin git@gitlab.com:ellopunk/portfolio.git

Create a README File

The best practice is to start with a README.md file. It should provide essential information about the project — what it is, how to use it, and how to contribute.

New-Item README.md

First Commit and Push

  1. Stage the file:
    git add README.md

    git add moves changes from the working directory to the staging area, telling Git which modifications to include in the next commit.

  2. Commit:
    git commit -S -m "Initializing Project"
    • -m — every commit requires a message; write something that communicates the intent of the changes.
    • -S — signs the commit with your GPG key. See Signing Git Commits Using PowerShell.
  3. Push:
    git push --set-upstream origin master
    • git push — uploads local repository content to a remote repository.
    • --set-upstream — sets the default upstream branch for the current local branch.
    • origin — the remote repository to push to.
    • master — the branch on the remote to push to.

    main and other names are now commonly used as the default branch to move away from the master/slave terminology.

Your project is ready. Confirm by visiting your GitLab (or GitHub) page.