In the world of software development, Git is one of the most widely used version control systems. Whether you’re working on a solo project or collaborating with a large team, Git provides a robust way to track changes in your code, manage versions, and collaborate effectively.
What is Git?
Git is a distributed version control system that helps developers track changes in their source code over time. This allows you to:
- Track changes: Keep a detailed history of every change made to a project.
- Collaborate: Work with other developers without overwriting each other’s work.
- Restore previous versions: Roll back to an earlier version of your project if something goes wrong.
Unlike centralized version control systems, Git allows every user to have a full copy of the project history on their local machine, making it easier to work offline or in different locations.
Basic Git Terminology
Before diving into how Git works, let’s get familiar with some essential terms:
- Repository (Repo): A Git repository is where your project’s files and version history are stored. It can be local (on your computer) or remote (on platforms like GitHub).
- Commit: A snapshot of changes made to the project. Each commit has a unique identifier (hash).
- Branch: A separate line of development, often used for working on features or bug fixes.
- Merge: The process of combining changes from different branches.
- Pull/Push: Pull refers to downloading changes from a remote repository, while Push refers to uploading your changes to a remote repository.
- Clone: Making a copy of a remote repository on your local machine.
Example of simple branch tree:
main main
|-- features
|-- new-login features/new-login
|-- audit-logs features/audit-logs
|-- fix
|-- missing-privacy-link fix/missing-privacy-link
Setting Up Git
Before you start using Git, you need to install it on your machine. Here’s how:
1. Install Git
- On macOS:
There are multiple ways how to do it on mac. The basic is:
$ brew install git
- On Linux:
Installation of git on linux machine always on distribution. Following command will install git on Ubuntu/Debian based systems.
$ sudo apt-get install git
- On Windows:
The best way how to use git on windows is to use WSL2 and git. For more info visit https://learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-git
2. Configure Git
Once installed, you should set up your user name and email, which will be associated with your commits.
$ git config --global user.name "Iron man"
$ git config --global user.email "iron.man@starkindustries.com"
You can check your settings with:
$ git config --list
Basic Git Workflow
Here’s a quick and easy workflow to help you get up and running with Git, walking you through the basics of setting up repos, tracking changes, and working with others smoothly.
1. Initialize a Repository
To create a new Git repository in an existing directory, navigate to that directory and run:
$ git init
This initializes a new, empty Git repository.
2. Add Files
After making changes or adding new files to your project, you need to stage the files for commit:
$ git add <filename>
You can also add all changes with:
$ git add .
3. Commit Changes
Once the files are staged, commit them with a descriptive message:
$ git commit -m "Bugs neutralized, and system’s running smoother than Jarvis. Let’s keep it snappy, people!"
4. Create a Branch
To create a new branch for a feature or bug fix:
$ git checkout -b feature-branch
This creates and switches to a new branch called feature-branch.
5. Merge a Branch
After working on a branch and committing your changes, you can merge it back into the main branch:
$ git checkout main
$ git merge feature-branch
6. Push Changes to a Remote Repository
If you’re working with a remote repository (like on GitHub or GitLab), and you’ve made some changes locally that you’re ready to share with the world (or your team), you can easily push those changes up to the remote repo. This way, everyone else can pull the latest version and stay in sync with what you’ve been working on.
- Git Push: The push operation sends your local commits to the remote repository, updating it with your changes.
$ git push origin main
Local Repo
|
o----o----o
|
git push
|
v
o----o----o
Remote Repo
To pull the latest changes from the remote repository:
- Git Pull: The pull operation fetches and merges changes from the remote repository into your local repository, keeping it up to date.
$ git pull origin main
Remote Repo
|
o----o----o
^
|
git pull
|
o----o----o
Local Repo
Undoing Changes
If you’ve made some changes but then had second thoughts and decided they aren’t quite what you want, or maybe you just want to start over, you can easily discard those changes before committing them:
$ git checkout -- <filename>
If you want to clear out the staging area (where files are prepped for the next commit) and remove any changes you’ve added, you can “reset” it. This will unstage all the files, but it won’t delete your actual changes—just removes them from being ready to commit. It’s like saying, “Oops, I’m not quite ready to commit these changes yet, let’s take a step back.
$ git reset
Working with Remote Repositories
Here’s how to connect your local repository to a remote one like GitHub or GitLab.
1. Clone a Repository
To clone an existing repository:
$ git clone https://github.com/username/repo-name.git
2. Add a Remote
If you’ve initialized a new repository locally and want to add a remote repository to push to:
$ git remote add origin https://github.com/username/repo-name.git
3. Push to Remote
To push your local changes to the remote repository:
$ git push -u origin main
Conclusion
Git is a powerful tool that allows developers to efficiently manage code changes and collaborate with others. This basic Git 101 guide should help you get started with version control. As you become more familiar with Git, you’ll discover advanced features such as rebasing, interactive staging, and more.
For more information, check out Git’s official documentation.