Table of contents
- What is Git and why is it important?
- What is the difference Between Main Branch and Master Branch??
- The difference between Git and GitHub?
- How to create a new repository on GitHub?
- What is the difference between local & remote repositories?
- How to connect local to remote?
- Set a user name and email address in git
- Let's do some tasks
What is Git and why is it important?
Git is a Version control system that allows developers to keep track of changes made to their code by others or by themselves. It was created by Linus Torvalds in 2005. Git allows developers to work on different versions of their code simultaneously, and then merge those changes back. This makes it easy to collaborate with other developers and to track the history of changes.
Git has so much importance because of the following reasons:
1. Collaborations
2. Version History
3. Branching and merging
4. Distributed Architecture
What is the difference Between Main Branch and Master Branch??
There’s no difference between the main and master branch as such, both serve the same purpose of being the primary branch that contains the latest stable version of the codebase. The only difference is their names 😉
The difference between Git and GitHub?
Git is a Version control system that is used to manage changes to the source code. It is a command-line tool that allows developers to track changes to their codebase over time.
Whereas, GitHub is the web platform that provides hosting for Git Repositories. It allows developers to store their Git Repositories remotely, share them with other developers, and collaborate on code by contributing to repositories or creating “pull requests”.
How to create a new repository on GitHub?
Log in to your GitHub profile and click on the "Create a new repository" button after tagging a name to your repository and selecting one out of "Private" and "Public".
Creating a Public repository does not cost you anything, wherein a Private repository is chargeable.
What is the difference between local & remote repositories?
A local repository is a version control system repository that is stored on your local computer. It contains all of the files and the history of changes for a particular project. When you make changes to your local repository, they are not reflected to other developers until you push those changes to a remote repository.
A remote repository, on the other hand, is a repository that is stored on a remote server, like GitHub, GitLab or Bitbucket. It is typically used as a central repository that multiple developers can access and collaborate on. When you push changes to a remote repository, other developers can then pull those changes down to their local repositories and see the latest changes.
How to connect local to remote?
Create a remote repository: Log in to your GitHub Profile and create a repository.
Initialize your local repository: Initialize your local repository by running a version control command "git init" on your machine.
Add a remote URL - Add a remote URL to your local repository to connect it to the remote repository.
To do that run the commandgit remote add origin <remote repository URL>
And it's done!
Set a user name and email address in git
To set a username
git config --global user.name "mynameis"
To set an email for the same
git config --global user.email "myemailis"
Let's do some tasks
Create a repository named "Devops" on GitHub
Bang! You've successfully created a new repository.
Now you can write a README file and you can create your first file or you can upload an existing file to the repository by clicking on the options available in the picture above.
Connect your local repository to the repository on GitHub.
This is how your new repository will look like, Now click on "<>Code" button
And copy the HTTPS address by clicking on the copy button
Now, Run the command: git clone "the https address you copied"
git clone https://github.com/amrendra0918/DevOps.get
Done!
Create a new file in Devops/newfile & add some content to it
Push your local commits to the repository on GitHub
Step 1: Adding the newfile to staging area.
git add .
You can check the status by running the follwing command:
git status
Step 2: Commiting the change with an added message
git commit -m "updating the code"
Step 3: pushing the code/change to the remote repository
git push origin main
Now, If you are getting this error, don't be affraid. It's okay to get errors. You get to learn from errors a lot. We always have a way to fix our errors.
This error message is indicating that the password authentication method for Git repositories hosted on GitHub has been deprecated as of August 13, 2021. Which means that this method will no longer work.
The alternative authentication methods would be personal access tokens (PATs) or SSH keys. We will use Personal Access Tokens to authenticate.
For that just follow a few steps:
Log in to your GitHub profile.
Click on your profile logo in the top right corner of the page.
Click on Settings
-
Click on "<> Developer settings" in the bottom left corner.
Click on Personal access tokens.
Select Tokens (classic)
Click on Generate New Token and select Generate New Token (classic)
Give it a personalized note to remember and select scopes that you'll require (For now I am selecting only repo) and click on Generate Token in the bottom of the page.
Yay! You've successfully created a personal access token.
Now, Copy the token, and go to your terminal.
-
Now run the command to set a new URL using the personal access token as below:
git remote set-url origin https://<your_personal_access_token>@github.com/your_remote_repository
e.g.
https://ghp_ydxFvh2eQ10***************gCw2yQw2w@github.com/amrendra0918/DevOps.git
Finally, you can run the command to push your code onto the remote repository
git push origin main
And it's done!
Step4: Let's check our remote repository
Good Job!
You've successfully pushed your changes to the remote repository.
Thank you for reading! I hope you learnt something from this blog.
Happy Learning😄