Open the WaytoAGI knowledge base on Feishu · 100M+ visits, open to everyone →
WaytoAGI

Knowledge Base · Learning paths

GitHub from Beginner to Expert: The Ultimate 10,000-Word Guide

Master Git and GitHub from zero to hero with this comprehensive guide. Learn essential commands like git init, add, commit, branching, pull requests, troubleshooting common errors, and best practices to efficiently manage projects, versions, and team collaboration.

GitHub tutorialGit beginner guidelearn Git and GitHubGit commandsGit branching

Translated from Chinese by AI. Spotted an issue? Use the feedback button.

🔗 Original post: https://x.com/miles_mazy/status/208...

If you want to make money with GitHub, the most direct route is fairly simple: find valuable open-source projects, make sure their licenses allow it, package deployment, Chinese-language documentation, and after-sales support into a service, and sell that service on platforms like Xianyu.

But what people really pay for is your ability to sift through information and put things into practice. Whether you want to work in AI, become a forward-deployed engineer (FDE), or build a one-person company (OPC), sooner or later your code, documentation, version history, and collaboration will end up on GitHub.

Even if you’re a creator or independent publisher, GitHub has plenty of topic-research tools, automation projects, and content-production workflows. But as files pile up and AI starts making changes, things can quickly spiral out of control without Git to manage versions. So it’s not just programmers who need to learn it—people building projects and creating content do too. It determines whether you can turn an idea into a manageable, reusable project that’s ready to deliver.

I spent more than half a month working on this tutorial on and off, going hands-on with everything from Git and GitHub to commits, branches, PRs, and common errors, starting from scratch. My upcoming livestreams will follow the same workflow. Before they begin, I’m sharing this tutorial openly so you can bookmark it or work through the whole thing yourself.

1. Git vs. GitHub: What Does Each One Do?

Git is a version control tool installed on your computer. Even without an internet connection, you can still commit changes, view history, create branches, and merge them. GitHub is a remote repository hosting and collaboration platform. It receives the commits you push with Git and provides Issues, Pull Requests, Actions, code reviews, and access management.

One of the most confusing things about Git is that the same set of changes can exist in four different places. The infographic below breaks these down into four layers: the working directory, staging area, local repository, and remote repository.

Hitting Save only writes your changes to disk. git add selects what to include, git commit records a version locally, and git push sends those commits to GitHub.

That’s why you should review the diff and run or test your project before committing. After a successful push, check the GitHub page to confirm the changes are there. That way, if something goes wrong, you can immediately tell which layer it got stuck at.

2. Before You Begin, Prepare Just Four Things

You need Git, a GitHub account, an editor, and a practice project. VS Code is all you need for an editor, and your project can be a web page or a Markdown document.

First, check Git in your terminal:

bash
git --version

This walkthrough uses macOS and Git 2.49.0. Windows users can use Git Bash or VS Code’s integrated terminal; the Git commands below are the same.

Next, configure the commit author:

bash
git config --global user.name "Your Name"
git config --global user.email "Your Email"

This is the author information recorded in your commits; it does not log you into GitHub. If you only want to configure it for the current practice project, replace --global with --local.

Authenticating with GitHub is a separate step. There are three common command-line options:

  • GitHub CLI: run gh auth login to authorize access through your browser;

  • HTTPS: use a Personal Access Token or a credential manager;

  • SSH: add your public key to GitHub, then authenticate with your key.

If you’re just starting out, GitHub CLI or HTTPS is a good choice. When using HTTPS, enter your token when the terminal asks for a Password—your regular account password no longer works for this. Never put tokens in commands, remote URLs, READMEs, chats, or screenshots.

3. Don’t Rush to Run init—First Check Where Your Terminal Is

This exercise starts with a simple web page. It opens in a browser, but it has no Git history yet.

The project contains three files:

index.html
style.css
.gitignore

In VS Code, choose “Open Folder” rather than opening just a single HTML file. Then run the following in the integrated terminal:

pwd
ls

pwd shows your current directory, and ls lists its files. Only continue once you can see index.html and style.css.

