Git Basics - Git Series Part 1

sohail
sohail

Table of Contents

This series will explain the purpose of git, how to clone GitHub repository, GitLab repository, or otherwise. How to view the changelog and how to revert to an older version of the repository, add and remove files, commit changes, update remote repositories, fetch the most recent versions of repo, and more. GUI front-ends will also be covered, as well as troubleshooting and how typical IDEs will handle source code files belonging to a git repo.  

What Is Git?

In a nutshell, git is a very powerful version control tool used for keeping track of changes of computer files, most often source code and websites, and is built to handle multiple users changing the same file, and the same set of files – even handles concurrency. Each commit is a change in the repository. Numerous open-source (and I suspect closed-source) projects use git every day to house their project files and keep track of changes. The Linux kernel is the most excellent example!  

Getting Started with Git

Git Clone Repository

First off, you will need to install it if you haven’t done so already. Use whatever package manager you have to install git. Once everything is complete you should get started. My first recommendation is to clone an existing repository. This could be any repository you want. For the sake of this guide, I would choose a small project on Github, Gitlabs, or elsewhere. $ git clone git://github.com/whatever/project
$ cd project

git clone repository

This will create a folder using the project name specified in the URL as the folder name. Then, it will download all of the files and changes that have been committed by any number of people. To fetch the latest changes, run ‘git pull’.  

Find Information About Each Change

Git Changelog

You can view information about every change that was ever made during the life of the repository using ‘git log’. It will open up the full changelog in a ‘less’ environment so you can scroll through what was changed in each commit. Hopefully, the repo you have chosen will have meaningful information about what each commit brought to the repo. $ git log

git log command

The information contained in the log is what the developers put in when they each ran the ‘git commit’ command. This command opens a text editor to edit the commit message, but more on that in the next guide.  

View An Older Version

Git reset command

The reason for why you want to view older revisions of a repo depends on the user. For example, the developer may be looking to reverse a change to isolate a bug that was introduced into the program. Suffice to say, the user can use ‘git reset’ to go back to an older commit.

To do this, you will need to run ‘git log’ to find the commit ID you want to parse to ‘git reset’. Once copied, paste it into your command: $ git reset –hard 444444commit0id The ‘–hard’ parameter tells git to reset the index and working tree to an older version and discard any changes since. Don’t forget, if you want to revert back to a newer or the latest commit, you will need to have its ID ready to copy from the log. Paste it in an editor for storage before going to an older version, if needed.

git reset hard

Conclusion

You’ve downloaded a git repository from GitLab, browsed its log and went to an older version of the repository. The next tutorial will demonstrate how to create a repository, prepare your environment so you can add files and commit changes, and push your repo to a different server.

Learn GitUncategorizedtutorial