Skip to content

Learn Github

A complete beginner-friendly guide to GitHub, from creating an account to pushing your code using the GitHub CLI (Command Line Interface).

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.

  • 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

  1. Sign Up

    1. Go to github.com
    2. Click Sign up button (top right)
    3. Enter your email address
    4. Create a strong password
    5. Choose a username (this will be your GitHub identity)
      • Example: bahrulbangsawan, johndoe, etc.
      • This appears in your URLs: github.com/your-username
    6. Click Continue
  2. Verify Your Account

    1. Check your email inbox
    2. GitHub will send a verification code
    3. Enter the code on GitHub
    4. Click Verify
  3. 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
  4. Choose a Plan

    • Select Free (perfect for most users)
    • Free plan includes:
      • Unlimited public repositories
      • Unlimited private repositories
      • GitHub Actions (automation)
      • And more!

Before we continue, let’s clarify the difference:

GitGitHub
Version control softwareWebsite/platform
Works on your computerWorks on the internet
Tracks file changesStores your repositories
Command line toolWeb interface + CLI
Free and open sourceFree for public repos

Check if Git is already installed:

Terminal window
git --version

If you see a version number, Git is installed! Skip to Part 4.

If not installed:

Terminal window
brew install git
  1. Download Git from git-scm.com/download/win
  2. Run the installer
  3. Use default settings (just click Next)
  4. Restart your terminal
Terminal window
sudo apt-get update
sudo apt-get install git
Terminal window
git --version

Set up your identity (this will appear in your commits):

Terminal window
# Set your name
git 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 settings
git config --global --list

Example:

Configure Git
git config --global user.name "Bahrul Bangsawan"
git config --global user.email "bahrul@example.com"

GitHub CLI (gh) makes it easy to work with GitHub from your terminal.

Terminal window
brew install gh

Verify installation:

Terminal window
gh --version
Terminal window
winget install --id GitHub.cli
Terminal window
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
echo "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/null
sudo apt update
sudo apt install gh
Terminal window
gh --version

Connect your terminal to your GitHub account:

  1. Login

    Terminal window
    gh auth login
  2. 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
  3. Copy the Code

    You’ll see:

    ! First copy your one-time code: XXXX-XXXX
    Press Enter to open github.com in your browser...
    1. Copy the code (highlight and Cmd+C or Ctrl+C)
    2. Press Enter
    3. Your browser will open
  4. Authorize in Browser

    1. Paste the code you copied
    2. Click Continue
    3. Click Authorize github
    4. Return to your terminal
  5. Verify Login

    Terminal window
    gh auth status

    You should see:

    ✓ Logged in to github.com as your-username

Before creating repositories, let’s understand how projects are organized:

  • 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
File/FolderPurpose
.git/Git’s database (never touch this!)
.gitignoreList of files Git should ignore
README.mdProject documentation (shows on GitHub)
src/Your code files
node_modules/Dependencies (don’t commit this!)

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/)

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 ”
  1. Create a Project Folder

    Terminal window
    # Create a new folder
    mkdir my-first-project
    cd my-first-project
  2. Initialize Git

    Terminal window
    # Initialize Git in this folder
    git init
  3. Create Some Files

    Terminal window
    # Create a README file
    echo "# 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 .gitignore
    echo "node_modules/
    .env
    .DS_Store
    dist/" > .gitignore

    Your structure now looks like:

    • Directorymy-first-project/
      • Directory.git/ Git tracking
      • .gitignore Ignore rules
      • README.md Project description
      • index.html Your code
  4. Check Status

    Terminal window
    git status

    You’ll see:

    Untracked files:
    .gitignore
    README.md
    index.html
  5. Stage Files (Add to Git)

    Terminal window
    git add .

    Check status again:

    Terminal window
    git status

    Now you’ll see:

    Changes to be committed:
    new file: .gitignore
    new file: README.md
    new file: index.html
  6. 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.html
    3 files changed, 15 insertions(+)
    create mode 100644 .gitignore
    create mode 100644 README.md
    create mode 100644 index.html
  7. Create GitHub Repository

    Terminal window
    gh repo create my-first-project --public --source=. --remote=origin --push

    You’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

  1. Create Repository on GitHub

    Terminal window
    gh repo create my-project --public

    You’ll be asked:

    ? This will create 'my-project' in your account. Continue?
    → Select: Yes
  2. Clone to Your Computer

    Terminal window
    gh repo clone my-project
    cd my-project
  3. Add Files & Push

    Terminal window
    # Create files
    echo "# My Project" > README.md
    # Add files
    git add .
    # Commit
    git commit -m "Initial commit"
    # Push to GitHub
    git push

Here’s your typical workflow when working on projects:

  1. Make changes to your files (edit code in your editor)

  2. Check what changed

    Terminal window
    git status
  3. See specific changes

    Terminal window
    git diff
  4. Stage files

    Terminal window
    git add .
  5. Commit with message

    Terminal window
    git commit -m "Add new feature"
  6. Push to GitHub

    Terminal window
    git push
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)