This check may seem trivial, but it prevents one of the messiest mistakes: running git init on the Desktop, in Documents, or even in your home directory, then using git add . to stage thousands of unrelated files. Git isn’t broken—you’re just in the wrong directory.

4. What git init Does

Now initialize the repository:

bash
git init -b main
git status --short

git init -b main creates .git in the current directory and names the initial branch main. .git is a hidden directory that stores information about commits, branches, the staging area, remote URLs, and more. Your project files stay where they are; Git starts monitoring them from this point on.

The ?? in the screenshot means untracked. The files exist, but Git has not yet been told to track them.

To confirm the repository’s root directory, run:

bash
git rev-parse --show-toplevel

The output should be your current project folder. If you see fatal: not a git repository, first check which directory you are in, then check whether you have run git init.

5. Your First Commit: Establish a Reliable Starting Point

Why commit before making any changes to the project? Because every change that follows needs a baseline for comparison. First, open the web page in a browser and confirm that the title, cards, and sign-up section display correctly. Narrow the window to a mobile-screen width and check for horizontal scrolling.

Next, review .gitignore. Here is what we are using:

.env
.env.*
*.log
node_modules/
dist/
build/

.gitignore helps keep secrets, logs, dependencies, and build artifacts out of the repository. It primarily applies to files that are not yet tracked. If a secret has already been committed, adding it to .gitignore later does not remove it from the history. Proper remediation also includes revoking or rotating the secret.

Start selecting files for the first commit:

bash
git add index.html style.css .gitignore
git status --short
git diff --cached --stat

The A in the status output stands for Added, meaning the files are now in the staging area. git diff --cached --stat shows how many files are staged for this commit and roughly how many lines have changed. To inspect the actual changes, run:

git diff --cached

Once you have checked everything, commit:

bash
git commit -m "chore: initialize campus AI recruitment page"
git log --oneline
git status

Think of a commit as a snapshot of your project, complete with an author, timestamp, message, and parent commit. ffdf4ff is the abbreviated hash of this commit. You can use it to identify this exact revision in the current repository.

feat, fix, docs, style, and chore are common commit types, not syntax enforced by Git. What matters more than the prefix is the message that follows: what you did, what you changed, and why.

6. The Second Commit: Treat AI Changes as a Draft to Review

Next, add a “How to Apply” button to the page. When using an AI coding tool, I spell out the boundaries in the prompt:

Modify only index.html. Add a “How to Apply” link below the introductory text,
pointing to #apply on the same page. Do not modify style.css or create a Git commit.
When finished, tell me which file you changed.

Making the change manually is just as simple:

html
<a class="cta" href="#apply">How to Apply</a>

Once the AI says it is done, don’t commit just yet. Run:

bash
git status --short
git diff -- index.html
git diff --check

git diff shows unstaged changes in your working directory. A green + marks an added line, and a red - marks a deleted line. If git diff --check produces no output, it means no obvious formatting issues, such as trailing whitespace, were found; it will not check whether the button actually works.

Return to the browser, refresh the page, click the button, and then narrow the window. The page should scroll to the application section, and the button and cards should still display correctly on a narrow screen.

Commit only after the tests pass:

bash
git add index.html
git diff --cached
git commit -m "feat: add an application link for quick access to application instructions"
git log --oneline -2

Your repository now has two clearly identifiable versions: the initial page and the application button. If the button causes problems later, you can pinpoint the commit that introduced it.

While we’re here, let’s distinguish between the two diff commands:

bash
git diff             # Differences between the working directory and the staging area
git diff --cached    # Differences between the staging area and the latest commit

If git diff produces no output, the file may not have been saved, or the changes may already have been staged or committed. Checking git status, git diff --cached, and git log in that order is more reliable than repeatedly typing git add .

7. Branches: A Place to Experiment with Uncertain Changes

The button adds only one line, so the risk is low. Changing the entire theme from purple to orange could look great—or tacky. That kind of change is a good candidate for a branch.

bash
git switch -c experiment/warm-theme
git branch --show-current

Conceptually, a branch is a name that points to a commit. When a new branch is first created, it points to the same commit as main, so the files are identical. The two lines of development diverge only when you create a new commit on the experimental branch.

