Understanding Git and GitHub: A Biginners Guide
Source: Dev.to
What is Git?
Git is a version control system that helps you keep track of changes made to your code. Instead of saving many copies of the same project, Git records each change and stores it as part of the project history.
With Git you can always go back to an earlier version of your code if something goes wrong. Git works locally on your computer and does not need an internet connection to track changes.
Why version control is important
Version control is important for several reasons:
- It helps you avoid losing your work.
- You can see what changes were made and when.
- It allows multiple people to work on the same project.
- You can easily fix mistakes by going back to a previous version.
In real software projects, version control is necessary because code is constantly changing.
Git and GitHub Difference
Git and GitHub are not the same thing.
- Git is the tool that tracks changes on your computer.
- GitHub is an online platform where you store and share your Git repositories.
You use Git commands to push your code to GitHub or pull code from GitHub.
Setting up Git on your computer
Before using Git, make sure it is installed and configured.
Check Git version
To confirm Git is installed:
git --version
Configure your name and email
Git uses this information to track who made changes.
git config --global user.name "okwemba"
git config --global user.email "okweembaajoseph@gmail.com"
To confirm the configuration:
git config --global --list
Setting up SSH
SSH allows you to connect to GitHub securely without entering your password every time.
Generate SSH key
ssh-keygen -t ed25519 -C "okwembaajoseph@gmail.com"
Press Enter to accept the default file location.
Start the SSH agent
eval "$(ssh-agent -s)"
Add SSH key
ssh-add ~/.ssh/id_ed25519
Copy the public key
cat ~/.ssh/id_ed25519.pub
Copy the output and add it to GitHub → Settings → SSH and GPG keys.
How to push code to GitHub
Open your project in the terminal and follow these steps:
-
Initialize Git
git init -
Check file status
git status -
Add files
git add . -
Commit changes
git commit -m "My first commit" -
Connect to GitHub repository
git remote add origin https://github.com/username/repository-name.git -
Push the code
git push -u origin main
Your code will now appear on GitHub.
How to pull code from GitHub
Pulling means getting the latest version of the project from GitHub:
git pull origin main
This is useful when collaborating with others or when switching devices.
How to track changes using Git
-
See changed files
git status -
See what exactly changed
git diff -
View commit history
git log
These commands help you understand how your project has changed over time.
Conclusion
Git is an essential tool for developers. It helps you manage your code, track changes, and collaborate with others.