How to get started with Git¶
Git is a free and open-source distributed version control system (VCS) designed for programmers to collaborate together on projects. Understanding Git is essential for anyone interested in contributing to open-source, and this tutorial will help you get started.
Do not confuse Git with GitHub or GitLab
Git is a version control system, while GitHub and GitLab are web-based Git repository hosting services. They are used to host Git repositories, and provide additional features like issue tracking, pull requests, etc.
More about Git¶
Git was originally authored by Linus Torvalds in 2005 for the development of the Linux kernel. It is used by millions of developers worldwide, and is the most popular VCS in the world. It is performant and feature-rich, and it is extensively used by almost all major tech companies.
Random Git trivia
Here's what Linus had to say about its name:
I'm an egotistical bastard, and I name all my projects after myself. First 'Linux', now 'Git'.
Git is a British slang for a silly, incompetent, stupid or annoying person.1 According to him, Git can mean anything according to your mood:2
- Random three-letter combination that is pronounceable, and not actually used by any common UNIX command. The fact that it is a mispronunciation of "get" may or may not be relevant.
- Stupid. Contemptible and despicable. Simple. Take your pick from the dictionary of slang.
- "Global information tracker": you're in a good mood, and it actually works for you. Angels sing and light suddenly fills the room.
- "Goddamn idiotic truckload of sh*t": when it breaks.
Installing Git¶
Git usually comes installed by default on most Mac and Linux machines. If your machine does not come with Git, grab an appropriate download from the official website.
If Git is already installed, you can skip this section.
Windows¶
By default, Git does not come with Windows. If you have winget, run the following command to install Git:
Verify installation¶
To check if Git is installed on your machine, run git version
. If Git is installed and configured correctly, the command will output the version of Git.
Getting started¶
Basic Git terminology
- Repository: Collection of files and folders that are tracked by Git. Also called repo for short.
- Branch: A branch is a version of the repository that diverges from the main repository.
- Commit: A snapshot of the changes made to a repository at a specific point in time.
- Merge: The process of combining two branches together.
- Pull: The process of fetching and merging changes from a remote repository (like GitHub) to a local repository.
- Push: The process of sending local commits to a remote repository.
The first thing to do after installing Git is setting up your identity. Your username and email will be used to identify your commits. Use the following commands to set it up:
git config --global user.name "your-username"
git config --global user.email "[email protected]"
Replace your-username
and [email protected]
with your username and email respectively.
Use an appropriate username and email
Since this information will be publicly visible in your commits, use an appropriate username and email unless you're developing locally. If you use GitHub, your GitHub username and email should be used. It is recommended to hide your real email from your commits in GitHub.
Global vs local configuration
You can use the --global
flag to set your identity globally, or you can omit it to set your identity for the current repository only.
Creating a repository¶
Let's start by creating a Git repository. Create a folder for your project, and run the following command inside it:
This will create a .git
folder inside your project folder. This folder contains all the information about your repository, including the history of all the changes made to your files. Congratulations, you have created your own Git repository!
Deleting the repository
If you want to delete your Git repository, simply delete the .git
folder. This will delete all the history of your repository, and you will not be able to recover it.
Adding files¶
Let's add some files to our repository. Create a file called README.md
.
What is README.md
?
README.md
is a markdown file that contains information about your project. It is usually present in most repositories, and contains information like how to install and use the project, etc.
Markdown is a lightweight markup language which is used to format text. It is human readable and is easy to learn. Markdown files end with .md
. Fun fact, this blog is written in Markdown!
Want to get started with markdown in under 10 minutes? Check out my tutorial on markdown!
Now that the file is created, let's add this file to our repository. Run the following command:
This will add README.md
to a temporary staging area which Git calls the index. The index is used to store all the files that you want to commit (save) to the repository.
Staging multiple files
You can stage multiple files by running git add
multiple times, or by adding a list of files separated by spaces.
or you can add all the files in the current directory by running:
Why do we need to stage files?
Staging files allows you to selectively commit only the files that you want to. This is useful when you want to commit only a few files, and not all the files in the repository.
Whenever we complete a task, we stage them to the index. Staged files are ready to be committed to the repository. Let's commit our changes to the repository.
Committing files¶
Our files are now waiting to be committed (saved) to the repository. Run the following command to commit the staged files:
You should add an appropriate commit message to indicate the changes in the commit.
Commit messages are important
Commit messages help you and others understand the changes made in the commit. A good commit message should be short and descriptive. It should be in the imperative mood, and should not end with a period. For example, a good commit message would be Add README.md
or Fix typo in README.md
instead of Added README.md
or fixed typo in README.md.
.
Git has taken a snapshot of the staged changes in the repository. The commit message, along with the author and timestamp, is stored with the commit. Next, let’s learn how to view the commit history.
Viewing commit history¶
To view the commit history of your repository, run the following command:
Use the --oneline
flag to view each commit in a single line:
Use Q to exit.
To push your local commits to a remote repository
To push your local repository to a remote (like GitHub), run the following command:
You will be prompted to enter your username and password. After entering them, your local repository will be synced with the remote repository.
There you go¶
You have successfully created your first Git repository, and have learned the bare basics of Git. This should help you get started with Git. I will post more tutorials on Git in the future, like working with branches, merging, etc. Stay tuned!
This is my first tutorial post. Please leave your feedback on the comments below. Thank you!
-
According to Wiktionary. ↩
-
Linus himself wrote this in the initial README file for Git. ↩
Comments
Comments are disabled until I find a compatible comment system.