Initializing and Cloning Repositories Using Git
Learn Git commands used to initialize and clone git repositories
Published on - by Jacob KyaloIntroduction
This article provides an overview of how to initialize a repository under Git version control. It will walk you through initializing a Git repository for a new or existing project. It will also guide you in cloning existing remote repositories from Github. This guide assumes a basic familiarity with a command-line interface like Git Bash.
If you have a project directory that is currently not under version control and you want to start controlling it with Git, you first need to go to that project’s directory and initialize Git in that project. Furthermore if you have a git repository hosted remotely or you want to collaborate on a project hosted remotely on Github, you have to clone it to your local machine as discussed below.
Initializing a Git repository
To initialize a Git repository in an existing or a new project, run the following command in Git Bash
git init
This command will create a .git directory with all Git configurations (folders and files) that will help you in version-controlling your project files. You don't need to worry about these Git folders and files for now.
After initializing a Git repository you can now start adding files to the staging area using git command, git add and also committing changes to the local repository using git command, git commit.
Cloning an existing repository
When a project is hosted in a central or remote repository, the git clone command can be used by other users to obtain a local development copy of repository. This helps get a local working copy.
git clone command creates a local copy or clone of remote repositories. To clone a remote repository you pass a remote URL(Universal Resource Locator) to git clone command.For example
git clone https://github.com/Jacobkyalo/my-blog.git
You can perform a shallow clone of the remote repository while eliminating extensive commit history by adding -depth=1 flag to the git clone command as:
git clone -depth=1 https://github.com/Jacobkyalo/my-blog.git
You can also clone a specific branch without cloning the remote HEAD branch as follows
git clone --mybranch https://github.com/Jacobkyalo/my-blog.git
Conclusion
That's it, you can now initialize Git and clone remote repositories. The above git clone commands are the most used ones but there are other git clone commands in the official Git documentation.
Happy version-controlling!