Integrating Git with PyCharm
To integrate Git with PyCharm, follow these steps. First, locate the main navigation bar and click on "VCS". From there, select "Enable Version Control Integration..." This process will link your PyCharm project with Git, enabling version control features for your codebase, such as tracking changes, committing, and collaborating with others.
You can expect another window to appear; in this window, choose "Git" and then click "OK."
If you've noticed that in the left box of your PyCharm, some file names, like "main.py", are shown in red, it means that these files are not currently being tracked by Git, the version control system. In other words, Git is unaware of these files and doesn't include them in its monitoring. To address this, you need to take action to make Git aware of these files by adding them to what's called the version control registry, also known as a repository. Essentially, Git keeps a record of all the files and folders in your project, and it needs to know about these untracked files so it can monitor and manage them effectively.
To make Git start monitoring a file, you have to perform a "commit" operation. To do this, simply click on the green checkmark icon located at the top right corner of the PyCharm window.
Once you click the green checkmark icon, it opens a new window on the side of PyCharm. In this window, you'll notice a blinking cursor at the bottom, and it's waiting for you to type something. This is where you enter what's called a "commit message". It should be short and meaningful, like a regular sentence, and it's used to describe what you've done.
In our case, we're creating a timestamp to mark the starting point of our project. This helps us keep track of changes. Typically, for the first timestamp, we use "initial commit" as the message.
To select the files you want to commit, start by expanding the "Unversioned Files" section. Here, you'll see all the files in your project directory, including the automatically generated gitignore file and your Python files like main.py. You might also notice some settings files created by PyCharm; these are typically unnecessary for version control. To focus on your project files, minimize the "commit" tab, and navigate to your project directory. This ensures you're only including the essential project-related files in your commit.
When you navigate to your project directory, right-click on the folders named "venv" and "git." In the context menu that appears, choose "Add to .gitignore" and click on it.
A prompt will appear asking if you want to create a .gitignore file. Select "create" in this prompt. As a result, you'll notice that a .gitignore file has been successfully created.
Next, you should click on "Add" to include the .gitignore file.
So, what we just did is we made a change by adding the venv folder to the .gitignore file, ensuring that Git won't track it. Similarly, we also excluded the hidden .idea folder, which contains PyCharm settings files, from version control. You can type in the name of the folder or file in the .gitingnore file as shown:
To commit your changes to PyCharm, simply click the green checkmark icon at the top right corner of the window. You'll notice that the ".gitignore" file is checked by default; this happens because we previously pressed the "add" button when creating the ".gitignore" file. PyCharm asked if you wanted to add it, and we did. To include other files in the commit, you can either manually check them one by one or use the "always add" option, which will automatically select them for you. This simplifies the process of adding multiple files to your commit.
After making your changes, click the "commit" button. A window will appear asking you to enter your name and email. Simply provide the requested information and click "OK" to continue. Please note that this window will only appear the first time and won't be asked again.
To view a file that you've previously committed in Git, follow these steps for clarity: First, go to the Git tab found at the bottom of your window. Next, double-click on the "Initial Commit." This action will reveal a list of files on the right side of that section, making it easy to access and review your committed files.
Now, suppose that we've made changes to our 'main.py' file. Initially, the code in 'main.py' looks like this:
class Coffee:
calories = 99
def __init__(self, name):
self.n = name
def showInfo(self):
print("Coffe: ", self.n)
drink = Coffee("Caramel Macchiato")
drink.showInfo()
We're going to include a new class named ‘Juice’ in our code and save these changes. To do this, go to 'main.py' file and input the provided code.
class Coffee:
calories = 99
def __init__(self, name):
self.n = name
def showInfo(self):
print("Coffe: ", self.n)
class Juice:
def __init__(self,name):
self.n = name
def showInfo(self):
print("Juice: ", self.n)
drink1 = Coffee("Caramel Macchiato")
drink1.showInfo()
drink2 = Juice("Orange Juice")
drink2.showInfo()
After making your changes, click the green checkmark icon in the top-right corner of the window to save your work. When you go to the commit tab, you'll notice that the 'main.py' file is automatically selected because it detects the changes you've made to it. Write a new message, for example: “Add class Juice.”. Then, press “commit” as shown above.
To review the code changes we've made, from the very beginning (the "initial commit") up to the point where we added the "Juice" class, you can simply navigate to the Git tab located at the bottom of the window. From there, you can easily explore and visualize all the changes made to your code.