# Git and GitHub

**<mark>GitHub is a web-based hosting service for version control using Git</mark>**.  
It is a central repository. It provides access control and several collaboration features such as bug tracking, feature requests, and task management.  
Public repository is free but maintaining a private repository is a paid service.  
Open-source developers created GitLab to make everything free. Bitbucket is also an alternative to GitHub.

### Connect to Central Repo (<mark>GitHub</mark>):-

First, you need to create your GitHub account and a repository in your account.

1. Go to the **GitHub** site \[[https://github.com](https://github.com)\] and create an account
    
2. Create a "**New Repository**"
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682193384689/27271a25-2603-4454-8a2a-d6704bc809da.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682193429720/a842985a-3f22-487d-8858-9cc4aa11c1b5.png align="center")
    
3. Login to your EC2 Instance and create your working directory  
    `mkdir new-git ; cd new-git ; git status ;`
    
4. Clone the Repository  
    `git clone` [`https://github.com/<Name>/firstrepo-public`](https://github.com/ketangharateG/firstrepo-public)`;`  
    `cd firstrepo-public`
    
5. Create a new file, add it to staging, and do the commit  
    `echo "New File..." > firstrepo.txt`  
    `git add firstrepo.txt`  
    `git commit -m "First Commit"`
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682193742980/e527dd06-884c-4a66-a349-2b079668fa4c.png align="center")
    
6. Check which remote repository is connected:  
    `git remote -v`
    
7. Generate a token to give permissions for the remote user for the GitHub Repo.  
    Else, you will be prompted for a username and password, will the pull and push requests.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682194514201/efdecb9d-2828-40f4-9be9-675cedd5cb28.png align="center")
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682194577261/8ac5b482-ccb4-4f8b-8de6-d01d572e6408.png align="center")

Login to your **<mark>Github</mark>** <mark> &gt; </mark> **<mark>Settings</mark>** <mark> &gt; </mark> **<mark>Developer Settings</mark>** <mark> &gt; </mark> **<mark>Personal access tokens</mark>** <mark> &gt; </mark> **<mark>Tokens(classic)</mark>** <mark> &gt; </mark> **<mark>Generate new token</mark>** <mark> &gt; </mark> **<mark>Generate new token(classic)</mark>** &gt; Assign a  
  
**Token Note** - choose the Expiration period and select what permissions you want to give to this particular token. -&gt; **Generate Token** -&gt; Copy the token

1. Use, the token and the Repo URL, that we created earlier
    
    `git remote set-url origin` [`https://ghp=L0i4HLonwhnKash2KzcnXGb2K3We1qy@github.com/kcG/firstrepo-public`](https://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@github.com/ketangharateG/firstrepo-public)  
      
    i.e  
    `https://<Personal-access-tokens-Key>@github.com/<Git-Username>/<Repo-URL>`  
    
    `https://ghp_49pQuYE7-PerSonap-Tokken-Key@github.com/ketangharateG/react_django_demo_app`  
    
2. Verify the origin will get changed  
    `git remote -v`
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682193706175/4e574364-471a-4e52-8f87-b975ef587d0e.png align="center")
    
3. Now, push your local repo to the central repo.  
    <mark>[</mark>**<mark>Your local repo data will be copied to the central repo</mark>**<mark>]</mark>
    
    `git push origin master`
    
4. Now, To pull from the remote repo.  
    <mark>[</mark>**<mark>Download from central repo to your local repo</mark>**<mark>]</mark>
    
    `git pull origin master`
    
    If pulling branches other than master, change the branch name.  
    Create a new file on the Central repo and try to pull them.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682194297723/c1bff768-d8e0-49ba-8007-44f6f0561f25.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682194475225/13b43366-b9cf-4f3d-80e2-f20f43416429.png align="center")
    

* **Collaborate between Local and Remote Repository using**  
    `git pull` and `git push`
    

Pull the commits from the Remote Repository  
`git pull origin master`  
`git push origin master`

**<mark>ORIGIN:</mark>** <mark>Git indicates that a remote repository can be added and identified with a keyword known as origin.</mark>

* Git enables moving files from the **Local Repository** to the **Remote Repository** using  
    `HTTPS`  
    OR  
    `SSH`
    
* **Create a Repository in GitHub using SSH**
    

1. Ensure the local Repois ready
    
2. Create SSH Key using (ssh-keygen)
    
3. Configure GitHub with SSH Public Key
    
4. Create a Github Repo
    
5. Push from Local Repo to GitHub
    

* **Create a Repository in GitHub using HTTPS**
    
    * Go to GitHub and login
        
    * Create a New Repository
        
    * Follow GitHub Instructions  
        `git remote -v`  
        `git remote add origin https://github.com/<Repo-Name>`  
        \- When you will create Repo you will be shown the cmd line instructions.  
        \- You can also copy the URL from GitHub &gt; Repo &gt; Code &lt;&gt;section  
        `git remote -v`  
        `git push origin master`
