In this video, you'll learn how to set up a GitHub account and upload a small SQL file.
Why would you want to do this?
Source control is an essential part of software development, and Github is a popular source
control website.
If you're a software developer, you've probably got some experience with Github.
But, if you work primarily with SQL code and databases, you may not have ever had the need
to use source control.
Using source control for SQL files and database work is helpful because you can store evolutions
of your code, have a backup of your code, deploy your code as part of an automated deployment
process, and share your code with others.
So, how do we do this?
There are a few steps involved, so let's get started.
Step 1 is to sign up for Github.
Visit the Github website at github.com.
The page should look something like this.
Click on the Signup button on the top right.
Creating an account and using Github is free.
There are paid components, but you can do everything you need to do in this video for
free.
On the Signup page, enter a username, email, and password you want to use, and click Create
Account.
On the next page you'll be asked to verify your email.
You'll receive an email from Github shortly to confirm your email address, so click on
the link in that email.
Once you have verified your email, you'll arrive at your Github homepage.
If you don't get taken here after confirming your email, you can login to Github using
the details you provided on the Signup page.
Step 2 is to create a personal token
We'll need to access our Github account from outside this website, using the command line
or our IDE.
In order to do this, we used to be able to provide our username and password.
However, in 2021, Github is encouraging and now forcing users to use a new access method.
The recommended access method is called a Personal Access Token.
Without going into too much detail, it's something we can set up inside our account on the website,
and provide it when we log in to other tools.
It's fairly easy to set up, so let's do that now.
On the main page, click on your profile icon on the top right.
Then, click Settings.
On the left, click Developer Settings.
Now, on the left, click Personal Access Tokens.
Click Generate New Token.
Enter a name for your new token.
Select the scopes you'd like you be able to use for this token.
Scopes are like permissions, and the scopes you select here will be activated for wherever
you use this token.
In this example, we'll select the "repo" checkbox at the top.
Click Generate Token.
A screen will appear with your token, which is a long series of letters and numbers.
Copy and paste this somewhere that you'll remember, as once you close this window, you
can't get the token again.
And now you have your token!
We'll use this in our login process shortly.
Step 3 in this process is to create a new repository.
We're going to create a place in your Github account to store your files.
This is called a repository.
You can have many repositories in your account, and a repository is created for each project
you work on.
Return to the main page on Github by clicking in the top left corner.
To create a repository, click on the green New button on the left of the screen, next
to Repositories.
You'll be taken to the Create a New Repository page.
Enter a name for your repository.
You can also enter a description if you like.
You can leave the visibility selection as Public for this demo, but if you want to restrict
access, change this to Private.
The last few options at the bottom are optional.
You can have a readme file automatically generated, which is handy, so we'll select that.
A gitignore file is also helpful, but we won't need one for our demo.
Same with license.
So we'll leave those unchecked for now.
Click Create Repository at the bottom.
Your repository is now created!
Your page should look something like this.
Your account name and repository name is shown at the top left here.
In this example, my account name is bbrumm and the repository name is sql_sample.
On the right, under the About section, is the description of the repository if you entered
one on the previous page.
In the middle of the page is a list of files in the repository.
If you selected to create a readme file, you'll see a [readme.md](http://readme.md) file here.
The MD extension stands for markdown.
Below the list of files is the readme file.
We see the name of the repository, which is sql_sample, and the description.
This readme file here can be edited to add more information.
So our repository is created.
We'll move on to the next step.
Step 4 is to get the files from this repository onto your computer.
We do this using the command line.
There are other tools that can be used, such as Git Desktop, or features in your IDE.
I'll go over those in other videos, but for now, we'll use the command line.
Open the command line on your computer.
On Mac, this will be Terminal.
On Windows, this will be the Command Prompt.
We're going to use the terminal to download and link the repository we created on Github
to a location on our computer.
The first step is to browse to a folder where we want this to be created.
My default folder here is BB.
I can use the "cd" command, short for "change directory".
In this example, I've changed to the Documents directory inside BB, then the "webdev" directory
inside Documents.
These directories already exist.
You can create a new directory using the mkdir command, for example mkdir my_new_folder
Then, use "cd" to change into it.
We're now inside this new folder.
This is the folder we want to use for our repository.
So, how do we get the repository onto our computer?
We use a command called "git clone".
This will download the repository and link the current folder to the repository for future
commands.
To do this, we enter "git clone" then the URL for our repository, which is https://github.com/bbrumm/sql_sample
Press Enter to run the command.
You'll be prompted for your username and password for Github.
Enter your username from when you signed up on github.com.
For the password, don't enter the password for github.com.
Instead, enter the token: that long text string you created on the Personal Access Token section
of the website.
This login should only need to be done once.
Once you do this, the files will be downloaded to that folder.
You can check the files have appeared by typing the "ls" command.
You'll see the sql_sample folder which was created by the git clone command.
Use the CD command to go into this folder, then run the ls command again.
You should see one file here, called readme.
This was the file you created when you created the repository.
So, we've got the repository on our computer.
What's next?
Step 5 is to create a new file in our repository.
It can be anything you like, but because we're focusing on using this for SQL files, we'll
create an SQL file.
There are several ways you can create a new file here.
You can use the command line, a text editor, or an IDE.
For this example, to keep things familiar, we'll use a simple text editor.
You can use the command line, but using a text editor will also show how edits in other
programs impact the repository.
Open up your favourite text editor.
I'm using Atom here, but you can use Notepad, Notepad++, Sublime, or anything else.
Create a new file and enter a simple SQL query.
It doesn't matter what the query is, as we're not going to run it.
It's just to demonstrate the purpose of source control.
We'll enter a query to get the current date.
SELECT NOW();
Once we've entered this, save the file.
We'll browse to the folder on our computer where the repository is set up, which in this
video was the sql_sample folder inside BB, then Documents, then webdev, then my_new_folder.
Save the file with a .sql extension, such as test_query.sql.
Now our file is saved, let's go to the next step and start adding this to our repository.
Step 6 is to add our new SQL file to the repository.
We'll run a few commands to do this.
Back in our Terminal application, we should be able to see this new sql file by entering
"ls" at the command line.
We can see two files there: our new test query, and the original readme file.
Now what do we do?
This folder is linked to the GIthub repository, and there is some built-in commands that let
us work with it.
One of those commands is called "git status".
This will let us see the status of the files in this folder.
Run git status on this folder.
You'll see a few things once you run this command.
It says "branch main", which just means you're working on the main set of your code.
We'll look at branches in another video.
Under the Untracked Files section, it shows that there is one file that is created in
this folder but not part of your repository.
This is the SQL file we just created, shown in red here.
Files added to the folder are not automatically added to the repository.
But you can add them pretty easily.
To add a file, use the "git add" command.
After the add keyword, we specify which files to add.
We can enter the full filename, such as git add test_query.sql.
Or we can select all files by using a . character, so we could enter git add .
Let's specify our file individually.
Run this git add command, and you'll just get taken back to the command line.
It seems like nothing has happened.
But we can run git status again, and see a different output.
Instead of untracked files, we can see changes to be committed.
We also see our filename in green.
This means it is added to the repository as a file we want to keep track of in our source
control.
The next step is to commit our changes.
This is separate to the add command.
The git add command will indicate that this file needs to be tracked, and is only run
once for each file.
The command to commit changes is called git commit, and is run each time you make a change
you want to save.
So, we run git commit.
Before we press enter, it's helpful to add a message to this commit.
The commit will get all files that are changed, which is just the green test sql file above,
and save these changes to the repository.
We can add a message by typing -m then our message in quotes.
For this example, we'll say git commit -m "Added our first SQL file"
Then press enter.
Our changes are committed to our local repository, which is the record of changes on our computer.
Now we have run that commit command, the changes to that file are stored in the repository's
records on our computer.
That's what the commit command does.
The last step is to get these changes onto our github account.
All the work we've done is on our own computer.
There's one more command to run to get these files and changes onto our Github account.
That command is "git push".
This will take the commit that we just created, which is the new SQL file, and send it to
our repository on the Github website.
We simply write "git push" and press enter.
We don't need any parameters.
Once you press Enter, you may be asked for your password.
This is related to the keychain feature on MacOS.
In this popup, enter your computer password and press Allow.
This is the password to log into your Mac, not the Github password.
Back on the Terminal, you'll see some new lines that indicate a few steps that were
performed, and ideally no errors.
The outcome is that the file should now have been added to your Github account repository,
also known as the remote repository.
On your Github page for your repository, you can refresh the page and it should look like
this.
You'll see a few things.
There's a new SQL file here in the file list.
In the second column you'll see your commit message.
This is the message from the most recent commit for that file, which in our case is the one
we just did.
On the right, it says when the file was last updated, which was a few minutes ago.
On the top of this table it says "2 commits".
You can click on that to see a history of the commits or changes made to this repository.
Here we can see the first one, called Initial Commit.
We can then see the one we just did, with the message "Added our first SQL file".
This is where a good commit message comes in.
We can see what the commit was about.
Click Back, or click on the sql_sample repository name on the top left to go back to the repository.
You can even click on the SQL file to see what's in the file.
In this case, it's our SELECT NOW query we wrote.
Well done!
You've done a lot in this video - created a repository, added an SQL file, and ran a
few commands to get it on the [GIthub.com](http://github.com) website.
Here's a visual summary of what we did.
The account on github contains our remote repository, called sql_sample.
We created a folder on our own computer, and ran git clone, which downloaded the remote
repository onto our computer.
This created a local repository.
We created a new SQL file in that folder, which didn't do anything to the repository.
We used the git add command to add this new file to the list of files that the local repository
should track.
We then used the git commit command to save the changes made to this file, which was the
fact the file was created, to our local repository as an update we've made to it.
Finally, we used git push to add these changes to the remote repository, which involved uploading
that SQL file to the github website, adding the commit and message.
That brings us to the end of this tutorial on setting up a Github repository for SQL
files.
You can grow your repository from here by making changes to files, adding more files
and folders, and updating that readme file to better describe what's in the repository.
If you learned something new from this video, make sure to subscribe to my channel.
If you want to learn more about database design and development, visit [databasestar.com](http://databasestar.com/).
That’s where I share my best database-related content.
Which part of this walkthrough was the most helpful for you?
Was it the different git commands to run?
Or was it the steps to create the personal access token?
Or something else?
Thanks for watching.