Getting Started
Introduction
If you’re new to software development, you may be wondering how to effectively manage the code you write and collaborate on your projects with others. Git is a popular tool used in the world of programming and software development that helps you securely store your code, manage versions, collaborate with others, and track changes to your project over time. In this beginner’s guide, we’ll explore the features of Git and go over its basic features to get you started on managing the code for your next project.
What is Git?
Git is a powerful version control system. At its core, it stores your codebase and its historical changes in a location known as a repository. A repository will contain a collection of commits, which is a bundle changes to a codebase. A repository will also contain one or more branches, each of which contain a separate sequence of commits allowing multiple sets of changes to be developed simultaneously. Although Git repositories can be stored locally, most developers manage their repositories on a centralized system.
Installing Git
Before you can start using Git, you need to install it on your computer. Git is available for almost every major operating system, including Windows, macOS, and Linux. You can download the installation package for your operating system from the official Git website and follow the installation instructions.
After your install Git, you should be able to run the following command from your terminal to verify the installation succeeded:
git --version
Configuring Git
After you install Git, you’ll want to configure it with your name and email address so that you are associated with your changes. Open your terminal and enter the following commands, ensuring that you replace “Your Name” and “youremail@example.com” with your actual name and email address:
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
This information will be associated with each commit you make in Git, allowing other developers to identify your contributions to a codebase.
Creating Your First Git Repository
Now that you’ve successfully installed and configured Git, you’ll want to create your first Git repository. A Git repository (often shortened as “repo”) is a project that is tracked in Git. To create a new repository, navigate to the directory where you want to store your project and enter the following commands:
git init
You can also use this command on an existing project if you’d like Git to begin tracking it.
Viewing the Repository Status
As you begin development, you’ll want to compare what changes you are in the process of making with the latest committed changes (often called HEAD
).
To view the status of these changes, run the following command:
git status
Since you haven’t made any changes to your new repository, you’ll get a response that there are no commits yet. That’s okay, next we’ll work on creating our first commit.
Adding and Committing Changes
With your first repository in place, you are ready to make your first change to the project.
Create a new file hello.txt
and modify its contents with whatever you’d like (you can add “Hello, world!” if you’re unsure what to save).
Save the change when you are ready.
Now that there’s a change, you can check the status again (using git status
) and see the change.
You should get a response showing that Git recognized a change to a new file hello.txt
.
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.txt
nothing added to commit but untracked files present (use "git add" to track)
Now it’s time to commit your change so that its written to the history of the repository. If you try to commit the change now, you’ll see that nothing happens. That’s because the change is unstaged, which means it’s not marked to be included in a commit. You’ll have to stage your change in order for it to be eligible to commit. This Git feature allows you to commit the specific changes you want, rather than requiring you to commit everything.
At any time, you can view the exact changes you are making by running git diff
.
This command will show you a line-by-line breakdown of the changes that have been made. Run the following command to stage your changes:
git add hello.txt
Now, if you run git status
, you’ll see that the change is staged to be committed:
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: hello.txt
When you’re ready, commit the change bey running the following command:
git commit -m "Initial commit"
The -m
flag means that you are providing a commit message.
You should always include a meaningful commit message to help you and other developers understand the purpose of each change.
Viewing Your Git History
You can view the commit history of a repository any time by running the following command:
git log
This command displays a list of commits, including the commit message, author, date, and a unique commit hash. The commit hash is a unique identifier that allows you to identify the commit when running other commands. We’ll cover the commit hash more in a later tutorial.
Conclusion
This is just the beginning of your journey in managing your codebase in version control. Git offers many more features such as branching and merging. As you become more familiar with Git, you’ll discover ways for it to help you streamline your development workflow and manage your projects.
In future tutorials, we’ll cover more advanced Git concepts and techniques. Until then, practice the basics covered in this guide until you are comfortable with staging and committing changes in your local repository.
Happy coding!