GitHub Tutorial: From Beginner to Advanced

GitHub Tutorial: From Beginner to Advanced

Some office gossip from a friend few days back about how even some senior developers these days are using advanced tools like Cursor but still can't get out of the habit of mindlessly typing git add . for everything. That conversation got me thinking, maybe it's time to compile my learning and knowledge of version control into something that could become a good reference, both for myself and for others who want to break out of the "add, commit, push" bubble.

Article content

Here's the thing: git add . is risky. Really risky. When you blindly stage everything, you're missing the entire point of version control. You're throwing away the opportunity to create meaningful, atomic commits that tell a story about your code's evolution. You're potentially committing sensitive files, debug code, or half-finished features that shouldn't see the light of day.

I know many developers prefer GUI tools for Git, and that's perfectly fine—they can even be better than CLI in certain situations. But personally, I feel like I have much more control over my code when I use the command line. That's the approach I'll focus on in this guide, because understanding Git at the command level gives you the foundation to use any tool effectively.

Whether you're just starting your coding journey or you're a senior developer looking to break some bad habits, this guide will take you from the basics to advanced Git mastery. Let's explore the world of version control beyond the simple three-command cycle and discover what makes Git truly powerful.


What is git and github

Git is a distributed version control system that tracks changes in your code over time. Think of it as a sophisticated "save" system that remembers every version of your project.

GitHub is a cloud-based platform that hosts Git repositories and adds powerful collaboration features, making it easy to share code, collaborate with others, and manage projects.

Setting Up Your Environment

  1. Install Git on your computer
  2. Configure your identity:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"        

This is just to setup your credentials on command line (I am assuming if anyone is reading this, they would know how to setup a github account)


Key Git Objects

Repository (Repo): A directory containing your project files plus a hidden .git folder that stores all version history.

Commit: A snapshot of your project at a specific point in time. Think of it as a save point in a video game—you can always return to it.

Branch: A lightweight, movable pointer to a specific commit. The default branch is usually called main or master.

HEAD: A special pointer that indicates which commit you're currently looking at (usually the latest commit on your current branch).


Article content

You can git init in your local folder to initaite a git repository.

Essential Git Terminology

Clone: Download a complete copy of a repository from GitHub to your computer

Fork: Create your own copy of someone else's repository on GitHub

Pull Request (PR): A request to merge your changes into another branch/repository

Merge: Combine changes from different branches

Conflict: When Git can't automatically merge changes and needs human intervention

Remote: A version of your repository hosted elsewhere (like on GitHub)

Origin: The default name for the remote repository you cloned from

Upstream: The original repository you forked from (if applicable)

The Git Workflow Philosophy

Git encourages a specific way of thinking about changes:

Atomic Commits: Each commit should represent one logical change

  • "Add user authentication"
  • " Fix login button styling"
  • " Add feature, fix bugs, update styles"

Meaningful History: Your commit history should tell the story of how your project evolved

  • Future developers (including you) should understand what happened and why
  • Each commit should be potentially revertible without breaking other features

Essential git commands

git status                   # ALWAYS start here(i spam my terminal with this whenever i am working) see what's changed

git diff                     # Review changes before staging

git add filename.txt         # Stage specific files intentionally

git add -p                   # Stage parts of files interactively

git commit -m "Your message" # Save changes with a description

git log --oneline            # View commit history concisely

git push                     # Upload changes to GitHub

git pull                     # Download latest changes        

Instead of the mindless "add everything" cycle, adopt this intentional approach:

  • Review what you've changed with git status and git diff
  • Stage selectively using git add filename for specific files
  • Use git add -p to stage parts of files when needed
  • Commit atomically with clear, descriptive messages
  • Push your well-crafted commits to GitHub


Let me introduce gh before getting into git workflows.

GitHub CLI is a game-changer for productivity. Why switch to your browser when you can do everything from the terminal?

Mac OS users can install github cli using - brew install gh.

Windows - I dont know honestly, claude says - winget install GitHub.cli (i have no way to check if this correct, so proceed with caution)


gh auth login  #login using your credentials
gh auth status #check authentication status        

The Power of gh repo create

If you have not used it before, once you just create a repository from cli you will feel the magic.

