JavaRush /Courses /Go SELF /Introduction to Git: Why Version Control is Needed

Introduction to Git: Why Version Control is Needed

Go SELF
Level 32, Lesson 0
Available

1. The problem without version control: why simply copying files is a bad idea

Let's start with a real-life situation. Imagine you are working on your Go project. Everything is going well until the moment of "experiments" arrives. You decide to change something, but you're afraid to break the working version. What do you do? Copy the project, of course!

As a result, such masterpieces appear on your disk:

MyProject/
├── main.go
├── main_backup.go
├── main_final.go
├── main_final2.go
├── main_really_final.go
├── main_really_really_final.go

Sounds familiar? Now imagine that a friend joins the project. They also like to copy files — only in their own way. How do you understand where the freshest and working version is? How do you know who changed what? How do you revert everything if an experiment failed?

Without version control:

  • It is easy to lose or mix up working code.
  • It is impossible to "roll back" to an older version.
  • It is difficult for two or three people to work together.
  • Chaos and fear of experiments.

These are exactly the problems solved by version control systems — such as Git.

Video

Watch the lecture from a JavaRush University mentor. The video will give you a strong foundation in working with Git via the terminal — this will help you understand the very mechanics of the processes hidden behind the IDE buttons.

The video consists of two parts:

  • First, you will learn the basics of working with Git through the terminal. This fundamental approach will help you understand what happens "under the hood" of the system.
  • Then you will move on to practice in IntelliJ IDEA, where you will learn to perform the same operations using a convenient graphical interface, just as professionals do daily.

In the video, the mentor uses examples in Java. Do not let this confuse you! Git is a universal technology, and the principles of working with versions are exactly the same for Go, Java, or Python. Focus on the Git commands and the logic of their work, not on the project code.

2. Why does a developer need Git?

Git is a powerful version control system used for tracking changes in source code during software development. It allows developers to save different versions of files and coordinate the work of multiple people on a shared project.

Core Git concepts:

Repository

A repository, or "repo", is a place where the entire history of the project is stored, including all changes and versions of files.

Commits

A commit is a saved state of a project. Each commit in Git contains information about what changes were made to the project, by whom, and when. Commits form the project's history and allow you to return to any previous version.

        gitGraph
        commit id: "1"
        commit id: "2"
        commit id: "3"
        commit id: "4"
        commit id: "5"
        commit id: "6"
    

Each commit is a "snapshot" of the project that follows the previous one, forming a sequential history of changes.

Branches

A branch is an independent line of development. By default, Git creates a main branch. You can create new branches to develop new features or fixes, and then merge them back into the main branch.

        gitGraph
        commit id: "1"
        commit id: "2"
        branch develop
        commit id: "3"
        commit id: "4"
        commit id: "5"
        checkout main
        commit id: "6"
        commit id: "7"
        merge develop
        commit id: "8"
        commit id: "9"
    

The develop branch "branches off" from the base main branch for parallel development. After the work is completed, changes from develop are merged back into main.

3. Basic Git commands (under the hood)

Below is a list of the basic commands for working with Git via the terminal. It is important to understand which commands underlie all operations. However, we will stick to the GUI approach and learn how to perform all these actions using the convenient graphical interface of IntelliJ IDEA. Look at these commands as what happens "under the hood".

Command Description
git init Initializes a new Git repository in the current directory.
git clone Clones a repository from a URL into a new directory.
git add Adds files to the staging area for the next commit.
git commit Records the staged changes into the repository.
git push Sends changes from the local repository to a remote one.
git pull Updates the current branch with the latest version from the remote repository.
git branch Lists, creates, or deletes branches.
git merge Merges the specified branch's history into the current branch.

These commands represent the core tools for working in Git, allowing you to manage code changes, branches, and merges in projects of any size.

        sequenceDiagram
        participant Working Directory
        participant Staging Area
        participant Local Repository
        participant Remote Repository

        Working Directory ->> Staging Area: git add (Stage)
        Staging Area ->> Local Repository: git commit (Save locally)
        Local Repository ->> Remote Repository: git push (Send to server)
        Remote Repository ->> Working Directory: git pull (Download updates)
    

