Git: a guide for beginners

SHARE

Using Git is a prerequisite for anyone working as a developer. However, for a novice specialist, this may seem difficult. Git has a lot of features that you have to get acquainted with in your work. To make it easier to get acquainted with Git, below we have collected all the most necessary commands that will for starters come in handy. In this article, we will tell you how to get started with Git and keep your mind sane.

CONTENTS

  • WHAT IS VERSION CONTROL SYSTEM
  • WHAT IS GIT
  • INSTALLING GIT
  • GIT SETUP
  • CREATING A NEW REPOSITORY
  • PREPARING FILES
  • WORK WITH BRANCHES
  • MEMO ON COMMANDS
  • USEFUL MATERIALS

WHAT IS VERSION CONTROL SYSTEM

Version Control System (VCS) is software for facilitating work with changing information. The version control system allows you to store multiple versions, keep track of changes, revert to a previous state in case of an error, and much more.

The simplest example is Google Docs which allows you to view changes to a document.

Main goals:

  • tracking the history of changes
  • rollback of changes
  • multi-user use

There are several types of version control systems. Globally, they are all divided into:

  • local
  • centralized
  • distributed

LOCAL VCS

RCS, SCCS

This software runs within the limits of the local machine. Such systems store sets of patches (differences between files) on disk in a special format. Using it can recreate the state of each file at a given point in time.

SCCS (Source Code Control System) is considered one of the first successful version control systems. It was developed in 1972.

These systems are still used in some operating systems and other software but they have a number of significant drawbacks. You can work with such systems only within a single file and have limited functionality.

Local VCS

CENTRALIZED VCS

CVS, SourceSafe, Subversion

Such systems use a single server containing all versions of files, and a number of clients that receive files from this centralized repository. They were popular enough for a long time and were the standard-bearers of VCS just 15 years ago. The main drawback is that the entire history is stored on the central server, and in case of problems with it, the history is almost impossible to recover, and it will be lost.

Centralized VCS

DISTRIBUTED VCS

Git, Mercurial, Bazaar

Unlike centralized ones, they store information not only on a central server. In such systems, clients don’t just download a snapshot of all files (the state of the files at a particular point in time) — they copy the entire repository. These are the most widely used version control systems today.

Distributed VCS

WHAT IS GIT

Git is a distributed version control system.

It was developed in 2005 by Linus Torvalds, better known as the author of the Linux kernel.

It is the absolute leader in popularity among modern VCS.

GIT IN BRIEF

Each saved project change is a commit. Each commit has a comment, that is, an explanation of what has been changed. From such commits, a branch is assembled. A branch is a history of changes with its own name. A repository can have multiple branches that are created from or merged into other branches.

BASIC TERMS

Repository is a file system directory that contains: configuration files, log files of operations performed on the repository, file location index and storage containing the controlled files themselves.

Local repository – a repository located on the developer’s local computer in a directory. The development and fixing of changes take place in there and then they are sent to the remote repository.

Remote repository – a repository located on a remote server. This is a shared repository where all changes come in and all updates are pulled from.

Commit is the committed state of the repository. A commit has metadata: identifier, author name, creation date, and the comment.

A branch is a separate history of changes (commits) within a single repository. Git encourages the creation of separate branches for each task in progress. Thanks to it, it is possible to achieve effective parallel work – each developer can work with his own history and not be afraid that changes in someone else’s code will affect his work.

Merge is a merge of changes from any branch of the repository with any branch of the same repository.

Clone – downloading a repository from a remote server to a local computer to a specific directory for further work with this directory as a repository.

Pull – getting the latest changes from a remote repository server.

Push – sending all uncommitted commits to a remote repository server.

INSTALLING GIT

To get started with Git, you need to install it on your machine. It’s pretty simple:

  • Windows — download the .exe file and run it. The bash console is enough to work.
  • Linux — usually Git is already installed and available in distributions. To check if Git is available, open a terminal and type: git –version. On Ubuntu, Git can be installed via the terminal. To do this, type sudo apt-get install git
  • MacOS — also check if Git is installed. If you have XCode, then you definitely have Git. If not, the easiest way is to download the latest version from here

GIT SETUP 

Now that Git is installed on your machine, you need to set it up. There are many options to experiment with. The git has a user setting which can serve a focal point for the work to start. When a commit is made, git collects this information for the Author field.

To set up a username and email address for all projects, you need to type:

 git config –global user.name “John Doe”
git config –global user.email john.doe@gmail.com

CREATING A NEW REPOSITORY

Git stores all files directly in the project folder. To create a new repository, you need to go to the project folder and run the git init command. This will create a .git folder in the same directory where the console is located.

.git is the folder that contains all the information about the repository. This is a system folder that you no longer need to access. Each file has a state. The states in the git need to be understood and memorized:

  • a file that has just been created and not added to the repository – untracked;
  • modified file in the repository – modified;
  • out of the modified files we select only those that we need, and they get into the staged state;
  • a commit is created from the staged state and transferred to the git repository. After all this, the staged state is empty, and modified can contain something.

To check out the status of a file, use the git status command.

Annotation from the official documentation

PREPARING FILES

Before you add files to local storage, you need to prepare them. To do this, there is a command git add or git add -A. You can add all or just several files.

To add tracking of new files, you must use the git add command to add multiple files by name.

To delete files, you must use the git rm <filename> <filename> command to remove multiple files by name.

After all the changes have been made, you can commit them. To do this, enter the command git commit -m “Comment to commit”.

If you’ve made changes but want to quickly revert them, use the git reset command.

To send a local copy of the repository, on which all actions were performed, to the server, there is a git push command.

WORK WITH BRANCHES

The git checkout -b branch-name  command will create a branch with the specified name and automatically switch to it.

Once a branch has been created, it can be pushed to the server using the git push origin branch-name.

You can switch to any local branch with the git checkout branch-name command.

TRASH

Various unnecessary, untracked files and other garbage may remain in the repository after work. To get rid of this, there is a command git clean -f -d.

MEMO ON COMMANDS 

There are many commands for working with Git. Here we will list some commands that allow you to perform basic actions with Git. In practice, work with repositories is carried out not through the console, but using visual editors, or tools that are built into software development tools.

git init — creating an empty repository.

git clone <url> <path> — cloning a remote repository.

git status — view current changes.

git add — add file to a local repository.

git commit — freeze commit in local repository.

git commit -m “Description” — a description of the commit in the local repository. This is a very important point, as the information should concisely describe the changes made.

git push — pushing changes to a remote repository.

git pull — getting changes from a remote repository

git log — view history of changes.

LIST OF COMMANDS TO WORK WITH BRANCHES

git branch — view the list of branches. The current branch will be marked with (*).

git branch <name> — create a new branch named <name>.

git checkout <name> — switch to branch <name>.

git checkout -b <name> — create a new branch named <name> and switch to it.

git push <remote> <name> — push branch to remote repository.

git branch -d <name> — delete branch named <name> in local repository.

git push <remote> –delete <name> — delete the branch named <name> in the remote repository.

git merge <name> — merge current branch into branch <name>.

USEFUL MATERIALS

A memo on working with GIT problems

Official GIT guide