Learn Github
A complete beginner-friendly guide to GitHub, from creating an account to pushing your code using the GitHub CLI (Command Line Interface).
What is GitHub?
Section titled “What is GitHub?”GitHub is a platform where developers store and share their code. Think of it as Google Drive, but specifically designed for code with powerful collaboration features.
Why Use GitHub?
Section titled “Why Use GitHub?”- Backup Your Code: Never lose your work
- Version Control: Track every change you make
- Collaboration: Work with others on the same project
- Portfolio: Show your work to potential employers
- Free: Public repositories are completely free
Part 1: Creating a GitHub Account
Section titled “Part 1: Creating a GitHub Account”-
Sign Up
- Go to github.com
- Click Sign up button (top right)
- Enter your email address
- Create a strong password
- Choose a username (this will be your GitHub identity)
- Example:
bahrulbangsawan,johndoe, etc. - This appears in your URLs:
github.com/your-username
- Example:
- Click Continue
-
Verify Your Account
- Check your email inbox
- GitHub will send a verification code
- Enter the code on GitHub
- Click Verify
-
Personalize Optional
Answer a few questions:
- Are you a student or teacher? (optional)
- What do you plan to use GitHub for?
- Skip these if you want - click Skip personalization
-
Choose a Plan
- Select Free (perfect for most users)
- Free plan includes:
- Unlimited public repositories
- Unlimited private repositories
- GitHub Actions (automation)
- And more!
Part 2: Understanding Git vs GitHub
Section titled “Part 2: Understanding Git vs GitHub”Before we continue, let’s clarify the difference:
| Git | GitHub |
|---|---|
| Version control software | Website/platform |
| Works on your computer | Works on the internet |
| Tracks file changes | Stores your repositories |
| Command line tool | Web interface + CLI |
| Free and open source | Free for public repos |
Part 3: Installing Git
Section titled “Part 3: Installing Git”For macOS
Section titled “For macOS”Check if Git is already installed:
git --versionIf you see a version number, Git is installed! Skip to Part 4.
If not installed:
brew install gitDownload from: git-scm.com/download/mac
For Windows
Section titled “For Windows”- Download Git from git-scm.com/download/win
- Run the installer
- Use default settings (just click Next)
- Restart your terminal
For Linux
Section titled “For Linux”sudo apt-get updatesudo apt-get install gitsudo dnf install gitsudo pacman -S gitVerify Installation
Section titled “Verify Installation”git --versionPart 4: Configuring Git
Section titled “Part 4: Configuring Git”Set up your identity (this will appear in your commits):
# Set your namegit config --global user.name "Your Name"
# Set your email (use the same email as GitHub)git config --global user.email "your.email@example.com"
# Verify your settingsgit config --global --listExample:
git config --global user.name "Bahrul Bangsawan"git config --global user.email "bahrul@example.com"Part 5: Installing GitHub CLI
Section titled “Part 5: Installing GitHub CLI”GitHub CLI (gh) makes it easy to work with GitHub from your terminal.
For macOS
Section titled “For macOS”brew install ghVerify installation:
gh --versionFor Windows
Section titled “For Windows”winget install --id GitHub.cliscoop install gh- Go to cli.github.com
- Download Windows installer
- Run the installer
For Linux
Section titled “For Linux”curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpgecho "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/nullsudo apt updatesudo apt install ghsudo dnf install ghVerify Installation
Section titled “Verify Installation”gh --versionPart 6: Authenticate GitHub CLI
Section titled “Part 6: Authenticate GitHub CLI”Connect your terminal to your GitHub account:
-
Login
Terminal window gh auth login -
Follow the Prompts
You’ll see a series of questions:
? What account do you want to log into?→ Select: GitHub.com? What is your preferred protocol for Git operations?→ Select: HTTPS (easier for beginners)? Authenticate Git with your GitHub credentials?→ Select: Yes? How would you like to authenticate GitHub CLI?→ Select: Login with a web browser -
Copy the Code
You’ll see:
! First copy your one-time code: XXXX-XXXXPress Enter to open github.com in your browser...- Copy the code (highlight and Cmd+C or Ctrl+C)
- Press Enter
- Your browser will open
-
Authorize in Browser
- Paste the code you copied
- Click Continue
- Click Authorize github
- Return to your terminal
-
Verify Login
Terminal window gh auth statusYou should see:
✓ Logged in to github.com as your-username
Part 7: Understanding Project Structure
Section titled “Part 7: Understanding Project Structure”Before creating repositories, let’s understand how projects are organized:
Basic Project Structure
Section titled “Basic Project Structure”Directorymy-project/ Project folder (root)
Directory.git/ hidden Git tracking
- …
- .gitignore Files to ignore
- README.md Project description
Directorysrc/ Source code
- index.js
Directorycomponents/
- …
Directorypublic/ Static files
- …
- package.json Node.js Project info
Important Files
Section titled “Important Files”| File/Folder | Purpose |
|---|---|
.git/ | Git’s database (never touch this!) |
.gitignore | List of files Git should ignore |
README.md | Project documentation (shows on GitHub) |
src/ | Your code files |
node_modules/ | Dependencies (don’t commit this!) |
What to Commit vs Ignore
Section titled “What to Commit vs Ignore”Push to GitHub:
- Your source code (
src/,*.js,*.css, etc.) - Configuration files (
package.json,tsconfig.json) - Documentation (
README.md,docs/) - Assets you created (
images/,fonts/)
Add to .gitignore:
- Dependencies (
node_modules/,vendor/) - Build outputs (
dist/,build/,.next/) - Environment variables (
.env,.env.local) - System files (
.DS_Store,Thumbs.db) - API keys and secrets
- Large binary files
Part 8: Creating Your First Repository
Section titled “Part 8: Creating Your First Repository”Let’s create a project and push it to GitHub!
Method 1: Create Locally, Then Push Recommended
Section titled “Method 1: Create Locally, Then Push ”-
Create a Project Folder
Terminal window # Create a new foldermkdir my-first-projectcd my-first-project -
Initialize Git
Terminal window # Initialize Git in this foldergit init -
Create Some Files
Terminal window # Create a README fileecho "# My First Project" > README.md# Create a simple HTML file (example)echo "<!DOCTYPE html><html><head><title>My Project</title></head><body><h1>Hello, GitHub!</h1></body></html>" > index.html# Create .gitignoreecho "node_modules/.env.DS_Storedist/" > .gitignoreYour structure now looks like:
Directorymy-first-project/
Directory.git/ Git tracking
- …
- .gitignore Ignore rules
- README.md Project description
- index.html Your code
-
Check Status
Terminal window git statusYou’ll see:
Untracked files:.gitignoreREADME.mdindex.html -
Stage Files (Add to Git)
Terminal window git add .Terminal window git add README.mdgit add index.htmlCheck status again:
Terminal window git statusNow you’ll see:
Changes to be committed:new file: .gitignorenew file: README.mdnew file: index.html -
Commit (Save Snapshot)
Terminal window git commit -m "Initial commit: Add README and index.html"You’ll see:
[main 1a2b3c4] Initial commit: Add README and index.html3 files changed, 15 insertions(+)create mode 100644 .gitignorecreate mode 100644 README.mdcreate mode 100644 index.html -
Create GitHub Repository
Terminal window gh repo create my-first-project --public --source=. --remote=origin --pushYou’ll see:
✓ Created repository username/my-first-project on GitHub✓ Added remote https://github.com/username/my-first-project.git✓ Pushed commits to https://github.com/username/my-first-project.git
Method 2: Create on GitHub First
Section titled “Method 2: Create on GitHub First”-
Create Repository on GitHub
Terminal window gh repo create my-project --publicYou’ll be asked:
? This will create 'my-project' in your account. Continue?→ Select: Yes -
Clone to Your Computer
Terminal window gh repo clone my-projectcd my-project -
Add Files & Push
Terminal window # Create filesecho "# My Project" > README.md# Add filesgit add .# Commitgit commit -m "Initial commit"# Push to GitHubgit push
Part 9: Daily Git Workflow
Section titled “Part 9: Daily Git Workflow”Here’s your typical workflow when working on projects:
The Basic Cycle
Section titled “The Basic Cycle”-
Make changes to your files (edit code in your editor)
-
Check what changed
Terminal window git status -
See specific changes
Terminal window git diff -
Stage files
Terminal window git add .Terminal window git add filename.js -
Commit with message
Terminal window git commit -m "Add new feature" -
Push to GitHub
Terminal window git push
Visual Workflow
Section titled “Visual Workflow”Working Directory → Staging Area → Local Repository → GitHub(your files) (git add) (git commit) (git push)
example.js → example.js → ✓ Committed → ☁️ GitHub(modified) (staged) (saved locally) (backed up)Part 10: Essential Git Commands
Section titled “Part 10: Essential Git Commands”Status & Info
Section titled “Status & Info”# Check statusgit status
# View commit historygit log
# View changesgit diff
# Show branch infogit branchAdding & Committing
Section titled “Adding & Committing”# Add all filesgit add .
# Add specific filegit add filename.js# Commit with messagegit commit -m "Your message"
# Add and commit in one stepgit commit -am "Your message"Pushing & Pulling
Section titled “Pushing & Pulling”# Push to GitHubgit push
# Pull from GitHub (get latest changes)git pull
# Push for the first timegit push -u origin mainBranching
Section titled “Branching”# Create new branchgit branch feature-name
# Switch to branchgit checkout feature-name
# Create and switch in one commandgit checkout -b feature-name
# List all branchesgit branch
# Delete branchgit branch -d feature-nameUndoing Changes
Section titled “Undoing Changes”# Discard changes in filegit checkout -- filename.js# Unstage file (undo git add)git reset filename.js# Undo last commit (keep changes)git reset --soft HEAD~1
# Undo last commit (discard changes)git reset --hard HEAD~1Part 11: GitHub CLI Commands
Section titled “Part 11: GitHub CLI Commands”Repository Management
Section titled “Repository Management”# Create repositorygh repo create repo-name --public
# Create private repositorygh repo create repo-name --private
# Clone repositorygh repo clone username/repo-name
# View repositorygh repo view
# Delete repository (careful!)gh repo delete repo-nameWorking with Issues
Section titled “Working with Issues”# Create issuegh issue create
# List issuesgh issue list
# View specific issuegh issue view 123
# Close issuegh issue close 123Pull Requests
Section titled “Pull Requests”# Create pull requestgh pr create
# List pull requestsgh pr list
# View pull requestgh pr view 123
# Merge pull requestgh pr merge 123Other Useful Commands
Section titled “Other Useful Commands”# Open repository in browsergh repo view --web
# Check authenticationgh auth status
# Logoutgh auth logoutPart 12: Real-World Example
Section titled “Part 12: Real-World Example”Let’s create a complete project from scratch:
Scenario: Creating a Portfolio Website
Section titled “Scenario: Creating a Portfolio Website”-
Create Project
Terminal window # Create and enter foldermkdir portfolio-websitecd portfolio-website# Initialize Gitgit init -
Create Files
Terminal window # Create READMEecho "# Portfolio WebsiteMy personal portfolio showcasing my projects.## Technologies- HTML- CSS- JavaScript" > README.md# Create HTML fileecho "<!DOCTYPE html><html><head><title>My Portfolio</title><link rel='stylesheet' href='style.css'></head><body><h1>Welcome to My Portfolio</h1><p>Check out my projects!</p></body></html>" > index.html# Create CSS fileecho "body {font-family: Arial, sans-serif;margin: 0;padding: 20px;background-color: #f5f5f5;}h1 {color: #333;}" > style.css# Create .gitignoreecho ".DS_Storenode_modules/.env" > .gitignore -
Project Structure
Directoryportfolio-website/
Directory.git/
- …
- .gitignore
- README.md
- index.html
- style.css
-
First Commit
Terminal window # Check filesgit status# Add all filesgit add .# Commitgit commit -m "Initial commit: Add portfolio structure" -
Create GitHub Repository & Push
Terminal window # Create repo and pushgh repo create portfolio-website --public --source=. --remote=origin --push -
Make Changes
Terminal window # Edit index.html (use your editor)# Add more content...# Check what changedgit diff# Stage changesgit add index.html# Commitgit commit -m "Add project section to homepage"# Push to GitHubgit push
Part 13: Common Scenarios & Solutions
Section titled “Part 13: Common Scenarios & Solutions”Scenario 1: I Made a Mistake in My Last Commit
Section titled “Scenario 1: I Made a Mistake in My Last Commit”# Change the commit messagegit commit --amend -m "New message"# Add forgotten files to last commitgit add forgotten-file.jsgit commit --amend --no-editScenario 2: I Want to Ignore Files I Already Committed
Section titled “Scenario 2: I Want to Ignore Files I Already Committed”-
Add to .gitignore first
Terminal window echo "secret.txt" >> .gitignore -
Remove from Git (but keep file)
Terminal window git rm --cached secret.txt -
Commit the change
Terminal window git commit -m "Stop tracking secret.txt"git push
Scenario 3: Someone Else Updated the Repository
Section titled “Scenario 3: Someone Else Updated the Repository”# Get latest changesgit pull
# If there are conflicts, Git will tell you# Edit the conflicted files# Then:git add .git commit -m "Resolve merge conflicts"git pushScenario 4: I Want to Start Over
Section titled “Scenario 4: I Want to Start Over”# Discard ALL local changesgit reset --hard
# Get fresh copy from GitHubgit pullScenario 5: Wrong Remote URL
Section titled “Scenario 5: Wrong Remote URL”# Check current remotegit remote -v
# Change remote URLgit remote set-url origin https://github.com/username/new-repo.git
# Verifygit remote -vPart 14: Best Practices
Section titled “Part 14: Best Practices”Commit Messages
Section titled “Commit Messages”git commit -m "Add user authentication"git commit -m "Fix navigation bug on mobile"git commit -m "Update README with installation steps"git commit -m "stuff"git commit -m "changes"git commit -m "idk"git commit -m "final final FINAL version"Commit Frequency
Section titled “Commit Frequency”.gitignore Essentials
Section titled “.gitignore Essentials”# Dependenciesnode_modules/vendor/
# Build outputsdist/build/*.min.js
# Environment variables.env.env.local.env.production
# System files.DS_StoreThumbs.db*.swp
# IDE settings.vscode/.idea/*.suo
# Logs*.lognpm-debug.log*README Best Practices
Section titled “README Best Practices”Part 15: Troubleshooting
Section titled “Part 15: Troubleshooting””Permission Denied (publickey)”
Section titled “”Permission Denied (publickey)”””fatal: not a git repository”
Section titled “”fatal: not a git repository”””Everything up-to-date”
Section titled “”Everything up-to-date”””divergent branches”
Section titled “”divergent branches””Accidentally Committed Large File
Section titled “Accidentally Committed Large File”# Remove from last commitgit rm --cached large-file.zipgit commit --amend -m "Remove large file"git push --force
# Add to .gitignoreecho "large-file.zip" >> .gitignorePart 16: Quick Reference
Section titled “Part 16: Quick Reference”Essential Commands Cheat Sheet
Section titled “Essential Commands Cheat Sheet”git config --global user.name "Your Name"git config --global user.email "email@example.com"gh auth logingit initgh repo create project-name --public --source=. --remote=origin --pushgit status # Check statusgit add . # Stage all filesgit commit -m "Message" # Commit changesgit push # Push to GitHubgit pull # Get latest changesgit branch branch-name # Create branchgit checkout branch-name # Switch branchgit checkout -b new-branch # Create and switchgit merge branch-name # Merge branchgh repo create name # Create repositorygh repo clone user/repo # Clone repositorygh repo view --web # Open in browsergh issue create # Create issuegh pr create # Create pull requestSummary
Section titled “Summary”You’ve learned how to:
- Create a GitHub account
- Install and configure Git
- Install and authenticate GitHub CLI
- Understand project structure
- Create repositories
- Commit and push code
- Use essential Git commands
- Follow best practices
- Troubleshoot common issues
Next Steps
Section titled “Next Steps”- Practice: Create a few test repositories
- Explore: Browse other projects on GitHub
- Contribute: Try contributing to open source
- Learn More:
- Git branching strategies
- GitHub Actions (automation)
- Collaboration workflows
- Advanced Git commands
Useful Resources
Section titled “Useful Resources”Happy coding!