11.1 Git Branches
Working with branches (branches
) in Git is one of
the key aspects of version control, allowing parallel
development lines in a single repository. Branching
makes Git a powerful tool for collaboration, experimentation, and
managing various project versions.
You can think of branches in Git as folders where Git copies your project. You have the main folder of your project in your local repository
— master
. Git can create copies of this folder to let you experiment without risking the main, well-working code. These copied folders are called branches — branches
.
Branches are alternative versions of the code. Suppose you want to make some major changes to a big project or run an experiment you're not fully confident about. How would you go about it without Git?
You might copy the project to a new folder and try making all the changes there. If you like the result, you could copy it to the main folder. If not, you could just forget about it or delete it altogether.
Let's consider a real-life example, like writing a book:
- You have a manuscript (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).
- You 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 display your current branch name at the top of the menu:
What was there before?
Previously, it displayed the name of your first and main branch —
master
.
Now it shows test
, which means that Git (under the guidance
of PyCharm) not only created a new branch but also switched to it
immediately.
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. Choose a branch.
Now let's switch back to our old branch. Click on the top menu, and what do we see there?
Don't get confused — it's super simple:
Local
— this is a list of branches in your local
git repository; there are two:
test
master
Remote
— this is your remote repository, hosted
on the GitHub server. We sent your changes there, but there's no new
branch, which makes sense. The remote repository is named origin,
and it contains only the master
branch.
Recent
— this is a list of names of the most recent branches you've worked with. This is added in PyCharm for convenience and speed.
The name origin/master
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 to.
Step 2. — Download branch code to the current folder.
Step 3. Check:.
My branch (master)
shows up and the old code.
11.4 Merging Branches
Let's now try to merge the code from our two branches.
Step 1. First, add another file to our project — init.py and write some code in it.
- Create a file init.py
- Write code print("Hello") in it
- Commit the file.
Here's what my two files look like in the master
branch:
Step 2. Merging branches.
Merge the changes made
in the test
branch into our current branch (master)
.
For this, we'll also use the top menu and the command “Merge ‘test’
into ‘master’”
.
Step 3. Check the result:.
Here's what we have:
- The branch
master
still displays at the top - We have two files: init.py and main.py
-
The main.py file contains code that was added in the
test
branch.
11.5 Merge Conflicts
Sometimes merge conflicts happen.
If you make changes to the same file in different branches and try to merge them, a conflict can occur.
Text file conflict
Git is a very smart system, it understands file types. If you make changes to different parts of files that it considers text files, it will just move the changes from one file to another in the right place, like a human would.
Binary file conflict
But if you make changes to an image or document, Git will not try to merge their parts into one file. It will just ask you to choose which version of the file you want to keep in the current branch.
Manual conflict resolution:
If you make changes to the same place in a text file, Git can’t merge the different versions properly and will ask you to do it yourself.
Here's how it might look:
Here's what you see:
- Left content of the main.py file in the master branch
- Right content of the main.py file in the
test
branch -
In the middle, PyCharm suggests you write the final version of the code
(you can click on the buttons
“>>”
and“<<”
to automatically insert changes from one of the files).
I accepted the master
option and wrote the code manually. Here's what I
got.
11.6 Change History
And another neat thing is that you can check the history
of changes for any file by clicking on the Show
History
button. There are two places it can be — find them.
Here's what my change history looks like for the main.py file:
Explanation:
-
On the left, you see the change history of a specific file:
- Newer changes are higher up; older ones are lower.
- It also shows the branch merge history.
- On the right are the changes that were made in a specific commit.
GO TO FULL VERSION