Git and GitHub for Beginners Tutorial

Git and GitHub for Beginners Tutorial

Version control is a common practice used in the software world to easily revert back to the previous stable version if the new changes malfunction. Git provides an excellent platform for this. So, what is Git and how to use Git is the discussion point of this blog. Moreover, we will also discuss GitHub, the open-source community to contribute to Git repositories.

What is Git?

Git is an open-source and free version control system that can track changes in almost any file. It is commonly used by programmers when they are developing source code in a collaborative environment. Moreover, Git can also be used to track the changes you do to your website and even revert back easily. In addition, Git allows you to compare differences between versions and track who made the changes. In short, it is a perfect source/version control management system.

Install Git

To start using Git, you have to download and install it on your computer. Follow the below steps to do it:

  1. Go to Git's official website: https://www.git-scm.com/
  2. Scroll down and click "Downloads".
  3. Select your operating system and follow the on-screen instructions to complete the download and installation process.

Once the installation is completed, it will provide you with a terminal called "Git Bash" to use Git. However, you can also use other terminals, such as Windows PowerShell, Command Prompt, etc.

Configure Git

In order to start using Git, we will take some sample files made by Kevin Stratvert that will help us in learning the use of Git as a beginner. So, download the sample files (in a separate folder) by clicking here.

Now launch Git Bash or any other terminal of your choice. If you want to launch Git Bash, simply search it in the Windows search bar and click the best match option. After the launch, you have to now configure a few things.

The first thing is to specify your name and email address, as it will help to keep track of what changes you did. Write the below command to add a name and then hit the Enter key:

$ git config --global user.name "John Smith"

Similarly, the command for adding email will be:

$ git config --global user.email john*****@***.com

Lastly, you also have to set the default branch name. You will learn more about the "branch" later in this blog. Currently, write the below command and hit the Enter key:

$ git config --global init.default branch main

Get Help

Above we have used the "git config" command to execute all our requests. Since you don't know much about this command, so you can ask Git to help you out, as follows:

$ git config -h

It will populate all the helpful information for the git config command. You can even get more detailed information by typing:

$ git help config

It will open a manual that provides more detailed information for the config command.

Initialize Repository

Go to the folder where you have downloaded the sample files and note its address. Now you have to change the directory in Git so that it looks for that folder. To do that, write the below command and hit the Enter key:

cd c:/users/John/Downloads/Samples

This way, Git will now look at that folder on your computer. Afterward, you have to turn it into a Git repository by writing the below command:

$ git init

This way, you will receive a message that it has initialized the Git repository.

Git Status

At any moment, you can check the status of your Git. To check that, the command is:

$ git status

Once you execute this command, you will see that you are currently in the main branch, while the 4 sample files are currently untracked.

Track and Untrack Files

As you noticed from the Git status, your files are currently untracked. What it means is that any changes you make to these files will not be tracked by Git. So, to make Git track the files, write the below command:

$ git add index.htm

Here "index.htm" is one of the files in your sample folder. After executing this command, Git will start tracking the index file. You can check it using the Git status command.

Similarly, if you want to un-track a file, the command will be:

$ git rm --cached index.htm

Ignore Files from Git

When you specify a folder where Git should look for files, it gets access to all the files in the folder. So, if there are some large files or sensitive files, such as "Employee Salaries.txt", you can make Git ignore them. Follow the below steps to do it:

  1. Go to the folder that contains the sample files.
  2. Right-click and then click New > Text Document.
  3. Rename the text document to ".gitignore". Click "Yes" if you see a warning message.
  4. Open Notepad or any other similar app and open the .gitignore file in it.
  5. Now you have to tell Git about all the files and folders to ignore. So, type the following text in the file:

# ignore all .txt files

*.txt

Here "#" represents the comment part, while "*" indicates that ignore all .txt files. Once done, save the file.

This way, you can make Git ignore accessing sensitive files or any large file that is of no use to you.

Track All Files

Now that you have ignored the sensitive file, you should make Git track all the remaining files. Below is the command to do it:

$ git add --all

Alternatively, you can also use the below commands to do the same job:

$ git add -A

