Table of contents
- What is Git and why is it important?
- What is the difference Between Main Branch and Master Branch?
- Can you explain the difference between Git and GitHub?
- How do you create a new repository on GitHub?
- What is the difference between local & remote repositories? How to connect local to remote?
- Tasks:
- Set your user name and email address, which will be associated with your commits.
- 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
- Push your local commits to the repository on GitHub
What is Git and why is it important?
Git is a version control system that acts as a code management tool which stores all the versions and history of the file system and provides flexibility to developers to collaborate with each other for software development. It is important because it tracks and saves all the versions and history of the file system thus giving developers the ability to go back and forth to the versions and allowing them to manage the code within the team.
What is the difference Between Main Branch and Master Branch?
Both "main" and "master" are used as default branch names in different contexts and this change is specific to GitHub's default behavior and other Git platforms. "main" is the default branch created in Github when an empty repository is created whereas "master" is the default branch created in Git when an empty repository is initialized and it still might be used as the default branch name on other Git platforms or repositories.
Can you explain the difference between Git and GitHub?
Git is a version control system that tracks and saves all the versions and history of the file system and allows developers to collaborate for code management in software development.
Github is a web based hosting service for Git repositories. It provides an open source platform where developers can store their git repositories remotely, making it easier to collaborate with others and access their codes from anywhere.
How do you create a new repository on GitHub?
i) Signup to GitHub if you don't have an account. If account already exists, login to GitHub - https://github.com/
ii) When you are on Dashboard, you will see Green Color Button on top right corner of the screen inside Top Repositories Section. Click on "New".
Or, you can click on the user profile in the top left section where a menu bar will open up and click on Your repositories.
A page will open up which will contain all the existing repositories and then click on "New".
iii) A new page will open up where fill in all the required details like repository name, description, type, etc and click on Create Repository button present at the bottom. A new repository will be created.
What is the difference between local & remote repositories? How to connect local to remote?
Local repositories is the repository located in our computer system or the development environment where we are actively working on the code. Remote repository is the repository located in our remote server like GitHub or GitLab. When we clone or download a repository from a remote server, we create a local copy of that repository in our local system. We make changes in the local repository and push them back to a remote repository to collaborate with developers and share the work.
We can connect local to remote using the command git clone url. Here, the url is the copied url of the GitHub repository which we want to clone to our local.
Tasks:
Set your user name and email address, which will be associated with your commits.
The username and email address can be set globally for all the repositories in our local using the following command.
git config --global user.name = "username"
git config --global user.email = "ektashilpi@gmail.com"
Example -
Create a repository named "Devops" on GitHub
Follow the above steps written above to create a repository named "Devops" on GitHub.
Connect your local repository to the repository on GitHub.
We can connect our local repsoitory to the repsoitory on GitHub by copying the url of the Devops Repository and then giving the below command in the terminal -
git clone <url>
Create a new file in Devops/Git/Day-02.txt & add some content
Firstly, we will check the status of git using
git status
command. Now, we will create a text file usingtouch Day02.txt
command and open vim editor by givingvim Day02.txt
. We will add some content to it and save it. Again, we will check the status usinggit status
where it asks to stage the file usinggit add Day02.txt
. We will add the file and commit the file to the local repository using gitcommit -m "Adding Day02.txt file"
. Now, when we check the git status, it shows us that our local branch is ahead of the origin main branch i.e. ahead of GitHub repository as one file got added to the local.Push your local commits to the repository on GitHub
To push our local commit to the repository on GitHub, we need to first establish a connection with the GitHub using Personal Access Token. Go to GitHub -> Settings -> Developer Settings -> Generate Tokens(Classic) -> Enter token name -> Select repos checkbox -> Click on Generate token. Copy the PAT and keep it safely. Now, we need to set the url using the PAT.
Check the urls of remote repository connected to local using the command -
git remote -v
Now, to push the local commit, we need to first set the remote url using Personal Access Token. The new url should be PAT@github.com...We will use the below push, add and delete commands to set the new url with Personal Access Token and delete the older url -
git remote set-url [--push] name <newurl> <oldurl>
andgit remote set-url --delete name <oldurl>
We can check the updated url using
git remote -v.
At last, we will give the push command to push the latest commit to GitHub i.e
git push origin main
We can check whether the changes of our local are added to GitHub or not by going to GitHub and checking the file in Devops Repository. We can see our local changes are updated and pushed successfully to the GitHub repository.
Thanks for reading!
~Shilpi