Learn the Basics of Git and GitHub

Learn the Basics of Git and GitHub

If you want to get started on learning about Git technology, you’ve come to the right place. This is a comprehensive beginner’s guide to Git. There are many clients for Git. The technology is all the same no matter the client. But in this guide we’ll be using GitHub to understand Git.

Let’s get started!

What is Version Control?

Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. So ideally, we can place any file in the computer on version control.

Creating a new Github repository!

Creat new project

When creating a new project on your local machine using git, you'll first need to create a new repository(or often "repo"). Now, to begin with, open up a terminal and move to where you want to place your project on your local machine using the cd(change directory) command.

Screenshot (10).png

Initialize the git

To initialize the git repository to the root folder run the git init command.Screenshot (12).png

Add new file to repository

Now, add a new file to the repository, using any text editor you like. I'll be using vscode for this. (On the terminal you can run "code ." and it will open up the vscode with the directory you are working on)

Screenshot (15).png

Git status command

After creating a new file you can run the git status command to know which file exists in your repository.

Screenshot (13).png In the command prompt, it basically tells you that it has noticed you have created a new mytext.txt file but unless you use the "git add" command we can't stage any changes.

Git add command

Now to stage our changes to Github, run the git add. You can now run the git status command again to check if your changes are staged or not.

Screenshot (14).png Here the "dot(.)" means it will stage of all the changes you have made, for instance, if you have multiple files then it will stage all the changes of each file you have made up till now, so it is recommended in a development environment to give the relative path of the file you want to add.

Git commit

It's time to commit your message now using git commit -m "Your message about to commit".

Screenshot (16).png The message in the commit should be something related to what you have done maybe it's a new feature or a bug fix or any refactoring of something.

Create new repo

Now create a new repository on Github.

lsz77I_tn.avif

After clicking the button, GitHub will ask you to name your repo and provide a brief description.

Screenshot (17).png

Git remote add origin

Now, open up your terminal and run git remote add origin to add your project from your local machine to GitHub.

Screenshot (18).png

I have used "git branch -M main" here for demo purposes since I am having only one file, but when you are working in a development environment it is recommended that you create a new branch for your work and push all the changes to that branch by using git checkout command.

Push your project

Now we'll push the commit on our new Github repo. This will allow other programmers to see the changes we've made or the work we have done. To push changes we'll run the git push command.

Screenshot (19).png

Did you find this article valuable?

Support Chhakuli Zingare by becoming a sponsor. Any amount is appreciated!