11.1 Git Branches
Working with branches in Git is one of the core aspects of version control, enabling you to operate multiple lines of development side-by-side in a single repository. Branching makes Git a powerful tool for collaboration, experimentation, and managing different versions of a project.
You can think of branches in Git as folders where Git copies your project. You’ve got the main folder of your project in your local repository — main. Git can make copies of this folder so you can experiment without the risk of messing up your main, perfectly working code. These folder copies are referred to as branches — branches.
Branches are alternate versions of the code. Let’s say you want to rework something in a big project or run an experiment that you’re not 100% sure about. What would you do without Git?
You could copy the project into a new folder and try changing everything there. If the result works, you’d copy it back to the main folder. If it doesn’t work, you just forget about it or straight-up delete it.
Let’s consider a real-life example, like writing a book:
- You have the manuscript of a book (main branch).
- You decide 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 turns out better, you replace the old one in the manuscript (merging branches).
- Delete the document with the new ending (deleting the branch).
11.2 Creating Branches
Creating a branch in PyCharm is super easy.
Enter the branch name.
PyCharm will immediately show the name of your current branch at the top of the menu:
What was it before?
Earlier, it displayed the name of your first and main branch — main.
Now it shows test, which means Git (guided by PyCharm) not only created a new branch but also switched to it right away.
Let’s add some code to the main.py file in the current branch test and commit it.
11.3 Switching Between Branches
Step 1. Select a branch.
Now let's switch to our old branch. Click the top menu, and what do we see?
Don't get confused — it's all super simple:
Local — this is the list of branches in your local git repository; there are two here:
testmain
Remote — this is your remote repository stored on the GitHub server. We pushed your changes there, but the new branch isn't there, which makes sense. The remote repository is named origin, and it only has the main branch.
Recent — this is a list of the most recent branches you've worked with. This feature was added in PyCharm for convenience and speed.
The name origin/main to the right of the local repository name is the name of the remote repository it's currently synced with and where changes will be pushed.
Step 2. Load branch code into the current folder.
Step 3. Check it:.
I can see the main branch and the old code displayed.
11.4 Merging Branches
Now let’s try merging the code from our two branches.
Step 1. First, let’s add another file to our project — init.py, and write some code in it.
- Create the file
init.py - Write the code
print("Hello repo")in it - Commit the file.
Here’s what my two files look like in the main branch:
Step 2. Merging branches.
We’ll merge the changes made in the test branch into our current main branch.
To do this, we’ll use the top menu and the command Merge ‘test’ into ‘main’.
Step 3. Checking the result:.
Let’s check:
- The branch
mainis still displayed at the top - We have two files:
init.pyandmain.py - The file
main.pycontains the code that was added in thetestbranch.
11.5 Merge Conflicts
Sometimes conflicts pop up when merging branches.
If you make changes to the same file in different branches and try to combine them, you might run into a conflict.
Text file conflict
Git is a super smart system, it understands file types. So if you make changes in different areas of files it considers text, it'll just transfer the changes from one file to another in the right spots, kinda like a human would.
Binary file conflict
But if you modify something like an image or document, Git won't try to merge those parts into one file. It'll simply ask you to pick which version of the file you want to keep in the current branch.
Manually resolving conflicts:
If you edit the same spot in a text file, Git won't be able to merge versions automatically and will ask you to handle it yourself.
Here's how it could look:
What you're seeing here:
- On the left, the contents of the
main.pyfile from themainbranch - On the right, the contents of the
main.pyfile from thetestbranch - In the middle, PyCharm suggests you write the final version of code (you can hit the
>>and<<buttons to auto-add changes from one of the files).
I went with the main version and manually added some code. Here's what I ended up with.
11.6 Change History
Here's something both useful and cool: you can check the change history of any file by clicking the Show History button. There are two places where you might find it — go and locate them!
Here's what the change history looks like for my file main.py:
Explanations:
- On the left, you see the change history for a specific file:
- The newer the change, the higher it is on the list; the older — the lower.
- It also shows the branch merge history here.
- On the right — you can see the changes made in a specific commit.
GO TO FULL VERSION