Mastering Git Basics
Git is a distributed version control system that tracks changes to files and coordinates work among multiple people. It allows developers to keep a history of changes, collaborate effectively, and manage different versions of their projects.
Installing Git
For Windows:
For macOS:
brew install git
Initialize a Git Repository
First, create a new project directory and initialize Git:
mkdir my-project
cd my-project
git init
Check Repository Status
git status
Add Files and Commit Changes
Create a file and add content to it:
echo "Hello, World!" > hello.txt
Add this file to the staging area and commit it:
git add hello.txt
git commit -m "Initial commit with hello.txt"
Create a New Branch
Branches allow you to work on features or fixes separately from the main project:
git checkout -b feature-branch
Make Changes and Commit
Modify the file and commit the changes:
echo "New feature" >> hello.txt
git add hello.txt
git commit -m "Added new feature to hello.txt"
Merge Changes
Switch back to the main branch and merge changes from feature-branch:
git checkout main
git merge feature-branch
Push to Remote Repository
If you’re working with a remote repository like GitHub, push your changes:
git remote add origin https://github.com/username/my-project.git
git push -u origin main
Git tracks each change, enables branching for isolated development, and facilitates collaboration by managing code versions and merging changes.
This article provides a comprehensive introduction to Git, covering essential commands to help you get started with version control. Whether you’re setting up a new repository or managing existing projects, these basics will get you up and running. For further learning and in-depth information, check out the official Git documentation.