Introduction
Git is an essential tool for modern developers. This guide documents my learning journey and serves as a resource for anyone starting with Git. Here, I’ve detailed the daily steps, commands, and exercises I followed to master Git, along with explanations and extra content to deepen your understanding.
Day 1: Introduction to Git
What is Git?
Git is a distributed version control system that tracks changes in your codebase, enabling collaboration and efficient management of your projects.
Key Features of Git
Tracks changes across versions.
Works offline, allowing local version control.
Supports branching and merging for independent and collaborative development.
Integrates seamlessly with platforms like GitHub, GitLab, or Bitbucket.
Basic Concepts in Git
Repository (Repo): A repository is where your project's files and their history are stored. It can be local (on your machine) or remote (on a server like GitHub, GitLab, etc.).
Commit: A snapshot of your code at a particular point in time.
Branch: A separate line of development, allowing you to work on features or fixes independently.
Merge: Combining changes from different branches into one.
Staging Area: A temporary area where changes are prepared before committing.
HEAD: The current branch or commit you are working on.
Step-by-Step Guide to Set Up Git
Frequently Used Git Commands
Here are some essential Git commands to get started:
Command | Description |
git status | Shows the current state of your repository. |
git add <file> | Stages a file for commit. Use git add . to stage all files. |
git commit -m "message" | Creates a commit with a descriptive message. |
git log | Displays the commit history. |
git branch | Lists all branches in the repository. |
git checkout <branch> | Switches to the specified branch. |
git merge <branch> | Merges the specified branch into the current branch. |
git pull | Updates your local repository with changes from the remote. |
git push | Sends your commits to the remote repository. |
Install Git
On Ubuntu or WSL (Windows Subsystem for Linux):sudo apt update sudo apt install git -y git --version
Verify installation. The output should display something like
git version 2.x.x
.Configure Git
Set up your name and email for commits:git config --global user.name "Your Name" git config --global user.email "your.email@example.com" git config --list
Generate SSH Keys
For secure interaction with platforms like GitHub:ssh-keygen -t rsa -b 4096 -C "your.email@example.com" eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_rsa cat ~/.ssh/id_rsa.pub
Copy the public key (
id_
rsa.pub
) and add it to your GitHub account under Settings > SSH and GPG Keys.Create Your First Repository
On GitHub:Create a new repository (e.g.,
my-first-repo
).Clone it locally:
git clone git@github.com:your-username/my-first-repo.git cd my-first-repo
Hands-On Exercise for Day 1
Create a new repository named
learning-git
on GitHub.Locally, create a file
notes.txt
with the content:echo "Git is a distributed version control system." > notes.txt git add notes.txt git commit -m "Added notes.txt with Git basics" git push origin main
Verify the changes are reflected on GitHub.
Outcome for Day 1:
Set up Git on your environment.
Configured SSH keys.
Created, cloned, and pushed changes to a GitHub repository.
Day 2: Core Git Commands and Workflow
1. Git Workflow Overview
Git operates through three main stages:
Working Directory: Files you’re actively working on.
Staging Area: Changes added using
git add
, ready to commit.Repository: The commit history of your project.
Key Commands Recap:
git status # Check repository state
git add <file> # Stage changes
git commit -m "Message" # Save changes to history
git push origin main # Upload changes to remote repository
2. Branching and Merging
Branches allow independent work on features or fixes.
Create a new branch:
git branch feature-branch
Switch to the branch:
git switch feature-branch
Merge changes back to
main
:git switch main git merge feature-branch
Hands-On Exercise: Branching and Merging
Create and switch to a branch named
add-welcome-message
.Add a welcome message to
README.md
and commit changes.Merge the branch back to
main
.Optionally delete the branch:
git branch -d add-welcome-message
3. Resolving Merge Conflicts
Conflicts occur when two branches modify the same lines in a file.
Steps to Resolve a Conflict
Simulate a conflict:
- Add different lines to the same file (
notes.txt
) on two branches.
- Add different lines to the same file (
Attempt to merge the branches:
git merge conflict-branch
Resolve the conflict manually by editing the file to keep the desired content.
Add the resolved file and commit:
git add notes.txt git commit -m "Resolved merge conflict"
4. Working with Remote Repositories
Remote repositories enable collaboration.
Fetch updates without merging:
git fetch origin
Pull updates and merge:
git pull origin main
Push changes to remote:
git push origin <branch>
Hands-On Exercise
Simulate collaboration by modifying a file on GitHub directly.
Pull the changes locally and merge them with your work.
Push your final changes back to the repository.
Outcome for Day 2:
Learned branching, merging, and resolving conflicts.
Practiced collaboration using remote repositories.
Next Steps
Day 3 will focus on advanced Git workflows, including:
Using tags for releases.
Cherry-picking commits.
Interactive rebasing for cleaner commit history.