Make sure Git is installed and configured before starting.
Before using Git in a project, you first need to create a repository (repo). A repository is the storage space where Git keeps your project files and records all changes made to them.
Step 1: Create a Project Folder
mkdir my-first-git
cd my-first-git
This creates a new folder for your project.
Step 2: Initialize Git Repository
This tells Git to start tracking this folder.
git init
Step 3: Add File to Staging Area
git add hello.txt
"hello.text" is the file which you want to track. Git won't track the new file until you tell it to. You can use the following command to tell Git to start tracking the file.
In Git, the staging area (also called the index) is like a waiting room for your changes before they become part of a commit. It is an intermediate place where Git keeps track of changes
Step 4: Commit (Save a Snapshot)
git commit -m "First commit: added hello.txt"
A commit is like a checkpoint in your project history.
This saves your changes to the Git repository with a message describing what you changed.
Step 5: Push - Upload code on GitHub repo
Think of Git as having two worlds:
- Local repository → This is on your computer (your project folder + Git).
- Remote repository → This is on GitHub (or GitLab, Bitbucket, etc.).
The git push command is used to send your changes from your local repository to the remote repository so others (or future you) can see them.
git push remote-name branch-name
Step 6: Pull - Upload code on GitHub repo
git pull takes the latest changes from the remote repository (GitHub) and updates your local repository.
git pull [remote-name] [branch-name]
git pull is used to update your local repository with the latest changes from the remote repository.
More commands:
There are many useful commands in Git. For example, the git status command shows the current state of your working directory and staging area.
- git status shows you the current state of your working directory and staging area.
It tells you:
- Which files are untracked (new files not in Git yet)
- Which files are modified but not staged
- Which files are staged and ready to commit
- Which branch you are on
- If you run:
You might see:git status
On branch main Your branch is up to date with 'origin/main'. Changes not staged for commit: modified: index.html Untracked files: style.css Changes to be committed: new file: app.js