What is Git? Simply put, it’s a “version control system”—like a “file diary” that helps you record every change to your files and enables collaborative editing by multiple people. GitHub, on the other hand, is an “online repository platform” built on Git. You can store your local code on GitHub and collaborate with others to modify the code together.
I. Preparation¶
- Install Git: First, install the Git tool on your computer. Windows users can download it from the Git official website and follow the default installation prompts. Mac users can use Homebrew (
brew install git) or download directly from the official website. After installation, open the terminal (Windows: “Git Bash”) and entergit --version. If the version number is displayed, the installation is successful. - Register a GitHub Account: Go to the GitHub official website to create an account. Once logged in, you can start creating repositories.
II. Creating a Repository on GitHub¶
A repository is like a folder for storing your project code.
- Log in to GitHub and Create a Repository
- After logging in, click the “+” icon in the top-right corner and select “New repository”.
- Fill in the repository details:- Repository name: Choose a name (e.g., “my-first-project”; only English letters and numbers are allowed, no spaces).
- Description: A brief description (e.g., “My first GitHub project”; optional).
- Visibility: Select “Public” (visible to everyone) or “Private” (visible only to you; free users can create only 1 private repository; use “Public” for beginners).
- Check “Add a README file”: Automatically generate a README file (useful for later pulls to avoid conflicts).
- Other options: You can ignore them for now (.gitignore and license can be configured later).
- Click “Create repository”. The page will display the repository URL (e.g.,
https://github.com/your-username/my-first-project.git). Copy this URL for later use.
III. Linking a Local Project to a GitHub Repository¶
Assume your local project is in the folder “my-project” with files like index.html and style.css.
1. Initialize a Local Git Repository¶
Open the terminal and navigate to the local project folder (e.g., Windows: cd C:\Users\Your-Username\my-project, Mac: cd ~/my-project).
Run: git init
A hidden .git folder will appear in the folder, indicating successful initialization.
2. Link to the Remote GitHub Repository¶
Run: git remote add origin [your-repository-URL]
Example: git remote add origin https://github.com/your-username/my-first-project.git
This binds your local repository to the remote GitHub repository. origin is the default alias for the remote repository.
3. Pull Remote Repository Content (Avoid Conflicts)¶
If the GitHub repository has a README (from the “Add a README” step), pull remote content first to avoid push errors:
Run: git pull origin main (Note: Most modern repositories use main; older ones may use master. If you get an error, try master instead).
Once successful, your local repository will have the README file, preventing conflicts during subsequent pushes.
4. Push Local Files to GitHub¶
- Stage files: Use
git add .to stage all modified files (.= all files; replace with specific filenames if needed, e.g.,git add index.html). - Commit to local repository: Add a commit message, e.g.,
git commit -m "Initial commit"(use a descriptive message like “First project upload”). - Push to remote repository: Send local changes to GitHub:
git push origin main.
Your uploaded files will now appear on the GitHub repository page!
IV. Quick Command Reference¶
| Command | Purpose |
|---|---|
git init |
Initialize a local Git repository (makes the folder Git-manageable). |
git add . |
Stage all modified files to the staging area. |
git commit -m "..." |
Commit staged changes to the local repository (use a message in quotes). |
git push origin main |
Push local commits to the GitHub main branch. |
git pull origin main |
Fetch and merge the latest content from the GitHub main branch (avoids conflicts). |
V. Common Issues¶
- Q: “Remote repository already exists”?
A: You may have duplicate remote associations. Check withgit remote -v, then remove the old one withgit remote remove origin, and re-add the correct URL. - Q: Push error with no README?
A: Rungit pull origin mainto fetch the remote README first, then re-push. - Q: Need to collaborate with others?
A: Learn advanced commands likegit branch(branching),git merge(merging), andgit clone(cloning repositories) later. For basic use, stick to “create + commit + push”.
Now your local project is linked to GitHub! To update future changes, repeat the steps: git add . → git commit -m "Update description" → git push origin main.