What You Need to Start with Git
A beginner-friendly introduction to the essential Git commands and concepts. This course will equip you with the fundamental skills to start versioning your projects and collaborating with other developers effectively.
Introduction to Git
Git is a distributed version control system. Think of it as a way to save snapshots (called "commits") of your project over time. This allows you to track changes, revert to previous versions, and collaborate with others without overwriting each other's work.
1. Initializing a Repository
First, you need to tell Git to start tracking a project.
git init: This command is used to start a new repository. You run this command in your project's root directory. It creates a hidden.gitsubdirectory where all the Git-related magic happens.
2. The Basic Workflow: Status, Add, Commit
This is the core cycle you'll use constantly.
git status: This is your "what's going on?" command. It shows you the current state of your working directory and staging area. It will tell you which files have been modified, which are new (untracked), and which are staged for the next commit.git add <file-name>: When you've made changes to a file that you want to include in your next snapshot (commit), you first need to add it to the "staging area." This command does just that. You can also usegit add .to add all modified and new files in the current directory and subdirectories.git commit -m "Your descriptive message": This takes all the files from the staging area and saves a snapshot of them to your project's history. The message (-m) is crucial; it should be a clear and concise description of the changes you made.
Example: Let's say we have a folder with 3 files
index.htmlstyles.cssscript.js
1git init
2# Output: Initialized empty Git repository in [path]/.git/
3
4git status
5# Output:
6# On branch main
7# No commits yet
8# Untracked files:
9# (use "git add <file>..." to include in what will be committed)
10# index.html
11# styles.css
12# script.js
13# nothing added to commit but untracked files present
14
15git add index.html styles.css script.js
16# No output
17# Or you can basically use "git add ." to add all files
18
19git status
20# Output:
21# On branch main
22# No commits yet
23# Changes to be committed:
24# (use "git rm --cached <file>..." to unstage)
25# new file: index.html
26# new file: styles.css
27# new file: script.js
28
29git commit -m "Initial commit: add basic website structure"
30# Output: [main (root-commit) abc1234] Initial commit: add basic website structure
31# 3 files changed, 15 insertions(+)
32# create mode 100644 index.html
33# create mode 100644 styles.css
34# create mode 100644 script.js3. Working with a Remote Repository (like GitHub)
To collaborate or to have an online backup of your code, you'll use a remote repository.
git push: This command sends your committed changes from your local repository to a remote repository (e.g., on GitHub). The first time you push a new branch, you might need to usegit push -u origin <branch-name>.git pull: This command fetches changes from the remote repository and merges them into your current local branch. It's how you get updates made by others. It's a good practice to pull before you start working to ensure you have the latest version.
Example:
Let's stay on the same project
First, set up a remote repository and push:
1# Add a remote repository (like GitHub, GitLab, etc.)
2git remote add origin https://github.com/yourusername/your-repo.git
3
4git push -u origin main
5# Output:
6# Enumerating objects: 3, done.
7# Counting objects: 100% (3/3), done.
8# Writing objects: 100% (3/3), 256 bytes | 256.00 KiB/s, done.
9# Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
10# To https://github.com/yourusername/your-repo.git
11# * [new branch] main -> main
12# Branch 'main' set up to track remote branch 'main' from 'origin'Later, when you want to pull changes:
1git pull origin main
2# Output:
3# remote: Enumerating objects: 1, done.
4# remote: Counting objects: 100% (1/1), done.
5# remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
6# Unpacking objects: 100% (1/1), 652 bytes | 652.00 KiB/s, done.
7# From https://github.com/yourusername/your-repo
8# * branch main -> FETCH_HEAD
9# abc1234..def5678 main -> origin/main
10# Updating abc1234..def5678
11# Fast-forward
12# README.md | 1 +
13# 1 file changed, 1 insertion(+)
14# create mode 100644 README.mdAfter making local changes and pushing again:
1git push
2# Output:
3# Enumerating objects: 5, done.
4# Counting objects: 100% (5/5), done.
5# Writing objects: 100% (3/3), 312 bytes | 312.00 KiB/s, done.
6# Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
7# To https://github.com/yourusername/your-repo.git
8# def5678..ghi9012 main -> main
4. Branching and Merging: Working in Parallel
Branches are a fundamental concept in Git. They allow you to work on different features or bug fixes in isolation without affecting the main codebase.
git branch <branch-name>: This creates a new branch.git checkout <branch-name>(orgit switch <branch-name>on newer Git versions): This switches your working directory to the specified branch.- You can create and switch to a new branch in one command:
git checkout -b <new-branch-name>
Once you've finished your work on a branch and are ready to integrate it back into the main codebase (often the main or master branch):
- Merging: First, switch back to the main branch (
git checkout main). Then, rungit merge <branch-name>. This will take the changes from your feature branch and combine them with themainbranch.
Example:
1# Create and work on a new feature branch
2git branch feature-new-header
3# (No output if successful)
4
5git checkout feature-new-header
6# Output: Switched to branch 'feature-new-header'
7
8# Or use: git checkout -b feature-new-header (creates and switches in one command)
9
10# Make changes to files, then add and commit
11git add index.html
12git commit -m "Add new header section"
13# Output: [feature-new-header def5678] Add new header section
14# 1 file changed, 5 insertions(+)
15
16# Switch back to main branch
17git checkout main
18# Output: Switched to branch 'main'
19
20# Merge the feature branch into main
21git merge feature-new-header
22# Output: Updating abc1234..def5678
23# Fast-forward
24# index.html | 5 +++++
25# 1 file changed, 5 insertions(+)
26
27# Check the log to see the merge
28git log --oneline
29# Output:
30# def5678 Add new header section
31# abc1234 Initial commit: add basic website structure
32
33# Delete the feature branch
34git branch -d feature-new-header
35# Output: Deleted branch feature-new-header (was def5678)
5. Pull Requests: Proposing Changes
When working in a team, instead of merging directly, you'll often use a "Pull Request" (PR) on platforms like GitHub or GitLab.
- Push your branch: After committing your changes to your feature branch, you push it to the remote repository:
git push origin <branch-name>. - Open a Pull Request: Go to the repository on GitHub. You will usually see a prompt to create a Pull Request from your recently pushed branch.
- Review and Merge: A Pull Request is a formal proposal to merge your changes. It allows your teammates to review your code, leave comments, and suggest changes before it's merged into the main branch. Once approved, the changes can be merged.

6. Ignoring Files: .gitignore
Often, there are files or entire directories in your project that you do not want to track with Git. These can include:
Dependencies managed by package managers (e.g., a node_modules folder).
Build artifacts or compiled code (e.g., /dist, *.exe).
Log files (*.log).
Files containing sensitive information like API keys or credentials (e.g., .env files).
Operating system-specific files (e.g., .DS_Store on macOS).
To prevent Git from tracking these files, you create a special file in the root directory of your project named .gitignore.
Each line in this file lists a pattern for a file or folder that Git should ignore. These ignored files won't show up in git status and cannot be added to a commit.
Example:
1# Ignore dependency folders
2node_modules/
3vendor/
4
5# Ignore log files
6*.log
7
8# Ignore environment variables file
9.env
10
11# Ignore build output directory
12/distWas this article helpful?
Let us know what you think! Your feedback helps us improve.