In the diagram, the blue main still points to the second commit, while the orange experiment points to the third. The project has not been duplicated into two sets of files; only the commits that the two branch names point to have changed.

Modify the color variables in style.css, refresh the page to check the result, and then commit:

bash
git diff -- style.css
git diff --check
git add style.css
git commit -m "style: try a warm color theme on the experimental branch"
git log --oneline --graph --decorate --all

HEAD indicates your current position. In the screenshot, HEAD points to experiment/warm-theme, while main remains at the button commit.

Once you decide to keep the warm theme, switch back to main and merge:

bash
git switch main
git merge experiment/warm-theme

You see Fast-forward here because no new commits were added to main during the experiment. Git simply moves the main pointer forward to the warm-theme commit, successfully merging the changes.

You can safely delete the merged branch:

bash
git branch -d experiment/warm-theme

The lowercase -d checks whether the branch has been merged. The uppercase -D forces deletion, which may leave unmerged commits on that branch without a reference. Don’t use it as a routine cleanup command.

8. Conflicts Aren’t That Mysterious—Git Just Won’t Choose for You

To test a conflict, I made another copy of the repository. On main, I changed the main heading to “Help Creative Ideas on Campus Reach More People.” On the feature branch, I changed the same line to “Turn an Idea into Something People Can Actually Use.” Git stopped during the merge:

The conflict markers divide the content into three sections:

<<<<<<< HEAD
Content from the current branch
=======
Content from the branch being merged in
>>>>>>> feature/rewrite-heading

To resolve it, edit the file, keep the text you want in the final version, remove all three marker lines, test the result, and then run:

bash
git add index.html
git commit

If you don’t want to resolve it right away, you can abort the merge:

bash
git merge --abort

A conflict means two people or two AI agents have provided different answers for the same location, and Git cannot make that choice for them.

9. Push Your Local Repository to GitHub

The project now has a local history. Next, create a repository on GitHub. Click the + in the upper-right corner, select New repository, and enter a repository name, for example:

campus-ai-demo

For your first practice run, I recommend setting it to Private. Since your local repository already has a README, a .gitignore, and a commit history, leave the new GitHub repository empty. Don’t initialize it with a README, license, or .gitignore on the website. Otherwise, the local and remote repositories will each have their own initial history, and you’ll need to reconcile them before your first push. GitHub’s official “Adding locally hosted code” documentation explicitly warns about this, too.

Copy the HTTPS URL:

https://github.com/your-username/campus-ai-demo.git

Return to your project’s terminal:

bash
git remote add origin https://github.com/your-username/campus-ai-demo.git
git remote -v
git push -u origin main

origin is an alias for the remote URL. Other names work as well; calling the primary remote origin is simply a community convention. The -u option sets up a tracking relationship between your local main branch and origin/main, so you can usually just run git push afterward.

The terminal screenshot below demonstrates a successful push and clone using a local bare repository, without making any changes to an existing GitHub account. To use GitHub instead, simply replace the origin URL. Git transfers commits and sets up tracking relationships in the same way.

After completing the actual push, refresh the GitHub page and confirm that you can see the files, README, default branch, and commit history. The success message in the terminal is one layer of verification; checking the website is another.

10. Your First Look at a GitHub Repository: How to Read the Page

Below is the actual page for GitHub’s official documentation repository, captured on August 15, 2026.

When you open a repository, start with these areas:

  • Code: Files, directories, branches, and commits;

  • Issues: Bugs, feature requests, tasks, and discussions;

  • Pull requests: Changes awaiting review or merging;

  • Actions: Automated testing, builds, and deployment;

  • Security: Security policies and vulnerability-related features;

  • Insights: Contributions, traffic, and repository activity;

  • README: An introduction to the project and a starting point for using it;

  • LICENSE: The terms governing use, modification, and distribution.

When exploring an unfamiliar project, don’t focus on its star count first. Start by answering five questions: What problem does it solve? How do I run it? What does it depend on? Has it been maintained recently? What does its license allow me to do? Stars reflect interest; they don’t check security, compatibility, or licensing permissions for you.

11. clone, fetch, pull, push: Don’t Mix Up the Four Operations

To bring a remote repository onto your computer for the first time:

bash
git clone https://github.com/OWNER/REPO.git

Clone brings over the files, commit history, and remote configuration, usually naming the remote origin automatically. Downloading a ZIP gives you only a snapshot of the files at that moment, without the full history or a connection to the remote repository.

After that, the three common operations are:

bash
git fetch origin   # Download remote information without changing your current working files
git pull           # Fetch, then integrate changes into the current branch
git push           # Send local commits to the remote

To see what has changed on the remote first, you can run:

bash
git fetch origin
git status -sb
git log --oneline HEAD..origin/main

When you have confirmed that your local branch has not diverged and want to accept only fast-forward updates:

git pull --ff-only

pull runs fetch first, then performs a merge or rebase depending on your configuration. Teams should agree on an integration strategy before collaborating for the first time. If branches diverge, don’t use force push to sweep the problem under the rug.

12. From a Personal Repository to Collaboration on GitHub

A Pull Request is a proposal to merge changes—and a place where collaboration happens. Discussion, code review, and automated checks all revolve around the same set of changes, which are merged into main only after approval.

Suppose the Issue is “Add event timing details.” Your local workflow could look like this:

bash
git switch -c feat/event-time

# Modify and test the page
git add index.html
git commit -m "feat: add event timing details"
git push -u origin feat/event-time

After you push, GitHub will usually prompt you to create a Pull Request. A PR is a proposal to merge changes, showing a description, commits, file diffs, comments, reviews, and automated checks. Creating one does not automatically merge its changes into main.

A PR that people are willing to review should explain at least three things: what changed, why it changed, and how to verify it. The more focused the changes, the easier it is for reviewers to spot problems.

Within a team, if you have write access to the repository, you can open a PR directly from a branch. When contributing to an unfamiliar open-source project, the usual approach is to fork it to your own account first, then clone your fork:

bash
git clone https://github.com/your-username/project-name.git
cd project-name
git remote add upstream https://github.com/original-author/project-name.git
git remote -v

You will usually have two remotes here:

origin    Your own fork
upstream  The original author's repository

To sync with the original project:

bash
git fetch upstream
git switch main
git merge --ff-only upstream/main
git push origin main

Next, make your changes on a new branch, push them to your own fork, and open a PR against upstream. Fork, clone, and branch serve three different purposes: a fork is a separate repository on GitHub, clone brings a repository onto your local machine, and a branch is a line of development within a repository.

13. README and LICENSE: Giving Others the Confidence to Use Your Project

A README should answer at least these questions:

  1. What is the project?

  2. What problem does it solve?

  3. How do you install or run it?

  4. What is its current state of development?

  5. Where are the main files?

  6. Who is the author, and where do the assets and references come from?

Even if the code runs, a vague README may leave your future self struggling to pick up where you left off three months later. A minimal README does not need to look polished. Start by clearly explaining the project, how to run it, and its current status.

A public repository does not automatically grant an open-source license, either. GitHub’s official licensing guidance is explicit: without a license, default copyright rules still apply, and the author retains the rights to reproduction, distribution, and derivative works. Making a repository public means others can view it and fork it under GitHub’s Terms of Service. Whether you can incorporate its code into your own public or commercial project still depends on the repository’s LICENSE.

Licenses such as MIT, Apache-2.0, and GPL impose different obligations. For commercial use, redistribution, or combining code under different licenses, read the full license text and seek professional advice when necessary. Don’t just ask AI, “Can I use this commercially?”

14. After a Mistake, First Identify Which Layer the Changes Are In

Choose your undo method based on the current state.

You staged the wrong file but want to keep its contents:

bash
git commit --amend -m "New commit message"

The most recent commit message is wrong, and you have not pushed yet:

bash
git commit --amend -m "New commit message"

You need to undo a commit on a shared branch:

bash
git revert commit-hash

Revert creates a new commit that reverses the changes while keeping the old history visible. This makes it a better fit for branches that have already been pushed and are used by multiple people.

