Setup a Git Repository via Command Line

Posted on

This is an article written about how to setup a Git Repository using the available command from git utility itself. The command is executed via command line. So, in order to setup a Git repository via command line, there are several steps which can be taken to accomplish the task and it is shown as follows :

1. The most important thing is to check whether ‘git’ utility has already been installed or not. For an example, to check it in Ubuntu Linux distributoin. just check the article titled ‘List Installed Program in Ubuntu Linux using apt command‘ in this link to perform on checking the ‘git’ utility whether it has already installed or not. Below is the actual execution for checking it :

root@hostname:~# apt --installed list | grep git

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

...
git/xenial,xenial,now 1:2.7.4-0ubuntu1 amd64 [installed,upgradable to: 1:2.7.4-0ubuntu1.1]
...

There are other tools or utilities provided or available in each operating system which can be used.

2. As shown in the first step, it is already installed. After checking it via apt command utility, it can also be checked by executing the ‘git’ utility itself by typing it in the bash prompt commad as shown below :

git --version

This is the example of the command execution above in a real situation :

user@hostname:~# git --version
git version 2.7.4
user@hostname:~#

Another method which can be used is just plainly type ‘git’ command as shown below :

user@hostname:~# git
usage: git [--version] [--help] [-C <path>] [-c name=value]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
clone Clone a repository into a new directory
init Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
add Add file contents to the index
mv Move or rename a file, a directory, or a symlink
reset Reset current HEAD to the specified state
rm Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
bisect Use binary search to find the commit that introduced a bug
grep Print lines matching a pattern
log Show commit logs
show Show various types of objects
status Show the working tree status

grow, mark and tweak your common history
branch List, create, or delete branches
checkout Switch branches or restore working tree files
commit Record changes to the repository
diff Show changes between commits, commit and working tree, etc
merge Join two or more development histories together
rebase Forward-port local commits to the updated upstream head
tag Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
fetch Download objects and refs from another repository
pull Fetch from and integrate with another repository or a local branch
push Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
user@hostname:~#

3. So, after carefully check that ‘git’ utility command can be executed, just type the following command to initialize a folder to be set as a Git repository.

git init .

For an example :

user@hostname:/var/www/html/test$ git init
Initialized empty Git repository in /var/www/html/test/.git/
user@hostname:/var/www/html/test$

The folder above named ‘test’ which is located in ‘/var/www/html/’ can be empty or it has already filled with files or directories of application source code. But in the above command execution, ‘git init’ means the initialization of the folder for a new and empty Git repository since it hasn’t been any git operation performed in the folder.

  1. The last but not least, check in the folder named ‘test’ whether it has a hidden folder located inside of it named ‘.git’ as shown below :
user@hostname:/var/www/html/test$ ls -al | grep .git
drwxr-xr-x 8 user user 4096 Jul 18 15:16 .git
-rwxrwx--- 1 www-data user 130 Jun 8 14:47 .gitattributes
-rwxrwx--- 1 www-data user 953 Jun 8 14:47 githash.php
You have new mail in /var/mail/root
user@hostname:/var/www/html/test$ ls -al | grep git
drwxrwxr-x 7 www-data user 4096 Jul 22 09:50 .git
user@hostname:/var/www/html/test$ 

4 thoughts on “Setup a Git Repository via Command Line

Leave a Reply