In the previous exercise, we pushed some files to GitHub. Now, if a developer wants a copy of those files, they can get it by using git clone.
🌀 How git clone Works
The git clone command is used to create a complete, local copy of an existing Git repository located elsewhere (usually on a remote server like GitHub, GitLab, etc.).
It performs several automated steps to set up your local workspace for development:
1. Connects to the Remote Repository
The process begins by connecting to the remote repository using the provided URL (HTTP, HTTPS, or SSH).
Authentication: If using SSH, it attempts to authenticate using your local SSH key. If using HTTPS, it may prompt for your username and password or use a Personal Access Token (PAT).
Negotiation: Git checks the remote repository to see what data it needs to copy.
2. Copies All Repository Data
Git copies the entire project history, including every version of every file ever committed, to a new directory on your local machine.
Cloning the .git Directory: This is the most crucial step. Git transfers all the internal data objects (commits, trees, and blobs) from the remote's hidden .git directory into the new local directory. This ensures your local repository has the complete history of the remote.
3. Sets Up Tracking Information
Git automatically configures your local copy with the necessary settings to communicate with the original remote:
Adds the Remote: It defines the remote connection under the default name origin (using the URL you provided).
Sets the HEAD: It sets the special HEAD pointer to point to the remote's default branch (usually main).
4. Checks Out the Default Branch
Finally, Git checks out a working copy of the remote's default branch (e.g., main).
Working Directory: It takes the files from the latest commit on the default branch and places them into your working directory (the visible files you edit).
Tracking Branch: It sets up your local default branch (e.g., main) to automatically track the remote branch (origin/main), which allows you to use simple commands like git pull and git push later.
Generate ssh keys from the developer machine.
user@DESKTOP-0AVRJL4 MINGW64 ~
$ pwd
/c/Users/user
user@DESKTOP-0AVRJL4 MINGW64 ~
$ ssh-keygen -t ed25519 -C "mahesh.p86@outlook.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/c/Users/user/.ssh/id_ed25519):
Enter passphrase for "/c/Users/user/.ssh/id_ed25519" (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/user/.ssh/id_ed25519
Your public key has been saved in /c/Users/user/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:E95yB/rzjG2vwPIk/olULiKlvAA7owPaS7rZsn/lQP4 mahesh.p86@outlook.com
The key's randomart image is:
+--[ED25519 256]--+
| |
| |
| . . |
| . . + . |
|. o . S + . |
|.o .oo. O . |
|*.o +=. = B |
|=B...oE+ B O. |
|**+o. o.=.=o. |
+----[SHA256]-----+
user@DESKTOP-0AVRJL4 MINGW64 ~
$
Add Public key to github.
user@DESKTOP-0AVRJL4 MINGW64 ~/.ssh
$ ls -lrt
total 3
-rw-r--r-- 1 user 197121 92 Nov 2 10:06 known_hosts
-rw-r--r-- 1 user 197121 419 Nov 2 10:20 id_ed25519
-rw-r--r-- 1 user 197121 104 Nov 2 10:20 id_ed25519.pub
user@DESKTOP-0AVRJL4 MINGW64 ~/.ssh
$ cat id_ed25519.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIM0WpRSZB8tdQdePkkxTD7FcfQfummRABxYciLQhPdwh mahesh.p86@outlook.com
user@DESKTOP-0AVRJL4 MINGW64 ~/.ssh
$
$ ssh -T git@github.com
Hi mahekarthya! You've successfully authenticated, but GitHub does not provide shell access.
user@DESKTOP-0AVRJL4 MINGW64 ~/.ssh
$
Clone the repository.
$ git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt (main)
$
$ cd .git/
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt/.git (GIT_DIR!)
$ ls -lrt
total 9
-rw-r--r-- 1 user 197121 73 Nov 2 10:35 description
drwxr-xr-x 1 user 197121 0 Nov 2 10:35 hooks/
drwxr-xr-x 1 user 197121 0 Nov 2 10:35 info/
drwxr-xr-x 1 user 197121 0 Nov 2 10:35 objects/
-rw-r--r-- 1 user 197121 112 Nov 2 10:35 packed-refs
drwxr-xr-x 1 user 197121 0 Nov 2 10:35 refs/
-rw-r--r-- 1 user 197121 21 Nov 2 10:35 HEAD
drwxr-xr-x 1 user 197121 0 Nov 2 10:35 logs/
-rw-r--r-- 1 user 197121 305 Nov 2 10:35 config
-rw-r--r-- 1 user 197121 305 Nov 2 10:36 index
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt/.git (GIT_DIR!)
$ cat config
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[remote "origin"]
url = git@github.com:mahekarthya/devops-demo-pvt.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt/.git (GIT_DIR!)
$
I've added a new offer.html
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt (main)
$ ls -lrt
total 16
-rw-r--r-- 1 user 197121 3660 Nov 2 10:35 about.html
-rw-r--r-- 1 user 197121 3164 Nov 2 10:35 index.html
-rw-r--r-- 1 user 197121 2530 Nov 2 10:35 services.html
-rw-r--r-- 1 user 197121 1456 Nov 2 10:41 offer.html
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt (main)
To push this changes to github, we need to setup the OS with git config.
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt (main)
$ git config --global user.name
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt (main)
$ git config --global user.name "mahesh.p86"
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt (main)
$ git config --global user.name
mahesh.p86
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt (main)
$ git config --global user.email
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt (main)
$ git config --global user.email "mahesh.p86@outlook.com"
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt (main)
$ git config --global user.email
mahesh.p86@outlook.com
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt (main)
$
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
offer.html
nothing added to commit but untracked files present (use "git add" to track)
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt (main)
$ git add .
warning: in the working copy of 'offer.html', LF will be replaced by CRLF the next time Git touches it
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: offer.html
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt (main)
$
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt (main)
$ git commit -m "Added offer.html"
[main df81309] Added offer.html
1 file changed, 54 insertions(+)
create mode 100644 offer.html
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt (main)
$
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt (main)
$ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt (main)
$
Now push the changes to github
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt (main)
$ git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 829 bytes | 414.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:mahekarthya/devops-demo-pvt.git
08b4859..df81309 main -> main
user@DESKTOP-0AVRJL4 MINGW64 ~/Apache-repo-clone/devops-demo-pvt (main)
$
We could simply push without specifying origin or branch name , this is because:
When you clone a repo using: git clone <repo-url>
refs/heads/main is the internal full name of "Main" branch.
Let's verify the new file from github
No comments:
Post a Comment