Git is an open-source software used for tracking project changes and revisions across different teams. Git saves different versions of projects in a folder known as a Git repository.
In this tutorial, we will go over what a Git repository is, how to create one, and how to work with repositories using Git commands.
- A system running Linux, Windows, or macOS
- Git installed and configured (See our guides on Installing Git on Windows, Installing Git on Mac, How to Install Git on Ubuntu, and How to Install Git on CentOS)
- A Git project set up
What is a Git Repository?
A Git repository tracks and saves the history of all changes made to the files in a Git project. It saves this data in a directory called
.git, also known as the repository folder.
Git uses a version control system to track all changes made to the project and save them in the repository. Users can then delete or copy existing repositories or create new ones for ongoing projects.
Types of Git Repository
There are two types of Git repositories, based on user permissions:
Software development teams use bare repositories to share changes made by team members. Individual users aren't allowed to modify or create new versions of the repository.
With non-bare repositories, users can modify the existing repository and create new versions. By default, the cloning process creates a non-bare repository.
How to Get a Git Repository
There are two ways of obtaining a Git repository:
- Turning an existing directory into a Git repository (initializing).
- Cloning a Git repository from an existing project.
Initialize a Repository
To initialize a Git repository in an existing directory, start by using the Git Bash terminal window to go to your project's directory:
cd [directory path]
[directory path]: The path to your project directory.
If you are using Windows 10, the directory path might look like:
Once you navigate to the project directory, initialize a Git repository by using:
Initializing a repository creates a subdirectory called
.git that contains the files Git needs to start tracking the changes made to the project files. The repository only starts tracking project versions once you commit changes in Git the first time.
Clone a Repository
git clone command to clone an existing repository and copy it to your system:
git clone [url] [directory]
[url]: The URL of the Git repository you want to clone.
[directory]: The name of the directory you want to clone the repository into.
Note: If you don't provide the directory name, Git will copy the name of the original repository folder.
How to Work with a Repository
Git provides various commands to create different versions of a project:
To add an author name to all the commits in the current repository:
git config --global user.name "[your_name]"
To add an email address to all the commits by the current user:
git config --global user.email "[email_address]"
To create a shortcut (alias) for commonly used Git commands:
git config --global alias.[alias_name] [git_command]
To make it easier to track repository changes by enabling Git's automatic command line coloring:
git config --global color.ui auto
Note: If you omit the
--global option, the commands above will only apply to the current local repository. You can achieve the same effect by replacing the
--global option with
To set a default text editor for Git:
git config --system core.editor [text_editor]
To open the global Git configuration file:
git config --global --edit
Making changes to the Git repository is done by using
git add and
git commit commands. The
git add command adds files to the staging area, while the
git commit command applies the staged changes to the repository.
git add command uses the following syntax:
git add [file/directory name]
Note: Add multiple files or directories to the staging area by listing their names after the
git add command.
To add all the files in the current directory to the staging area:
git add --all
Git also lets you add all files in the current directory ending with the same extension. For instance, add all
.txt files to the staging area with:
git add *.txt
Note: You can also unstage files in Git before committing them to the project.
Once you've added all the necessary files to the staging area, commit the changes to the repository by using:
git commit -m "Committing files to the repository"
-m: Allows you to attach a message to the command, in this case "Committing files to the repository." Use messages to describe the changes being committed.
Git allows you to copy and share repositories with other developers by using the
git push and
git pull commands.
git push command lets you share all the commits and files in the current repository with one or more remote repositories. The
git push command uses the following syntax:
git push [remote repository] [branch name]
[remote repository]: The name of the remote repository you want to share your commits and files with.
[branch name]: The name of the branch of your local repository you want to share.
git pull command to copy the content of a remote repository and merge it with your local copy:
git pull [remote repository]
Note: Learn how to remove a Git remote from a repository once you no longer need it.
After reading this tutorial, you should have a Git repository of your project set up and ready to use. You should also have a basic understanding of how Git repositories work.
For more information on Git commands, download phoenixNAP's Git command cheat sheet.