Git explained: History, use and benefits of source code management. Learn about branching, commands and increasing productivity! 🚀
Git is a distributed version control system that was originally developed by Linus Torvalds for the development of the Linux kernel. Since its introduction in 2005, Git has established itself as the leading tool for source code management in software development. In this article, we will explore the history of Git, its use in an example project, the most common commands and the benefits of source code management.
The-history-of-git 📜
The history of Git begins with the need for a version control system for the Linux kernel. Before Git, developers used a proprietary system called BitKeeper. Due to license disputes in 2005, the Linux community was forced to develop its own solution. Linus Torvalds designed Git with the following goals in mind:
- Speed
- Simplicity of design
- Support for non-linear development (thousands of parallel branches)
- Fully distributed
- Strong support for non-linear development
Torvalds’ goal was to create a system that was efficient and robust enough to meet the needs of a project as large and complex as the Linux kernel.
Using Git in an example project 🚀
To demonstrate the use of Git, let’s create a simple example project. Let’s assume we are working on a website. Here are the steps to use Git in our project:
Initialize repository
First, we initialize a new Git repository:
git init
This creates a hidden .git
directory that stores all Git-related information.
Add and commit files
Now we add our project files and create our first commit:
echo "# My website" > README.md
git add README.md
git commit -m "Initial commit"
Track and commit changes
Suppose we add a new file index.html
:
echo "<!DOCTYPE html><html><head><title>My website</title></head><body><h1>Welcome!</h1></body></html>" > index.html
git add index.html
git commit -m "Add index.html"
Using branches
Branches are a powerful feature of Git. They enable parallel development threads. Let’s create a new branch for a new feature:
git checkout -b feature-new-function
Now we are working in a new branch and can make changes without affecting the main
branch.
Merge changes
Once we have finished working on our new function, we merge it back into the main
branch:
git checkout main
git merge feature-new-function
If there are conflicts, these must be resolved manually.
Most common Git commands in a table 📋
Here are the most common Git commands summarized:
Command | Description |
---|---|
git init | Initializes a new Git repository |
git clone | Clones an existing repository |
git add | Adds a file to the staging area |
git commit -m “” | Commits the files in the staging area with a message |
git status | Displays the status of the working directory |
git log | Displays commithistory |
git branch | Lists all branches |
git checkout | Switches to another branch |
git merge | Merges a branch into the current branch |
git pull | Gets and merges changes from a remote repository |
git push | Sends local commits to a remote repository |
Advantages of source code management with Git 🌟
Git offers many advantages that make it an indispensable tool in software development:
- Traceability: All changes in the code are traceable and documented.
- Branching and merging: Facilitates parallel development and the merging of changes.
- Distributed development: Every developer has a complete copy of the repository and can work offline.
- Collaboration: Facilitates collaboration in teams through central repositories and pull requests.
- Fixing errors: Easy reversion to previous versions in the event of errors.
- Security: The source code is only available to one developer and can therefore be quickly restored in the event of problems or failures and, if necessary, restored.
Working with branches 🌿
Branches allow developers to work on different features or bugs in parallel without jeopardizing the stability of the main code. Each branch represents a separate line of development.
Create and switch branches:
git branch <branchname>
git checkout <branchname>
Merge changes:
git checkout main
git merge <branchname>
Delete branch:
git branch -d <branchname>
Increase productivity and speed with Git 🚀
Git helps to increase productivity by:
-
Simplifying conflict resolution: Tools like
git merge
andgit rebase
help resolve conflicts. - Continuous integration: Integration tools such as Jenkins or Travis CI work seamlessly with Git.
- Automation: Scripts and hooks can perform automated tasks before or after certain actions.
Git hosting services 🖥️
In addition to the local use of Git, there are numerous hosting services that manage Git repositories online. The best known include:
- GitHub: The most popular Git hosting service, which is home to a large community and many open source projects.
- GitLab: Offers similar functions to GitHub, but also integrated CI/CD pipelines.
- Bitbucket: Particularly popular with teams that work with Atlassian products such as Jira.
- SourceForge: One of the oldest platforms for managing open source projects.
These services not only offer hosting, but also numerous tools for code review, project management and collaboration.
Git Hooks 🔗
Git Hooks are scripts that are automatically executed when certain events occur in a Git repository. They can be used to automate workflows and ensure that certain conditions are met before or after a commit.
Examples of hooks are:
- Pre-commit: Executes actions before a commit is made (e.g. code formatting).
- Pre-push: Executes actions before changes are pushed (e.g. code testing).
- Pre-commit: Executes actions before changes are pushed (e.g. code testing).
- Post-merge: Executes actions after a branch has been merged (e.g. installing dependencies).
How to handle large files 🗂️
Git is not optimized for managing large binary files. For this purpose, there are extensions such as Git Large File Storage (LFS) that make storing and managing large files more efficient. Git LFS replaces large files with references in your repository and stores the actual files in a separate storage.
Best practices for using Git 📏
To get the most out of Git, the following best practices should be followed:
- Small, frequent commits: It is better to commit small changes frequently than large changes rarely.
- Meaningful commit messages: Commit messages should describe what was changed and why.
- Branching strategies: Use strategies such as Git Flow or GitHub Flow to manage branches and releases.
- Code reviews: Regular code reviews by other team members improve code quality and promote knowledge transfer.
Git in DevOps and CI/CD 📈
In modern development environments, Git plays a central role in DevOps and CI/CD pipelines. Automated processes such as Continuous Integration (CI) and Continuous Deployment (CD) are almost inconceivable without a reliable version control system. Git makes it possible to continuously integrate and deploy changes, which improves software quality and shortens time-to-market.
- Continuous Integration: Automates the testing and merging of code changes.
- Continuous Deployment: Automates the deployment of code changes to production environments.
Git GUI tools 🖱️
For developers who want to work with Git from more than just the command line, there are a variety of Git GUI tools that make working with Git easier:
- Sourcetree: A free GUI tool from Atlassian that supports Git and Mercurial repositories.
- GitKraken: A user-friendly Git client with numerous functions for managing repositories.
- TortoiseGit: A Windows Explorer plugin that integrates the management of Git repositories in Windows.
- GitHub Desktop: An official GUI tool from GitHub for managing repositories on GitHub.
So you don’t have to be a console nerd to use Git. Many IDEs, like my favorite Visual Studio Code, already have Git integrated.
Microsoft’s move from TFS to Git 📈
Since Microsoft acquired GitHub in 2018 for 7.5 billion dollars, the company has begun to focus more of its development tools on Git. This move was part of Microsoft’s larger strategy to provide more support to the developer community and promote open development models. As a result, Team Foundation Server (TFS), Microsoft’s own version control system, is increasingly being replaced by Azure DevOps and the integration of Git.
TFS has been in use in many organizations for years, but Microsoft is now actively promoting the use of Git. This includes comprehensive support and migration tools designed to ease the transition from TFS to Git. For example, Microsoft provides detailed guidance and tools such as Git-TFS to support the migration from TFVC (Team Foundation Version Control) to Git.
This change not only shows the acceptance of Git as the standard for source code management, but also Microsoft’s commitment to developer freedom, openness and innovation. The integration of GitHub into Microsoft’s product suite aims to increase the use of Git in the enterprise world and empower developers with modern, efficient tools.
Conclusion 📃
Git is a powerful and flexible tool for source code management. It facilitates collaboration, improves traceability and increases productivity. With its extensive features and support for distributed development, Git is an indispensable tool for developers. To learn about Git and the way it works, there are very interesting projects:
Oh My Git! by blinry, bleeptrackAn open source game about learning Git!
Git ImmersionNo image availableA guided tour that walks through the fundamentals of Git
Learn Git BranchingAn interactive Git visualization tool to educate and challenge!
Have any questions or want to share your experience with Git? Leave a comment below! ☝️
If you like my posts, it would be nice if you follow my Blog for more tech stuff.