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.
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 repository — master, 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:
- You have a manuscript of the book (main branch).
- You want to try changing the ending (creating a new branch).
- You write the new ending in a separate document (working in the new branch).
- If the new ending is better, you replace the old one in the manuscript (merging branches).
- Delete the separate document with the new ending (deleting the branch).
11.2 Creating Branches
Creating a branch in IntelliJ IDEA is super easy:
Enter the branch name:
IntelliJ IDEA immediately displays your current branch's name at the top of the menu:
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:
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?
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.
Step 3. Verify.
I see the branch "master" and the old code:
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:
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’":
Step 3. Check the result.
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:
This is what it might look like:
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:
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:
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
GO TO FULL VERSION