Join us

How to use Git & GitHub for Version Control

0_WhUOmJ6Yppn3drRD.jpeg

In the last post we created a simple authenticated website using Auth0 and RedwoodJS. Before we add more features to this project, it’s important to talk about version control.

Version control provides us with a way to save and backup our code to a repository. I really liked this article listing reasons why source control is important — even as a solo developer.

In this post, I will show you how to start tracking changes in your code using git and how to store and manage your code in GitHub. This workflow will apply to any local project you may want to set up with git.

Getting Set Up

git

Verify you have git by opening a terminal and typing git --version. If you don't already have git installed, you can download the appropriate version for your OS here: git-scm.com/downloads.

Before we can start using git, we need to add our user information to the gitconfig file using the following commands

GitHub

There are two ways to connect our local repository to our GitHub account, HTTPS and SSH. HTTPS will require you to enter your username and personal access token every time. (Although you can cache your credentials)

SSH is a little more involved. We will need to setup SSH Keys and connect them to our GitHub account. GitHub has documentation on how to setup ssh keys and add them to your GitHub account. Follow those steps and then come back.

Creating Our Repository

Before we can create commits we need to initialize our git repository. The git init command sets up the .git directory which stores all of the objects and references needed to maintain the change history.

Open up terminal. If you’re following the RedwoodJS / Auth0 site project, go to the redwood-site directory, otherwise, if you are following this to set up a git repository for your own local project, navigate to that project directory. run the following command to create the repository.

Our First Commit

We’ll want all the files we created added to our repository. RedwoodJS already supplies a default .gitignore file so that files and folders that we wouldn't want pushed to the repository are not committed. These items include node_modules, build files, and sensitive files, like our .env file. Never commit sensitive information to a public repository. You don't want to risk any private keys or database connection strings getting out into the wild.

Using the terminal we will add all of the files we wish to commit and then actually commit them, including the message that will display with the commit.

If you run git log you should see your 'First Commit' message.

Pushing to GitHub

Currently our repository only lives on our local machine. To protect our hard work, we’ll push our code to GitHub so that it is safely stored off of our machine.

Set Up Repo on GitHub

Log in to your GitHub account and go to https://github.com/new. You can also access the New Repository page from the + menu at the top right of the screen. Fill out the screen as below, although feel free to change the repository name to whatever you wish, and click ‘Create repository’. You can choose to make the repository private, if you wish, but you do not enable any initialization options as we already have a repository.

As I mentioned, we have an existing repo already, so we’re going to follow the ‘…or push an existing repository…’ instructions below.

In terminal, use the following commands to add the remote (the place we want the code to go), create the main branch, and then push the code to our remote on GitHub.

If your commands were successful, you should be able to refresh your page at GitHub and see your code stored safely. 🎉


Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies, reach more readers and earn rewards!

Join other developers and claim your FAUN account now!

Avatar

Amanda Giannelli 👩‍💻🚀

Engineer II, Auth0

@giannellitech
Wife, mother, cat lady, cook, baker, crafter, Engineer II at @auth0, and @RedwoodJs contributor 👩🏻‍💻🚀
User Popularity
30

Influence

3k

Total Hits

1

Posts