Day-09: Deep Dive in Git & GitHub for DevOps Engineers
Today is Day-09, diving deeper into Git & GitHub.
Table of contents
Introduction
π Welcome to Day 9 of the 90-Day DevOps Challenge!
π Let's embark on another exciting day of learning and leveling up your DevOps skills!
Topics of the Dayπ»π§
What is GIT & Why is it important?
Git is like a time-traveling superhero for coders! π¦ΈββοΈ
It's a super-duper version control system that tracks changes in your code. π¦Ύ
Picture Git as your code's spy, always lurking in the shadows. π΅οΈββοΈ
Instead of shouting "ABRACADABRA!" to make code disappear, Git says "Commit!" πͺ
Git creates checkpoints called "commits," saving your code's historyπ
If you mess up, Git lets you rewind time and undo your evil deeds. π°οΈ
Collaboration is a cakewalk! Git helps teams work together without chaos. π
You can also create branches, like code clones, to experiment without breaking stuff. π³
Once your code is super cool, you can summon Git's sidekick, GitHub, to show it off! π¦ΈββοΈ
GitHub makes your code shine on the internet for all to admire! π
What is GitHub then?
GitHub is like a fancy web hotel for Git repositories!π
π₯ Developers unite! Share, store, and collaborate with ease!
π¬ Got issues? No problem! GitHub's got your back with issue tracking! π
Pull requests? Yes, please! It's like a digital high-five for your code!
π€Teamwork makes the dream work! GitHub helps teams collaborate like never before!
π Code reviews made simple! Git it together, people! π
So, if Git is a train π, then GitHub is the ultimate party wagon! πππΊ
What is the difference Between Main Branch and Master Branch?
π³ Main Branch:
The cool, hip, and modern cousin of the Master branch.
It's like the "latest and greatest" version of your project.
All the newest features and bug fixes hang out here, partying together.
π΄ Master Branch:
The old and wise grandparent of version control.
It's the more traditional name for the main branch, but it's not as popular anymore.
Think of it as the "official" version of your project, the one you show off to the world.
π Relationship:
Main and Master are like twins, born at the same time.
Any changes you make in one, the other knows about it.
They might have different names, but they're still family.
π» What to use:
Nowadays, everyone's using "Main" as the default branch.
Master is taking a well-deserved retirement, sipping tea and watching from afar.
π Celebrations:
When you finish a feature, you party hard and merge it into Main.
Master gets a break, sipping more tea while you do the partying.
π Bottom line:
Main and Master are just two names for the same thing, like "pizza" and "pie."
Both are essential for version control, so use Main and make everyone happy!
What is the difference between local & remote repositories?
Local Repository π‘
Your local repo is like your own cozy little room where you code in peace.
It's where all the magic happens: creating, editing, and deleting files with reckless abandon!
You can think of it as your stash of chocolate cookies; only you can reach them.
Remote Repository π
- The remote repo is like a magical cloud where your local repo's stuff gets backed up.
It's a bit like a mystical treasure chest; everyone can see it, but only the chosen ones can contribute to it.
How to connect local to remote?
Connecting Local to Remote π
- Pick Your Dance Partner ππΊ
- Choose a remote repository hosting service like GitHub, GitLab, or Bitbucket. It's like a dating app for code!
- Create the Remote Repository π
Sign up on the hosting service and create a new repository there.
Think of it as giving birth to a new digital baby!
- Link the Two Worlds π
On your local machine, use the Git command
git remote add origin <remote_url>
to connect your local repo to the remote one.It's like sending a postcard with the address of the cloud treasure chest!
- Push Your Code to the Stars π
Make your local changes and commit them using
git commit
.Then use
git push origin <branch_name>
to send your changes to the remote repo.Imagine your code as a rocket being launched into the cloud!
- Pull in Remote Changes πͺοΈ
If others made changes in the remote repo while you were busy coding, fear not!
Use
git pull origin <branch_name>
to pull those changes into your local repo.It's like summoning the magical cloud genie to bring you the latest goodies!
- Show Off Your Code π
Share your remote repository's link with your friends, colleagues, or minions, and let them admire your coding prowess!
It's like hosting a coding talent show!
Task-1:
Set your user name and email address, which will be associated with your commits.
Point to Remember:
GitHub no longer supports password authentication for pushing changes to repositories via HTTPS. Instead, it requires the use of a personal access token or SSH key for authentication. I'll talk about both.
I. Using SSH Authentication
Generate SSH Key
On your local machine, open a terminal or command prompt.
Type the following command to generate an SSH key:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Press Enter to run the command.
It will prompt you to choose a location to save the key and create a passphrase (optional but recommended for added security). If you don't want a passphrase, just press Enter.
Add SSH Key to GitHub Account
Go to your GitHub account in a web browser.
Click on your profile picture in the top right corner and select "Settings."
In the left sidebar, click on "SSH and GPG keys."
Click on "New SSH key" or "Add SSH key," depending on the GitHub version.
Give your key a title (e.g., "My SSH Key on Laptop") in the "Title" field.
Now, go back to your terminal and display the contents of your public key with the following command:
cat ~/.ssh/id_rsa.pub
Copy the entire output of the command (the SSH key).
Paste the SSH Key into GitHub
Go back to your browser where you're adding the SSH key to your GitHub account.
Paste the SSH key you copied from the terminal into the "Key" field.
Click on "Add SSH key" to save it.
Changing Remote URL for All Repositories
- To use SSH authentication for all the different repositories in your GitHub account, you'll need to change the remote URL for each repository on your local machine.
Find the SSH URL for a Repository
Go to the repository on GitHub (e.g., https://github.com/MeenalJy/MyRepo).
Click on the "Code" button (green) to reveal the cloning options.
Make sure "SSH" is selected, not "HTTPS."
Copy the SSH URL (e.g., git@github.com:MeenalJy/MyRepo.git).
Change the Remote URL for Each Repository
In your terminal, navigate to the local copy of a repository on your machine (e.g.,
~/ninthday/DevOps
).Use the following command to change the remote URL for that repository to the SSH URL you copied earlier:
git remote set-url origin git@github.com:MeenalJy/MyRepo.git
Replace
git@github.com
:MeenalJy/MyRepo.git
with the SSH URL for the specific repository you're working on.Repeat this step for each repository you want to use SSH authentication with.
Result:
That's it! Now you should be able to push changes to your repositories without being prompted for a password. The SSH key on your local machine will serve as your authentication method when interacting with your GitHub account. Remember to keep your private SSH key secure and do not share it with others.
Sure! Here's a step-by-step guide in simple terms for creating a personal access token and using it for all different repositories in the same GitHub account:
II. Create a Personal Access Token
Go to GitHub Account Settings:
Open your web browser and go to https://github.com/.
Log in to your GitHub account.
Access Developer Settings:
Click on your profile picture in the top right corner.
From the dropdown menu, select "Settings."
Navigate to Personal Access Tokens:
- In the left sidebar, scroll down and click on "Developer settings."
Generate New Token:
- Under "Developer settings," click on "Personal access tokens."
Create a New Token:
- Click on the green button labeled "Generate new token."
Configure Token Settings:
You will be asked to provide your GitHub password for security purposes.
After entering your password, you'll see a page to configure the token settings.
Select Scopes for the Token:
For each scope, there will be a checkbox. You need to select the appropriate scopes for your token to access repositories.
At a minimum, you should select "repo" or "public_repo" to access your repositories. If you need additional access, select other relevant scopes.
Generate the Token:
- Scroll down to the bottom of the page and click on the green button labeled "Generate token."
Copy and Store the Token Securely:
Once the token is generated, GitHub will display it. Copy this token to your clipboard.
IMPORTANT: Treat this token like a password. Do not share it publicly or store it in an insecure location.
Using the Personal Access Token for Different Repositories
Now that you have your personal access token, you can use it for all the different repositories in your GitHub account. Here's how to do it:
In the Terminal:
- Open your terminal or command prompt on your local machine.
Navigate to the Repository:
- Use the
cd
command to navigate to the local directory of the repository you want to work with.
- Use the
Update Remote URL:
Check the current remote URL for the repository using the command:
git remote -v
Change Remote URL:
If the remote URL starts with "https://github.com," change it to use your personal access token. The command to change the remote URL is as follows (replace
<token>
with your actual token):git remote add origin https://<token>@github.com/MeenalJy/RepoName.git
This command updates the remote URL to include your access token in place of the password.
Verify Remote URL:
- Confirm that the remote URL has been updated correctly using:
git remote -v
- Confirm that the remote URL has been updated correctly using:
Push Your Changes:
- Now, when you push your changes to the repository, you won't be asked for your GitHub password. Git will use the personal access token for authentication.
Repeat for Other Repositories:
If you have multiple repositories in the same GitHub account, repeat the above steps for each repository.
Use the same personal access token for all your repositories.
Output
That's it! You have successfully created a personal access token and set it up to use for all the different repositories in your GitHub account. This token will act as your authentication method instead of your GitHub password when interacting with your repositories over HTTPS. Remember to keep your access token secure and do not share it with anyone.
Task-2
Create a repository named "Devops" on GitHub Connect your local repository to the repository on GitHub. Create a new file in Devops/Git/Day-02.txt & add some content to it Push your local commits to the repository on GitHub.
Conclusion
Remember, connecting local and remote repositories is like making code friends around the world. And don't be afraid to play around; just don't delete someone else's digital treasures accidentally!
Disclaimer: No coding treasures were harmed during the making of this explanationπ