Hi everyone, I’m happy to share my knowledge by writing my first blog - Git and GitHub for Beginners!
What is Git?
Git was initially designed and developed by Linus Torvalds. It is a version control software by which we can have code in the common place. Git is particularly useful when multiple people work on multiple things, but they are working in the same team and are all making changes to the same files at the same time.
Git is a local repository and Github serves as a remote repository.
Git Installation:
1. Download Git from the official website, Git downloads
2. Install Git on your local machine by following the instructions provided on the official website: Installing Git
Git bash is now available and next step is to configure the Git.
Configuring your name and email address on your local machine is an essential step in setting up Git.
To set up your name, you need to type the following command in your terminal:
After setting up the user name, we also need to set up the user email.
Whatever email id we have given in the Github, we have to give the same email id.
Now that we have finished setting up your username and email for Git and GitHub, let's verify that everything is configured .
What is Github?
Github is a hosting service for Git repositories. Git is the tool ,while Github is the service to use Git.
It allows multiple people to work on a project, track revisions, and contribute to code from anywhere in the world.
Creating a GitHub Account
Go to github.com and enter the required user credentials asked on the site and then click on the SignUp for GitHub button.
After entering the credentials, need to click sign in.
Github Dashboard will appear like this
Repository can be created by GitHub website or the command line.
Create a Repository Using the Github Website:
After logging into your GitHub account, you can create a new repository by following these steps:
1. Click on the + sign in the top right corner of the page and select New Repository from the dropdown menu.
2. You will be directed to a new page where you can fill in the details of your new repository.
3. Give the name of your repository. It should be unique and descriptive.
4. You can give a brief description of your repository.
5. You can choose to make your repository public or private. Public repositories are visible to everyone, while private repositories are only visible to you and the people you share them with.
6. You can choose to initialize your repository with a README file.
After giving all the details, click on the create repository button.
You have successfully created a new repository on GitHub. You can now start adding files and make changes to your repository.
Create a Repository Using the Git Command Line:
1. Open your terminal and go to the directory where you want to create your new repository.
2. Use the git init command to create a new repository. This command will create a new directory called .git in your current directory, which will contain all the necessary files for your repository.
We have now initialized a new repository called demo.
3. Once you have created your new repository, you can start adding files and making changes to it. You can also connect your local repository to a remote repository on GitHub by following the instructions provided on the GitHub website.
Now we have successfully created a repository using the GitHub website and the command line – but how do we connect them? Let's learn how to connect a local repository to a remote repository on GitHub.
Connecting a Local Repository to a Remote Repository on GitHub:
1. Go to the main page of the repository you created earlier.
2. Click on the Code button to copy the URL of your repository.
3. In your terminal, go to the directory of your local repository by selecting the file path and press cmd.
4. Use the git remote add origin command to connect your local repository to the remote repository on GitHub. Replace repository-URL with the URL of your repository.
The image above shows the command to connect your local repository to the remote repository on GitHub.
Let’s take a look at the branches using Git commands.
To Check the Status of the Current Branch:
The git status command shows the status of the current branch, including any changes that have been made to the files in the repository. It provides information about which files have been modified, which files have been staged, and which files are untracked.
Stage Changes:
The git add command adds files to the staging area, preparing them for the next commit.
If you want to add a specific file, use the git add <file-name> command, replacing <file-name> with the name of your file. This process is known as staging, which prepares files for the next commit.
How to Commit Changes:
The git commit command commits the changes to the current branch. You can use the -m flag to add a message to your commit. This message should give a brief summary of the changes you've made.
For instance, "First commit" could be your commit message. This command is used to save the changes to the local repository.
We've successfully committed the changes to the current branch. Next, we'll push these changes to the remote repository on GitHub using the git push command.
How to Push changes to a Remote Repository:
The git push command pushes changes from your local repository to a remote repository on GitHub.
You can now view your changes on the GitHub website.
To create a new branch:
Use the git branch command to create a new branch.
The git branch command creates a new branch but does not switch to it. To switch to your newly created branch, use the git checkout command.
Conclusion:
Hope this blog is informative and happy integrating!