Git & GitHub Tutorial for beginners
Read this easy-to-understand quick tutorial to know Git and GitHub
If you want to work in the IT field it is essential to learn GIT and GitHub. Let’s read this interesting conversion between Mr. Bhola and Mr. Gola and learn Git and GitHub.
Mr. Bhola: What is GIT?
Mr. Gola: GIT is a Version Control System.
Mr. Bhola: But what is this version control now?
Mr. Gola: Version control helps you to track your project’s versions. You can go back to the previous version any time you want. If you make some mistakes in your project or delete a few files by mistakes then you can recover those files or correct your mistakes by using a version control system. GIT is the most popular version control system.
Mr. Bhola: Thanks. I understand the GIT now. but what is GitHub?
Mr. Gola: Suppose one day your system did not start. You lost all your codes for your project. What can you do now? Don’t worry. GitHub is here to rescue you. GitHub is an online website where you can keep a copy of your project. A few lines of commands and your project will be available to you again.
GitHub also helps us with collaborative work. A team can work together on the same project from different locations using GIT and GitHub.
Mr. Bhola: Great. I have started liking GIT and GitHub. I want to use it for my project. How can I use it?
Mr. Gola:
As you are a beginner you can follow the following steps to start your GIT journey.
1) Installations
a. If you have not created your account then create an account on GitHub. Use this link to signup - https://github.com/signup
b. If you have not Installed GIT in your system install it. Use this link to download the executable file - https://git-scm.com/download/win. After download, run the executable file and follow the steps and install it. Run this command to check if you have installed GIT correctly
git --version
c. Introduce yourself to GIT. Open your terminal and run the following commands
git config --global user.name "YOUR_USERNAME"
git config --global user.email "YOUR_EMAIL_ADDRESS"
NOTE - Replace YOUR_USERNAME with your username and YOUR_EMAIL_ADDRESS with your email address.
NOTE- This one-time thing. No need to introduce yourself to GIT daily.
2) Add GIT to your project
Suppose you have this simple project. And you want to add GIT to this project.
Go to the project folder, then “right-click” and click on the open in the terminal or you can use your IDE to open the terminal.
a. Run this command to initialize the GIT for your project.
git init
NOTE - You have to run this command only once for a project.
b. Now you have to add file(s) / folder(s) to your GIT. You can run any of these three commands as per your requirement.
If you want to add a file run this command.
git add filename
If you want to add a folder run this command
git add foldername
If you want to add all the files and folders of the current project.
git add .
3) Commit your changes
Committing means you are telling git to keep the track of these changes. Run this command to commit your changes.
git commit -m "your commit message"
4) When you modify your codes
Suppose you have added the following code.
You need to run the following commands whenever you have made some changes and want to commit your changes.
git add .
git commit -m "your new massage"
5) Pushing your code to GitHub
Now if you also want to push your projects to the GitHub website. Follow the following steps.
c. Click on "Create Repository" and create your new repository.
d. After creating the repository, you will see this screen. We want to push an existing project to GitHub we will use highlighted commands from our terminal.
git remote add origin https://github.com/rhtm123/MyRepoName.git
git branch -M main
git push -u origin main
When you will push your codes. Your Git repo will look similar to this.
7) Making Changes & Pushing your code to GitHub
Whenever you make some changes to your project, and you want to push that change to your GitHub website you need to run the following commands
git add .
git commit -m "some other your new message"
git push
And you keep doing this again and again whenever you make some changes and want to push it.