git clone command
In Git, we copy a repo by cloning it using the git clone command. We can clone a repo no matter where it's stored (local or remote), if we have a URL or path to point it to. A single repository can be cloned any number of times.
Syntax • git clone <repo-path>
The git clone command takes a URL or path to an existing repository as a parameter.
• git clone github-repo-link directory-name
It cloned the repository in the directory name specified and make it a git repository. When the directory name contains space, it should be put in double quotes.
Bash
![Git clone command Git clone Command](media/git-clone-01.webp)
In the following image we can see what happens when we clone the github repository.
![Git clone command Git clone Command](media/git-clone-02.webp)
In the following image we can see what happens when we specify the directory name with the git clone command.
![Git clone command Git clone Command](media/git-clone-03.webp)
Important Points in cloning a remote repo: 1. Permissions? Anyone can clone a repository from Github, provided the repo is public. We do not need to be an owner or collaborator to clone the repo locally to our machine. We just need the URL from Github.
But pushing up our own changes to the Github repo we need permission to do that.
2. When we clone a repo, it also brings its git history.
3. When we clone a repository, Git creates a reference to the original repo called a "remote". Git uses the name "origin" to refer to the remote repo.
Remote Branches
Once we have cloned a repository, we have all the data and Git history for the project at that moment in time. However, that does not mean it's all in my workspace!
The github repo has a branch called development, but when I run git branch, I can see only main branch, but not the development branch.
![Git Clone Command Git clone Command](media/git-clone-04.webp)
We can use the git branch -r command to view the remote branches our local repository knows about. In the following image we can see we have two branches in the github repo.
![Git Clone Command Git clone Command](media/git-clone-05.webp)
In the following image we can see, the main branch at local is connected to the main branch at the remote, and it is by default connected.
![Git Clone Command Git clone Command](media/git-clone-06.webp)
To create the development branch locally and it will track the remote branch, we can use the git switch <remote-branch-name> command to create a new local branch from the remote branch of the same name.
So, git switch development makes a local development branch and sets it up to track the remote branch origin/development.
![Git Clone Command Git clone Command](media/git-clone-07.webp)
Now, the remote development branch is connected to the local development branch.
![Git Clone Command Git clone Command](media/git-clone-08.webp)
git clone -b
We can clone only a particular branch from a repository. For this we need to specify the branch name with -b option with the git clone command.
Syntax git clone -b <branch-name> <repository-URL>
See the below command:
Bash
In the given output, only the main branch is cloned from the given github repository in the folder named Demo folder.