# Playing with git and GitHub

# Generate a token from GitHub remote repository via --&gt;

1.  Check the value of the origin variable which is the URL of my GitHub repo

**git remote -v**

*origin*[*https://github.com/Titir250/devops-journey.git*](https://github.com/Titir250/devops-journey.git)*(fetch)*

*origin*[*https://github.com/Titir250/devops-journey.git*](https://github.com/Titir250/devops-journey.git)*(push)*

Copy the token from remote GitHub repository

***Click on settings***

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708532818320/6de8b820-1a70-4b08-9ecd-4a44febd4178.png align="center")

***Click on Developer settings***

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708532846300/f1bbaece-5725-4e94-a853-dd650740e61d.png align="center")

***Click on tokens(classic) preferably and then Generate a new token. In the token settings fill up desired access, expiry date and once the token is generated copy it and save it.***

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708532949338/69cf3516-f379-4b42-bb6c-902e710d1ad6.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708532989211/fffe4daa-ef6a-4d79-8168-3cec711031f6.png align="center")

2. Now change the origin URL value with the new token generated by GitHub.Add the token value then @github……..

**git remote -v set-url origin***https://ghp\_o3mw5z9YAmNNvLQr980VaqZ0gx6i0W0cDcRi@github.com/Titir250/devops-journey.git*

3.Now the value is changed so when we do push it will hit the new URL with the token

**git remote -v**

*origin*[*https://ghpo3mw5z9YAmNNvLQr980VaqZ0gx6i0W0cDcRi@github.com/Titir250/devops-journey.git*](https://ghpo3mw5z9YAmNNvLQr980VaqZ0gx6i0W0cDcRi@github.com/Titir250/devops-journey.git)*(fetch)*

*origin*[*https://ghpo3mw5z9YAmNNvLQr980VaqZ0gx6i0W0cDcRi@github.com/Titir250/devops-journey.git*](https://ghpo3mw5z9YAmNNvLQr980VaqZ0gx6i0W0cDcRi@github.com/Titir250/devops-journey.git)*(push*)

4.Now the new changes in  the local is pushed to the remote repository using git push command

**git push origin main**

*Enumerating objects: 4, done.*

*Counting objects: 100% (4/4), done.*

*Delta compression using up to 8 threads*

*Compressing objects: 100% (2/2), done.*

*Writing objects: 100% (3/3), 314 bytes | 314.00 KiB/s, done.*

*Total 3 (delta 1), reused 0 (delta 0), pack-reused 0*

*remote: Resolving deltas: 100% (1/1), completed with 1 local object.*

*To*[*https://github.com/Titir250/devops-journey.git*](https://github.com/Titir250/devops-journey.git)

*90f6c96..16f01ff  main -&gt; main*

*clone*

## Clone a GitHub remote repository using https protocol  --&gt;

1. Open GitHub remote repository and copy the path under the clone section below https.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708533061534/f76c1e38-4fcc-4ea8-9062-6579b0f3ef47.png align="center")

1. Go to your local file system and run git clone command.
    

**git clone**[**https://github.com/Titir250/glearning-git-session.git**](https://github.com/Titir250/glearning-git-session.git)

*Cloning into 'glearning-git-session'...*

*remote: Enumerating objects: 13, done.*

*remote: Counting objects: 100% (13/13), done.*

*remote: Compressing objects: 100% (8/8), done.*

*remote: Total 13 (delta 1), reused 11 (delta 0), pack-reused 0*

*Receiving objects: 100% (13/13), done.*

*Resolving deltas: 100% (1/1), done.*

**ls**

*devops-journey  glearning-git-session*

### Clone a GitHub remote repository using ssh  key pair--&gt;

1. GitHub remote repository and copy the path under the clone section below ssh.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708533166485/441f920b-2722-440d-b879-0b812729c020.png align="center")

1. Try to clone using git clone shows an error permission denied because there is no ssh key generated in my local system which is not known by github.
    

**git clone**[**git@github.com**](mailto:git@github.com)**:Titir250/devops-journey.git**

*Cloning into 'devops-journey'...*

*The authenticity of host '*[*github.com*](http://github.com)*(20.207.73.82)' can't be established.ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.*

*This key is not known by any other names*

*Are you sure you want to continue connecting (yes/no/\[fingerprint\])? yes*

*Warning: Permanently added '*[*github.com*](http://github.com)*' (ED25519) to the list of known hosts.*

[*git@github.com*](mailto:git@github.com)*: Permission denied (publickey).*

***fatal: Could not read from remote repository.***

*Please make sure you have the correct access rights*

*and the repository exists.*

1. Generate a pair of  ssh key using the ssh-keygen method and copy the public key from there.
    

**I.   Go to the *./ssh* folder to generate the new ssh key pair.**

**cd .ssh**

**ssh-keygen**

*Generating public/private rsa key pair.*

*Enter file in which to save the key (/home/titir/.ssh/id\_rsa): github\_key*

*Enter passphrase (empty for no passphrase):*

*Enter same passphrase again:*

*Your identification has been saved in github\_key*

*Your public key has been saved in github\_*[*key.pub*](http://key.pub)

*The key fingerprint is:*

*SHA256:GJyi/g6Hy+pnVNFKW6lGKflCj++uhV+bk/v7JPVNznY titir@Titir*

*The key's randomart image is:*

*+---\[RSA 3072\]----+*

*|    . o .        |*

*|   + \* =         |*

*|  . O X          |*

*|   + X o         |*

*|  . = . S  .   . |*

*| . o..    . . =  |*

*|  =.o. ... . . =E|*

*| . Bo..oo o   . .|*

*|oo=o=o ++oo.     |*

*+----\[SHA256\]-----+*

**Now 2 keys *github\_key*and*github\_***[***key.pub***](http://key.pub)**private key and public key respectivelyare generated.**

ls

github\_key   github\_[key.pub](http://key.pub)  known\_hosts

**II.   Copy the public key *github\_***[***key.pub***](http://key.pub)***.***

cat github\_[key.pub](http://key.pub)

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCmbBArUi0801QW71DbF1PM1PDEjaG5oBTv83XHDt7gSLnmkoiddgs9AC3FSWfMNxaTVprZBpTjNHc66kDdKAEhoQpjMGkQZhrc9+MEibUXBOsoaOp2tPgEO4wk636rsW1t/3yJv7cGWvJJcdDG8vO+jT6jVNW+GR9gs3VU51zRAGiYu9T7sead4Ax5ZVyE3lD4JA2jWtDfJrQp/B3mHT2A0zu8aDNEhvw77CLPIKjP5SZ08wSS7wG7kYb/u2ce+fMS1kzKv29mWsV9+mruLuuhyK2n8sErLAa253+u8BSnPrzGrH5Ebvy6bhl6o20Gs1M4IDaVUusH5RbQhWRLg8Xoa3TwCmLNQFoEkG8Zx0n3xHkpTT6bba6Pk5V/GwssUaVYk/+8jrUyLQJNaWBrlpKvbaiWlwp+WQRL5qZdr/pL2DJgxfHFHCua5SBre+A9z2tCgP3KcG5RZ0pbExcZY3DP/7pRDQ9KyqiPxStgM79Clxo0Xotesj+zVWRi8jxIi80= titir@Titir

4. Go to GitHub remote repository and paste the public key value there.

***Go to GitHub settings and the SSH and GPG keys and click on new ssh key and save the generated public key there.***

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1708533236911/840eb4fa-3566-4e27-8c84-add2ce7f349b.png align="center")

5.  Now go to your local git repository and run the below command to successfully clone the remote git repository.

**GIT\_SSH\_COMMAND="ssh -i /home/titir/.ssh/github\_key" git clone**[**git@github.com**](mailto:git@github.com)**:Titir250/devops-journey.git**

*Cloning into 'devops-journey'...*

*remote: Enumerating objects: 12, done.*

*remote: Counting objects: 100% (12/12), done.*

*remote: Compressing objects: 100% (6/6), done.*

*Receiving objects: 100% (12/12), done.*

*remote: Total 12 (delta 2), reused 4 (delta 1), pack-reused 0*

*Resolving deltas: 100% (2/2), done.*
