How to Setup a New Git Repository

By How to

Git is a popular version control system for tracking changes in computer files and coordinating work on those files among multiple people. Every Git directory on every computer is a full-fledged repository with complete history and full version tracking abilities, independent of network access or a central server.

This article contains the very basic steps to set up a Git repository in your server.

Check if git is installed

Run the following commands to see if git is installed in your server:

[server]$ which git
/usr/bin/git
[server]$ git --version
git version 2.7.3

Create your identity

The following two commands create your username and email address to be used with Git.

[server]$ git config --global user.name "John Doe"
[server]$ git config --global user.email johndoe@example.com
Those commands create a file named .gitconfig in your server user’s directory with the following content:

[user]

name = John Doe
email = johndoe@example.com

With the following command you can check if your settings were saved properly:

[server]$ git config --list
user.name=John Doe
user.email=johndoe@example.com

Create a brand new Git repository

Navigate to the directory of your application. In the example below, the application is in the directory named /mygitapp.

Run the following command to initialize the new repository:

[server]$ git init
Initialized empty Git repository in /home/username/mygitapp/.git/

That command will create the /.git directory, used by Git to track all the changes of your project.

Assuming that your project was not empty, you can add all files to the Git repository with the following command:

[server]$ git add .

Run the status command to confirm which files are in the staging area:

[server]$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: file1.txt
# new file: file2.txt
# new file: file3.txt
# new file: subdirectory1/file4.txt
#

If you are sure you are ready to commit those files, run the following command:

[server]$ git commit -m "Enter custom comment about commit here"

The output of that command would be something like this:

[master (root-commit) 0bd2848] Enter custom comment about commit here
0 files changed
create mode 100644 file1.txt
create mode 100644 file2.txt
create mode 100644 file3.txt
create mode 100644 subdirectory1/file4.txt

This creates your first commit which records the snapshot you set up in your staging area.

I recommend you to follow these Git best practices and conventions to avoid code conflicts when you collaborate with other developers. It will also allow you to track changes more effectively and organize your code releases.

Finally, it is also recommended to use a service like Github or Bitbucket to keep your Git repository in a centralized place, so you don’t depend on your server availability to access your repository.

Tagged under: