What is Git?
Git is a free and open-source version control system. It helps you track changes in your files, collaborate with others, and manage code history efficiently.
1. Install Git
🔹 Windows / macOS / Linux:
-
Go to the official site: https://git-scm.com/
-
Download the version for your operating system.
-
Follow the installation instructions.
-
After installing, open a terminal (Command Prompt, Git Bash, or Terminal) and type:
git --version
You should see something like:
git version 2.xx.x
2. Set Up Git
Before using Git, set your name and email (used for commits):
git config --global user.name "Your Name" git config --global user.email "[email protected]"
You can check your settings:
git config --list
3. Create or Clone a Repository
Create a new local repo
mkdir my-project cd my-project git init
This creates a new Git repository in your folder.
git clone https://github.com/username/repo-name.git cd repo-name
4. Add and Commit Changes
Track a file:
git add filename.txt
Or add all files:
git add .
Save a snapshot (commit):
git commit -m "Write a short message about what you did"
5. View the Status and History
Check what’s going on:
git status
See commit history:
git log
6. Push to GitHub (or another remote)
Make sure your repository is connected to a remote:
git remote add origin https://github.com/yourusername/repo.git
Now push your code:
git push -u origin main
Replace main
with your branch name if needed.
7. Pull Updates from Remote
To get the latest changes from others (or from GitHub):
git pull origin main
Common Git Commands (Cheat Sheet)
Command | Description |
---|---|
git init |
Create a new repo |
git clone URL |
Download repo |
git status |
Check status |
git add . |
Stage changes |
git commit -m "msg" |
Save changes |
git push |
Upload to remote |
git pull |
Download changes |
git log |
Show commit history |