4. The Four States (storage locations) of Code

When you use a version control system for your code, it logically passes through four states:

1. Working Directory

The working directory on your computer contains the actual project files you are currently working on. This is where you can view and modify files, write code, add new features, or fix bugs.

2. Staging Area

This is the intermediate zone between your working directory and the local repository. In terms of the IDE graphical user interface (GUI), these are the very checkboxes you check next to modified files before clicking the Commit button. You choose exactly which files and changes are ready to go into the next "snapshot" of history.

3. Local Repository

The local repository is your personal copy of the code's history stored on your computer. In this repository, you can perform all Git operations (commits, branching, merging) without needing an internet connection.

4. Remote Repository

This is a centralized place for storing your code, usually hosted on services like GitHub, GitLab, or Bitbucket. They provide reliable code storage on servers and are the foundation for collaborative team work.

5. GitHub — Your Portfolio

It is important to clearly distinguish between two concepts: Git and GitHub. Git is a program that runs locally on your computer (even without the internet) and tracks file versions. And GitHub is a cloud service (website) where remote copies of your Git repositories are stored.

GitHub is the leading web platform for hosting source code, using the Git version control system. Founded in 2008, it quickly became one of the key tools for developers worldwide.

GitHub allows users to create repositories to manage projects, track and control code changes, and collaborate with other developers. For a modern developer, a GitHub profile is an important part of a portfolio to show potential employers.

6. Creating Your First Repository on GitHub

Creating a new repository

Step 1. Go to https://github.com and sign up.

Step 2. Click on the New repository button to create a new repository.

New repository button

Step 3. Set the parameters for the repository:

  1. Repository name: come up with a meaningful name.
  2. Public or private: for educational projects it is better to choose "Public" so others can see it.
  3. Add a README file: be sure to check this box. A README is the "face" of your project.
  4. Add .gitignore: click the dropdown and select the Go template. This is critical: the Go compiler creates executable binaries, and testing tools generate cache and code coverage files that shouldn't clutter your repo. The template will help Git automatically ignore them.
  5. Choose a license: you can skip this.
  6. Click Create repository.

Step 4. Congratulations, your first remote repository has been created!

Finished repository on GitHub

7. Installing and Configuring Git

Although the basics of Git can be learned via console commands (as shown in the video), in daily work 99% of developers use the convenient tools built into the development environment. Our goal is to teach you to work just like the professionals do.

The interface for working with Git in all modern JetBrains IDEs — whether it's GoLand for Go, IntelliJ IDEA for Java/Kotlin, or PyCharm for Python — is practically identical. This means that once you learn how to work with Git in one environment, you can easily apply these skills in any other.

In this course, we will use screenshots from IntelliJ IDEA to demonstrate examples. Do not be intimidated: in your JetBrains IDE (whether GoLand or IntelliJ IDEA with the Go plugin installed), all the buttons, menus, and panels are in exactly the same places.

To work with Git on your computer, Git must first be installed. If you are using a JetBrains IDE, it will most likely offer to install Git automatically if it's not found on your system. We recommend agreeing to this offer — it's the simplest way.

Close the current project by selecting File | Close Project from the top menu. On the Welcome Screen, click the Clone Repository button (in some interface versions, it might be called Get from VCS).

IntelliJ IDEA Welcome Screen

If you want to install it manually, use the official website: https://git-scm.com/downloads.

An Important Detail

When you make your first commit in the IDE, IntelliJ IDEA will ask you to identify yourself — to provide a username and Email. It is highly important to specify the exact Email you used to register your GitHub account.

The fact is that GitHub links the authorship of your commits to your profile specifically by this Email address. If the addresses match, your avatar will appear next to your commits, and those coveted "green squares" of activity, which employers love to evaluate, will start filling up on your profile page.

8. A Bit of History: main vs master

Previously, the default branch in Git was called master. However, in 2020, the developer community and major platforms, including GitHub, transitioned to using a more neutral term — main.

It is important to know this, as in some older articles or projects you might still encounter the mention of the master branch. In our lectures and in modern projects, the main branch will always be main.

You can learn more about the transition to main at the following links:

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION