Basic Commands
30 min`git init` creates a new Git repository. This command initializes a new Git repository in the current directory, creating a hidden `.git` folder that contains all the repository metadata. You only need to run this once per project. After initialization, Git starts tracking changes in that directory. Understanding what happens during initialization helps you understand Git's structure.
`git add <file>` adds a file to the staging area. The staging area (also called the index) is where you prepare changes before committing them. You can add individual files, multiple files, or entire directories. The `git add .` command adds all changes in the current directory. Understanding the staging area is crucial—it allows you to selectively commit changes, creating logical, focused commits.
`git commit -m 'message'` records the changes to the repository. A commit creates a snapshot of all staged changes with a message describing what changed. Commit messages should be clear and descriptive, explaining why the change was made, not just what changed. Good commit messages make it easier to understand project history and debug issues. The `-m` flag allows you to specify the message inline.
`git status` shows the current state of the working directory and staging area. This command tells you which files are modified, which are staged, and which are untracked. It's one of the most frequently used Git commands, helping you understand what Git knows about your files. Regular use of `git status` helps you stay aware of your repository state.
`git log` displays the commit history with detailed information about each commit. By default, it shows commits in reverse chronological order with commit hash, author, date, and message. Various options allow you to customize the output: `--oneline` for compact view, `--graph` for branch visualization, `--all` to see all branches. Understanding commit history helps you track project evolution and find when bugs were introduced.
Key Concepts
- git init creates a new repository.
- git add stages changes for commit.
- git commit creates a snapshot with a message.
- git status shows repository state.
- git log displays commit history.
Learning Objectives
Master
- Initializing Git repositories
- Staging and committing changes
- Checking repository status
- Viewing commit history
Develop
- Understanding Git's three-stage workflow (working directory, staging, repository)
- Writing clear, descriptive commit messages
- Using Git commands effectively in daily workflow
Tips
- Check git status frequently to stay aware of your repository state.
- Write clear, descriptive commit messages explaining why, not just what.
- Stage related changes together for logical commits.
- Use git log --oneline --graph --all to visualize branch history.
Common Pitfalls
- Forgetting to stage files before committing, resulting in empty commits.
- Writing vague commit messages like 'fix' or 'update', making history useless.
- Committing too many unrelated changes in one commit.
- Not checking git status, losing track of what's changed.
Summary
- git init creates a new repository.
- git add stages changes for commit.
- git commit records changes with a message.
- git status and git log help you understand repository state.
Exercise
Simulate initializing a repository, adding a file, and committing it.
git init
git add README.md
git commit -m "Initial commit"
Exercise Tips
- Use 'git add .' to stage all changes in the current directory.
- Check status before committing: git status.
- View commit history: git log --oneline.
- Use descriptive commit messages: git commit -m 'Add user authentication feature'.