Introduction to Git and Github

Sumit Kumar
7 min readJan 9, 2023

--

What is Git and Github?

GitHub hosts Git repositories and provides developers with tools to ship better code through command line features, issues, pull requests, code review.

Just imagine that you have an application or a project and you want to collaborate with others or you want to see the history of the codebase of your application and the changes made within it.

you can contribute into an open-source project easily with 100s of other contributors around the world.

So, Git and Github maintains the history of the project like which person made which changes at what particular time.

Bitbucket and Gitlab are also some other platforms which helps us to do the same things.

Look at the above image, here you can see 1250 commits. It is basically the history of that project, you can go back to if you find a bug, or want to make a change.

Just click on the Commits icon and you will see which person made what changes at what particular time in the project as shown in the above image.

You can see each and every changes made within the project folder.

Let’s get started by downloading Git . Just go to https://git-scm.com/downloads and download it.

Install and set up the application.

I work on windows device and prefer Notepad as my Git’s default editor as it is very simple and easy-to-use text editor.

Once it’s installed write ‘git’ in your terminal emulator to see if it’s properly installed or not as shown in the image below.

If you see the list like this in your shell then your Git application is properly installed.

Now we can maintain the history of our project using Git. These histories are stored in an folder provided by Git.

Open the application named ‘Git Bash’ in your computer.

It should look like this.

So how do we get the project folder where the histories are saved. Just use ‘git init’ command in the Git Bash shell.

It’s showing ‘Reinitialized existing Git repository’. So it’s showing nothing over there because that files are hidden. Just use ‘ls -a’ to see the list of all those hidden files.

In the above image you can ‘.git’ is there.

We can also see that what’s inside that ‘.git’ folder. Just use ‘ls .git’ command in the shell.

You can see that Git folder as shown in the image above.

Now We can maintain the history of our project. Any changes made within the project will be picked up by Git.

Now start by making a project. Use ‘touch’ command to make a folder in our Git repository.

So ‘Prince.txt’ file is created. You can check your ‘.git’ folder by using ‘ls -a’ command as we have discussed above.

Use ‘git status’ to see the changes made within the folder.

Here you can see that it says the file is ‘untracked’. The ‘git status’ command tells us that the changes made in the project are currently not in the history of the project.

Now we want these changes to be in the history of our project. Use ‘git add names.txt’ command and check the ‘git status’ again. Let’s see.

Now these changes are ‘tracked’ because it saved and recognized the new file ‘Prince.txt’.

To save the changes in our git history. Use ‘git commit -m “message you want to put”.

Here you can clearly see that ‘1 file changed, 0 insertions, 0 deletions’ written in the shell.

Now check the git status again.

It says ‘nothing to commit, working tree clean’.

Let’s add something in this file. So use ‘notepad Prince.txt’ command and see what happens.

you will see that the ‘Notepad’ application in your computer will be opened automatically and let’s write something and save.

Use ‘cat names.txt’ to display whatever is available in the file.

Now you can see that ‘Prince.txt’ file has been modified. Just check the ‘git status’ again.

Isn’t it cool?

Now if you want to undo the changes you have made. Use ‘git restore — staged names.txt’ and check the ‘git status’ again.

Let’s find out where can we see all the history of the file. Just use ‘git log’ command.

You can find more git commands on Google.

Let’s get started with Github.

Create a new account on https://github.com/ or sign in if you already have one.

It will look like this.

Let’s create a new repository for our project. Click on the ‘Create repository’ icon and give it a cool name and again click on ‘Create repository’ icon. It will look like this.

Just copy the URL of your repository and attach with your project. Use ‘git remote set-url origin URL’ command in the shell.

Use ‘git remote -v’ command to show all the URLs attached to that folder.

Use ‘git push origin master’ to share the changes on that URL.

Now refresh your repository URL.

You can see that the ‘Prince.txt’ file is added.

When we do multiple commits, it forms some ‘Branches’.

Image Courtesy : Google

Whenever you are working on other features in your project or resolving a bug always set a separate branch.

Let’s consider a Github repository as shown in the image below. You will see there are many branches of that repository but the main branch is the default branch. The main branch is the one that is used by the people. This is the reason we never commit directly on the main branch because our code is not finalized yet and it may contain some errors.

So all the codes that are not finalized yet should go to a separate branch.

Now let’s work with some existing repositories.

Take this repository and suppose you want to change anything in the folder of this repository. But you won’t be able to do that because you don’t have access to this account.

So what is the way to contribute to that project?

We take a copy of this project in our own account. Click on the ‘fork’ icon and it will look like this. This is the main folder as shown in the image below.

Click on ‘create fork’ and you will see yours forked repository.

Now you can do anything in your forked repository. Go to ‘code’ icon and copy the clone URL.

Go to your Git Bash shell and use ‘git clone URL’ command to clone your forked repository to your device.

You can’t make changes in anyone’s account but you can fork it.

When you make changes in your forked repository then you can make a request to the people maintaining the original project to merge the changes in their repository. This process is called a pull request.

When you make a pull request then people will review your code, suggest some changes and you will make the changes. When your code samples are merged, you will see the codebase of your forked repository in the original repository.

This was the basic idea about Git and Github. Get some hands on practice and start your open-source contribution.

Thank you reading.

Ciao.

--

--

Sumit Kumar
Sumit Kumar

Written by Sumit Kumar

struggling to have everything I want

No responses yet