If you missed the previous article on how to install Git, you can check it here!
Now that you have Git installed, what’s next? You’ll need to set up a repository using GitHub as a platform to store our project, and understand some Version Control concepts before we start using Git.
Don’t worry, it’s way easier than you think. In less than 10 minutes, you’ll learn about Version Control, and you’ll be ready to use Git on your own.
Specifically, we’ll set everything up for a Unity project, but everything you learn from this article will help you set anything up for other gamedev or software related stuff if you’d like.
GitHub: Creating an account and a repository
GitHub is a code hosting platform for version control and collaboration. It lets you and others work together on projects from anywhere. It’s one of the best platforms out there, and not only that, you can use it for free!
You’ll need a GitHub account if you want to start creating your own repository to store all your files. If you haven’t created an account already, you can do so here!
So, how does someone create a repository? In your main page, at one of the sides, you’ll see a Repository section and a green button to create one. Click on the button and lets check the repository options.
You’ll have a bunch of options. Let me explain what every option does:
- Owner: Assigns ownership to a user. In your case, just leave it as is (after all, you are creating a repo for yourself).
- Repository Name: Names the repository after the name you write in. Always try to use names that represent well the content of your project (imagine trying to figure out a repository content if it’s called “Repo1”).
- Description: Self-Explanatory. Use this if you need to add anything relevant that can help you or others identify the repo’s content.
- Public / Private: Do you want strangers seeing your project? Or do you want to make it open-source? It’s all up to you.
- Add a README file: A longer description that displays in the repo’s main page. For now, you don’t need to create one.
- Add .gitignore: THIS STEP IS IMPORTANT. This file ignores certain files from your project. Sometimes, there are some unnecessary files that you wouldn’t want to be uploaded to the repo as it could bloat it. In our case, choose the Unity template.
- Choose a License: In case you want to legally protect your code, you can choose a license. Not that necessary for a test repo.
Once you reviewed every option, click on “Create repository” and you’re done! You’ll be redirected to the main page of your repository, which will look like this:
Version Control Basics
Before we move onto how to use Git, you need to understand the four basic Version Control commands. These commands are used in a standard workflow you’ll always want to follow, no matter what. In order, before you start working again in a project, you always want to:
- Fetch: Checks if there are any new changes in the repository ready to be downloaded.
- Pull: Ask the remote server (our GitHub repository) for new files. You always want your project in your computer and in the repository to be on the same page.
If you forget to pull before you start working, you’ll probably end up with conflicts (mismatching content in your files), and you’ll have to face a choice: use your new files, or use the files in the repo (this could mean erasing progress you or your team has made!).
- Commit: Contains new files or changes you’ve staged, ready to be sent to the repository. It’s like a box where you define its contents. As a industry standard, you always want to commit with a message describing what you did or what you added / removed, so you and others can identify where some changes were made in case something happens.
Also, if you truly forgot about pulling the files, and you’ve already made changes, it’s best to commit first, then pull the new files. That way, you’ll secure your progress, and you’ll be able to update your project without losing the changes you’ve made (but, get ready for any possible conflict).
- Push: Sends your commits to the repository.
How to set up and use Git
Now that we’re finally here, follow the next steps:
- Create a new Unity Project (or for other related projects, a new folder where you’ll store your files).
- Once you have your project’s folder, right click it and choose “Git Bash here”. It will automatically access your folder, and you won’t have to navigate through it to get the folder’s path.
- Now, write in the following: “ git init ” . This will initialize the local repository.
- The local repository is set up. Next, is to define the online repository were we’ll upload our files. To do that, go to your GitHub repository, click on the green button that says code, and copy the HTTPS link to your repo.
Then, put this command in Git Bash: “ git remote add origin <GitHub_Repo_Link> “.
- To verify we linked it correctly, do “ git remote -v ”.
- If everything went right, the last few steps to start working with Git Bash are: Pull, Commit, and Push. Pay attention to these steps, as these are the ones you’ll work with every single time.
- Pull: “ git pull origin main ”. “origin” stands for the online repository (GitHub), and “main” is the default branch of your repository. You can’t just do “ git pull ” because Git Bash needs you to specify where to locate your project and where to download the files.
- Commit: “ git commit -m <your_message_here> “. Before you put that command, you first need to stage new or modified files.
* To check if you have new changes, do “ git status “. It will show you all the changes that are not staged.
* To add those changes, you can add them all with “ git add . ” (“.” will stage every change of your root folder), or add specific files with the same command, but instead of a dot, you specify the folder / file you want to add.
* After you’ve staged everything you wanted, now you put the commit command (always remember to leave a message!).
- Push: “ git push origin main ”. Just like you did with the pull command, you need to specify where you want to upload the files. Once you enter that command, your files will be stored in your GitHub repository.
And that’s it! You have set up your first repository using Git. Congrats! 🎉
I taught you all the basics for you to start and update your projects using Version Control and Git. Still, there is more for you to learn, as I didn’t explain many other Git commands or Version Control stuff, such as Branches.
I hope this helped you begin your journey using Version Control! Follow me for more articles or to check my progress in the GameDevHQ Unity Program!