Getting and Creating Projects
git init
Description
It is used to create a new Git repository. It sets up all the necessary files and directories that Git needs to track changes to a project.
Usage
git init [<directory>]If
<directory>is specified, Git will create a new repository in the specified directory.If no directory is specified, it initializes the repository in the current directory.
What It Does
Creates a
.gitDirectory: This directory contains all the repository metadata and configuration files necessary for Git to function, including subdirectories for objects, refs, and template files.Initializes a New Repository: If you run
git initin an existing directory, it will convert that directory into a Git repository without altering any existing files.Sets Up Initial Branch: It sets up the default branch (commonly named
mainormaster)
Common Scenarios
Starting a New Project
This creates a new directory called new-project, changes into that directory, and initializes an empty Git repository.
mkdir new-project
cd new-project
git initInitializing an Existing Directory
This initializes a Git repository in the existing existing-project directory without changing any existing files.
cd existing-project
git initSpecifying a Directory
This creates a new directory called my-repo and initializes an empty Git repository inside it.
git init my-repoPost-Initialization Steps
After initializing a Git repository with git init, we typically perform the following steps:
Adding Files:
This stages all the files in the current directory, preparing them to be committed.
git add .Making the First Commit:
This creates the first commit, which includes all the files added in the previous step.
git commit -m "Initial commit"Setting Up a Remote Repository (Optional):
If we want to link local repository to a remote repository (e.g., on GitHub, GitLab, or Bitbucket), we would add a remote repository URL.
git remote add origin https://github.com/yourusername/your-repo.gitThen push the local commits to the remote repository. Replace main with master if that is the default branch name in the setup.
git push -u origin maingit clone
Description
The git clone command is used to create a copy of an existing Git repository. This is one of the most commonly used commands in Git, allowing to obtain a working copy of a repository that is hosted remotely or locally.
Usage
git clone [<options>] <repository> [<directory>]<repository>: The URL or path of the repository to be cloned. This can be a remote repository URL or a local directory path.[<directory>]: The name of the new directory to be created for the cloned repository. If omitted, Git will create a new directory named after the repository.
Options
-lor--local: When the repository to clone is on the local machine, this flag will perform a local clone by making hard links, not actual file copies, when possible.--no-hardlinks: Clone without using hard links (default behavior when cloning from a local repository).--sharedor-s: Set up a shared clone with theobjectsdirectory being shared with the source repository.-qor--quiet: Suppress output during cloning.-vor--verbose: Show detailed output during cloning.--depth <depth>: Create a shallow clone with a history truncated to the specified number of commits.--branch <name>or-b <name>: Clone a specific branch.--single-branch: Clone only the history leading to the tip of a single branch, rather than all branches.--recurse-submodules: Initialize all submodules within the clone.--mirror: Clone a repository as a mirror, including all refs.
Basic Examples
Clone a Remote Repository:
git clone https://github.com/username/repository.gitThis creates a directory named repository and clones the repository into that directory.
Clone to a Specific Directory:
git clone https://github.com/username/repository.git myprojectThis clones the repository into a directory named myproject
Clone a Specific Branch:
git clone -b develop https://github.com/username/repository.gitThis clones only the develop branch of the repository.
Create a Shallow Clone:
git clone --depth 1 https://github.com/username/repository.gitThis creates a shallow clone with only the latest commit history.
Clone with Submodules:
git clone --recurse-submodules https://github.com/username/repository.gitThis clones the repository and initializes and clones any submodules within it.
Last updated