Git is a distributed version control system (VCS) used for tracking changes in code or files. It allows multiple developers to work on a project simultaneously without overwriting each other’s changes. Git keeps a detailed history of all changes, so you can easily go back to previous versions, collaborate with others, and manage your code more efficiently.
Git is often used in conjunction with platforms like GitHub to store code remotely and facilitate collaboration.
Key Features of Git
- Version Control: Keeps track of every change made to the files in a project.
- Collaboration: Multiple developers can work on the same project simultaneously using branches.
- Backup and Recovery: Since the entire history of the project is stored, you can recover files from any point in time.
- Branching: You can create branches to work on new features without affecting the main codebase.
Why Use Git?
- Collaboration: Multiple developers can work on the same project without interfering with each other’s work.
- History Tracking: Every change is logged, so you can see who made changes, what was changed, and why.
- Backup: Since Git tracks every change, you can recover lost code.
- Branching: Developers can experiment with new features on separate branches and only merge them into the main code when they’re ready.
Example of How Git Works
Let’s walk through a simple Git workflow:
1. Initialize a Repository
Start by creating a local Git repository to track changes.
git init
This creates an empty Git repository in your project folder, which will start tracking changes to your files.
2. Create a File
Create a new file, say hello.txt
, and add some content.
echo "Hello, World!" > hello.txt
3. Stage the File
Before Git can track changes, you need to stage the file. Staging means you’re telling Git to include this file in the next commit (save point).
git add hello.txt
4. Commit the Changes
Now, commit the changes to save them in the repository with a message describing what was changed.
git commit -m "Add hello.txt with a greeting"
At this point, Git has saved the file and its content, along with the commit message.
5. Push the Code to GitHub
If you want to share your code with others or store it remotely, you can push it to a GitHub repository.
git push origin main
A Simple Real-World Example
Let’s say you are working on a web application with a team, and you need to add a new login feature. You don't want to mess up the stable version of the app, so you create a new branch called login-feature
.
- You develop the login feature on this branch.
- You stage and commit your changes.
- Once the feature is ready and tested, you merge the
login-feature
branch into themain
branch and push it to GitHub so others can access the updated version.
Summary of Basic Git Commands:
git init
: Initialize a new Git repository.git add <file>
: Stage a file to be committed.git commit -m "message"
: Commit the staged changes with a descriptive message.git branch <branch-name>
: Create a new branch.git checkout <branch-name>
: Switch to a different branch.git merge <branch-name>
: Merge changes from one branch into another.git push origin <branch-name>
: Push commits to a remote repository (e.g., GitHub).