IntroductionGit 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 RepositoryThere are two types of Git repositories, based on user permissions:
Bare RepositoriesSoftware 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.
Non-Bare RepositoriesWith 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 RepositoryThere 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 RepositoryTo 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.
Once you navigate to the project directory, initialize a Git repository by using:
Initializing a repository creates a subdirectory called
.gitthat 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 RepositoryUse the
git clonecommand 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. You can also clone tags to avoid copying the entire repository.
How to Work with a RepositoryGit provides various commands to create different versions of a project:
Configuring RepositoriesTo add an author name to all the commits in the current repository:
To add an email address to all the commits by the current user:
git config --global user.name "[your_name]"
To create a shortcut (alias) for commonly used Git commands:
git config --global user.email "[email_address]"
To make it easier to track repository changes by enabling Git's automatic command line coloring:
git config --global alias.[alias_name] [git_command]
git config --global color.ui auto
To set a default text editor for Git:
Note: If you omit the
--globaloption, the commands above will only apply to the current local repository. You can achieve the same effect by replacing the
To open the global Git configuration file:
git config --system core.editor [text_editor]
git config --global --edit
Saving ChangesMaking changes to the Git repository is done by using
git commitcommands. The
git addcommand adds files to the staging area, while the
git commitcommand applies the staged changes to the repository.The
git addcommand uses the following syntax:
git add [file/directory name]
To add all the files in the current directory to the staging area:
Note: Add multiple files or directories to the staging area by listing their names after the
Git also lets you add all files in the current directory ending with the same extension. For instance, add all
git add --all
.txtfiles to the staging area with:
git add *.txt
Once you've added all the necessary files to the staging area, commit the changes to the repository by using:
Note: You can also unstage files in Git before committing them to the project.
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.
Enabling CollaborationGit allows you to copy and share repositories with other developers by using the
git pushcommand lets you share all the commits and files in the current repository with one or more remote repositories. The
git pushcommand 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 pullcommand 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.