---
# System prepended metadata

title: 5. VCS using Git and GitHub

---

# 5. VCS using Git and GitHub

:::warning
[toc]
:::

### Setting up the secondary Lab environment
#### Working with DigitalOcean and Termius
:::warning
- Create an account on Digital Ocean
````
Digital Ocean Referrel Link --> https://m.do.co/c/7a4461a50be1
Termius (Optional) --> https://termius.com/
````
- Create a Digital Ocean Droplet
    - OS --> Ubuntu
    - Region --> Choose your nearest one
    - Create a Keypair as explained or use password based authentication
- Configure a host on Termius to access the Droplet you created in the previous step.
- Validation
    - You should be able to access the droplet via ssh using termius

**SSH command to connect**

Syntax:
ssh -i <path-to-private-key> username@ip-address

Example:
ssh -i id_rsa root@159.65.28.147
:::

### Agenda
:::info
- Introduction
- Installation and Configuration
- Basic Git operations and commands
    - Clone
    - Config
    - init
    - push/pull
- GitHub
    - Working with GitHub
    - Fork in GitHub
- Branching and Merging
- Git remote
- Pull requests in GitHub
- Ignoring files in git
:::

### Why SCM / VCS? 
:::warning
- Versioning
- Investigation and Auditing (traceability)
- Collaboration
- Content Tracking
- Code review
- Distributed development (Branching)
- Ability to Rollback
- CI/CD integration (GitOps)
- Backup (Local and remote repositories)
:::

### SCM / VCS softwares
:::warning
- Git
- SVN
- Perforce
- CVS
- TFS
:::

:::warning
Linux Kernel --> RedHat, Debian, Suse, Ubuntu
Git --> GitHub, BitBucket, GitLab

- **Cloud Based**
    - AWS CodeCommit
    - Azure Repos

:::
### Types of VCS
:::warning
- Centralized --> SVN, CVS
    - Connected to the server
    - Slow because of the connectivity

![](https://hackmd.io/_uploads/Hy1fQHAV3.png)

- Distributed --> Git, Mercurial
    - Clone get you a full local copy so you can work offline
    - Speed
    - Simultaneous collaboration
![](https://hackmd.io/_uploads/rJNmmHC42.png)

:::
:arrow_right: *Source: https://git-scm.com/book/en/v2*

### Git Concepts
:::warning
- [x] Clone
- [x] commit
- [x] Push
- [x] Pull
- [x] Fork
- [x] Pull request
- [x] Branching
- [x] Merging
- [x] Remote
:::

### Git Commands
````yaml=

git init (initialize a new git repo)
git status
git add <filename>
git add . (or "git add *")
git commit
git commit -am "your commit message here"
git log
git log --oneline


**Establish Author details**

git config --global user.name "name"
git config --global user.email "email"
Validate:
git config --list


Cloning a repository:

Syntax:
git clone <url>

Example:
git clone https://github.com/sk12k/sl-demo-repo.git

Branching and Mergin commands:

git branch --> list all branches
git branch <newbranchname> --> create a new branch

git checkout <branchname> --> switch to the specified branch
or
git switch <branchname> --> switch to the specified branch

git checkout -b <newbrname> --> create a new branch and switch (in a single action)

Merging a branch:
git merge <currentmain> scf
````

### Class activity (create a local git repo)
:::warning
- Create an empty directory and cd into it
- Initialize a git repository inside it (using git init)
- Create some sample files (using touch or echo command)
- Check repo status (git status)
- Track all files (git add .)
- Setup Author details (git config)
- Commit changes (git commit)
- View your changes (git log)
:::

![](https://hackmd.io/_uploads/HyFf4GlUn.png)

### Class activity (working with GitHub)
:::warning
- Create an account on GitHub (if you don't have one already)
- Login to your GitHub account and create a new public repository
- Make some changes on the browser to your files
- upload some files from your local machine or create on GitHub browser
- commit all the changes
- Clone the repository to your Lab machine
- Verify the Changes
:::

### Class activity --> Git Push on your own GitHub repo

:::warning
**Part 1**
- Make sure you hav a Github account
- Login to your github account in a browser
- Configure a token for logging in from CLI to perform "git push"
- For token go to Settings --> Developer Settings --> Personal Access Tokens --> Tokens (Classic)
- Create a token and save it locally on your machine from where you can retrieve it when needed.

**Part 2**
- Go to local git cli
- Do a git clone in order to have the repo locally on your system
- Go inside the git repo
- Do a git pull
- git config credential.helper store
- Do a git push
    - Provide your Github user name and Token when prompted for password.
- git push should be successful.
- Validate changes on your GitHub repository on a browser.
:::

#### **Git Credentials helper**
:::info
If you want git to store your password/token so that you don't have to put it repeatedly, use the following commands:

$ git config credential.helper store
$ git push https://github.com/repo.git

Username for 'https://github.com': <USERNAME>
Password for 'https://USERNAME@github.com': <PASSWORD>
:::

### Git Fork and Pull request

![](https://hackmd.io/_uploads/rkCKpbcI3.png)
    

### Branching and Merging in Git
````yaml=
git branch --> list all branches
git branch <newbranchname> --> create a new branch
git checkout <branchname> --> switch to the specified branch
or
git switch <branchname> --> switch to the specified branch

git checkout -b <newbrname> --> create a new branch and switch (in a single action)

````
### Git remote
````yaml=
git push <remotename> <branchname>
git push --> git push origin master/main 

git pull <remotename> <branchname>
git pull --> git pull origin master/main

Git remote commands:
git remote --> list all remotes for current repo
git remote -v --> list all remotes with URL

Syntax:
git remote add <remotename> <URL>

Example:
git remote add sldocsrm 
````

### Additional Git concepts
:::warning
- git tag
- git rebase
- git stash
- git cherry-pick
- git revert/reset
- gitignore
:::

### Assignments
:::warning
- Create a GitHub account, if you don't have one already (https://github.com/)
- Go through the Slidedeck for Chapter 4
- Go through the second chapter (**2. Git Basics**) of Official Git Book (https://git-scm.com/book/en/v2/)
:::
**13/14May**
:::warning
- Lesson 4 demo 2,3,4,5,6,7
- Go through the slide deck for Module 4
- Complete the following chapters from Official Git Book
    - Chapter 1 --> Getting Started
    - Chapter 2 --> Git Basics
    - Chapter 3 --> Git Branching
:::


### References
:::warning
**Git references**
- https://linuxize.com/post/how-to-setup-passwordless-ssh-login/
- https://www.tecmint.com/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps/
- https://www.ssh.com/academy/ssh/keygen
- https://git-scm.com/
- [Official Git book](https://git-scm.com/book/en/v2/)
- [Git Hub Documentation](https://docs.github.com/en)
- [Learn GitLab with tutorials](https://docs.gitlab.com/ee/tutorials/)
- [Learn Git using Bitbucket](https://www.atlassian.com/git/tutorials)
- [Git Atlassian Tutorials](https://www.atlassian.com/git/tutorials/what-is-version-control)
- https://nvie.com/posts/a-successful-git-branching-model/

**Interactive Git**
- https://www.w3schools.com/git/exercise.asp
- https://learngitbranching.js.org/
- https://learn.gitkraken.com/collections
:::

