Source Control
Part of this chapter is based on tutorials by Django Girls Tutorial : https://tutorial.djangogirls.org
Git
Recap: Git is a "version control system" used by a lot of programmers. This software can track changes to files over time so that you can recall specific versions later. A bit like the "track changes" feature in Microsoft Word, but much more powerful.
Starting our Git repository
Git tracks changes to a particular set of files in what's called a code repository (or "repo" for short). Let's start one for our project. Open up your console and run these commands, in the textbasedgame
directory:
Note Check your current working directory with a pwd (Mac OS X/Linux) or cd (Windows) command before initialising the repository. You should be in the textbasedgame folder.
$ git init
Initialized empty Git repository in ~/textbasedgame/.git/
$ git config --global user.name "Your Name"
$ git config --global user.email [email protected]
Initializing the git repository is something we need to do only once per project (and you won't have to re-enter the username and email ever again).
Git will track changes to all the files and folders in this directory, but there are some files we want it to ignore. We do this by creating a file called.gitignore
in the base directory.
Open up your editor and create a new file with the following contents:
*.pyc
*~
__pycache__
myvenv
db.sqlite3
/static
.DS_Store
And save it as .gitignore
in the "textbasedgame" folder.
Note The dot at the beginning of the file name is important! If you're having any difficulty creating it (Macs don't like you to create files that begin with a dot via the Finder, for example), then use the "Save As" feature in your editor; it's bulletproof.
Note One of the files you specified in your
.gitignore
file isdb.sqlite3
. That file is your local database, where all of your posts are stored. We don't want to add this to your repository because your website on PythonAnywhere is going to be using a different database. That database could be SQLite, like your development machine, but usually you will use one called MySQL which can deal with a lot more site visitors than SQLite. Either way, by ignoring your SQLite database for the GitHub copy, it means that all of the posts you created so far are going to stay and only be available locally, but you're going to have to add them again on production. You should think of your local database as a good playground where you can test different things and not be afraid that you're going to delete your real posts from your blog.
It's a good idea to use a git status
command before git add
or whenever you find yourself unsure of what has changed. This will help prevent any surprises from happening, such as wrong files being added or committed.
Type the following in the console:
$ git status
The git status
command returns information about any untracked/modified/staged files, the branch status, and much more. The output should be similar to the following:
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
manage.py
myfirstgame/
tbgsite/
nothing added to commit but untracked files present (use "git add" to track)
And finally we save our changes. Go to your console and run the following commands.
Add the new files first.
$ git add --all .
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .gitignore
new file: .vscode/settings.json
new file: manage.py
new file: myfirstgame/__init__.py
new file: myfirstgame/admin.py
[...]
new file: tbgsite/__init__.py
new file: tbgsite/settings.py
new file: tbgsite/urls.py
new file: tbgsite/wsgi.py
Commit the changes.
$ git commit -m"My first commit"
[master (root-commit) 1f68f86] My first commit
17 files changed, 276 insertions(+)
create mode 100644 .gitignore
create mode 100755 manage.py
create mode 100644 myfirstgame/__init__.py
create mode 100644 myfirstgame/admin.py
create mode 100644 myfirstgame/apps.py
create mode 100644 myfirstgame/migrations/0001_initial.py
[...]
create mode 100644 myfirstgame/models.py
create mode 100644 myfirstgame/templates/myfirstgame/index.html
create mode 100644 myfirstgame/tests.py
create mode 100644 myfirstgame/urls.py
create mode 100644 myfirstgame/views.py
create mode 100644 tbgsite/__init__.py
create mode 100644 tbgsite/settings.py
create mode 100644 tbgsite/urls.py
create mode 100644 tbgsite/wsgi.py
Congrats on your first commit!
You can check what you commit by running git log
and you get something similar to the following example output:
$ git log
commit e232a4224d98ca6c6d072158026e0d82a899e214 (HEAD -> master)
Author: Vicky Twomey-Lee <[email protected]>
Date: Wed May 16 21:07:45 2018 +0100
My first commit