Or

$ git add .

You can check if all files are getting tracked using the Git Status command ($ git status).

Commit

Now that files are getting tracked, it's time to commit them. Commit is basically taking a screenshot of the files at this moment in time. This way, you can use commit to easily roll back to that version of the file in the future. Follow the below command to do it:

$ git commit -m "my first commit of files to the repository"

Here the message is just like a comment that goes along with the commit. This way, you have now committed all the files, or in other words, made their snapshot in the history book.

Change Files and View Differences

Let's now do some changes in a file and see how you can view the differences. To do that, open Notepad and then drag and drop the index.htm file into it. Once the content of the file loads, change some parts of the text and re-save the file.

Now go back to Git Bash and type:

$ git status

You will notice that it shows in red that the index file is modified. You can also use Git to view the differences. For that, write the below command:

$ git diff

It will populate the paragraph from the previous file version and the current new file version to let you see the difference. So, if you are satisfied with the editing, then you can save the file in the history book. For that, type the below command:

$ git add index.htm

Once you execute this command, the file is now sitting in staging. This is the place where the file resides until it is committed. It is like a holding place until you are all set to add that file edits into the history book.

There are three environments in Git. The first one is the "Working Files", which is where you make the edits to the files. The second one is the "Staging", which is a holding place until you want to log the changes into the history book. The third one is the "Commit", which occurs when the changes have become part of the history book.

Let's assume you want to remove the above index file from staging, as you don't feel comfortable to commit it. You can do it by typing:

$ git restore --staged index.htm

Once you hit the Enter key, the file is now moved from the staging environment back to the Working environment.

Bypass Staging and Do Commit

If you want to commit the index file, you again have to first stage it and then commit it. However, you can also skip staging and directly commit the file. To do that, write the below command:

$ git commit -a -m "index file text updated"

This way, you have committed the changes made to the index file without involving the staging phase.

Delete/Remove or Restore Files

You can delete files directly from the file explorer or you can also use Git to do the deleting. For example, let's say if you want to delete the "secret recipe.htm" file, the command will be:

$ git rm "secret recipe.htm"

This way, the file will be deleted. However, if you want to restore it, you can do it by writing the below command:

$ git restore "secret recipe.htm"

So, this is one of the main benefits of working with Git, as it allows you to access back the files if they are previously committed.

Rename Files

Just like deleting, you can rename the files directly in File Explorer or do it with Git. Let's see how to do it with Git by changing the name of the "KCC Logo.png" file to "Company Logo.png". The command will be:

$ git mv "KCC Logo.png" "Company Logo.png"

You can also add this renaming change in the history book by typing:

$ git commit -m "logo nammed changed"

View Commit History

Until now, we have performed many edits and committed them to the history book. So, let's now see how to view the commit history. Follow the below command to do it:

$ git log

Once you hit Enter, you will see all the changes you did along with the comments you added for them. Moreover, you can notice that every commit has a unique identifier and also mentions the author's details. You can also view the brief version of it by typing:

$ git log --oneline

This way, it will show a brief version of the commit history.

Amend Commit

You can also change the comment texts in the commit. For example, let's change the comment "logo nammed changed" to "logo named changed". The command will be:

$ git commit -m "logo named changed" –amend

This way, you can easily amend the commit.

View Changes in Commit

You can also view all the changes you did in the commits. Here's the command for it:

$ git log -p

From there, you can see all the changes in commits. You can exit from this view by pressing the "Q" key.

The "git log" command offers many advanced capabilities. You can look for specific text, changes before/after a specific date, and many more. You can learn all the capabilities of git log by typing "$ git help log".

Reset to Previous Commit

You can also reset to the previous commit quickly. Follow the below command to do it:

$ git log --online

It will show you a brief version of all the commits. From there, note down the yellow-colored reference value next to the commit you want to reset. Afterward, type:

$ git reset [enter reference value]

This way, the targeted commit will reset to the previous version.

Rebase Git Repository

You also have the capability to modify what you see in the history book and the order in which the commits show up. Type the below command:

$ git rebase -i --root

It will open the editor that allows you to modify your history book in different ways. So, you can explore the options to experience this out.

Branches

If you remember, at the start of this tutorial we created the branch named "Main". Till now, we have done all the editing in that branch. But you can create multiple branches as well. If you create another branch, it will be identical to the main branch. It is helpful in cases when you are working on a new feature or fixing a bug. So, you can create a separate branch, work on the feature/bug, and stay assured that your main branch isn't affected by your changes. This approach is highly popular in software development.

Now let's discuss how to create additional branches. To create an additional branch, write the below command:

$ git branch submain

Here "submain" is the name of the new branch. This way, you have created a new branch. However, you are still in the main branch. To shift to the new branch, type:

$ git switch submain

So, now you are in the new branch. Moreover, now the folder where you have downloaded the sample files is for the "submain" branch. What this means is that all the changes you do now are linked to that branch not the main branch. So, let's do some changes to the index file.

Open Notepad and then open the "index.htm" file. Do some changes in the text and save the file. Go back to the Git Bash and type:

$ git status

It will show you that the index file is modified. You can also commit this change to this branch by typing:

$ git commit -a -m "did some changes in index file"

This way, the changes in the index file are committed to the submain branch. Now let's switch back to the main branch by typing:

$ git switch main

Now the folder will again show the files of the main branch. So, if you open the index file, you won't see the changes you did recently.

Merge Branches

Now that we have two branches and we did some changes in the index file in the submain branch, let's now bring those changes to the main branch. To do that, write the below command:

$ git merge -m "branches getting merged" submain

This way, the changes of the submain branch are now merged into the main branch and you can check it by opening the index file.

Delete a Branch

Since the purpose of submain branch is over now, let's now delete that branch by writing the below command:

$ git branch -d submain

This way, the branch is deleted instantly.

Merge Conflicts

Above we have discussed the simple creation and merge of two branches. But sometimes the situation can be a bit complex. For example, the main branch might also have changed now. This will cause the merge conflict. Let's discuss it in more detail.

First, let's create and switch to the new branch. The one-line command will be:

$ git switch -c thirdbranch

This way, not just you are creating the branch but also switching to it. Now do some edits in the index file just like you did above and save those changes. Once done, commit those changes by typing:

$ git commit -a -m "new changes in the index file of thirdbranch"

Now let's switch back to the main branch by typing:

$ git switch main

Once you are in the main branch, open the index file, do some changes, and save those changes. Now commit these changes to the main branch by typing:

$ git commit -a -m "new changes in the index file of main branch"

Now let's try to merge the changes made in the thirdbranch to the main branch. So, the command will be:

$ git merge thirdbranch

Once you hit the Enter key, you will see it shows an error message of a merge conflict. You will also notice that you are currently in the "main|MERG" branch. So, open the index file now. There you will see something like "<<<<HEAD" and ">>>>thirdbranch".

The content beneath "<<<<HEAD" is the current content of the main branch, while the content above ">>>>thirdbranch" is the content of the thirdbranch. So, you can review both contents, keep one of them, and delete the other. Once done, save the file.

Go back to Git Bash and commit the changes as:

$ git commit -a -m "conflict resolved"

This way, the conflict is resolved and the changes are merged.

GitHub

Until now we have discussed what is Git and how to use it. For that, we were using the Git repository that was hosted on our local computer. But you can also host the Git repository in the cloud and collaborate with others. This is where GitHub comes into action.

GitHub is the most popular cloud repository, which also allows you to set up projects, track issues, assign features/bugs to different people, and do a lot more. In simple words, it is a website for collaborative coding. Let's now discuss how you can use GitHub:

Create GitHub Account

You need to start by creating an account on GitHub. So, go to the GitHub website, click "Sign Up", and complete the account creation process.

Create a New Cloud Repository

Once you have created the account, you can now set up a new cloud repository in GitHub. You may be wondering why set up a cloud repository when you are doing good with the local repository? Well, if something happens to your computer or if you want to collaborate with others, then the local repository fails to assist you. So, the cloud repository seems beneficial in such cases.

