How to set up a GIT repository locally

Sometimes all you need is to set up a local repository in your own machine. You may not require to use a remote machine. In that case, you can follow the following steps

The first step is to create a new user in your machine called git. You can use any other name of your choice. I have chosen the name git just for clarity in usage

$ sudo adduser git

Follow the steps like entering the password and other user details as and when prompted.

If your new user is successfully created, the next steps is to create the repository.
Go to the terminal and follow these steps
$ su git

Enter the password
$ cd ~

This step ensures that you are using the git(or the new userid you created)’s home directory.
Now create the repository and initialize it
$ mkdir MyProject
$ cd MyProject
$ git --bare init

Now you can logout from the git.
$ exit

Now go to the place where you want to work with your project.
$ mkdir MyProject
$ cd MyProject

Initialize git and add a new file

$ git init
$ touch README
$ git add README
$ git commit -m "Adding README file" README
[master (root-commit) f6f5b10] Adding README file
0 files changed
create mode 100644 README

Let’s try to push the code we have commited. Remember that we have not specified any remote host
$ git push
fatal: No configured push destination.
Either specify the URL from the command-line or configure a remote repository using

git remote add

and then push using the remote name

As you can see from the errors that remote host is missing
git push

The next step is to specify the remote host, which in our case is the localhost.

$git remote add origin git@localhost:MyProject
$git push origin master

That’s it, you can make any changes in your new project. Keep commiting and pushing the changes. Thus you can track your project locally using git, without using any remote server