Ctrl K

Git Init and First Push

Initialize a local repo, set the main branch, connect to a remote, and push for the first time.

This document provides a standard initial git setup when starting a project locally and later pushing to remote repository instead of starting from a pull operation.

Quick reference

git init
git branch -M main
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/YOUR_USERNAME/YOUR_REPO.git
git push --set-upstream origin main

Orient yourself first

pwd           # confirm you are in the right directory
ls -la        # list all files including hidden (e.g. existing .git)
du -sh ./*    # check size of each item before committing

Init and branch

git init            # initialize repo in current directory
git branch -M main  # rename default branch to main
git branch          # confirm branch name

Stage and commit

git status        # see untracked files
git add .         # stage everything
git status        # confirm what is staged
git commit -m "Initial commit"

Connect remote and push

git remote add origin https://github.com/YOUR_USERNAME/YOUR_REPO.git
git remote -v                        # confirm remote URL
git push --set-upstream origin main  # first push, sets tracking branch

Verify

git branch   # confirm you are on main
git status   # should say "nothing to commit, working tree clean"