Basic Repository Creation:

# Create a new repository on GitHub
gh repo create my-awesome-project

# Create with description and visibility
gh repo create my-project --description "My awesome project" --public

# Create and clone immediately
gh repo create my-project --clone

# Create from existing local directory
cd my-existing-project
gh repo create --source=. --push        

More customised version of repository creation:

gh repo create my-project \
  --description "Description here" \
  --homepage "https://myproject.com" \
  --public \
  --add-readme \
  --gitignore=node \
  --license=mit \
  --clone        

Understanding the Git Workflow

The basic cycle every developer uses daily:

  1. Modify files in your working directory
  2. Stage changes with git add
  3. Commit changes with git commit
  4. Push to GitHub with git push

Branching and Merging Strategy

Now that you understand the daily workflow, let's dive deeper into branching strategies that make collaboration smooth.

Branches allow you to work on features without affecting the main codebase. But there's more strategy involved than just creating branches randomly.

git branch feature-name      # Create new branch
git checkout feature-name    # Switch to branch
git checkout -b feature-name # Create and switch in one command
git merge feature-name       # Merge branch into current branch
git branch -d feature-name   # Delete branch after merging
git branch -v                # See all branches with latest commit info        

GitHub CLI Commands You'll Use Daily

Repository Operations:

gh repo view                    # View repository details in terminal
gh repo edit --enable-wiki     # Enable wiki without opening browser
gh repo edit --description "New description"
gh repo clone owner/repo       # Clone with enhanced GitHub integration
gh repo fork owner/repo        # Fork a repository
gh repo sync                   # Sync fork with upstream        

Best Practices for Branching:

  • Use descriptive names: feature/user-authentication or bugfix/login-error
  • Keep branches focused on single features or fixes
  • Always create branches from the latest main/branch

Pull Requests: The Heart of Team Collaboration

Before diving into GitHub CLI commands for PRs, let's understand what Pull Requests actually are and why they're fundamental to modern software development.

What is a Pull Request?

A Pull Request (PR) is GitHub's way of saying "Hey team, I've made some changes to the code. Please review them before we merge them into the main branch."

How PRs Work:

  1. You create a feature branch and make changes
  2. Push your branch to GitHub
  3. Open a Pull Request comparing your branch to main
  4. Team members review your code
  5. Discussion happens, changes are requested/approved
  6. PR gets merged into main branch.

The Code Review Process

Code review isn't just about catching bugs—it's about knowledge sharing, maintaining code quality, and building better software as a team.

What Reviewers Look For:

Functionality & Logic:

  • Does the code do what it's supposed to do?
  • Are there edge cases that aren't handled?
  • Is the logic clear and correct?

Code Quality:

  • Is the code readable and well-organized?
  • Are variable names descriptive?
  • Is there appropriate error handling?
  • Are there any code smells or anti-patterns?

The Review Workflow

For PR Authors:

Opening the PR:

# Push your branch
git push origin feature/specific-feature

# Create PR with context
gh pr create --title "completed feature implementation" \
  --body "Certain feature implemented #45"

# Check PR status
gh pr status        

Addressing Feedback:

# Make requested changes
git add src/auth.js
git commit -m "fix: certain fix applied"
git push  # Automatically updates the PR

# View PR conversation
gh pr view --comments

# Mark PR as ready after addressing feedback
gh pr ready  # If it was in draft mode        

For reviewers:

# Check out PR locally for testing
gh pr checkout 123

# View PR details and diff
gh pr view 123 --diff

# Add review comments
gh pr review 123 --approve
gh pr review 123 --request-changes --body "Please add error handling"
gh pr review 123 --comment --body "Looks good overall, minor suggestions"        

Types of PR Reviews

Approval: Code is ready to merge

gh pr review --approve --body "LGTM! Great work on the error handling."        

Request Changes: Issues that must be fixed before merge

gh pr review --request-changes --body "Please add unit tests for the new authentication methods"        

Comment: Feedback without blocking merge

gh pr review --comment --body "Minor suggestion: consider extracting this logic into a helper function"        

Handling Conflicts and Common Issues

Merge Conflicts happen when Git can't automatically combine changes. Here's how to resolve them:

  • Git will mark conflicts in your files:

