Git & GitHub for Beginners: How to Set Up Repositories, Branches, and Pull Requests Safely

LT By Lucas Thorne (Web Infrastructure & Tech Career Editor) Hands-On Lab Tested • Last Updated & Tested 24 November 2026 • 6 min read • 31,940 views

Quick Summary & Prerequisites

Lab Environment Tested Cloud DNS/HTTPS Lab • Git • Production Web Stacks
Topic Pillar & Level Web & Career Tech • Step-by-Step
Verification Standard Audited by Lucas Thorne without sponsored bias.
Git version control terminal workflow and GitHub pull request branch review
Quick Solution: To collaborate on software using Git and GitHub without breaking production code, follow the standard Feature-Branch Workflow: Initialize your repo with git init, create a separate feature branch with git checkout -b feature/your-feature-name, stage and commit changes with git add . and git commit -m "Descriptive imperative message", push to GitHub using git push -u origin feature/your-feature-name, and open a Pull Request (PR) for peer review before merging into the protected main branch.

Whether you are building web applications, writing automated scripts, or collaborating with open-source communities, Git and GitHub are the universal standard for modern software development. Yet for beginners, version control often feels full of anxiety: one misplaced command, and you fear you will erase weeks of hard work or overwrite a colleague’s changes.

The secret to mastering Git is realizing that it is not merely a backup tool—it is a time machine and collaboration engine designed specifically to make experimentation safe. When you follow established branch hygiene and pull request conventions, you can test daring code refactors, roll back defects instantly, and merge features with absolute confidence. Below is our complete, hands-on field manual for mastering Git and GitHub from the command line.

Step 1: Configure Your Git Identity and Local Repository

Before making your first commit, you must introduce yourself to Git. These global parameters attach your cryptographic identity and contact details to every change you submit:

# Set your global username and email (matching your GitHub account)
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# Set modern default branch name to 'main'
git config --global init.defaultBranch main

# Verify your configuration
git config --list

To turn any folder on your computer into a version-controlled repository, open your terminal inside the folder and run git init. This creates a hidden .git/ directory where Git stores your commit tree and change history.

If you need a refresher on moving through directories and inspecting hidden folders on the command line, check our field guide on 25 essential Linux terminal commands for beginners.

Step 2: Understand the Three Git States (Working, Staging, Commit)

Unlike simple cloud drives (like Dropbox or Google Drive) that automatically sync every keystroke, Git requires deliberate snapshots. Your code exists in one of three distinct areas:

  1. Working Directory: The actual files you see and edit in VS Code or your text editor.
  2. Staging Area (The Index): The staging room where you gather specific files ready to be packaged into a commit snapshot using git add.
  3. Git Directory (Repository): The permanent history where commits are securely stored after executing git commit.
# Check which files have been modified or untracked
git status

# Stage a specific file for the next snapshot
git add index.html style.css

# Stage all modified files in the current directory
git add .

# Save the staged snapshot with a clear, descriptive message
git commit -m "Add responsive mobile navigation drawer"
Try Our Free Browser Tool: AI Pull-Request Cost Estimator

Using LLM bots to review GitHub pull requests? Before connecting CI pipelines, estimate AI diff-review token counts and monthly API spend across GPT, Claude, and Gemini.

Step 3: Essential Git Commands Reference Cheatsheet

Below is our consolidated cheatsheet detailing core Git operations, flags, and real-world safety ratings:

Command Syntax Operational Scope Safety Level Practical Purpose
git status Working Tree / Staging Completely Safe (Read-Only) Inspects modified, staged, and untracked files.
git log --oneline --graph Commit History Completely Safe (Read-Only) Displays visual commit history tree.
git diff Working Tree Completely Safe (Read-Only) Shows line-by-line changes before staging.
git checkout -b <branch> Branch Management Completely Safe Creates and switches to an isolated new branch.
git add -p Staging Area Safe Interactively stages individual code hunks.
git commit --amend Latest Commit Caution (Local Only) Updates commit message or adds forgotten files.
git restore <file> Working Directory Moderate (Discards Work) Reverts unstaged local modifications to match HEAD.
git merge <branch> Branch Integration Safe (Creates Merge Commit) Combines feature history into target branch.
git push -u origin <branch> Remote Repository Safe (First-Time Remote) Uploads local branch commits to GitHub.
git reset --hard HEAD~1 Working + Git Tree Destructive Deletes local commit history; use with extreme care.

Step 4: Connect to GitHub via Secure SSH Keys

Never authenticate to GitHub using legacy HTTPS passwords (which GitHub deprecated in 2021). Instead, authenticate using your Ed25519 SSH public key for instant, frictionless security.

