This guide covers three main scenarios for working with Git repositories.
- Building a New Repository
- Updating an Existing Repository
- Removing All Diffs and Creating Clean Initial Commit
- Checking Status Commands
- GitHub CLI Commands
You have TWO options to create the repository:
Step 0a.1: Check if GitHub CLI is installed
gh --versionIf not installed, download from: https://cli.github.com/
Step 0a.2: Check authentication status
gh auth statusStep 0a.3: Authenticate with GitHub (if not logged in)
gh auth loginFollow the prompts:
- Select "GitHub.com"
- Choose "Login with a web browser"
- Copy the one-time code shown
- Press Enter to open browser
- Paste code and authorize
- Return to terminal
Alternative: Login with web browser directly
gh auth login --webFollow the prompts:
- Select "HTTPS" as preferred protocol
- Choose "Yes" to authenticate Git with GitHub credentials
- Copy the one-time code
- Press Enter to open browser
- Paste code and authorize
Step 0a.4: Create repository
Option 1: Create repository only (then follow Steps 1-6 below)
gh repo create repo-name --publicOption 2: Create repository and push in one command (requires git init, add, commit done first)
# Make sure you've done: git init, git add ., git commit -m "message" first!
gh repo create repo-name --public --source=. --remote=origin --pushNote: If using Option 2, you must initialize git, add files, and commit BEFORE running this command. Then skip to Step 6 (Push).
GitHub now uses main as the default branch name (changed from master in 2020). When creating a repository:
- GitHub CLI creates repositories with
mainas default branch - GitHub Web Interface creates repositories with
mainas default branch - Local Git may still use
masteras default (depending on your Git version)
When to use main (Recommended):
- β New repositories (created after 2020)
- β Most modern projects
- β GitHub's default standard
- β
Better for collaboration (most developers expect
main) - β Industry standard now
When to use master:
β οΈ Legacy repositories (created before 2020)β οΈ Working with old projects that still usemasterβ οΈ Team/organization still usesmasterconventionβ οΈ Older Git installations that default tomaster
Scenario 1: Local branch is master, GitHub uses main (Most Common)
# After git init, check your branch name
git branch
# If it shows "master", rename it to "main" before pushing
git branch -M main
# Then push
git push -u origin mainScenario 2: Local branch is already main (Ideal)
# Just push directly - branch names match!
git push -u origin mainScenario 3: Local branch is main, but GitHub repository uses master (Rare)
# Check what branch GitHub created
git ls-remote --heads origin
# If GitHub shows "master", rename your local branch to match
git branch -M master
# Then push
git push -u origin masterOr, better option: Change GitHub's default branch to main:
- Go to repository Settings β Branches
- Change default branch from
mastertomain - Then use
mainlocally and push:git push -u origin main
To check what branch GitHub created:
# After creating repo, check remote branches
git ls-remote --heads origin
# Or check via GitHub CLI
gh repo view username/repo-name --json defaultBranchRefQuick Decision Guide:
- New project? β Use
main(rename localmastertomainif needed) - Old project with
master? β Can keepmasteror migrate tomain - Team uses
master? β Follow team convention - Unsure? β Use
main(it's the modern standard)
Step 0b.1: Go to GitHub.com
- Visit https://github.com/new
- Or click the "+" icon in the top right, then "New repository"
Step 0b.2: Fill in repository details
- Repository name:
your-repo-name - Choose Public or Private
- Important: Do NOT check:
- β "Add a README file" (if you already have one)
- β "Add .gitignore" (if you already have one)
- β "Choose a license" (unless you want one)
- Click "Create repository"
Step 0b.3: Copy the repository URL
- GitHub will show you the URL (e.g.,
https://github.com/username/repo-name.git) - Note: GitHub creates the repository with
mainas the default branch name
Now that you have created the repository, follow these steps to set up your local project:
git initAdd all files and folders:
git add .Add specific files (any file type):
git add filename1.js filename2.html
git add README.md LICENSE
git add script.js styles.css index.htmlAdd entire folders:
git add src/
git add images/
git add .github/Note: You can add any file type (JavaScript, HTML, CSS, images, documents, etc.) and any folder structure!
git commit -m "Initial commit: Your project description"What does -m mean?
-mstands for "message" - it allows you to add a comment/description to your commit- The text inside quotes is your commit message that describes what changes you made
- Every commit should have a message - it helps you remember what you changed and why
Examples of commit messages:
git commit -m "Initial commit: Complete study guide with JavaScript examples"
git commit -m "Add new JavaScript functions"
git commit -m "Fix bug in calculation"
git commit -m "Update README with installation instructions"
git commit -m "Add Git commands guide"
git commit -m "Remove unused files"Why commit messages are important:
- They help you understand what was changed in each commit
- They make it easier to track your project history
- Other collaborators can understand your changes
- You can search through commit history by message
Tip: Write clear, descriptive commit messages that explain WHAT you changed and WHY (if needed).
If you used Option A (CLI) and it didn't auto-add remote:
git remote add origin https://github.com/username/repo-name.gitIf remote already exists but URL is wrong:
git remote set-url origin https://github.com/username/repo-name.gitCheck remote configuration:
git remote -vWhy this step is important:
- GitHub uses
mainas the default branch name (since 2020) - Older Git installations may create
masterbranch locally - You need to match your local branch name with GitHub's branch name
- Mismatched branch names can cause confusion and errors
Check your current branch name:
git branchCheck what branch GitHub expects:
git ls-remote --heads originScenario A: Local is master, GitHub uses main (Most Common)
# Rename local branch from master to main
git branch -M mainScenario B: Local is already main (Ideal - No action needed)
# Branch names already match, skip this step
# Or verify: git branch (should show: * main)Scenario C: Local is main, but GitHub uses master (Rare)
# Only if GitHub repository was created before 2020 or uses master
# Rename local branch from main to master
git branch -M masterWhat does -M mean?
-Mstands for "move/rename" - it renames your current branch- If your branch is named
master, this renames it tomain(or vice versa) - If target branch already exists,
-Mwill force rename it - This ensures your local branch matches GitHub's branch name
Verify the branch name:
git branch
# Should show: * main (or * master if that's what GitHub uses)Recommendation: Use main unless working with a legacy repository that requires master.
Before pushing, verify branch names match:
# Check local branch
git branch
# Check what GitHub expects
git ls-remote --heads originPush based on your scenario:
Scenario A: Both local and GitHub use main (Recommended)
git push -u origin mainScenario B: Both local and GitHub use master (Legacy)
git push -u origin masterScenario C: Mismatched branches (Fix this first!)
# If local is master but GitHub is main:
git branch -M main
git push -u origin main
# If local is main but GitHub is master:
git branch -M master
git push -u origin master- Always match branch names - Local and GitHub should use the same branch name
- GitHub defaults to
main- New repositories usemainby default - If you push mismatched names, you'll create two separate branches (confusing!)
- Best practice: Use
mainfor new projects,masteronly for legacy projects
Check what branch GitHub expects:
- After creating repo on GitHub, it will show: "push an existing repository from the command line"
- The command will show either
mainormaster- use that branch name - Or use:
git ls-remote --heads originto check
What does -u mean?
-ustands for "upstream" - it sets up tracking between your local branch and the remote branch- After using
-uonce, Git remembers the connection, so you can just usegit pushin the future - Only needed the first time you push a new branch to GitHub
- After that, you can simply use
git pushwithout-u
How it works:
# First time pushing a branch (use -u)
git push -u origin main
# After that, you can just use:
git push # Git remembers origin/main automaticallyWithout -u:
git push origin main # Works, but doesn't set up tracking
git push # Won't work - Git doesn't know where to pushWith -u:
git push -u origin main # Sets up tracking
git push # Now works! Git knows to push to origin/mainImportant: Use -u only the first time you push a branch. After that, use git push for simplicity.
git statusAdd all changes (files and folders):
git add .Add specific files (any file type):
git add filename.js
git add filename.html
git add filename.css
git add filename.md
git add filename.json
git add README.mdAdd entire folders/directories:
git add folder-name/
git add src/
git add images/
git add .github/Add multiple files at once:
git add file1.js file2.html file3.cssAdd files by pattern/extension:
git add *.js # All JavaScript files
git add *.html # All HTML files
git add *.md # All Markdown files
git add src/**/*.js # All JS files in src folder and subfoldersAdd files by type:
git add "*.js" # JavaScript files
git add "*.html" # HTML files
git add "*.css" # CSS files
git add "*.md" # Markdown files
git add "*.json" # JSON files
git add "*.png" # PNG images
git add "*.jpg" # JPEG images
git add "*.svg" # SVG imagesExamples for different file types:
# JavaScript files
git add script.js
git add app.js
git add ΓΆvning-fil.js
# HTML files
git add index.html
git add script-prompt.html
# CSS files
git add styles.css
git add main.css
# Markdown files
git add README.md
git add GIT_COMMANDS_GUIDE.md
# JSON files
git add package.json
git add config.json
# Image files
git add logo.png
git add image.jpg
# Folders with files
git add .github/ # GitHub Actions folder
git add src/ # Source code folder
git add docs/ # Documentation folder
git add images/ # Images folderImportant: Git tracks ANY file type you add - text files, images, videos, documents, etc. You can create folders, subfolders, and any file structure you need!
git commit -m "Description of what you changed"What does -m mean?
-mstands for "message" - it allows you to add a comment/description to your commit- The text inside quotes is your commit message that describes what changes you made
- Every commit should have a message - it helps you remember what you changed and why
Examples of commit messages:
git commit -m "Add new feature for user authentication"
git commit -m "Fix bug in calculation function"
git commit -m "Update README with new instructions"
git commit -m "Remove unused console.log statements"
git commit -m "Add error handling to API calls"
git commit -m "Update CSS styles for better responsiveness"Tip: Write clear, descriptive commit messages for each commit so you can track your changes over time.
git push origin mainor simply:
git pushWhen to use which:
git push origin main- Always works, explicitly specifies where to pushgit push- Only works if you used-uflag the first time (which sets up tracking)
If you used -u the first time:
# After first push with -u, you can just use:
git push # Git remembers origin/main automaticallyIf you didn't use -u the first time:
# You need to specify each time:
git push origin maingit pull origin main --no-rebase- Edit conflicted files
- Stage resolved files:
git add filename.js - Complete merge:
git commit -m "Merge remote changes"
git push origin maingit checkout --orphan new-maingit add .git commit -m "Initial commit: Complete study guide with all JavaScript concepts and examples"git branch -D maingit branch -m new-main maingit push -f origin maingit statusShows:
- Modified files
- Untracked files
- Files ready to commit
- Branch information
git logShows full commit history with details
git log --onelineShows one line per commit
git log --oneline -5Shows last 5 commits (change number as needed)
git diffShows unstaged changes
git diff --stagedShows changes ready to commit
git ls-filesLists all files tracked by Git
git remote -vShows:
- Remote name (usually
origin) - Fetch URL
- Push URL
git branch -rShows all remote branches
git fetch origin
git statusFetches latest info from GitHub and shows status
git log HEAD..origin/mainShows commits on remote that you don't have locally
git log origin/main..HEADShows commits you have locally that aren't on remote
git statusShows: "Your branch is ahead/behind 'origin/main' by X commits"
What it does: Adds a commit message/comment
Use with: git commit
Example:
git commit -m "Your commit message here"Why: Every commit needs a message to describe what changed
What it does: Sets up tracking between local and remote branch
Use with: git push
Example:
git push -u origin mainWhy: Only needed first time - after that you can use git push alone
What it does: Force push - overwrites remote history
Use with: git push
Example:
git push -f origin mainWarning:
What it does: Pulls changes from GitHub without "replaying" your local commits on top of the new changes.
In plain language: This means that when you use git pull with --no-rebase, Git will keep your commit history as it is and make one new commit that combines your changes with the changes from GitHub. It does NOT try to re-apply each of your commits one-by-one like a rebase would.
Use with: git pull
Example:
git pull origin main --no-rebaseWhy: It creates a merge commit so you can see a clear point where changes from different places came together, instead of re-writing your commit history.
User-Friendly vs Industry Standard:
- β
User-Friendly: Uses
--no-rebaseexplicitly - makes it clear what's happening, easier to understand for beginners - π Industry Standard: Default
git pullbehavior (without any flag) actually does merge by default, so--no-rebaseis redundant but explicit - π‘ Best Practice: Use
git pull --no-rebasewhen learning to understand what's happening, or justgit pull(which does merge by default)
Note: Many professional teams use --rebase for cleaner history, but --no-rebase (or default merge) is safer and easier for beginners.
What it does: Pulls changes and replays your local commits on top of the remote changes
Use with: git pull
Example:
git pull origin main --rebaseWhy: Creates a linear commit history without merge commits - looks cleaner
User-Friendly vs Industry Standard:
β οΈ Advanced/Beginner Caution:--rebaserewrites commit history - can be confusing if conflicts occur- π Industry Standard: Used by many professional teams for cleaner, linear history
- π‘ Best Practice: Use
--rebaseonly if you understand Git well and your team prefers it. For beginners, stick with--no-rebaseor default merge.
When to use:
- β You want clean, linear history
- β Your team prefers rebase workflow
- β You're comfortable resolving conflicts
- β Avoid if you're learning Git
- β Avoid if you've already pushed commits (unless you know what you're doing)
What it does: Adds all changes including deletions
Use with: git add
Example:
git add -AWhy: Includes file deletions, not just new/modified files
What it does: Shows remote branches
Use with: git branch
Example:
git branch -rWhy: Lists all branches on remote repository
What it does: Shows detailed information
Use with: git remote
Example:
git remote -vWhy: Shows both fetch and push URLs
What it does: Shows compact one-line commit history
Use with: git log
Example:
git log --onelineWhy: Easier to read - one commit per line
What it does: Allows merging unrelated Git histories
Use with: git pull
Example:
git pull origin main --allow-unrelated-historiesWhy: Needed when merging repositories with different histories
What it does: Force deletes a branch
Use with: git branch
Example:
git branch -D branch-nameWarning:
What it does: Renames or moves files/branches
Use with: git branch or git mv
Example:
git branch -m old-name new-name
git mv old-file.js new-file.jsWhy: Renames branches or moves/renames files in Git
What it does: Creates branch with no history
Use with: git checkout
Example:
git checkout --orphan new-branchWhy: Starts fresh branch without commit history
What it does: Undoes commit but keeps changes staged
Use with: git reset
Example:
git reset --soft HEAD~1Why: Undo commit but keep your changes ready to commit again
What it does: Undoes commit and discards all changes
Use with: git reset
Example:
git reset --hard HEAD~1Warning:
| Flag | Command | Purpose | User-Friendly | Industry Standard |
|---|---|---|---|---|
-m |
git commit |
Add commit message | β Yes | β Yes |
-u |
git push |
Set upstream tracking (first time only) | β Yes | β Yes |
-f |
git push |
Force push (overwrites remote) | ||
-A |
git add |
Add all changes including deletions | β Yes | β Yes |
-r |
git branch |
Show remote branches | β Yes | β Yes |
-v |
git remote |
Show verbose remote info | β Yes | β Yes |
--oneline |
git log |
Compact commit history | β Yes | β Yes |
-D |
git branch |
Force delete branch | ||
--soft |
git reset |
Undo commit, keep changes | β Yes | β Yes |
--hard |
git reset |
Undo commit, discard changes | ||
--orphan |
git checkout |
Create branch without history | β Yes | |
--no-rebase |
git pull |
Pull without rebasing (explicit merge) | β Yes (Beginner-friendly) | β Yes (Default behavior) |
--rebase |
git pull |
Pull with rebase (linear history) | β Yes (Many teams prefer) | |
--allow-unrelated-histories |
git pull |
Merge unrelated histories | β Yes | β Yes |
Legend:
- β User-Friendly: Safe and easy to use, especially for beginners
- π Industry Standard: Commonly used in professional development
β οΈ Dangerous/Advanced: Can cause data loss or requires advanced knowledge
# Check status
git status
# Add all changes
git add .
# Commit changes
git commit -m "Your message"
# Push to GitHub
git push origin main
# Pull from GitHub
git pull origin mainYou can add these to your Git config for shortcuts:
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commitThen use: git st instead of git status, etc.
git pull origin main --allow-unrelated-historiesgit reset --soft HEAD~1git reset --hard HEAD~1git checkout HEAD -- filename.jsGitHub CLI (gh) is a command-line tool that lets you work with GitHub directly from your terminal. It's a powerful alternative to using the GitHub web interface.
Check if GitHub CLI is installed:
gh --versionIf not installed:
- Download from: https://cli.github.com/
- Or use package manager:
- Windows:
winget install GitHub.cli - macOS:
brew install gh - Linux: See https://cli.github.com/manual/installation
- Windows:
gh auth statusShows:
- Whether you're logged in
- Your GitHub username
- Authentication token status
gh auth loginInteractive login process:
- Select "GitHub.com" or "Other"
- Choose authentication method:
- "Login with a web browser" (recommended)
- "Paste an authentication token"
- Follow the prompts to complete authentication
gh auth login --webSimplified web-based login:
- Select "HTTPS" as preferred protocol
- Choose "Yes" to authenticate Git with GitHub credentials
- Copy the one-time code shown
- Press Enter to open browser
- Paste code and authorize
gh auth logoutgh auth refreshgh repo create repo-name --publicCreates a public repository
gh repo create repo-name --privateCreates a private repository
gh repo create repo-name --public --source=. --remote=origin --pushThis command:
- Creates the repository on GitHub
- Sets the current directory as source
- Adds remote named "origin"
- Pushes all commits to GitHub
Options:
--public- Make repository public--private- Make repository private--source=.- Use current directory as source--remote=origin- Set remote name to "origin"--push- Push commits after creating
gh repo listShows all your repositories with:
- Repository name
- Visibility (public/private)
- Last updated date
- Description (if available)
List repositories with more details:
gh repo list --limit 10 # Show first 10 repositories
gh repo list --json name,url,description # Show as JSONList repositories by language:
gh repo list --language JavaScriptList repositories by visibility:
gh repo list --public # Only public repositories
gh repo list --private # Only private repositoriesgh repo view username/repo-namegh repo clone username/repo-namegh repo fork username/repo-namegh repo delete username/repo-name# 1. Check if CLI is installed
gh --version
# 2. Check authentication
gh auth status
# 3. Login if needed
gh auth login --web
# 4. Initialize git (if not done)
git init
git add .
git commit -m "Initial commit"
# 5. Create repository and push
gh repo create my-repo --public --source=. --remote=origin --push# If remote wasn't added automatically
git remote set-url origin https://github.com/username/repo-name.git
# Verify remote
git remote -v| Feature | GitHub CLI | Web Interface |
|---|---|---|
| Speed | β‘ Faster (no browser needed) | π Slower (requires browser) |
| Automation | β Easy to script | β Manual only |
| Learning Curve | β Beginner-friendly | |
| Repository Creation | β One command | |
| Best For | Terminal users, automation | Beginners, visual learners |
Recommendation:
- Use CLI if you're comfortable with terminal commands
- Use Web Interface if you're just starting with Git/GitHub
# Authentication
gh auth status # Check login status
gh auth login # Login to GitHub
gh auth login --web # Login via web browser
gh auth logout # Logout from GitHub
# Repositories
gh repo create name --public --source=. --push # Create and push
gh repo list # List your repositories
gh repo view user/repo # View repository details
gh repo clone user/repo # Clone a repository
gh repo fork user/repo # Fork a repository
gh repo delete user/repo # Delete repository (β οΈ dangerous)
# General
gh --version # Check CLI version
gh --help # Show helpUsing GitHub CLI:
gh repo listUsing Git command:
git ls-remote --heads originView on GitHub:
- Visit: https://github.com/Ajaxy12?tab=repositories
- Or visit your profile and click "Repositories" tab
- funktions2 - Git Commands Guide & Hello World application
- Pseudokod - JavaScript Study Guide & Practical Examples
- create-react-app-auth-amplify - React app with AWS Amplify authentication (Forked)
Last Updated: 2025
Repository: https://github.com/Ajaxy12/funktions2