To create a cloud repository, click the "New" button located on the left. In the next screen, assign the repository name. Beneath it, you can set it as a Public (anyone can contribute) or Private (only authorized people can contribute) repository. Similarly, you can also add a README and .gitignore file. Once you are done with all settings, click the "Create repository" button.

Transfer Local Repository to GitHub

Once the cloud repository is created, you will see a guide that explains some of the commands for main operations. Since you already have a local repository, let's transfer it to the cloud. So, copy the first command written beneath "push an existing repository". Afterward, open Git Bash, past the command, and hit the Enter key. In that command, the "remote" means that you are setting up a remote connection with GitHub and the name of the remote connection is "origin".

The next command you have to write is:

$ git branch -M main

This command will send the target branch to main. Afterward, the last command is:

$ git push -u origin main

This command will push the local repository to the cloud. Now go back to your GitHub account and there you will see all the files of the local repository. You will notice that it shows only one branch. But if you remember, you also created another branch called "thirdbranch". So, to transfer all branches to the cloud, write the below command:

$ git push --all

This way, now you have all the branches in GitHub.

GitHub and Files

GitHub makes it a lot easier to edit files and view previous changes. If you look into your GitHub account and the files section, you can click any file to open it within GitHub. Moreover, it also shows the last changes you did to the files. If you want to edit the file, you can open it and then click the pencil-shaped icon to make the edit. Afterward, you can commit it right away in the same window.

You can also change your repository settings by clicking the settings icon located on the right side. Inside settings, you can add a description and other details you want.

Issues Tab

In GitHub, you will see a lot of tabs on the top. Let's click the "Issues" tab and see that is about.

Issues is the place where you and other contributors can request new features, point-out bugs, and request other things. You can create an issue by clicking the "New issue" button located on the right side. Afterward, add the details, assign the issue to someone if you want, add a label, and fill out other details. Once done, click "Submit new issue". Once the issue is posted, others can review it, leave a comment, or help in fixing it.

Pull Requests

If you make changes to a file in GitHub, you get an option to commit those changes. You can access this option at the end of the editing page. There you will notice that you get two options. You can commit directly to the main branch or you can create a new branch and start a pull request. The pull request will mean that the change must be reviewed by someone before it is committed to the main branch.

When you create a pull request, it appears in a separate pull request tab. You can also link the pull request with the issue it is resolving. To do that, open the pull request, click "Developer" from the bottom right side, and select the issue it is meant to resolve.

Other Tabs

Other than the Issues and Pull Requests tab, you will see a lot of other tabs on GitHub. The brief details of all those tabs are as follows:

  • Actions: This is where you can build, test, and deploy your code.
  • Projects: This is a project management view to visualize all your issues, pull requests, etc.
  • Wiki: This is a place to document your code.
  • Security: This is where you can set up security policies.
  • Insights: This is where you get details on who is contributing, active issues, etc.
  • Settings: This is where you can set up different settings for your project.

Create Releases

If you go to the main tab of your GitHub account, you will see the option "Releases" on the right. So, if you have something to release, like the first version of your code, you can release it through this option.

Fetch and Pull

You can also get changes you did in the cloud repository back to your local repository. To do that, write the below commands:

$ git fetch

This command will download all the history of changes made in the cloud repository. Afterward, type:

$ git merge

This way, the changes are now merged into the local repository.

Alternatively, you can type "$ git pull" to do the same job but with a single command.

Wrapping Up

Git and GitHub are widely used in the software development world for collaborative development with effective version control management. As of today, GitHub has over 200 million repositories and over 83 million developers. So, you can imagine the global use of Git and GitHub. The above tutorial is a first step into the world of Git and GitHub. It is meant to clear your basic concepts around Git and help you get familiar with basic commands. So, try out the above commands yourself and gradually uplift your skills with more advanced operations with Git.

If you need more assistance with your company's IT infrastructure, then we at Sun IT Solutions offer the best Toronto IT services. You can get all managed IT solutions under one roof including IT consulting, network support, remote IT support, business continuity planning, cybersecurity, backup and disaster recovery, and many more. The years of market experience have led us to offer the best-in-class outsourced IT services in Toronto and elsewhere.