Git is a version control system that helps you keep track of changes to your code over time. It allows multiple people to work on the same project without overwriting each other's work.
GitHub is a web-based platform that hosts Git repositories online. It provides a centralized place to store your code, collaborate with others, review changes, and manage projects.
To start using Git on your computer, you need to install it.
Windows:
Download the Git installer from git-scm.com and run it. During installation, you can accept the default options.
macOS:
You can install Git using Homebrew with the command
brew install git
Alternatively, install Git via the official installer from git-scm.com.
Linux:
Use your package manager. For example, on Ubuntu:
sudo apt-get update
sudo apt-get install git
A repository (repo) is a storage space where your project’s files and version history live.
Create a new local repository:
Navigate to your project folder in the terminal and initialize Git:
git init
Clone an existing repository:
If you want to get a copy of a project from GitHub, use:
git clone https://github.com/username/repository.git
Git tracks three states of files:
- Working directory: Files you see and edit.
- Staging area: Files marked to be committed.
- Local repository: Committed snapshots of your files.
This is the core workflow when working with Git.
-
Check the status:
See which files have changed or are untracked:
git status
-
Stage changes:
Add files you want to include in the next commit:
git add filename
-
Commit changes:
Save a snapshot with a descriptive message:
git commit -m "Add feature X or fix bug Y"
-
Push changes to GitHub:
Upload your commits to the remote repository:
git push origin main
Branches allow you to work on new features or fixes independently from the main codebase.
-
Create a new branch:
git branch feature-branch
-
Switch to a branch:
git checkout feature-branch
-
Work on the branch:
Make commits as usual.
-
Merge changes back
Switch to your main branch and merge the feature branch:
git checkout main git merge feature-branch
-
Resolve conflicts:
git checkout main git merge feature-branch
Congratulations! You now have a solid understanding of the basics of Git and GitHub. With these skills, you can effectively manage your projects, collaborate with others, and keep a detailed history of your work.
To continue improving, consider exploring more advanced Git features like rebasing, stashing, and hooks. Also, try contributing to open source projects on GitHub to gain real-world experience.