Introduction to Git and version Control.
- What version control is and why it matters. Git is a system that helps manage a project. Insted of keeping one copy of your work version control with git creates a history of snapshots called commits. Each commit records the state of your project at a specific moment, so you can always revisit, compare or restore past versions.
Benefits of using Git.
- Safety & Reliability. Automatic backups using commit is a snapshot of your project so you never lose your work. Easy recovery if something breaks, you can roll back to the version.
- History & Tracking. There is a detailed change logs of who mdae changes, what changed and when. It helps compare versions and see differences between any two points and helps maintain accountability.
- Collaboration. Multiple developers can work on the same project without overwriting each other's code. Developers can merge changes safely and git provides tools to handle overlapping edits.
- Git makes coding safer, smarter and more collaborative.
Difference between local and remote repositories
A local repository is stored on your computer and lets you work offline, while a remote repository is hosted on a cloud server like Github and enables collaboration, back up and sharing across multiple developers.
In a local repository you make changes stage them using git add and the then git commit. You push your commits using git push to the remote repository to share with others.
Setting up Git
Installing Git on your system.
Download and install git
Configuring username and email.
git config --global user.name "your Name"
git config --global user.email"Your.email@example.com"
SSH Key Generation.
ssh-keygen -t ed25519 -C "your.email@example.com
eval "$(ssh-agent -s)"
cat ~/.ssh/id_ed25519.pub
- This commands wil generate a key that you use to connect to your git hub account/ remote repository.
Creating your first local Repository
A repository is a folder that holds and tracks all your project.
Create a folder and file for your project
mk dir folder1cd folder1Create a file_file1.txt
Initializing a repository.
When you start an new project and want Git to track it, you need to initialize a repository. This sets up the hidden
.gitfolder where Git store all the history, configuration, and metadata for your project.To tell Git to start tracking your
git init
Tracking changes
Checking repository status
- The git status shows you the current state of your working directory and staging are. It helps you understand what's going on in your project before committing changes.
git status
Staging files.
- This isthe process of preparing changes before committing them. This ia a waiting are where you select which edit will be included in the next snapshot (commit)
git add file1.txt
committing changes
A commit is saving a snapshot of you project. Once you have staged files, committing records into the repository's history.
git commit -m "Describe your changes here"
Pushing code to a remote repository.
Connecting to a remote
Link your local repository to GitHub:
we then have to connect out remote repository (github repo) to our locally hosted repository. This ensure that changes commited locally reflect on our github
git remote add origin"Your_remote_repo_url
Staging and commiting changes
git add .
git commit -m "Your commit message"
Pushing Commits
This is pushing changes from your local repository to our remote repository in github.
git push -f origin main
origin- Remote repositorymain- the branch you are pushing-u- Sets the upstream branch.
Pulling changes from Github.
When someone else makes changes—or when you switch computers—you’ll want the latest version of the project. This is called pulling
git pull- This command will fetch and merge the changes.When to pull:
When starting work on a project
When teammates have made changes
Before making your own changes
Git is a powerful tool that becomes intuitive with practice.
Top comments (0)