Git is a powerful version control tool that helps us track code changes, collaborate on development, and roll back errors. For beginners, the most basic and important process is from “cloning a repository” to “committing changes”. This article breaks down this process step by step in a simple and easy-to-understand way, with text-based flowcharts to help you easily master Git’s basic operations.
1. First, Understand the 3 Core Areas (Essential Concepts)¶
Before starting operations, it’s crucial to understand Git’s 3 key areas, which form the foundation for subsequent steps:
- Working Directory: The files you are currently editing (e.g., modifying a code file), in an “unmanaged by Git” state.
- Staging Area: A temporary storage for changes to be committed (files enter here after executing git add).
- Local Repository: The repository on your local computer that stores all historical commit records (changes are permanently recorded after executing git commit).
2. Complete Process from Clone to Commit¶
The following are the most common Git operations, connected via a flowchart:
graph TD
A[Clone Remote Repository] --> B[Enter Project Directory]
B --> C[Check Working Directory Status]
C --> D[Modify Files (Working Directory)]
D --> E[Stage Changes (Staging Area)]
E --> F[Commit to Local Repository]
F --> G[View Commit History]
G --> H[Push to Remote Repository]
1. Step 1: Clone the Remote Repository to Local¶
Purpose: Copy the project from the remote server to your local computer.
Command: git clone <remote-repository-url>
Example: If the remote repository URL is https://github.com/YourUsername/YourProject.git, run:
git clone https://github.com/YourUsername/YourProject.git
Result: A new folder with the same name as the remote repository will be created locally, containing all project files and historical records.
2. Step 2: Enter the Project Directory and Check Working Directory Status¶
Purpose: Confirm if there are any uncommitted changes in the current project.
Steps:
- Navigate to the cloned project directory: cd YourProjectFolder (e.g., cd my-project).
- Check the status: git status
Result:
- If it shows On branch master (assuming the branch is master) and nothing to commit, working tree clean, there are no uncommitted changes.
- If it shows Changes not staged for commit, some files have been modified but not yet staged.
3. Step 3: Modify Files (Working Directory Operation)¶
Purpose: Edit files locally (e.g., add features, fix bugs).
Action: Open project files (e.g., index.html, app.js) with an editor, make changes, and save.
Key: Modified files will be marked as “modified” and need further processing.
4. Step 4: Stage Changes to the Staging Area¶
Purpose: Temporarily save changes from the working directory to the staging area for commit.
Command: git add [filename] or git add . (. denotes “all changes”)
Examples:
- If only index.html is modified: git add index.html
- If multiple files are modified (e.g., index.html, style.css): git add .
Result: Modified files move from the “working directory” to the “staging area”. Running git status now will show Changes to be committed.
5. Step 5: Commit Staged Changes to the Local Repository¶
Purpose: Permanently save the staged changes as a new version.
Command: git commit -m "Commit message"
Example:
git commit -m "Added homepage navigation bar"
Key: The -m parameter is required, followed by a brief commit message (e.g., “Fixed a bug”, “Added new feature”) for easy history tracking later.
Result: Staged changes are recorded in the local repository, generating a new commit record.
6. Step 6: View Commit History (Optional but Recommended)¶
Purpose: Confirm if the commit was successful or view historical modifications.
Command: git log
Result: Displays all commit information, including commit ID, author, timestamp, and commit message. Press q to exit.
7. Step 7: Push to the Remote Repository (Share with the Team)¶
Purpose: Sync new local commits to the remote server so team members can see them.
Command: git push origin [branch-name]
Example: If using the master branch: git push origin master
Notes:
- For the first push, you may need to enter your GitHub/GitLab username/password or configure an SSH key (newcomers are recommended to use HTTPS first, then learn SSH later).
- If using a different branch (e.g., dev), replace master with dev.
3. Summary: Quick Reference for Key Commands¶
| Step | Command | Purpose |
|---|---|---|
| Clone Repository | git clone <remote-url> |
Copy remote repo to local |
| Check Status | git status |
Inspect modified files |
| Stage Changes | git add [filename] or git add . |
Add changes to staging area |
| Commit Changes | git commit -m "message" |
Save staged changes to repo |
| View History | git log |
Review commit history |
| Push to Remote | git push origin [branch] |
Sync local commits to remote |
By following these steps, you’ve completed the full process from “cloning a repository” to “committing and pushing”! Git’s core logic is “tracking changes”—practice regularly to familiarize yourself with commands. For advanced learning, explore branch operations (git branch) and conflict resolution later. More content will be shared soon~