Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Initialize a Local Git Repository

For more information on concepts covered in this lesson, you can check out:

Here are the command-line snippets used in this lesson:

Shell
$ git init
$ ls -l
$ ls -la
$ git status
$ vim .gitignore
$ git add .
$ git commit -m "Initial commit for my portfolio project"
$ git log --oneline

Download a sample .gitignore file or copy and paste its contents from here:

Configuration File
# Created by https://www.toptal.com/developers/gitignore/api/python,django,pycharm+all
# Edit at https://www.toptal.com/developers/gitignore?templates=python,django,pycharm+all

### Django ###
*.log
*.pot
*.pyc
__pycache__/
local_settings.py
db.sqlite3
db.sqlite3-journal
media

# If your build process includes running collectstatic, then you probably don't need or want to include staticfiles/
# in your Git repository. Update and uncomment the following line accordingly.
# <django-project-name>/staticfiles/

### Django.Python Stack ###
# Byte-compiled / optimized / DLL files
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
#  Usually these files are written by a python script from a template
#  before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo

# Django stuff:

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
#   For a library or package, you might want to ignore these files since the code is
#   intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
#   According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
#   However, in case of collaboration, if having platform-specific dependencies or dependencies
#   having no cross-platform support, pipenv may install dependencies that don't work, or not
#   install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

### PyCharm+all ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# AWS User-specific
.idea/**/aws.xml

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn.  Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

### PyCharm+all Patch ###
# Ignores the whole .idea folder and all .iml files
# See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360

.idea/

# Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023

*.iml
modules.xml
.idea/misc.xml
*.ipr

# Sonarlint plugin
.idea/sonarlint

### Python ###
# Byte-compiled / optimized / DLL files

# C extensions

# Distribution / packaging

# PyInstaller
#  Usually these files are written by a python script from a template
#  before PyInstaller builds the exe, so as to inject date/other infos into it.

# Installer logs

# Unit test / coverage reports

# Translations

# Django stuff:

# Flask stuff:

# Scrapy stuff:

# Sphinx documentation

# PyBuilder

# Jupyter Notebook

# IPython

# pyenv
#   For a library or package, you might want to ignore these files since the code is
#   intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
#   According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
#   However, in case of collaboration, if having platform-specific dependencies or dependencies
#   having no cross-platform support, pipenv may install dependencies that don't work, or not
#   install all needed dependencies.

# PEP 582; used by e.g. github.com/David-OConnor/pyflow

# Celery stuff

# SageMath parsed files

# Environments

# Spyder project settings

# Rope project settings

# mkdocs documentation

# mypy

# Pyre type checker

# pytype static type analyzer

# Cython debug symbols

# End of https://www.toptal.com/developers/gitignore/api/python,django,pycharm+all

00:00 In this lesson, you’ll create a local Git repository for your project, which will become essential in the next steps. If you’re already comfortable using Git, then jump ahead to the next lesson now.

00:11 Either way, you should already have installed a command-line Git client. If you’ve missed that part, then you’ll find links to Git installation instructions in the description below. Stop your Django development server now if it’s still running or open another terminal window and make sure that you’re in the project root folder.

00:29 You should see the manage.py script among a few other files and folders. Don’t worry about activating your virtual environment just yet unless you intend to install another third-party module or start a local development server again.

00:45 Now, type git init to initialize a new local Git repository. This will create a subfolder named .git with the entire history of changes tracked by Git. However, you won’t see that folder right away if you’re on macOS or Linux because names starting with a dot are hidden on those operating systems.

01:04 If that’s the case, then you must append the -a flag to the ls command to list all directory entries. At this point, you can make your first snapshot of the project and save it in Git.

01:17 Type git status to reveal the files that Git would like to save for you. As you can see, there are currently some files and folders that Git shouldn’t track.

01:27 For example, both the SQLite database and the virtual environment contain binary data that’s specific to your computer, so it won’t make sense on someone else’s machine.

01:37 Apart from that, this data might contain sensitive information like personally identifiable information, passwords, or API secret keys. Fortunately, Git lets you ignore certain file patterns and exclude them from tracking.

01:52 You can type them manually in a special file named .gitignore, which you would typically place at the root of your project. Note the leading dot (.) in the filename, which is important.

02:03 I’m using Vim here because it’s one of the most popular and readily available text editors on many Linux distributions. Unfortunately, it has a bad reputation for its clunky and unique interface, so if you haven’t done any editing with Vim before, then be sure to check out the command cheat sheet that I’ve included in the supporting materials.

02:24 When editing .gitignore, just place the individual patterns on separate lines, such as *.sqlite3, venv, or __pycache__.

02:35 Since specifying those entries in .gitignore by hand is a mundane and repetitive task, you might as well consider using the gitignore.io website, which will automatically create those entries for you based on convenient tags such as Python or Django.

02:58 Now, when you save the file and retype the git status command, you’ll only see the files that aren’t ignored by Git. Perfect! You want those to be tracked, so go ahead and type git add followed by a dot (.) to indicate the current directory.

03:13 This will scan the directory recursively and place all non-ignored files in the so-called staging area, which you can inspect and modify on will. When you are happy about the state of your staged files, then take their snapshot by making a commit in your local Git repository.

03:30 Don’t forget to provide a descriptive message such as "Initial commit for my portfolio project".

03:37 Congratulations! Git has just updated your local repository with a new entry, which you’ll be able to see in your Git log.

03:48 Next up, you’re going to see how Heroku cleverly integrates with Git.

Become a Member to join the conversation.