Skip to content

Git Setup & Workflow

Use this page for the commands that turn a local project into a GitHub-backed repo and keep day-to-day work moving through dev and feature branches.

Create an SSH key on the local machine, start the SSH agent, add the key, and copy the public key so it can be added to GitHub under Settings -> SSH and GPG keys.

Terminal window
ssh-keygen -t ed25519 -C "you@example.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
cat ~/.ssh/id_ed25519.pub

After adding the public key to GitHub, test the connection.

Terminal window
ssh -T git@github.com

When GitHub SSH is working, use SSH remotes instead of HTTPS remotes so terminal pushes do not ask for a GitHub username or password.

Terminal window
git remote set-url origin git@github.com:USERNAME/REPO.git
git remote -v

Use named SSH hosts when one machine needs more than one GitHub identity. Each host points at a different SSH key.

Terminal window
cat ~/.ssh/config

Example ~/.ssh/config entries:

Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes

Use the matching host in the repository remote.

Terminal window
git remote set-url origin git@github.com-personal:USERNAME/REPO.git
git remote -v

Check which Git identity is active in the current repo.

Terminal window
git config user.name
git config user.email
git config --list --show-origin

Set repo-specific identity values when needed.

Terminal window
git config user.name "Your Name"
git config user.email "you@example.com"

Use the SSH clone URL from GitHub so future pulls and pushes use the SSH key instead of asking for a GitHub username or password.

Terminal window
git clone git@github.com:USERNAME/REPO.git
cd REPO

If using a named SSH host for multiple GitHub accounts, use that host in the clone URL.

Terminal window
git clone git@github.com-personal:USERNAME/REPO.git
cd REPO

Run this from the project root when the files already exist locally.

Terminal window
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin git@github.com:USERNAME/REPO.git
git push -u origin main

Create a Rails app with PostgreSQL and Git

Section titled “Create a Rails app with PostgreSQL and Git”

Create the Rails app with PostgreSQL, enter the project directory, make the first commit, and push main.

Terminal window
rails new APP_NAME -d postgresql
cd APP_NAME
git add .
git commit -m "Initial Rails app"
git branch -M main
git remote add origin git@github.com:USERNAME/REPO.git
git push -u origin main

Create the Astro project, enter the project directory, make the first commit, and push main.

Terminal window
npm create astro@latest APP_NAME
cd APP_NAME
git add .
git commit -m "Initial Astro app"
git branch -M main
git remote add origin git@github.com:USERNAME/REPO.git
git push -u origin main

Push the first clean baseline to main, then immediately create dev locally and push it to GitHub. Use dev as the integration branch for normal work.

Terminal window
git checkout -b dev
git push -u origin dev

Update dev, create a focused branch, commit the work, and push the branch.

Terminal window
git checkout dev
git pull origin dev
git checkout -b feature/short-description
git add .
git commit -m "Describe the change"
git push -u origin feature/short-description

Use short branch names that say what changed.

Terminal window
git checkout -b feature/add-login
git checkout -b bugfix/fix-navbar-overlap
git checkout -b chore/update-dependencies

Open a pull request on GitHub from the feature or bug branch into dev. After GitHub merges the PR, switch back to local dev and pull the updated remote branch.

Terminal window
git checkout dev
git pull origin dev

Delete the merged branch locally.

Terminal window
git branch -d feature/short-description

Optionally delete the remote branch after it has been merged.

Terminal window
git push origin --delete feature/short-description

Check what changed and which branch is active.

Terminal window
git status
git branch

Fix the last commit message before pushing.

Terminal window
git commit --amend -m "Better commit message"

Add a missed file to the last commit before pushing.

Terminal window
git add path/to/file
git commit --amend --no-edit

Undo local changes to one file.

Terminal window
git restore path/to/file

Undo all unstaged local changes in tracked files.

Terminal window
git restore .

Unstage a file after git add.

Terminal window
git restore --staged path/to/file

Temporarily stash unfinished work when switching branches. This is still useful, but treat it like short-term storage and apply it again soon.

Terminal window
git stash push -m "Short note about the work"
git stash list
git stash pop