Deep Dive in Git & GitHub
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 a local and remote repository? How to connect local to remote?
- Tasks
- Task-1: Set your user name and email address, which will be associated with your commits.
- Task-2: Create a repository named "Devops" on GitHub
- Task-3: Connect your local repository to the repository on GitHub.
- Task-4: Create a new file in Devops/Git/Day-02.txt & add some content to it
- Task-5: Push your local commits to the repository on GitHub
"GitHub is not just a platform; it's a global community of developers building the future together, one commit at a time."
What is Git and why is it important?
Git is a tool used by developers to save and manage their code changes. It's like a time machine for code, allowing them to track changes, work together on projects, and easily undo mistakes. It's essential because it makes collaboration easier and helps keep code safe.
What is the difference between Main Branch and Master Branch?
The Main Branch and Master Branch are two different names for the same thing in Git. In the past, "Master" was a commonly used default name for the main branch, but to be more inclusive and respectful, some communities now use "Main" instead. Functionally, they serve the same purpose as the primary branch in a Git repository.
Can you explain the difference between Git and GitHub?
Git is a version control system that helps developers manage code changes locally on their computers. GitHub, on the other hand, is a web-based platform that uses Git to store and manage code repositories in the cloud. While Git is the tool, GitHub provides a user-friendly interface to collaborate, share code, and work together with others.
How do you create a new repository on GitHub?
To create a new repository on GitHub:
Log in to your GitHub account.
Click on the "+" sign in the upper right corner and choose "New repository."
Give your repository a name, description, and choose whether it should be public or private.
Optionally, you can add a README file, license, and .gitignore file to customize your repository.
Click on "Create repository," and you're done!
What is the difference between a local and remote repository? How to connect local to remote?
A local repository is the version of your project that exists on your own computer. It's where you make changes and work on your code. A remote repository is a copy of your project that exists on a remote server, like GitHub, allowing you to share your work with others and collaborate.
To connect a local repository to a remote one:
Create a new repository on GitHub (if you haven't already).
On your local machine, navigate to the project folder using the terminal or command prompt.
Use the command "git remote add origin [remote repository URL]" to set the remote repository URL for your local repository.
Use "git push -u origin [branch name]" to push your local code to the remote repository. The "-u" option links the local and remote branches together.
Now, your local and remote repositories are connected, and you can easily push and pull changes between them.
Tasks
Task-1: Set your user name and email address, which will be associated with your commits.
To set your user name and email address in Git, open your terminal or command prompt and enter the following commands:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Replace "Your Name" with your actual name and "your.email@example.com" with your email address. This information will be associated with your commits.
Task-2: Create a repository named "Devops" on GitHub
Log in to your GitHub account.
Click on the "+" sign in the upper right corner and choose "New repository."
Give your repository the name "Devops."
Optionally, you can add a description, choose whether it should be public or private, and add a README file, license, and .gitignore file.
Click on "Create repository," and your "Devops" repository will be created on GitHub.
Task-3: Connect your local repository to the repository on GitHub.
Assuming you have already initialized a local Git repository for your "Devops" project, use the following commands to connect it to the repository on GitHub:
cd /path/to/your/Devops/repository
git remote add origin https://github.com/your-username/Devops.git
Replace "/path/to/your/Devops/repository" with the actual path to your local "Devops" repository, and "your-username" with your GitHub username.
Task-4: Create a new file in Devops/Git/Day-02.txt & add some content to it
Assuming you want to create the file "Day-02.txt" in the "Devops/Git" directory of your local repository and add some content to it, use the following commands:
cd /path/to/your/Devops/repository
mkdir -p Git
echo "This is Day 02 of DevOps learning!" > Git/Day-02.txt
Replace "/path/to/your/Devops/repository" with the actual path to your local "Devops" repository.
Task-5: Push your local commits to the repository on GitHub
To push your local commits to the "Devops" repository on GitHub, use the following command:
git add .
git commit -m "Added Day-02.txt to Git directory"
git push origin master
This will push your local commits to the "master" branch of the remote repository on GitHub. If you have set a different primary branch name, replace "master" with your branch name in the last command.
Subscribe to my newsletter
Read articles from Utsav Gohel directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by