
Version Control: Git

mohamad
Published 21/11/2024 - Last Updated 22/11/2024
#Git#Github
What is Version Control
Version Control is a system that helps developers track and manage changes to software code over time. It allows multiple developers to collaborate on a project, keeping a history of every modification made to the codebase, and provides a way to revert back to previous versions if needed. This helps prevent issues caused by overwriting work, losing code, or breaking parts of the project. Version control is crucial for any software development process, ensuring that changes are organized, collaborative, and easily reversible.
Git is one of the most widely used version control systems today. Developed by Linus Torvalds (the creator of Linux), Git is a distributed version control system, meaning that every developer’s copy of the project (local repository) contains the full history of the project, unlike centralized systems where the history is stored only on a central server. This makes Git highly efficient for working both offline and in distributed teams.
With Git, developers can:
- Track changes to the codebase.
- Branch and merge code for experimentation and new features.
- Collaborate easily by sharing changes with other developers through platforms like GitHub or GitLab.
In short, Git enables developers to manage code changes with speed, efficiency, and security, making it an essential tool for modern software development.
Life Cycle
- Initializing a Git Repository: initiate git or add git to project
git init
- Check status:
git status
- Stage: add files for tracking
When you stage changes, Git takes a snapshot of the files at that moment in time. This snapshot includes all of the changes you've made since the last commit.
Staging changes allows you to carefully review your changes before committing them. You can stage changes in small chunks and commit them separately, or stage all changes and commit them together.
git add .
- Commit: commit current changes
This creates a new snapshot of your repository with the changes you made.
git commit -m "message"
- Add a remote repository
By adding a remote repository, you establish a connection between your local repository and the remote repository, allowing you to push and pull changes between them.
git remote add origin repository_URL
- Push: Push to remote This uploads your changes to a shared repository that others can access.
git push -u default_branch
- Pull The git pull command fetches the latest changes made by other contributors from a remote repository and automatically merges them into the current branch.
Collaboration
- Cloning Cloning creates a local copy of a remote repository, including all the files and history of the project.
git clone repository_URL
- Branching Create new branch
git branch branch_name
To switch to the new branch
git checkout branch_name
or join the above steps:
git checkout -b branch_name
- Merging
git merge
Restore Delete
Gitignore
create a file ".gitignore" with the files and folders to be ignored
Directory names (for example, node_modules/, dist/, build/) File extensions (for example, *.log, *.tmp, .env) Specific files (for example, secrets.txt, config.ini)
Related Posts

