Software development is inherently full of challenges. This ranges from architecting your software, maintaining it, fixing bugs, deploying, and the list goes on. When starting out, you'd think that the easy part would be to share you code with others. After all, they're just text files, right? This may not be too difficult with extremely simple programs that have only one developer, but issues compound as the number of collaborators grows, as well as the size of the project.
For example, how do you share updates to a codebase with another developer working on the same project? Would you constantly email changed files back and forth? What if you're working on the same files simultaneously? How do you detect and merge these changes?
This was exactly why version control systems (VCS) were created, to allow simultaneous collaboration, sharing, and merging of changes to a project.
Git, a distributed version control, was released in 2005 and created by Linus Torvalds, who is also the creator of Linux. It is so popular that it's at the core of multiple multi-billion dollar companies, like GitHub, GitLab, and Atlassian's BitBucket, which host public and private Git repositories.
With Git being so popular among developers, it's an essential skill to learn. In this article, we'll cover the basics of what Git is, how it works, and where you can find resources to learn more.
What is Git?
Various VCSs have different structures. Take SVN for example, which is a centralized repository, meaning it has one "master" repository. This differs from Git, which is decentralized and has no master. In practice, a single hosted repository is usually shared among a team, but projects can be more easily forked and not have any dependency on the original repo.
This is a big reason for Git's popularity, it isn't inherently constrained by its architecture, and it has some features (like branching and merging) that are "first class citizens", as opposed to being an after-thought like in other VCSs.
You typically interact with Git through the command line tool, appropriately named
git. Although, there are now quite a few GUI tools out there (i.e. GitHub Desktop, SourceTree, and GitKraken) that helps you use some of the more common features, like committing, branching, and merging. With Git being a bit complex to use, some beginners opt to start off with these GUI tools. And while they can be extremely useful and help you be productive, I'd recommend you at least become comfortable with the command line version as well to deepen your understanding.
Git works by tracking what changed in each file in a project, who made that change, and why. Internally, this is done in a graph structure, and each new commit creates a new node in the graph.
A branch in Git creates a new node off of the main tree (the yellow node in the image below) in which you can make any change you want without affecting the main codebase. This way other developers can continue to work on their new features in separate branches while you work on yours, all without disrupting the progress of one another.
Once you've finished the updates on your branch, you can then merge it back in to the master branch. When this is done, everyone else working in the same repository will then pull in your changes to their local systems. By making changes in this way, you avoid disrupting the main branch.
Git can be complicated, which is probably why you're here. Because of that, we'll just cover a few of the main features of the command-line tool here, and provide resources if you want to dive deeper in to each function.
First of all, to get started you'll want to install
git on your system. Most Mac and Linux distributions come with Git pre-installed, but just in case yours doesn't, here is how to do it:
sudo apt install git-all
sudo dnf install git-all
Download the installer for Git for Windows and follow the instructions.
Once installed, you can create a new repository by entering the directory of your project (a Node.js project in our example) and using the
cd my-project git init
This will initialize Git, but before anything is tracked, you need to explicitly add your project files to the repo:
Free eBook: Git Essentials
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
git add package.json config.json
Git makes this process a bit easier and allows wildcards to select files in bulk:
git add src/*.js
Now, our files aren't yet committed to Git, they're just staged. We can view staged files using the
git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: config.json new file: package.json new file: src/index.js new file: src/api.js
Now we're ready to commit our changes to the repository using the
git commit -am "Initial commit"
-a flag tells Git to commit all staged files, and the
-m flag let's us add a commit message via the command line. If this is omitted then your default text editor will open, allowing you to enter the message there. This message is used to describe what changes took place.
From here you can do a lot of things, like, for example, tagging the commit with a version number, creating a branch to work on a new feature, or reverting back to an older commit if you made a mistake.
To complete our short example, let's push our changes to a remote repository, allowing other developers to get the changes. For this to work, we first need to create a repository on GitHub account, which we can then use to push to:
git remote add origin https://github.com/scottwrobinson/my-project.git git push origin master
The first command above tells Git the location of our remote repository, which we've named "origin". In the second command, we tell Git to push our code from the master branch to our origin server. Depending on how you've set up your GitHub account, you may be required to enter a password or specify an SSH key.
While it's helpful to see the basics of using a complicated tool like Git, there is still a lot to learn and this example just scratches the surface of what Git is capable of. Many of these features are out of the scope of this article, so we've provided a number of resources that dive in to more detail on more specific Git topics.
Resources to Learn More
- Create a Git Repository
- Clone a Repository
- Create a New Branch
- Checkout a Remote Branch
- Add All Files to a Repo
- Merge Branch into Master
- Delete Branch Locally and Remotely
- Push Local Branch and Track it
For even more articles on the subject, check out all of our Git articles.