Git Notes
Step-by-step guide to creating your first Git repository locally or cloning from GitHub, understanding basic Git workflow, making your first commit, and pushing to remote.
Creating your first Git repository is a major milestone. This guide walks through creating locally and connecting to GitHub, from initialization to first commit.
Option 1: Create Locally
Initialize Repository
# Create project folder
mkdir my-first-project
cd my-first-project
# Initialize Git
git init
# Verify
ls -la
# Output: total includes .git folderMake Your First Commit
Workflow Visualization
Option 2: Clone from GitHub
On GitHub
- Create repository on GitHub
- Click "Code" → Copy HTTPS URL
- Or generate SSH key and use SSH URL
On Your Computer
# Clone repository
git clone https://github.com/username/repo-name.git
cd repo-name
# Verify
git remote -v
# Output: origin https://github.com/username/repo-name.git
# View history
git logMaking Changes
Workflow
# Check current status
git status
# Make edits to file
echo "More content" >> README.md
# Check what changed
git diff
# Output: Shows additions with + prefix
# Stage changes
git add README.md
# Commit
git commit -m "Add more content to README"
# Push to GitHub (if cloned)
git push origin main
# Output: Uploading commit to remoteUnderstanding Git Areas
| Working | Staging | Local | Remote |
|---|---|---|---|
| Directory | Area | Repository | Repository |
| (Files) | (git add) | (commits) | (GitHub) |
Your First Commit
Good Commit Messages
# Excellent
git commit -m "Add user authentication module"
# Good
git commit -m "Fix login button styling on mobile"
# Poor
git commit -m "fixed stuff"
git commit -m "WIP"
git commit -m "asdf"Commit Message Format
| - feat | Add user login functionality |
| - fix | Resolve memory leak in database |
| - docs | Update README installation section |
| - style | Reformat code to meet standards |
| - refactor | Simplify authentication logic |
Multiple Commits
# Make several changes
echo "Feature 1" > feature1.txt
echo "Feature 2" > feature2.txt
echo "Feature 3" > feature3.txt
# Stage selectively (NOT everything at once)
git add feature1.txt
git commit -m "Add feature 1"
git add feature2.txt
git commit -m "Add feature 2"
git add feature3.txt
git commit -m "Add feature 3"
# View all commits
git log --oneline
# Output: commit hashes with first line of messagesView Commit History
# Simple one-liner view
git log --oneline
# Output: abc1234 Add feature 1
# def5678 Add feature 2
# ghi9012 Add feature 3
# Detailed view
git log
# Shows full hash, author, date, message
# Specific number of commits
git log -5 # Last 5 commits
# Graph view (branches)
git log --graph --oneline --allConnecting to GitHub
Push Existing Local Repository to GitHub
# On GitHub, create empty repository (no README)
# In your local repo, add remote
git remote add origin https://github.com/username/repo-name.git
# Verify remote
git remote -v
# Push to GitHub (first time)
git push -u origin main
# -u sets upstream, linking branches
# Future pushes (simpler)
git pushVerify on GitHub
- Go to your GitHub repository
- Refresh page
- See your files and commit history
- Click commit hash to view changes
Common First-Time Issues
"fatal: not a git repository"
# Problem: Running git command outside repository
# Solution: cd into repository folder first
cd /path/to/my-project
git status # Now worksNothing to commit
# Problem: Made changes but forgot to git add
git add . # Add all changes
git commit -m "message""origin does not appear to be a git repository"
# Problem: Remote not set up
git remote add origin https://github.com/username/repo.git
git push -u origin mainPermission denied (publickey)
# Problem: Using SSH without key setup
# Solution 1: Use HTTPS instead
git clone https://github.com/username/repo.git
# Solution 2: Set up SSH key
ssh-keygen -t ed25519
# Add public key to GitHubBest Practices for First Repository
# 1. Create meaningful README
# - Project description
# - How to install/run
# - Contributing guidelines
# 2. Use .gitignore
# Prevent committing: node_modules/, venv/, .env
# 3. Commit frequently
# Small logical changes, not huge chunks
# 4. Write clear messages
# "Add login form" not "fixed bugs"
# 5. Review before committing
git diff # See changes before staging
# 6. Keep branches clean
# main branch for production-ready codeFirst Repository Checklist
Interview Q&A
Q1: Explain the steps from creating files to having commits on GitHub.
A: (1) Initialize with git init locally or clone existing repo. (2) Create/modify files in working directory. (3) Stage with git add. (4) Commit with git commit -m "message" which creates snapshot. (5) If using GitHub, set remote with git remote add origin URL. (6) Push with git push -u origin main to upload commits to GitHub.
Q2: What's the difference between git add and git commit?
A: git add stages changes to the staging area, indicating intent to include them in next commit. git commit creates permanent snapshot of staged changes in local repository with a message. You can add multiple files before a single commit, or commit separate changes as separate commits. Commit creates the permanent record.
Q3: Why stage changes before committing instead of committing directly?
A: Staging allows selective inclusion of changes. You might have 10 file changes but only want to commit 3 related ones. Staging provides control and enables logical grouping of related changes. You can view staged changes with git diff --staged before committing.
Q4: How do you connect a local Git repository to GitHub?
A: Create empty repository on GitHub (without README). In local repo, run git remote add origin [URL] to add GitHub as remote. Verify with git remote -v. Push with git push -u origin main to upload commits. The -u flag sets upstream tracking, linking branches for future pushes.
Q5: What should first commit message be and why does it matter?
A: First message typically "Initial commit". Commit messages should be clear describing what changed. Poor messages ("fixed stuff", "WIP") make history hard to understand. Good messages ("Add user authentication") document why changes were made. When reviewing commit history or debugging, clear messages save time for entire team.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for First Git Repository - Getting Started.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, setup, first, repository, first git repository - getting started
Related Git & GitHub Topics