Git 101
Discover Git: The version control software that is used to manage this website as a repository on GitHub.
I will be listing out various things about git and how it works; and how you can use it :)
101-01: Version Control
Git
This is a free and open source version control system.
Version Control
This is managing changes to a file, folder, or a program, or a website like Incident Clarity. Giving a version to the changes made. Example: v01 is old, v02 is new.
Repository
Project or a folder where your files are kept.
Github
A website to host your repository.
Is there only one way to host your repository? NO
Here are a few more ways to host your git repository:
101-02: Git Commands
- git init: Boot up a new Git repository; it’s like creating your own digital fortress.
- git clone {repo}: Duplicate a repository to your local machine; think of it as creating a local honeypot.
- git add {file}: Stage changes for the next commit. This creates the v02 (as per my above example)
- git commit -m “message”: Save staged changes with a message; think of it as leaving a breadcrumb trail for your mess.
- git status: Check the status of your working directory.
- git log: View commit history; your chronological evidence of code.
- git diff: Compare differences between your commits.
- git branch: List, create, or delete branches.
- git checkout {branch}: Switch to another branch.
- git merge {branch}: Combine changes from another branch.
101-03: GitHub
Go to the website, solve the problem given, create an account, and you are ready for action.
Create a new public repository called Public_Repo
; we will use this later in the tutorial.
Create a file called README.md within the Public_Repo
.
Creating an SSH keys
To push any of your repository to github using the CLI, for an ease of use, we will create an SSH key.
Commands:
Creating the key:
cd ~/.ssh
ssh-keygen -t ed25519 -C "your-email@domain.com"
# give it a name
~/.ssh/github-ssh # name is github-ssh
# Add a passphrase for security (or leave it black, live the wild life)
Copying the key:
ls ~/.ssh | grep git
github-ssh
github-ssh.pub
# This creates 2 files; A public key (github-ssh.pub) and a Private key (github-ssh).
# Private key is yours, dont share it with anyone
# Copy the public key.
cat ~/.ssh/github-ssh.pub | wl-copy
# Use the above command if you have wayland compositor (and wl-copy installed)
# for mac -> pbcopy < ~/.ssh/github-ssh.pub
Now, go to GitHub settings and add this key which you just coppied.
Let’s make sure the CLI know that you generated a key.
eval "$(ssh-agent -s)"
ssh-add
Add the following to the file ~/.ssh/config if it’s not already there.
Host github.com
User git
Hostname github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github-ssh
IdentitiesOnly yes
Finally, test your connection with the key
ssh -T git@github.com
# Attempts to ssh to GitHub
# You will see:
# Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.
101-05: Working with Git repos
Method 1
Clone the repository we just created on GitHub using the SSH .
git clone git@github.com:Username/Public_Repo.git
# Add contents to your README.md file
echo "Hi, This is some test text" > README.md
git add .
git commit -m "Modifying README.md" -m "Comment"
git push -u origin main
Now when you do a git push the main branch, it will ask you for your ssh key password that you set while creating the (github-ssh) file. If everything worked correctly, your modification is not recorded and pushed to GitHub for anyone to see at https://github.com/Username/Public_Repo
Method 2
Now, Let’s say you did not create a repository using git at first.
We have a folder called, repo-demo-2
; this is a different repository
cd repo-demo-2
Make files or folders inside the directory.
touch README.md
echo "Demo 2" > README.md
mkdir private_folder
touch private_folder/file.txt
Now turn this repo-demo-2
direcotry into a git repository.
# Initialized the git repository in this folder
git init
# This shows that files and folders are created but not monitored (untracked)
git status
# Add all the files and folders we just created
git add .
git commit -m "Created Readme and text.txt" -m "Comment here"
# Set the branch to main
git branch -M main
# Now let's push this to a remote repository on github
# Create an empty repository called repo-demo-2
git remote add origin git@github.com:Username/repo-demo-2.git
# Confirm that remote repository is added using: git remove -v
git push -u origin main
# It will prompt for your ssh key password
This will make your repo-demo-2
directory into a git repository.
Now you can clone the same repository on any computer.
git clone git@github.com:Username/repo-demo-2.git
101-06: Git Branches
Just like the main branch, we can create different branches to for various purposes.
This is enough to get started with Git and GitHub.