Git software is a version control system (or VCS), a type of software designed to track changes among selected files. Some important actions provided by a VCS is to save, review, redo and merge file changes. Git can also create new copy of the project and maintain it as a separate version, a feature commonly known as branching. Today, Git is the go-to software for maintaining source codes, and various tools have been created in order to make working with Git easier. We will start by installing Git locally on our computer.
Git Setup
Installing Git is very straightforward for all platforms:
OSX
Mac usually comes with Git already installed. Try run git --version
from the Terminal, and if it’s not installed yet, A macOS Git installer is available here.
Windows
Download Git for Windows and run it. The configurations can be set to its default.
Linux
Simply use the package manager of your distro. e.g.
$ sudo apt-get install git
or
$ sudo yum install git
Initializing a Git repository
Now that Git is installed, you can start using it from the command line interface. Type git
and hit enter.
Now before we discuss any of the commands above, let’s talk about what is a repository first.
Repository — or repo for short, is a directory or folder that tracks every file that exist on its parent folder. Think of repo as a kind of database where all changes of your file is being tracked. In Git, a repo is generated by using the command git init
. A hidden folder named .git
will appear in the directory where you run the command.
Let’s created a file into our initialized repo
$ echo "Hello World" > README.txt
Now run git status
and you’ll see that an untracked file will appear.
Staging area — is where a file is being added, but not yet committed into Git repository.
In order to make Git track the README.txt file, run the command git add README.txt
If you are adding a wrong file, you can remove it from staging by running git reset README.txt
. For now, let’s commit this README.txt file
Commit is taking a snapshot of all files put into the staging area at the time the command is run. This will be a checkpoint and permanently save a record in Git. You need to add a short message describing the commit, or it will fail to run.
Run a commit by using git commit -m
$ git commit -m 'My first commit'
All files in the staging area will be committed, and git status
will show there is nothing to commit. We can run git log
to check on history of commits in the repo.
Congratulations on your first commit! Let’s take a short break and move on to explore branching