CodeGym /Java Course /Frontend SELF EN /Working with git branches in IntelliJ IDEA

Working with git branches in IntelliJ IDEA

Frontend SELF EN
Level 20 , Lesson 3
Available

11.1 Git Branches

Working with branches in Git is one of the key aspects of version control that lets you run multiple lines of development concurrently in one repository. Branching makes Git a powerful tool for collaboration, experimentation, and managing different versions of a project.

Git Branches

You can think of branches in git like folders where git copies your project. You have the main folder of your project in your local repositorymaster, and git can create copies of this folder to experiment with without breaking the main, well-functioning code. Such copy-folders are called branches.

Branches are alternative versions of code. Say you want to redo something in a big project, an experiment you're not totally sure about. How would you do it without git?

You could copy the project to a new folder and try changing everything there. If you like the result, you can copy it into the main folder. If not, forget about it, or just delete it.

Or let's take a real-life example. Like writing a book:

  1. You have a manuscript of the book (main branch).
  2. You want to try changing the ending (creating a new branch).
  3. You write the new ending in a separate document (working in the new branch).
  4. If the new ending is better, you replace the old one in the manuscript (merging branches).
  5. Delete the separate document with the new ending (deleting the branch).

11.2 Creating Branches

Creating a branch in IntelliJ IDEA is super easy:

Creating Branches

Enter the branch name:

Creating Branches

IntelliJ IDEA immediately displays your current branch's name at the top of the menu:

Creating Branches

What was there before?

It was the name of your first and main branch — master.

Now it shows test, meaning Git (under IntelliJ IDEA's guidance) not only created a new branch but also switched to it right away.

Let's add some code to the file main.html in the current branch (test) and commit it:

Creating Branches

11.3 Switching Between Branches

Step 1. Choose a branch.

Now let's switch back to our old branch. Click on the top menu, and what do we see?

Switching Between Branches

No worries — it's simple:

Local is the list of branches in your local git repository. There are two:

  • test
  • master

Remote is your remote repository that's on the GitHub server. We've been pushing your changes there, but the new branch isn't, which makes sense. The remote repository is named origin, and it only contains the master branch.

Recent is just a list of the last branches you worked with: IntelliJ IDEA adds this for convenience and speed.

The name origin/master next to the local repository means it's synced with the remote repository, where changes will be pushed.

Step 2. Load the branch code into the current folder.

Switching Between Branches

Step 3. Verify.

I see the branch "master" and the old code:

Checking

11.4 Merging Branches

Let's now try merging the code from our two branches.

Step 1. First, let's add another file to our project — index.html and write some code in it:

  • Create the file index.html
  • Write code in it <h1>Hello</h1>
  • Commit the file

Here's how my two files look in the master branch:

Merging Branches

Step 2. Merging branches.

We'll pour into our current branch (master) the changes that were made in the test branch.

To do this, we'll also use the top menu and the command "Merge ‘test’ into ‘master’":

Merging Branches

Step 3. Check the result.

Merging Branches

Check:

  • The branch master still shows up top
  • We have 2 files: index.html and main.html
  • The file main.html contains code that was added in the test branch

11.5 Merge Conflicts

Sometimes merging branches causes conflicts.

If you make changes to one file in different branches and try to merge them, a conflict may occur.

Text file conflict

Git is a very smart system — it understands file types. If you make changes in different parts of the files it considers text, it will just transfer changes from one file to another in the right place (just as a human would do).

Binary file conflict

But if you tweak an image or document somewhere, Git won't try to merge their parts into one: it will just ask you which version of the file you want to keep in the current branch.

Manual conflict resolution

If you make changes in the same part of a text file, Git won't be able to merge different versions correctly and will ask you to do it:

Merge Conflicts

This is what it might look like:

Merge Conflicts

What you see here:

  • On the left — content of the main.html file in the master branch
  • On the right — content of the main.html file in the test branch
  • In the middle IntelliJ IDEA suggests you write the final version of the code (you can click the ">>" and "<<" buttons to automatically insert changes from any file)

I accepted the master option and wrote the code by hand. Here's what I got:

Merge Conflicts

11.6 Change History

Another useful and interesting thing. You can view the change history for any file by clicking the Show History button. There are two places it might be located. Find it.

Here's what change history looks like for me for the file main.html:

Merge Conflicts

Explanations:

  • On the left, you see the change history of a specific file:
    • The newer the change, the higher it is; the older – the lower
    • The branch merging history is also displayed here
  • On the right — the changes made in a specific commit
1
Опрос
Decorative Elements,  20 уровень,  3 лекция
недоступен
Decorative Elements
Decorative Elements
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION