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.
Set up an SSH key for GitHub
Section titled “Set up an SSH key for GitHub”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.
eval "$(ssh-agent -s)"ssh-add ~/.ssh/id_ed25519cat ~/.ssh/id_ed25519.pubAfter adding the public key to GitHub, test the connection.
When GitHub SSH is working, use SSH remotes instead of HTTPS remotes so terminal pushes do not ask for a GitHub username or password.
git remote -vUse multiple GitHub accounts
Section titled “Use multiple GitHub accounts”Use named SSH hosts when one machine needs more than one GitHub identity. Each host points at a different SSH key.
cat ~/.ssh/configExample ~/.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 yesUse the matching host in the repository remote.
git remote -vCheck which Git identity is active in the current repo.
git config user.namegit config user.emailgit config --list --show-originSet repo-specific identity values when needed.
git config user.name "Your Name"Clone an existing repo with SSH
Section titled “Clone an existing repo with SSH”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.
cd REPOIf using a named SSH host for multiple GitHub accounts, use that host in the clone URL.
cd REPOInitialize a repo for the first time
Section titled “Initialize a repo for the first time”Run this from the project root when the files already exist locally.
git initgit add .git commit -m "Initial commit"git branch -M maingit push -u origin mainCreate 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.
rails new APP_NAME -d postgresqlcd APP_NAMEgit add .git commit -m "Initial Rails app"git branch -M maingit push -u origin mainCreate an Astro site with Git
Section titled “Create an Astro site with Git”Create the Astro project, enter the project directory, make the first commit, and push main.
npm create astro@latest APP_NAMEcd APP_NAMEgit add .git commit -m "Initial Astro app"git branch -M maingit push -u origin mainCreate dev after the initial main push
Section titled “Create dev after the initial main push”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.
git checkout -b devgit push -u origin devCreate feature or bug branches from dev
Section titled “Create feature or bug branches from dev”Update dev, create a focused branch, and push the new branch to GitHub with upstream tracking. After making and committing changes, push the commit.
git checkout devgit pull origin devgit checkout -b feature/short-descriptiongit push -u origin feature/short-descriptiongit add .git commit -m "Describe the change"git pushBranch naming examples
Section titled “Branch naming examples”Use short branch names that say what changed.
git checkout -b feature/add-logingit checkout -b bugfix/fix-navbar-overlapgit checkout -b chore/update-dependenciesPull request and branch cleanup
Section titled “Pull request and branch cleanup”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.
git checkout devgit pull origin devDelete the merged branch locally.
git branch -d feature/short-descriptionOptionally delete the remote branch after it has been merged.
git push origin --delete feature/short-descriptionGit helpers and utilities
Section titled “Git helpers and utilities”Check what changed and which branch is active.
git statusgit branchFix the last commit message before pushing.
git commit --amend -m "Better commit message"Add a missed file to the last commit before pushing.
git add path/to/filegit commit --amend --no-editUndo local changes to one file.
git restore path/to/fileUndo all unstaged local changes in tracked files.
git restore .Unstage a file after git add.
git restore --staged path/to/fileTemporarily stash unfinished work when switching branches. This is still useful, but treat it like short-term storage and apply it again soon.
git stash push -m "Short note about the work"git stash listgit stash pop