Version control is a system that helps track and manage changes to files, especially in software development. It allows multiple people to collaborate on a project without overwriting each other's work and provides a history of modifications. Version control helps in:
Version control systems (VCS) help developers track changes, collaborate efficiently, and manage different versions of a project. There are three main types:
Before Git, developers relied on centralized version control system. It had limitations like single points of failure and restricted offline work. Developers cannot have their own copy of the repository and hence, cannot work offline independently.
These drawbacks led to the rise of Git, which offers a distributed approach for better flexibility and resilience. Git was created to address the challenges of managing code efficiently, especially in large-scale projects with multiple contributors.
Git is a distributed version control system that helps developers track changes in their code, collaborate efficiently and manage different versions of a project. Git was initially developed by Linus Torvalds in 2005 for managing the Linux kernel, but its advantages quickly made it the industry standard for version control.
Here is why it is widely used:
In Git, distributed means that every developer has a complete copy of the repository, including its entire history, on their local machine. Unlike centralized version control systems, where a single server holds the main repository, Git allows developers to work independently without needing constant access to a central server.
Git is the engine, while GitHub is the garage where teams collaborate.
Feature | Git | GitHub |
---|---|---|
Type | Software | Hosting Service |
Function | Version control | Repository management |
Installation | Local | Cloud-based |
Collaboration | Command-line based | Web inteface with additional tools |
Ownership | Open-source (Linux foundation) | Owned by Microsoft |
A temporary locker — Temporarily save changes which we’re not ready to commit.
This is where we create or modify files. Changes here are not yet tracked by Git.
Where we prepare changes before committing.
Where committed changes are stored locally.
Shared repository for collaboration (e.g., GitHub).
Scenario: We're working on a new feature, but suddenly need to fix a bug on the main branch. Want to stash your current work, switch branches, fix the bug, and then return to the feature.
git checkout -b feature/login-form
git status
git add login.js
git stash push -m "WIP: login form"
git checkout main
# Fix the bug in app.js
git add app.js
git commit -m "Fix: crash on login redirect"
git push origin main
git checkout feature/login-form
git stash pop
Git Area | What happened |
---|---|
Workspace | Edited files like login.js and style.css. |
Staging Area | Staged login.js before stashing. |
Stash | Saved both staged and unstaged changes temporarily. |
Local Repository | Committed the bug fix to the local repo. |
Remote Repository | Pushed the fix to GitHub (or another remote). |
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub
Run:
ssh -T git@github.com
If successful, will see a message like:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Purpose | Commands |
---|---|
Set Identity Git requires a username and email for commits. |
|
Configure Default Branch Name By default, Git initializes repositories with the master branch. Can change it to main: |
|
Configure Line Endings (Windows/Linux Compatibility) | If on Windows, set up automatic line ending conversion:
|
Set Default Editor Choose preferred text editor for Git commit messages. |
|
Enable Credential Caching To avoid entering credentials repeatedly. |
|
View All Configurations To check current Git settings |
|
Here we're assuming that you already have a repository on github and you're ready with the url.
git clone https://github.com/your-organization/repository-name.git
Move into the cloned repository:
cd repository-name
Ensure the remote is correctly set:
git remote -v
If needed, add the remote:
git remote add origin https://github.com/your-organization/repository-name.git
To keep local repository updated with the latest changes. Replace main with the default branch name if it's different
git pull origin main
If there are conflicts, resolve them manually, then commit the changes
git add .
git commit -m "Resolved merge conflicts"
git push origin main
Synchronization is very much required when working in a collaborative environment. It ensures that your local changes are up-to-date with the remote repository and that the contributions are integrated with the work of others.
Before pushing, let's check which files have been modified.
git status
Stage all modified files.
git add .
Or, to add specific files
git add filename
Commit the changes with a meaningful message.
git commit -m "Updated files with latest changes"
Ensure that the local branch is up to date before pushing. Replace main with the branch name if different
git pull origin main --rebase
Push the updates to the remote repository.
git push origin main
Check if the changes are successfully pushed
git log --oneline -n 5