Git is a popular tool that helps developers work on projects together and keep track of changes to their code. It's fast, reliable, and works well for projects of any size. With Git, you can create separate branches to try new ideas, save different versions of your work, and combine changes easily without messing up the main project. It's easy to use and an essential tool for modern software development.
Before using Git, ensure the following:
- Git Installation:
- Download Git from git-scm.com.
- Follow the instructions to install it for your operating system.
- Basic Terminal Knowledge:
- Understanding how to navigate directories and execute commands in a
terminal or command prompt.
- Understanding how to navigate directories and execute commands in a
- Setting Up Git for the First Time
Purpose: Configures your identity in Git.
- Commands:
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com" --global: Ensures these settings apply across all projects.
- Check your configuration:
git config --list
- Initializing a Repository
- Command:
git init
Purpose: Creates a new Git repository in the current folder.
- example:
mkdir my-project
cd my-project
git init - Staging Changes
- Command:
git add <file>
Purpose: Adds changes to the staging area.
- Examples:
git add index.html
git add . git add . : Adds all changes in the current directory.
- Committing Changes
- Command:
git commit -m "Your commit message"
Purpose: Saves the staged to the repository with a message.
- Example:
git commit -m "Added index.html"
- Viewing Repository Status
- Command:
git status
Purpose: Displays the state of the working directory and staging area.
- Output Example:
- Files added for commit.
- Untracked files.
- Viewing Commit History
- Command:
git log
Purpose: Shows a history of commits in the repository.
- Options:
- git log --oneline: Display a concise one-line log.
- Cloning a Repository
- Command:
git clone <repository_url>
Purpose: Creates a local copy of a remote repository.
- Example:
git clone https://github.com/user/repo.git
- Checking for Updates
- Command:
git fetch
Purpose: Retrieves changes from the remote repository but doesn't apply them.
- Merging Updates
- Command:
git pull
Purpose: Fetches and integrates changes from the remote repository into your local branch.
- Example:
git pull origin main
- Pushing Changes
- Command:
git push
Purpose: Sends your commits to the remote repository.
- Example:
git push origin main
- Creating Branches
- Command:
git branch <branch_name>
Purpose: Creates a new branch for feature development.
- Example:
git branch feature-login
- Switching Branches
- Command:
git checkout <branch_name>
Purpose: Moves to a different branch.
- Example:
git checkout feature-login
- Merging Branches
- Command:
git merge <branch_name>
Purpose: Combines changes from another branch into the current branch.
Undoing a Commit
- Command:
git reset --soft HEAD~1
Purpose: Reverts the last commit but keeps the changes staged.
- Files: .gitignore
Add file patterns to .gitignore to exclude them from Git tracking.
-
node_modules/
*.log