Git & GitHub

Git & GitHub


Version Controlling

notion image

Git

  • Git is used to track versions of our code & was created by Linus Torvalds
  • As git is a software we need to have it installed on our system, Linux comes with it preinstalled
  • We can check for git installation using this command: git --version
notion image
They are only required once per system
They are only required once per system

Git Workflow & Commands

  • Some folders includes things like /node_modules & files like .env file that contains our credentials
  • There’s a special .gitignore file that we can use to untrack files & folders
  • A special README.md file contains general information of our project, which is shown differently in our GitHub repository
  • To track changes of any folder we first have to initialize it with git by running: git init
  • Then we need to add it to the staging index by running: git add .
  • And then we can commit our changes: git commit -m "Initial commit" (m for message)
  • Then we can push it to our remote repo by running: git push origin main
  • For this our remote should be connected by running: git remote add origin https://github.com/username/repo.git
  • To disconnect our remote repo for some reason we can do: git remote remove origin
  • To clone from a remote repo: git clone https://github.com/username/repo.git
notion image
notion image
notion image
notion image
notion image

Authentication

  • We have to authenticate our computer with GitHub if we want to perform push operation to our remote
  • There are multiple ways to authenticate but the simplest one is to use VS Code’s built in authentication
  • We can also utilize using SSH keys or a PAT token

SSH Keys

We need to perform these steps to authenticate our GitHub account. Using VS Code directly helps us to skip these steps
  • Read official docs here
  • We use ssh to generate cryptographic keys which helps us create connection between two computers
  • We generally buy a server from a cloud hosting company such as DigitalOcean and generate SSH keys and put the public keys on the server then connect it with our system using the private keys
notion image
notion image

Generating SSH Keys

This is not important if we have already authenticated GitHub using VS Code
notion image
  • This will generate a public and a private key inside the home or user directory
  • The public key ends with a .pub we need to upload this to our GitHub
    • notion image
  • GitHub > Settings > SSH and GPG Keys > Add new SSH Keys
    • notion image
  • After this we need to add it to the SSH agent eval "$(ssh-agent -s)” ssh-add ~/.ssh/id_ed25519
    • notion image
  • Now run this command: ssh -T git@github.com
  • And to clone a Git repository use the SSH link
    • notion image
      notion image

🙇🏻 git commit -m “uncle linus torvalds”