In the previous articles in this series we discussed the use of version control and how Git can be used as a version control tool. In this article we will explain how to set up a repository so you can apply version control to a folder on your local device.
Folder Set Up
Repositories track changes to files in a folder. When setting up a project you should start by creating a project folder. I like to keep all of my projects in a central Repositories folder, with each project having its own sub-folder. Each project then has a repository set up in it. To make life easier you should consider the following when setting up your project folder:
- Avoid having too many parent folders as you want the folder to be fairly close to your root directory. This avoids having to type out long file paths and makes it easier to find the folder
- Avoid having spaces in file or folder names. Use underscores to separate words instead or use CamelCase. This will make it much easier to navigate to files and folders when using the command line
- Don’t place the folder in a folder that is synced with a service like Dropbox or OneDrive. This will often lead to syncing issues and causes problems when using different branches or pulling earlier versions of files.
Initial Files Set Up
You need two files inside your project folder to be able to set up a repository: a .gitignore file and a README.md file.
The .gitignore file lists the files or file extensions that you do not want Git to track. These will be ignored. You can list the name of a file or you can use the * wildcard to ignore all files with a given extension. I usually include Excel and CSV files, text files and log files as these are either loaded in as data or created by the scripts that I am writing. As they are not part of the source code I do not want Git to track these files, and there could be a lot of them! Add each file or file type you want to ignore to a text file called .gitignore on separate lines. Note that the file name starts with a dot ‘.’.
You also need a README file which is saved as a Markdown file. As you build out your project you will need to flesh out this README file, but for now it can be an empty file. Save it with as a Markdown file with the extension ‘.md’.
There are different methods for creating a repository. For this article we are going to create a new repository on GitLab and then push the project folder to the remote repository. On the GitLab website I will select New Project and choose to create a blank project. Once you have made your initial settings selections you will be presented with a screen that gives you various options for getting underway with your project. Some of these options include:
- Cloning the repository
- Uploading or creating files
- Creating a new repository
- Pushing an existing folder
- Pushing an existing Git repository.
As we have already set up the project folder on our local device, we will follow the instructions under Push an existing folder.
One aspect of GitLab that I like is that it gives you the command line instructions required to set up the repository on your local device in the project folder. You type each line into the command line to set up the repository in your project folder. We will cover using the command line in a future article. For now we will just look at what the above commands are doing to set up our repository.
Pushing an existing folder
Let’s break down each line of the commands provided by GitLab for pushing an existing folder.
This command has you navigate to the project folder so that the rest of the commands are executed in your project folder.
git init –initial-branch=main
The second line creates the repository in the project folder. The name of the first branch is main which is the convention. Each line from this command on starts with ‘git’. This tells the command line to use the Git software to complete the instructions that follow.
git remote add origin https://….
This line connects your local repository to the remote repository that we set up on GitLab. ‘origin’ is the name we have provided locally as a shorthand for the full path that follows it. This allows us to refer to ‘origin’ in future rather than having to type out the full path, such as when pushing updated files.
git add .
We use ‘.’ to add all files that are present in the folder. These will be placed in the staging area.
git commit -m “Initial commit”
Here we commit the changes we have made that are currently in the staging area. The -m flag allows us to include a message with the commit. You should always include a short message that states what changes have been made.
git push -u origin main
The final command will push the changes that have been committed locally up to the remote repository.
After running the commands above a new (hidden) folder is created in the project folder. This .git folder is the repository and will contain all of the information that tracks the files that we are controlling the versioning for. This is a hidden folder so you will only see it if you have selected to show hidden folders for your system.
We have created a project folder to hold our project files. We created a remote repository (using GitLab in this example) and then used the command line to create the local repository and to push the files held in it to our remote repository. In our next article we will take a look at using the command line tool that comes with Git.