What is a Git Repository and Initialization¶
Before starting with Git, let’s briefly understand two core concepts: Repository and Initialization.
- Repository: Think of it as a “special folder” that secretly stores Git’s “change logs.” Git records every modification you make to your code, who made it, and when.
- Initialization: This is like installing Git’s “monitoring system” on that “special folder” to start tracking subsequent code changes.
Initializing a Local Repository (Most Common)¶
To create a new project on your computer and manage it with Git, follow these steps:
- Open the terminal/command line (Windows: Command Prompt or PowerShell; Mac/Linux: Terminal).
- Navigate to your project folder (create one if it doesn’t exist, e.g.,
my-first-project):
cd /path/to/your/project/folder # Replace with your folder path, e.g., cd ~/Documents/my-first-project
- Run the initialization command:
git init
If successful, you’ll see a message like:
Initialized empty Git repository in /path/to/your/project/folder/.git/
This means Git has created a hidden .git folder in your directory, which stores all of Git’s “secret logs.” Note: Do not manually modify the contents of the .git folder, as this may corrupt the repository!
Basic Configuration: Tell Git “Who You Are”¶
Git needs to know who is modifying the code, so you must configure your identity (name and email). These two steps are essential for beginners:
1. Configure User Identity (Global)¶
Open the terminal and run these commands (replace with your name and email):
git config --global user.name "Your Name" # e.g., git config --global user.name "Xiaoming"
git config --global user.email "your@email.com" # e.g., git config --global user.email "xiaoming@example.com"
--globalapplies to all Git repositories on your computer. To configure only the current repository, omit--globaland use--local(global is simpler for beginners).
2. (Optional) Configure Default Editor¶
When committing code, Git opens an editor for your commit message (e.g., “Fixed a bug”). To set a custom editor (e.g., Notepad for Windows, vim for Mac/Linux):
git config --global core.editor "notepad" # Windows default (Notepad)
# For Mac/Linux: git config --global core.editor "nano" or "vim"
If not configured, Git uses the system default editor (which works for beginners).
View Configuration (Check Setup)¶
Run this command to see all configured settings:
git config --list
You’ll see output like:
user.name=Xiaoming
user.email=xiaoming@example.com
core.editor=notepad
...
If your identity isn’t listed, recheck the configuration commands.
What to Do After Initialization? (Example)¶
After initializing the repository, start creating/modifying files. Here’s a simple workflow:
- Create a file: In your project folder, create
README.txtwith content like “My first Git project!” - Check status: Run
git status—the file will show as “untracked” (Git hasn’t started monitoring it yet). - Stage the file:
git add README.txt # Adds the file to the "staging area" (temporary holding)
Re-run git status to confirm the file is “staged.”
4. Commit to the repository:
git commit -m "First commit" # -m adds a commit message
Git will record the file’s state:
[main (root-commit) a1b2c3d] First commit
1 file changed, 1 insertion(+)
create mode 100644 README.txt
Your project is now fully tracked by Git!
Key Tips for Beginners¶
.gitFolder: Appears only after initialization. It’s Git’s “heart”—never delete or modify its contents!- Global vs. Local Config:
--globalapplies to all repos;--localis for the current repo (e.g., separate work/personal emails). - Clone vs. Initialize: To copy an existing repo (e.g., from GitHub), use
git clone <repo-url>instead ofgit init.
With these steps, you’ve completed Git repository initialization and basic setup—your first step into Git! Now you can manage code and track every change.