<<<<<<< HEAD
Your changes
=======
Conflicting changes
>>>>>>> branch-name        

  • Edit the file to resolve conflicts
  • Remove conflict markers
  • Stage and commit the resolution

Common Commands for Fixing Mistakes:

git reset HEAD~1             # Undo last commit (keep changes)
git reset --hard HEAD~1      # Undo last commit (lose changes)
git checkout -- filename     # Discard changes to specific file
git stash                    # Temporarily save changes
git stash pop                # Restore stashed changes        

GitHub Actions: Automation Powerhouse

Automate testing, building, and deployment with workflows. And here's where GitHub CLI shines even more - you can manage workflows directly from the terminal:

# Watch your workflow in real-time
gh run watch

# Re-run failed workflows
gh run rerun --failed

# Download artifacts
gh run download        

Repository Management Best Practices

Security and Access Control

Branch Protection Rules:

  • Require pull request reviews
  • Require status checks to pass
  • Restrict who can push to main
  • Require up-to-date branches

Managing Sensitive Data:

  • Use .gitignore to exclude sensitive files
  • GitHub Secrets for API keys and passwords
  • Environment variables for configuration
  • Regular security audits

Advanced Git Operations: Rebase, Cherry-Pick, and Merge Strategies

These are the operations that separate intermediate developers from advanced ones. Understanding when and how to use each technique is crucial for maintaining clean project history.

Git Rebase: Rewriting History

Rebase rewrites commit history by moving commits to a new base. Think of it as saying "pretend my changes were made on top of the latest main branch."

# Visual representation:
# Before rebase:
#   main:    A---B---C
#   feature:      \---D---E
#
# After rebase:
#   main:    A---B---C
#   feature:           \---D'---E'        

Interactive Rebase for Clean History:

git rebase -i HEAD~3         # Edit last 3 commits

# In the interactive editor:
# pick   = use this commit
# reword = use commit, but edit the message  
# edit   = use commit, but stop for amending
# squash = use commit, but meld into previous commit
# fixup  = like squash, but discard commit message
# drop   = remove commit entirely        

Practical Rebase Scenarios:

# Keep feature branch updated with main
git checkout feature/user-auth
git fetch origin main
git rebase origin/main     # Move your commits on top of latest main

# Clean up messy commits before PR
git rebase -i HEAD~5       # Squash "fix typo" commits together        

Cherry-Pick: Selecting Specific Changes

Cherry-pick applies specific commits from one branch to another without merging entire branches.

git cherry-pick commit-hash        # Apply specific commit
git cherry-pick abc123 def456      # Apply multiple commits
git cherry-pick branch-name        # Apply latest commit from branch        

Merge Strategies: Three Ways to Combine Branches

Understanding different merge strategies is crucial for maintaining the right project history.

1. Regular Merge (Preserve Branch History)

git checkout main
git merge feature/user-auth        

2. Squash Merge (Clean Linear History)

git checkout main
git merge --squash feature/user-auth
git commit -m "feat: add complete user authentication system"        

GitHub CLI Squash Merge:

gh pr merge --squash --delete-branch        

3. No-Fast-Forward Merge (Always Create Merge Commit)

git checkout main  
git merge --no-ff feature/user-auth        

Advanced Debugging

Finding Changes:

git blame filename           # See who changed each line
git log --grep="keyword"     # Search commit messages
git log -S "function_name"   # Search for code changes
git diff HEAD~2 HEAD         # Compare commits        

The CLI Advantage: When you master both Git and GitHub CLI, you gain unprecedented control and speed. You can create repositories, manage issues, review code, and deploy applications—all without leaving your terminal. This isn't just about efficiency; it's about maintaining focus and building a professional workflow that impresses collaborators and employers alike.


References:

https://github.blog/news-insights/product-news/github-cli-1-0-is-now-available/

https://www.atlassian.com/blog/it-teams/pull-request-merge-strategies-the-great-debate

https://anyantudre.hashnode.dev/git-fundamentals-for-data-science-and-machine-learning

https://worknme.wordpress.com/2017/04/03/git-notes-4-commands-list/





To view or add a comment, sign in

More articles by Amritesh Anand

Others also viewed

Explore content categories