For complete instructions on generating and testing your SSH cryptographic keys, follow our comprehensive guide on how to set up SSH keys and disable password logins.

Once your SSH key is added to GitHub (under Settings > SSH and GPG keys), connect your local project to a new GitHub repository:

# Link your local repo to GitHub
git remote add origin git@github.com:yourusername/your-repository-name.git

# Push your main branch and set upstream tracking
git push -u origin main

Step 5: The Feature-Branch Workflow & Pull Requests

In professional software teams, nobody pushes commits directly to the main branch. The main branch represents production-ready code. All development occurs on isolated feature branches:

  1. Create a Branch: git checkout -b feature/user-authentication.
  2. Develop and Commit: Make your edits, run local tests, stage files, and commit.
  3. Push the Feature Branch: git push -u origin feature/user-authentication.
  4. Open a Pull Request on GitHub: Navigate to your repository on GitHub. Click the green Compare & pull request button. Write a clear summary explaining:
    • What problem this PR solves.
    • Key architectural decisions or dependencies added.
    • How reviewers can test the changes locally.
  5. Review and Merge: Once automated CI/CD tests pass and teammates approve the changes, merge the pull request using Squash and merge or Rebase and merge, then delete the remote feature branch.

Common Mistakes & Troubleshooting Fixes

1. Accidentally Committing Secrets or .env Files

The Fix: If you accidentally commit a .env file containing API keys, immediately un-stage it without deleting the file locally: git rm --cached .env. Add .env to your .gitignore file, commit the change, and rotate your compromised API keys immediately.

2. Falling into a “Detached HEAD” State

The Fix: If you checked out a specific commit hash (e.g., git checkout a1b2c3d), Git warns you of a detached HEAD. Any new commits will be lost when switching branches. To preserve your work, simply create a new branch from your current position: git switch -c recovery-branch-name.

3. Resolving Merge Conflicts Cleanly

The Fix: When Git cannot automatically combine changes, it inserts conflict markers (<<<<<<< HEAD, =======, >>>>>>> feature) into the affected files. Open the file in VS Code, click Accept Incoming Change, Accept Current Change, or manually edit the code to keep the best of both. Then stage the resolved file (git add file.js) and finish the merge with git commit.

Frequently Asked Questions

What is the core difference between Git and GitHub?

Git is a distributed version control tool that runs locally on your computer to track file history and branches without needing an internet connection. GitHub is a cloud-hosted web platform that hosts Git repositories online, providing code review tools, issue tracking, continuous integration (GitHub Actions), and team collaboration features.

What is the difference between “git fetch” and “git pull”?

git fetch downloads the latest commits and branches from the remote GitHub server into your local repository without altering your working directory. git pull executes git fetch followed immediately by git merge, combining remote updates directly into your current local branch.

How do I undo my last commit if I made a typo in the message?

If you haven’t pushed the commit to GitHub yet, run git commit --amend -m "Corrected commit message". If you need to undo the commit entirely while keeping your modified files in your editor, run git reset --soft HEAD~1.

Frequently Asked Questions

Who tested and verified this Software Development & Git guide?

This tutorial was authored and lab-tested by Lucas Thorne (Web Infrastructure & Tech Career Editor). All configurations, workflows, and instructions are independently verified in isolated testbeds following LearnTechGuide's 4-stage Editorial Guidelines.

Is this workflow safe to apply on a primary device or work environment?

Yes. Every step in our Web & Career Tech guides prioritizes non-destructive settings, built-in system controls, and reversible configurations. Whenever advanced permissions are needed, we recommend creating a quick restore point or backup first.

How frequently is this Software Development & Git guide audited for software updates?

LearnTechGuide conducts scheduled quarterly audits across all 7 technical pillars. Whenever major operating system updates, security patches, or interface changes occur, our specialist editors re-test and update this page within 48 business hours.

Where can I get help if a step behaves differently on my version?

You can reach our editorial desk directly via our Contact Us page. Share your OS or app version and what happened at that step so our Web & Career Tech editor can verify and assist.

LT

Lucas Thorne

Web Infrastructure & Tech Career Editor
Verified Field Editor

Lucas writes and reviews tutorials covering web hosting, DNS/SSL architecture, front-end coding fundamentals, and technical career roadmaps. After 10 years architecting high-availability cloud deployments and mentoring engineering candidates, he turns complex infrastructure into clear step-by-step guides.

Credentials: AWS Solutions Architect • DevOps & Cloud Engineer • Testing Methodology →