CodeGym /Courses /Python SELF EN /Working with GitHub from PyCharm

Working with GitHub from PyCharm

Python SELF EN
Level 12 , Lesson 2
Available

10.1 Cloning a Repository

Remember when I told you about the 3 places where files are stored? Just a reminder: there’s the remote repository, the local repository, and the working directory.

So, now you’ll need to clone your remote repository (the one you created on GitHub) to your local repository (the one on your computer).

So fire up PyCharm and let’s get started...

Step 1. Close the project if it’s open. Then hit Clone Repository

Step 2. Enter the url of the remote repository you wanna clone to your computer.

This method is handy if you’re cloning someone else’s repository.

If you’re cloning your own repo, it’s easier to just log in to GitHub through PyCharm.

Select your project and hit Clone

Step 3. Trust no one. Not even yourself.

PyCharm warns you not to run code from unknown repos. Since this is your own repository, go ahead and check the "trust" box.

Step 4. If you’re on Windows, the default antivirus might freak out because of unknown files. Antivirus programs really hate unfamiliar apps.

BUT! We’re gonna be creating these very apps, so tell your IDE to convince the antivirus not to block your future programs. Click “Automatically,” then — “YES.”

10.2 First Commit

If you cloned a newly created repository, it will only contain one file — README.md

Step 1. Open the file README.md.

Important! The md extension means Markdown – it’s a super basic format for styling text files. You can see how it works by clicking the button in the top right corner.

Edit this file however you want: write a welcome message or a description of the repository...

Step 2. Once you’ve made changes to the file, PyCharm starts showing it in blue in the file tree on the left. Files marked in blue are the ones you’ve modified, but the changes are still only in your working directory and haven’t been added to your local repository yet.

Step 3. Commit.

Once you’ve made all the planned changes to the file or files (and made sure they’re correct), it’s time to push them to your local git repository. Right now, they’re only in your working directory.

To do this, click the Commit button, select all the files you want to add to the local repository, and add a comment describing your changes:

Step 4. Then click the «Commit» button.

Step 5. Check.

Switch over to your project and check the file colors: if the files are no longer blue, it means all the changes from your working directory have been successfully added to your local repository.

10.3 Working with Files

Step 1. Let's create a file in PyCharm.

Since we're coding in Python — let's make it a main.py file.

Step 2. Add the file to your local repository.

As soon as PyCharm notices you've created a file, it immediately suggests adding it to your local repository. PyCharm only tracks changes to files in your working directory that are already in your local repository. Those files are marked with blue.

If a file exists in your working directory but isn't in your local repository, it'll be marked red, like in the image below:

You can always just click Add, but let's click Cancel this time and add the file manually. This way, you'll remember how it's done.

Step 3. Write your favorite code in the file.

For example, I wrote: print("first commit")

Step 4. Now let's add our main.py file to the local repository.

Just right-click anywhere in the file and select Git -> Add File

If everything goes smoothly, the added file will be highlighted green.

Step 5. Next, commit the file the usual way:

10.4 The First push

Now we need to upload our changes from the local repository to the remote one on GitHub. When multiple devs are collaborating on the same project, this is how they sync their code changes.

Step 1. Click on the Push button.

Honestly, this is super easy: just hit the Push button in the top menu of your IDE:

Step 2. Confirm the commits you've made.

At this step, you can review all the changes you’ve made to ensure you didn’t accidentally break something important. Or maybe forgot to add a file other files depend on.

Step 3. Check it out.

If everything went smoothly, you'll see messages like this:

Step 4. Now hop over to your GitHub repository and take a look:

The magic of technology!

10.5 Using .gitignore Files

If you’ve added some service files to your project and don’t want them to accidentally end up in the repository, you can exclude them. For this, there’s a file named .gitignore. This is super handy when your project has files that don’t need to be stored in version control (e.g., temporary files, logs, passwords).

Step 1: Start by creating a file in the project directory that you want to ignore. For example, a .txt file with any name. After creating the file, click Cancel.

Step 2: Right-click on the newly created file hacky_fix_that_somehow_works.txt in the "Project" window. Navigate to Git --> Add to .gitignore --> Add to .gitignore. This option adds the selected file to the .gitignore file at the root of your project.

Step 3: Create a .gitignore in your project. When we created the repository, we didn’t create a .gitignore file.

PyCharm will automatically add the file name hacky_fix_that_somehow_works.txt to your .gitignore file.

After adding files to .gitignore, they will be displayed in brown color. When trying to commit changes, these files will be ignored.

The .gitignore file only affects files that haven’t been committed to the Git repository yet. If a file has already been committed, you’ll need to remove it from the repository first before ignoring it.

Don’t forget to commit the .gitignore file itself to the Git repository and push the changes to GitHub so that all project members use the same file ignore rules.

Excluding Local Files with .git/info/exclude

Aside from the .gitignore file, which is used to exclude files for all project users, Git also allows you to create local ignore rules with .git/info/exclude, which won’t be committed to the repository and will only apply to your local copy of the project.

This can be handy, for example, for ignoring files created by your IDE that shouldn’t end up in version control.

Important! Local ignore rules only apply to your local repository copy.

Rules for .gitignore

The .gitignore file contains patterns for file and folder names that Git should ignore.

Empty lines are ignored. To add a comment, start the line with #.

Patterns:

  • * - matches any number of any characters. For example, *.log ignores all files with the .log extension.
  • ? - matches any single character. For example, temp?.txt ignores files temp1.txt, temp2.txt, etc.
  • [] - specifies a range of characters. For example, [abc] matches any of the characters a, b, or c.
  • ! - excludes files matching the pattern. For example, !important.txt will track the file important.txt, even if there’s a rule *.txt.
  • / - at the beginning of the pattern indicates the repository root. /temp/ ignores the temp folder in the repository root.
  • ** - matches any number of nested folders. For example, **/temp ignores temp folders at any nesting level.

Example File


# Bytecode
*.pyc
**/__pycache__/
*.pyo
*.pyd

# Passwords and secret files
.env
credentials.json

# Temp files
*.tmp
*.swp
*~

# Logs
logs/*
*.log

# PyCharm folders
.idea/
*.iml

# Test directories
tests/
test/

# Virtual environments
venv/
env/

# Files created during packaging
dist/
build/
*.egg-info/

Ready-Made Templates

  1. Collection of .gitignore files for various programming languages and development environments: https://github.com/github/gitignore
  2. gitignore.io - a web service to help create .gitignore files
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION