GitHub 基础
连接到 remote 仓库, pushing, pulling, and collaborating on GitHub.
你将学到
- 什么 remote 仓库是 and 如何 添加 one
- Pushing 你的局部提交使用 git 推送
- Pulling 更改从 others 使用 git 拉取
- Cloning existing 仓库使用 git 克隆
- 基础的 拉取请求 for collaboration
概念讲解
什么是远程仓库?
remote 是一种 version of your repository hosted on a server — like GitHub, GitLab, or Bitbucket. Your local repository can connect to one or more remotes to sync changes.
When you clone a repository, Git automatically sets up a remote named 原点 pointing to the source URL. If you created a repo locally, you add a remote manually:
git remote add origin https://github.com/username/repo.git
git push — Sending Changes Up
git 推送 sends your local commits to the remote repository:
# First push: set up tracking and push to origin/main
git push -u origin main
# Subsequent pushes (tracking is already set up)
git push
The -u (or --设置-upstream) flag tells Git to remember the connection between your local 主要 branch and 原点/主要. After the first push, you can just type git 推送.
git pull — Getting Changes Down
git 拉取 fetches changes from the remote and merges them into your local branch:
git pull
Always pull before you push if you are collaborating — someone else may have pushed changes that you do not have yet. If you push without pulling, Git will reject your push and ask you to pull first.
git clone — Copying a Repository
git 克隆 downloads an entire repository, including all history, and sets up the remote:
git clone https://github.com/user/repo.git
cd repo
This is how you start working on an existing project — for example, contributing to open source.
SSH vs HTTPS
You can connect to GitHub using HTTPS or SSH:
- HTTPS — uses your username and a personal access token. Easier to set up, but you may be asked for credentials frequently.
- SSH — uses an SSH key pair. More secure and no repeated password entry, but requires generating and uploading a key.
# HTTPS URL
git clone https://github.com/user/repo.git
# SSH URL
git clone [email protected]:user/repo.git
Pull Requests (PRs)
A pull request is GitHub's collaboration feature. The workflow:
- Fork the repository (or create a branch if you have write access)
- Make your changes and commit them
- Push to your fork or branch
- Open a pull request on GitHub asking the maintainers to merge your changes
- Review — maintainers review your code, request changes, and eventually merge
Pull requests are where code review happens — they are the heart of team collaboration on GitHub.
Common Remote Commands
git remote -v # list all remotes
git remote add origin <url> # add a remote
git push -u origin main # push and set tracking
git pull # fetch and merge remote changes
git fetch # fetch without merging (safer)
git clone <url> # download a repository示例代码
# Scenario 1: Push a local repo to a new GitHub repository
# First, create the repo on GitHub (via the website), then:
git remote add origin https://github.com/username/my-project.git
git branch -M main # ensure the branch is called main
git push -u origin main
# Scenario 2: Clone an existing repository and make changes
git clone https://github.com/username/existing-project.git
cd existing-project
# Create a feature branch
git checkout -b add-login-feature
# Make changes, commit, and push the branch
echo "new feature" > login.js
git add login.js
git commit -m "feat: add login feature"
git push -u origin add-login-feature
# Now open a pull request on GitHub
# Scenario 3: Sync with the remote (collaboration)
git pull # get the latest changes from teammates
# resolve any conflicts, then:
git push
# List your remotes
git remote -v
# Fetch without merging (see what's new without changing your files)
git fetch
git log --oneline origin/main # see what the remote has
Three 常见 workflows: pushing 局部仓库到 GitHub, cloning and contributing 通过分支 + 拉取请求, and syncing 使用 teammates. 获取 + log 模式 lets 你 preview remote 更改之前合并.
动手试一试
- SSH 键 Generator
Generate SSH 键 pair for secure GitHub 认证.
- CI/CD YAML Generator
Generate GitHub 操作流水线那个运行 on 每个推送.
常见错误
错误写法
Forgetting 到拉取之前 pushing and 获取 rejection 错误
正确做法
当 collaborating, 总是运行 git 拉取之前 git 推送. 如果某人 else pushed 更改, 你的推送将 是拒绝直到你 拉取 and 合并它们的工作首先.
错误写法
Using git 拉取不使用理解它 能创建合并提交
正确做法
git 拉取获取 AND 合并. 如果你 想要到 see 什么是 新不使用更改你的 files, use git 获取首先. Consider git 拉取 --rebase 到 avoid extra 合并提交 in 你的 history.
相关资源
相关工具
- .gitignore Generator
Generate .gitignore 之前 pushing 到 avoid committing secrets.
相关代码片段
- Git Remote Snippet
高级 remote management commands.
相关速查表
- Git Cheatsheet
Remote, 推送, 拉取, and 克隆 commands.
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。