Setting up a repository


From now on, when you think of starting a new project, you should think about creating a Git repository by using 'git init'. You're starting to write a new book; you're writing code, you're designing on photoshop... 'git init' will be your first step.


What is a Git repository?

A Git repository is a virtual storage of your project. It contains a collection of files, a history of changes, and any special configuration.

The main goal of the Git repository is to track all modifications made to files over time.


Initializing a repository from scratch: git init

You are thinking about starting a new project, and with your good reflex, you thought to use Git for tracking different versions of your files.

To initiate a new Git repository, you will use git init. You can follow the steps below :

  • Create a new folder to contain your project

    $ mkdir my_new_project
    
  • Go to the new folder

    $ cd /path/to/your/new/repository/my_new_project
    
  • Type git init, which will create a new subfolder named .git containing all your necessary repository files.

    $  git init
    
  • Write some content

    $ echo " some random content for git tutorial" >> git_tutorial_test.txt 
    
  • Tell Git to track your new file using the git add command (see the git add for more details)

    $ git add git_tutorial_test.txt 
    
  • Commit your new file with a nice message using git commit command (see the git commit page for more details)

    $ git commit -m "Added git_tutorial_test.txt to the repository"
    

Note : for more details for git commands: add and commit you can visit git add and git commit pages where both commands are covered more in depth.


Initializing a repository in an existing directory: git init

If you have an existing project directory, and you want to start tracking it with Git.

  • First, you need to go to the project's folder.

    $ cd /path/to/your/existing/repository/my_old_project
    
  • And type git init to initialize the repository

    $ git init
    
  • If you want to start version-controlling existing files, you should probably be adding those files using the git add command

    $ git add *
    
  • And finally committing your files using the git commit command

    $ git commit -m 'Initial commit'
    

Connect a local repository to a central repository

You created a local git repository. You can use your repository locally, but the most common is to push your repository to a central repository (Github in this example).

It's a good reflex to use a central repository because it's safer. Hence, if your local repository crashed or lost access to your local machine, you can easily clone a new repository from your remote one.

The central repository gives you the possibility to share your project and collaborate with others.

To push your local repository, you can follow the steps below:

  • Connect to your GitHub account.

  • On the main page, click the new button on the top of the page.

  • Type a repository name 'my_new_project'.

  • Choose the visibility of the remote repository. You can make it Public accessible by anyone or Private.

  • Click the create repository button.

  • Navigate to your local repository using your command line

  • Add the remote repository to your local git configuration by typing:

    $ git remote add origin https://github.com/username/my_new_project
    
  • Pushing the local repository to GitHub

    $ git push -u origin master 
    

Cloning an existing repository: git clone

When you create a repository on a central repository, it exists as a remote repository. To create a local copy on your computer, Git provides the git clone command. Like git init, cloning is generally a one-time operation.

In the steps bellow, you will clone a repository from a central repository (GitHub)

  • On GitHub, navigate to the main page of the repository.

  • Click on the Code button

  • Select HTTPS to clone the repository using the protocol HTTPS, and click on the copy button !!!!!!

  • Open the terminal (or git bash).

  • Change to the location where you want the cloned directory.

  • Type git clone, and paste the URL you already copied.

    $ git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
    


ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.