Git is a distributed version control system (DVCS) that tracks changes to your project files, enabling you to revert to previous versions and collaborate effectively with others. This tutorial provides a foundational understanding of Git, covering essential commands and workflows.
Table of Contents
- Getting Started with Git
- Staging Changes with
git add
- Creating Commits with
git commit
- A Basic Git Workflow
- Next Steps: Expanding Your Git Knowledge
Getting Started with Git
Before diving into commands, ensure Git is installed on your system. You can download it from the official Git website. Once installed, navigate to your project directory using the command line or terminal. Initialize a Git repository using:
git init
This creates a hidden .git
folder in your directory, tracking changes within it.
Staging Changes with git add
The git add
command stages changes, preparing them for your next commit. Think of it as selecting the modifications you want to save as part of a specific snapshot. git add
takes files or directories as arguments.
Examples:
git add README.md
: Stages changes only inREADME.md
.git add *.txt
: Stages changes in all.txt
files.git add .
: Stages all changes in the current directory and its subdirectories. Use with caution!
After using git add
, the changes are staged but not yet saved permanently. You’ll see them listed if you run git status
.
Creating Commits with git commit
The git commit
command saves (commits) your staged changes to the local Git repository. Each commit represents a snapshot of your project at a specific point in time and includes a descriptive message.
The command:
git commit -m "Your descriptive commit message"
The -m
flag adds your message directly to the command. A well-written message is crucial for understanding the history of your project. Without -m
, Git opens a text editor for you to write the message.
A Basic Git Workflow
- Make changes: Edit your project files.
- Stage changes: Use
git add
to select the changes for your next commit. - Commit changes: Use
git commit -m "Your message"
to save the staged changes.
This cycle of making changes, staging them, and committing them forms the core of Git’s version control.
Next Steps: Expanding Your Git Knowledge
This tutorial covered the fundamentals. To fully utilize Git’s power, explore additional commands like:
git push
: Uploads your commits to a remote repository (like GitHub or GitLab).git pull
: Downloads changes from a remote repository.git branch
: Creates and manages branches for parallel development.git merge
: Combines changes from different branches.git status
: Shows the current status of your working directory and staging area.git log
: Displays the commit history.
Mastering these commands will allow you to collaborate effectively, manage complex projects, and maintain a clean, organized version history.