Skip to content

Instantly share code, notes, and snippets.

@eallenOP
Last active May 3, 2022 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eallenOP/5b7e2f9729d0cc68a4d768e8eda4a5cc to your computer and use it in GitHub Desktop.
Save eallenOP/5b7e2f9729d0cc68a4d768e8eda4a5cc to your computer and use it in GitHub Desktop.
Git beginner instructions

Studio 1 git instructions

This is a quick reference guide for using git in Studio 1. Keep in mind that we have only covered the very basics of git - as you learn more you will need to adjust your workflow to suit.

We are deliberately staying away from branching at this stage so that you can focus on learning and practicing the basics, but if you feel confident enough to dive right in then be my guest.

Quick tip: the only way to really learn git is to practice. Make a practice repo on GitHub and mess around with it as much as you can.

First steps

To get started, you should be familiar with the various states your work can exist in. In other words, you need to know about the workspace, the local repo and the remote repo. It would also be useful to understand what staging is. We will cover these concepts in class. Head over to the Version Control tab to review some information about git basics.

Creating a repository for the first time

Each project you want to work with should have its own repo. You can either make the local repo first or the remote repo first. I think it's easier to start with the remote, so that's what these instructions will do.

  1. Log in to GitHub (or your remote hosting platform of choice) and create a new repo. Give it a descriptive name. Add a README file so that the repo is not empty.
  2. Decide if you need a .gitignore file. If you are working with Visual Studio then you definitely need one, as the temp files it creates will mess you up otherwise. You can grab a template for that from GitHub.
  3. Clone the new repo onto your computer.
    • To do this, navigate to the folder you want your new project to live in (don't make a special new one because cloning will do that, just pick the location you want it in), right click and select Git Bash here.
    • Back in your browser, find the option in your remote repo to clone either with SSH or HTTP. If you haven't set up SSH in class, then pick HTTP. Copy the text provided.
    • Back in git bash, use the following command to clone your repo: (without the <>)
      git clone <paste that text you copied here>
      You will probably be asked for your username and password. This is the one you use to log into GitHub (or whatever you're using).

You have now set up your local and remote repos, and linked them together. It's time to get work done in your workspace (aka working directory).

Basic source code control

The basic unit of any git repo is the commit. Making a commit is a bit like taking a snapshot of your chosen files in their current state, and this creates a safety net that you can go back to if you stuff up in the future. It costs nothing (not even any disc space really) to make a commit, so you should do it often. A good rule of thumb is to make a new commit every time you have enough changed to describe in one short statement (e.g. "Add the user menu" or "Improve the game loop").

There are a few habits you should form right from the start to make your git journey smooth.

  1. Open git bash in your project directory and check what branch you are on (you should see the word "main" in blue).
  2. Type git pull to make sure you're starting with the most up-to-date code.
  3. Do some work on your code as normal (don't forget to save as normal too!).
  4. Type git status to see what has been changed.
  5. Type git add and then the file paths of the files you want to include in the commit. Only add files you changed yourself! If you accidentally have a temp file that the .gitignore missed, you'll create all sorts of mess if you add it.
  6. Type git status again to check what files are staged.
  7. Type git commit -m "Describe what you changed" and hit enter to make the commit.
  8. Rinse and repeat from step 3.
  9. When you are done working, type git pull again to merge in any recent remote changes.
    • If something has changed in the remote, this will trigger a merge. It will automatically merge if it can.
    • If it auto-merges, you will be sent to Vim (text editor) to make a commit message. You can use the default that's already there. Make sure you're not in insert mode (hit esc) and type :wq to go back to the git bash command prompt.
    • If git can't auto-merge what you pulled, you will need to go back into your code and edit the parts that conflict. Once you're happy with how the code looks, do steps 4-7 to commit it again.
  10. Type git push in order to send your commits to the remote repo.

Remember to always carefully read the messages that git will give you in git bash. These often explain exactly what you should do if the command you entered hasn't worked (it's not an error message, but most likely a hint).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment