By YusufMTech
Git is a distributed version control system designed to track changes in your code over time. Before Git, developers often saved multiple copies of files with confusing names like project-final.html, project-final2.html, etc. Git solves this by keeping a full history of your project, allowing you to revert to previous versions easily.
Created in 2005 by Linus Torvalds, Git handles large projects efficiently and safely. Developers worldwide use it to manage websites, software, and apps.
Benefits of learning Git include:
Version control records changes to files so you can revert to earlier states if needed. Without version control, accidentally deleting code could cause major problems.
Types of version control:
Version control is essential for tracking changes, collaboration, and managing project history.
Git helps developers in multiple ways:
GitHub is an online platform for hosting Git repositories. It allows you to store projects, collaborate with others, and showcase your portfolio. Key features:
Git is a version control tool installed locally. GitHub is an online platform that hosts Git repositories. Use Git to manage projects on your computer and GitHub to store and share them online.
Steps to install Git:
git --versionAfter installation, configure your name and email:
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
Check configuration:
git config --list
Git Bash is a terminal to run Git commands. You can navigate folders, create files, and execute Git operations efficiently.
ls
cd foldername
mkdir project
Create your first project:
mkdir myproject
cd myproject
git init
touch index.html
git status
You now have a local Git repository ready for version control.
The Git workflow:
git add)git commit)git push)Example:
git add .
git commit -m "Initial commit"
git push
The git init command initializes a new Git repository in your project folder. This creates a hidden .git folder that stores all the version history and configuration.
Example:
mkdir myproject
cd myproject
git init
After running git init, your folder becomes a Git repository, ready to track changes. Always use this for new projects.
git status shows the state of your repository. It tells you:
Example:
git status
Always check git status before committing to avoid mistakes.
The git add command moves changes from the working directory to the staging area. Only staged changes will be committed.
Example:
git add index.html
git add .
Using git add . stages all changes in your folder. Be careful to stage only what you want.
git commit saves staged changes to the repository with a descriptive message.
Example:
git commit -m "Added homepage HTML structure"
Best practice: Write meaningful commit messages that describe the changes.
git log shows the history of commits in your repository.
Example:
git log
Each commit has a unique ID, author, date, and message. This allows you to track all changes made to your project.
GitHub is an online platform to store and manage your repositories. To create an account:
Once your account is ready, you can start hosting repositories online.
After logging into GitHub, you can create a new repository:
myproject)This is where your project will be stored online.
To connect your local repository to GitHub, you need to add a remote:
git remote add origin https://github.com/username/myproject.git
This tells Git where to push your code online. Use git remote -v to verify the connection.
The git push command uploads your local commits to the GitHub repository.
git push -u origin main
“-u origin main” sets the upstream branch. After the first push, you can simply use git push.
This is how your project becomes visible on GitHub.
git pull downloads the latest changes from GitHub to your local repository.
git pull origin main
This keeps your local project up-to-date, especially when working in a team.
Best practice: Always pull before starting new changes to avoid conflicts.
git clone is used to copy a repository from GitHub to your local machine.
git clone https://github.com/username/myproject.git
This is essential when starting to work on an existing project.
Branches allow you to work on new features or experiments without affecting the main project.
Example: You can create a feature-login branch to build login functionality without touching the main branch.
Create a new branch:
git branch feature-login
List all branches:
git branch
Switch between branches using git checkout:
git checkout feature-login
Now, any changes you make affect only this branch.
Merge changes from one branch into another:
git checkout main
git merge feature-login
This integrates your feature branch into the main project.
Sometimes Git cannot merge automatically. Conflicts happen when two branches change the same line.
Steps to resolve:
<<<<<<<GitHub Pages allows you to host static websites for free.
You can connect your own domain to GitHub Pages:
CNAME file in your repositoryTo secure your site with HTTPS:
HTTPS ensures your website is secure and trusted.
Combine everything you’ve learned:
This final project demonstrates mastery of Git and GitHub workflow, and it can serve as a portfolio project for freelancing.