Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

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:

Locked learning resources

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

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

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

Unlock This Lesson

Already a member? Sign-In

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.