Terminal window
# Check status
git status
# View commit history
git log
# View changes
git diff
# Show branch info
git branch
Terminal window
# Add all files
git add .
# Add specific file
git add filename.js
Terminal window
# Push to GitHub
git push
# Pull from GitHub (get latest changes)
git pull
# Push for the first time
git push -u origin main
Terminal window
# Create new branch
git branch feature-name
# Switch to branch
git checkout feature-name
# Create and switch in one command
git checkout -b feature-name
# List all branches
git branch
# Delete branch
git branch -d feature-name
Terminal window
# Discard changes in file
git checkout -- filename.js

Terminal window
# Create repository
gh repo create repo-name --public
# Create private repository
gh repo create repo-name --private
# Clone repository
gh repo clone username/repo-name
# View repository
gh repo view
# Delete repository (careful!)
gh repo delete repo-name
Terminal window
# Create issue
gh issue create
# List issues
gh issue list
# View specific issue
gh issue view 123
# Close issue
gh issue close 123
Terminal window
# Create pull request
gh pr create
# List pull requests
gh pr list
# View pull request
gh pr view 123
# Merge pull request
gh pr merge 123
Terminal window
# Open repository in browser
gh repo view --web
# Check authentication
gh auth status
# Logout
gh auth logout

Let’s create a complete project from scratch:

  1. Create Project

    Terminal window
    # Create and enter folder
    mkdir portfolio-website
    cd portfolio-website
    # Initialize Git
    git init
  2. Create Files

    Terminal window
    # Create README
    echo "# Portfolio Website
    My personal portfolio showcasing my projects.
    ## Technologies
    - HTML
    - CSS
    - JavaScript" > README.md
    # Create HTML file
    echo "<!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 file
    echo "body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 20px;
    background-color: #f5f5f5;
    }
    h1 {
    color: #333;
    }" > style.css
    # Create .gitignore
    echo ".DS_Store
    node_modules/
    .env" > .gitignore
  3. Project Structure

    • Directoryportfolio-website/
      • Directory.git/
      • .gitignore
      • README.md
      • index.html
      • style.css
  4. First Commit

    Terminal window
    # Check files
    git status
    # Add all files
    git add .
    # Commit
    git commit -m "Initial commit: Add portfolio structure"
  5. Create GitHub Repository & Push

    Terminal window
    # Create repo and push
    gh repo create portfolio-website --public --source=. --remote=origin --push
  6. Make Changes

    Terminal window
    # Edit index.html (use your editor)
    # Add more content...
    # Check what changed
    git diff
    # Stage changes
    git add index.html
    # Commit
    git commit -m "Add project section to homepage"
    # Push to GitHub
    git push

Scenario 1: I Made a Mistake in My Last Commit

Section titled “Scenario 1: I Made a Mistake in My Last Commit”
Terminal window
# Change the commit message
git commit --amend -m "New message"

Scenario 2: I Want to Ignore Files I Already Committed

Section titled “Scenario 2: I Want to Ignore Files I Already Committed”
  1. Add to .gitignore first

    Terminal window
    echo "secret.txt" >> .gitignore
  2. Remove from Git (but keep file)

    Terminal window
    git rm --cached secret.txt
  3. 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”
Terminal window
# Get latest changes
git pull
# If there are conflicts, Git will tell you
# Edit the conflicted files
# Then:
git add .
git commit -m "Resolve merge conflicts"
git push
Terminal window
# Discard ALL local changes
git reset --hard
# Get fresh copy from GitHub
git pull
Terminal window
# Check current remote
git remote -v
# Change remote URL
git remote set-url origin https://github.com/username/new-repo.git
# Verify
git remote -v

Terminal window
git commit -m "Add user authentication"
git commit -m "Fix navigation bug on mobile"
git commit -m "Update README with installation steps"
.gitignore
# Dependencies
node_modules/
vendor/
# Build outputs
dist/
build/
*.min.js
# Environment variables
.env
.env.local
.env.production
# System files
.DS_Store
Thumbs.db
*.swp
# IDE settings
.vscode/
.idea/
*.suo
# Logs
*.log
npm-debug.log*

Terminal window
# Remove from last commit
git rm --cached large-file.zip
git commit --amend -m "Remove large file"
git push --force
# Add to .gitignore
echo "large-file.zip" >> .gitignore

Terminal window
git config --global user.name "Your Name"
git config --global user.email "email@example.com"
gh auth login

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
  1. Practice: Create a few test repositories
  2. Explore: Browse other projects on GitHub
  3. Contribute: Try contributing to open source
  4. Learn More:
    • Git branching strategies
    • GitHub Actions (automation)
    • Collaboration workflows
    • Advanced Git commands

Happy coding!