git restore filename discards uncommitted changes; git reset --hard resets the commit pointer, staging area, and working tree to the specified point; git push --force can overwrite remote commits. Before using any of these operations, confirm the target and make a backup. As a beginner, do not treat them as universal fix buttons.

15. The Eight Most Common Errors: Troubleshoot in This Order

fatal: not a git repository

pwd
ls
git status

Usually, you are in the wrong directory, or the current project has not been initialized with git init.

Author identity unknown

bash
git config --local user.name "Your name"
git config --local user.email "Your email"

nothing to commit

Check whether the file has been saved, whether you edited a different copy, and whether the changes have already been committed:

bash
git status
git log --oneline -3

remote origin already exists

bash
git remote -v
git remote set-url origin correct-GitHub-URL

src refspec main does not match any

The repository may not have any commits yet, or the current branch may not be named main:

bash
git log --oneline
git branch --show-current

Authentication failed or 403

Check the remote URL, repository ownership, account permissions, and authentication method. Do not share your token with anyone for troubleshooting.

rejected non-fast-forward

The remote contains commits that you do not have locally. Fetch first and inspect the differences rather than immediately force-pushing:

bash
git fetch origin
git status -sb
git log --oneline --graph --decorate --all -10

Merge Conflicts

Run git status to find files marked UU, manually determine their final contents, then test before running add and commit. If you do not want to resolve the conflicts right now, run git merge --abort.

When a student or colleague simply says, “Git is broken,” ask them to provide the output of these five commands:

bash
pwd
git status
git branch --show-current
git log --oneline -5
git remote -v

Also ask for their operating system, the full command they just ran, and the complete error message. Most problems can quickly be traced to one of these layers: directory, state, identity, remote URL, or permissions.

16. In the AI Era, Git Is More Like an Acceptance System

AI can type commands for you, but it cannot automatically know which changes match your business intent. If a single prompt changes 20 files and you do not review the diff, run the project, or check for secrets, Git will simply record that mess faithfully.

A more reliable approach is to narrow the task and put a human in charge of acceptance. Once the scope, diff, tests, and checks for secrets all pass, a person should decide whether this batch of changes is ready to become a commit.

Set boundaries when asking AI to operate Git:

First inspect git status and git diff, and only summarize the current changes.
Do not discard any uncommitted content or run reset --hard, clean, or force push.
After making changes, report the verification results first. Do not automatically commit or push.

Memorizing commands is no longer as important. You need to be able to read the current state, understand what AI changed, judge whether verification is sufficient, and stop dangerous operations before they happen.

17. Run Through the Entire Workflow Again

bash

# 1. Confirm your location
pwd
ls


# 2. Initialize
git init -b main
git status

# 3. First commit
git add index.html style.css .gitignore
git diff --cached
git commit -m "chore: initialize project"


# 4. Modify, review, test, and commit again
git status --short
git diff
git diff --check
git add index.html
git commit -m "feat: add sign-up entry point"


# 5. Experiment on a branch
git switch -c experiment/warm-theme
git add style.css
git commit -m "style: try a warm color theme"
git switch main
git merge experiment/warm-theme


# 6. Connect to GitHub
git remote add origin https://github.com/your-username/repository-name.git
git remote -v
git push -u origin main


# 7. Final checks
git status
git log --oneline --graph --decorate --all
git remote -v
git diff --check
git ls-files

Once you can explain which layer each command changes and independently recover from working in the wrong directory, staging the wrong changes, and encountering a merge conflict, GitHub becomes more than just a website for storing code. You can now turn a personal project into a repository with a traceable history that others can review and contribute to.

Your next step isn’t to keep collecting commands. Pick a real, small project and work on it for seven consecutive days: make just one small change each day, review the diff, test it, commit it, and push it to GitHub. Your commit history will gradually turn this workflow into a habit.


I’m Miles, an AI algorithm specialist who transitioned from Big Tech to a forward-deployed engineer (FDE) role. My experience spans algorithm R&D, optimization and deployment, and corporate training. Follow me @miles_mazy—let’s grow and earn together.