CodeGym /Courses /JAVA 25 SELF /Introduction to Git: why version control matters

Introduction to Git: why version control matters

JAVA 25 SELF
Level 25 , 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 Java project. Everything is going well until the moment for “experiments” arrives. You decide to change something but are afraid of breaking the working version. What do you do? Of course—copy the project!

As a result, your drive ends up with masterpieces like these:

MyProject/
├── Main.java
├── Main_backup.java
├── Main_final.java
├── Main_final2.java
├── Main_really_final.java
├── Main_really_really_final.java

Sound familiar? Now imagine that a friend joins the project. They also like copying files—just in their own way. How do you know which version is the newest and working one? How do you find out who changed what? How do you roll everything back if the experiment fails?

Without version control:

  • It’s easy to lose or mix up working code.
  • It’s impossible to “roll back” to an older version.
  • It’s hard to work in pairs or small teams.
  • Chaos and fear of experimenting.

These are exactly the problems that version control systems solve—such as Git.

2. Why developers need Git

Git is a powerful version control system used to track 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.

Key Git concepts:

Repository

A repository (or “repo”) is where the entire project history is stored, including all changes and file versions.

Commits

commit is a saved state of the project. Each commit in Git contains information about what changes were made to the project, by whom, and when. Commits form the history of the project 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

branch is an independent line of development. By default, Git creates the main branch. You can create new branches for 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 main main branch for parallel development. After the work is done, changes from develop are merged back into main.

3. Core Git commands (what’s under the hood)

Below is a list of the core commands for working with Git in the terminal. It’s important to understand which commands underpin all operations. However, we will stick to a GUI approach and learn to perform all these actions using IntelliJ IDEA’s friendly graphical interface. Think of these commands as what’s happening “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 index (staging area) for the next commit.
git commit Records the staged changes in the repository.
git push Pushes changes from the local repository to the remote.
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 into the current branch.

These commands are the fundamental tools in Git, allowing you to manage code changes, branches, and merges in projects of any size.

    
        sequenceDiagram
        participant Working directory
        participant Staging area (Staging)
        participant Local repository
        participant Remote repository

        Working directory ->> Staging area (Staging): git add (Stage)
        Staging area (Staging) ->> Local repository: git commit (Save locally)
        Local repository ->> Remote repository: git push (Push to server)
        Remote repository ->> Working directory: git pull (Pull updates)
    

4. The three places your code lives

When you use a version control system for your code, it will, roughly speaking, be stored in three places:

1. Remote repository

This is the central place to store your code, typically hosted on services such as GitHub, GitLab, or Bitbucket. They provide centralized code storage and form the basis for collaboration. The remote repository serves as an integration point for automation processes such as building, testing, and deploying applications.

2. Local repository

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

3. Working directory

The working directory on your computer contains the current project files you are actively working on. This is where you can view and modify files, add new functionality, or fix bugs.

Together, these components provide a powerful infrastructure for source code management, enabling developers to manage project history and collaborate effectively.

5. GitHub is your portfolio

GitHub is a 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, control and track code changes, and collaborate with other developers. For a modern developer, a GitHub profile is an important part of a portfolio that can be shown to potential employers.

6. Creating your first repository on GitHub

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

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

Step 3. Set the repository parameters:

  1. Repository name: pick a meaningful name.
  2. Public or private: for learning projects, choose “Public” so others can see it.
  3. Add a README file: be sure to check this option. The README is the “face” of your project.
  4. Add .gitignore: open the dropdown and choose a template for your language.
  5. Choose a license: optional.
  6. Click Create repository.

Step 4. Congratulations—your first remote repository is created!

7. Installing and configuring Git

While you can learn Git basics through console commands (as shown in the video), in everyday work 99 % of developers use the convenient tools built into their IDE. Our goal is to teach you to work the way professionals do.

The Git interface across all modern JetBrains IDEs—be it IntelliJ IDEA for Java/Kotlin, Rider for C#, or PyCharm for Python—is almost identical. This means once you learn Git in one IDE, you can easily apply those skills in any other. Therefore, we will use IntelliJ IDEA as a universal example. Everything you see here will look and work exactly the same in your favorite IDE.

To work with Git on your computer, you must first install it. If you use IntelliJ IDEA, it will likely prompt you to install Git automatically if it’s not found on your system. We recommend accepting this prompt—it’s the simplest path.

Close the current project by selecting File > Close Project, and click Clone Repository.

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

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, switched to the more neutral term—main.

This is important to know because in some older articles or projects you may still encounter the master branch. In our lectures and in modern projects, the default branch will always be main.

Learn more about the transition to main at the following links:

1
Task
JAVA 25 SELF, level 25, lesson 0
Locked
Virtual Zootopia: Meeting the Voices 🧬
Virtual Zootopia: Meeting the Voices 🧬
1
Task
JAVA 25 SELF, level 25, lesson 0
Locked
Pet Personalization: Assigning Names with super 🔼
Pet Personalization: Assigning Names with super 🔼
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION