Git Basics
What is Git?
Git is a distributed version control system that allows multiple developers to work on a project simultaneously without interfering with each other. It tracks changes in source code during software development.
Key Concepts:
- Version Control: Keeps track of changes to files over time.
- Repository: A directory that stores your project files and the history of changes.
- Commit: A snapshot of your files at a specific point in time.
Installation
To install Git, follow the instructions for your operating system:
- Download the Git installer from git-scm.com.
- Run the installer and follow the setup instructions.
- Verify the installation by running
git --version
in your terminal.
Basic Commands
Here are some fundamental Git commands:
git init # Initializes a new Git repository
git clone # Clones an existing repository
git add # Stages a file for commit
git commit -m "message" # Commits staged changes
git status # Shows the status of the working directory
git log # Displays commit history
Branching
Branches are used to develop features isolated from each other. Here's how to manage branches:
git branch # Lists all branches
git branch # Creates a new branch
git checkout # Switches to a branch
git merge # Merges a branch into the current branch
Use branches to keep your main codebase clean while experimenting with new features.
Best Practices
- Commit frequently with clear messages.
- Use branches for new features and bug fixes.
- Keep your commit history clean and organized.
- Regularly pull changes from the main repository to avoid conflicts.
FAQ
What is a commit?
A commit is a snapshot of your project at a specific point in time. It includes changes made to the files in the repository.
How do I undo a commit?
You can revert a commit using git revert
or reset your branch with git reset --hard
.
What is a merge conflict?
A merge conflict occurs when two branches have changes to the same line in a file, and Git doesn’t know which change to keep.
Git Workflow Flowchart
graph TD;
A[Start] --> B{Changes Made?};
B -- Yes --> C[Add Changes];
C --> D[Commit Changes];
D --> E{On a Branch?};
E -- Yes --> F[Merge Changes];
F --> G[Push to Remote];
E -- No --> H[Create Branch];
H --> F;
B -- No --> I[End];