Intro to Git
As a Djangonaut (or aspiring Djangonaut), you may be aware just how important version control systems such as Git are.
Git is in fact the most popular version control system out there, and tons of projects use it to track changes to their code, including Django itself.
Git can also be scary, even to experienced developers like me, who manage to mess things up regularly. However, as it's definitely core knowledge every developer should have, this section will hopefully make it feel more approachable.
In this section, we'll introduce what Git is, some basic commands, and how to work with Git online. Later in this book, we will build up on these skills to run automated tests, deploy code to a server, and more.
What is Git anyway?
Git is a free and open-source distributed version control system that can be used to track changes to your code. It was created by Linus Torvalds and others in 2005 to track changes to the Linux kernel.
What is a version control system?
A version control system is a system that helps you manage changes to code, documents, or other digital content over time. It allows you to:
- Record each change to every file in your repository.
- Share your code with co-workers, or other collaborators, so you can work on it together or separately.
- Publish it to private or public online repositories.
- Revert changes to any file to back to an earlier state.
- Fork projects, so you can have your own copy that diverges from an existing one.
- Contribute to open source projects.
What is a distributed version control system?
A distributed version control system is a type of version control system that stores data in a decentralized manner, meaning that every developer working on a project has a full copy of the entire history of the project.
It differs from older client-server such as CVS and Subversion, which store data in a centralized manner.
Core concepts and key words
A Git repository, or repo, is a central location where all the files and history of changes are stored. You can think of it as a database that contains all the commits made to the project. It can be hosted on your own machine, as a directory, or online.
The git root is the top-level directory of your project; the directory containing the .git
folder.
A local repository is a copy of the project's code on your computer. It can be a repository you created on your own machine, or copied from another location.
A remote repository is a copy of your local repository hosted on a server (e.g., GitHub, GitLab). You can push and pull changes to/from these repositories.
Branches allow you to work on different versions of your codebase simultaneously. A branch represents a separate line of development, allowing multiple developers to collaborate without conflicts.
There are different branching strategies, but some common branches include:
- The
main
ormaster
branch. This is the default branch of any project, and where production-ready code lives. - Feature branches, where new features are added to the project.
You could call it foo
instead of main
, but as it's a common convention, we will refer to the primary branch as main
throughout this book.
Note
Historically, the initial branch name was master
. Depending on your Git version, this may still be the case. As the name master
may be considered offensive, most projects today use main
.
The staging area is an index of changes to be committed.
A commit is a snapshot of your project's state. It's a record of all the changes you have made to your codebase.
Merging is a way of combining two branches into a single branch.
HEAD is a pointer to the latest commit in the current branch.
Rebasing is a way of changing the history of a branch. It allows you to apply the commits of one branch to another.
Tags are labels that mark specific commits in the history. They're useful for tracking releases or milestones.
Don't worry if this is too much right now. We will look at some practical examples in the next couple of chapters, which will hopefully make everything clearer.
Links
- Pro Git: a book that explains Git in greater detail.
- Dangit, Git!?!: a few solutions to common Git problems and mistakes.