UNDERSTANDING VERSION CONTROL USING GIT : FOR BEGINNERS
Source: Dev.to
What is Version Control?
Version control is a way of keeping track of changes made to files over time. Instead of saving many copies of the same file with different names, version control records each change in an organized way. This makes it easy to see what was changed, when it was changed, and to go back to an earlier version if something goes wrong. It is especially useful for projects that grow over time or when working with other people on the same code.
What is Git?
Git is a version control system that helps developers manage and track changes in their projects. It works locally, saves snapshots of the project, and makes it easy to fix mistakes and review work.
What is GitHub?
GitHub is a website where you can store your Git projects online. It keeps your code safe, lets you share it with others, and makes collaboration simple. You can also access your projects from any computer, always having the latest version.
Importance of Version Control
- Avoid losing your work
- Undo mistakes by going back to previous versions
- Track your project’s progress over time
- Work on the same project from different devices
- Collaborate with others without overwriting their changes
How to Track Changes, Push Code and Pull Code on GitHub
Step 1: Install Git
- Go to the Git official website and download the version suitable for your operating system.
- Run the installer and follow the steps, leaving most settings as default.
After installation, open Git Bash or the command prompt and type:
git --version
This shows the version of Git installed.
Step 2: Create a GitHub Account
Register or sign up for a GitHub account.
Once that’s done, open your terminal (Git Bash) and set up your username and email so Git knows who made changes:
git config --global user.name "YOUR_USERNAME"
git config --global user.email "YOUR_EMAIL"
Replace YOUR_USERNAME and YOUR_EMAIL with your actual details.
Step 3: Create a Folder for Your Project
Create a folder on your computer for your project (e.g., testProject). Then open Git Bash and navigate into that folder:
cd testProject
Git will now work inside that folder.
Step 4: Initialize Git
Initialize Git in the folder:
git init
This turns the folder into a Git repository.
Step 5: Add a New File
Create a new file (e.g., firstfile.txt):
touch firstfile.txt
Check which files Git is tracking:
git status
Untracked files appear in red; tracked files appear in green.
Step 6: Track and Save Changes
Stage the new file:
git add firstfile.txt
To add all files at once:
git add .
Commit the changes with a descriptive message:
git commit -m "Commit Message"
Step 7: Connect Your Local Repository to GitHub
- On GitHub, create a new repository for your project.
- Copy the repository URL (e.g.,
https://github.com/username/repository-name.git). - Add the remote origin in your local repository:
git remote add origin https://github.com/username/repository-name.git
Replace the URL with your repository link.
Step 8: Push Code
Send your local commits to GitHub:
git push origin main
Step 9: Pull Code
To work on the project from another computer or get the latest version, clone the repository:
git clone https://github.com/username/repository-name.git
cd repository-name
To update your local copy with the latest changes from GitHub:
git pull origin main
Key Takeaway
Understanding how Git commands work is more important than memorizing them. Master the concepts, and the commands will become intuitive.