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.
- Step 1: Configure Your Git Identity and Local Repository
- Step 2: Understand the Three Git States (Working, Staging, Commit)
- Step 3: Essential Git Commands Reference Cheatsheet
- Step 4: Connect to GitHub via Secure SSH Keys
- Step 5: The Feature-Branch Workflow & Pull Requests
- Common Mistakes & Troubleshooting Fixes
- 1. Accidentally Committing Secrets or .env Files
- 2. Falling into a “Detached HEAD” State
- 3. Resolving Merge Conflicts Cleanly
- Frequently Asked Questions
- What is the core difference between Git and GitHub?
- What is the difference between “git fetch” and “git pull”?
- How do I undo my last commit if I made a typo in the message?
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:
- Working Directory: The actual files you see and edit in VS Code or your text editor.
- Staging Area (The Index): The staging room where you gather specific files ready to be packaged into a commit snapshot using
git add. - 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"
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:
- Create a Branch:
git checkout -b feature/user-authentication. - Develop and Commit: Make your edits, run local tests, stage files, and commit.
- Push the Feature Branch:
git push -u origin feature/user-authentication. - 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.
- 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.
