Git Repository

What is a Git Repository?

A Git Repository is a storage of your project files, which makes it possible to save code versions and have access to them.

GitRepository

Watch a course Git & GitHub - The Practical Guide

Git Init for Initializing a new Repository

First of all, you need git init command to create a new repository. This command is only used once, while initialising a new repository.

git init

Consequently, a new subdirectory and a new master branch will be created:

git init <directory>

This creates an empty Git Repository in the specified directory. Running this command will create a new folder which contains the .git subdirectory.

Git clone for Cloning an Existing Repository

The git clone command is used for creating a local clone of an already existing repository. It is a one-time operation, too.

git clone <repo url>

Git add and Git Commit for Saving Changes to the Repository

Using git add and git commit commands, you can save the file version changes to your repository. Here is an example of the usage of these two commands:

  • change directories to /path/of/project
  • create a new file GitCommit.txt with contents "commit example for git repo"
  • git add GitCommit.txt to the repository staging area
  • create a new commit message describing the work done
cd /path/of/project
echo "commit example for git repo" >> GitCommit.txt
git add GitCommit.txt
git commit -m "added GitCommit.txt to the repo"

Git Push for Repo-to-Repo Interaction

If you executed git clone command, it means that you already have a remote repository, so you can run git push command to push your changes to that repository. But if you used git init, you have no remote repository. In this case, you can use a hosted Git service, like Github or Bitbucket, and create your repo there, which will give a URL that you can add to your local repository and git push to the hosted repository.

Git Config for Configuration and Set up

A remote repository is added to the local git config with the help of git remote command:

git remote add <remote_name> <remote_repo_url>

After adding the remote repo, you can push local branches to it:

git push -u <remote_name> <local_branch_name>

When you use the -u option, Git not only pushes your changes to the remote repository but also sets the remote branch as the default for future git pull and git push commands without specifying the remote branch explicitly.

You may also need to set global Git configuration options such as username, or email. With the help of the git config command, you can configure your Git installation from the command line. This command defines everything (user info, preferences, the behavior of a repository). Below you will find some configuration options.

Use the --global flag to set configuration options for the current user. Define the name of the author of all the commits in the current repository.

git config --global user.name <name>

Adding the --local option or not using a configuration option at all, will define the user.name for the current local repository. Define the e-mail of the author of all the commits by the current user.

git config --local user.email <email>

Use the --system option to set the configuration for the whole system, that is to say all users and repos on a machine.

git config --system core.editor <editor>

Practice Your Knowledge

What are the key aspects and operations related to a Git repository?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?