W3docs

Git Repository

See the definition of Git repository, learn how to initialize & clone it with git init & git clone commands, save changes to the repository & push them.

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

Git Init for Initializing a New Repository

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

git init

git init

Consequently, a new .git subdirectory will be created:

git init directory

git init <directory>

This creates an empty Git Repository in the specified directory. The default branch (usually main or master) is created automatically upon the first commit.

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 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"
  • run git status to verify the new file is untracked
  • git add GitCommit.txt to the repository staging area
  • create a new commit message describing the work done

git add and git commit

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 Remote Add for Connecting to a Remote Repository

A remote repository is added to your local project with the git remote command:

git remote add

git remote add origin <remote_repo_url>

You can verify the remote setup with git remote -v before pushing.

Git Push for Repository Interaction

If you used git clone, a remote repository is already configured, so you can run git push to push your changes. If you used git init, you first need to add a remote repository (see above). You can use a hosted Git service like GitHub or Bitbucket, create a repository there, and use the provided URL to connect your local project.

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

git push

git push -u origin main

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.

Git Config for Configuration and Setup

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. These settings apply to all repositories for that user.

git repository, git config global user.name

git config --global user.name <name>

Adding the --local option or not using a configuration option at all will define the setting for the current local repository. Sets the author email for all commits by the current user.

git repository, git config local user.email

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 editor

git config --system core.editor <editor>

Practice

Practice

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