This page describes a few git commands. You will need to do these commands to submit weekly assignments and to complete the Ruby on Rails Back End course. If you are unfamiliar with git, you should complete the Introduction to Git course on Treehouse. Another, perhaps better, introduction is here. Unfortunately, the Treehouse class does not explain branching, forking, and other operations that you will have to do. These are explained in the courses on Git Branches and Merging and Github Basics , but these are long courses, more than you need to know now. So, here’s a cookbook.
First, if you don’t have a github account, create one at https://github.com . If you use a Mac or Linux, you should have git already installed on your workstation. You can check this by typing
git --version
at your command line.
If you are using Windows, we provide a vagrant virtual machine image for you to run, and git is already installed in that virtual machine image, as described in the lesson here. Be sure that you do all git operations from within the vagrant session, and not from Git Bash. For Windows, you need to do some additional one time setup, by doing the following commands in your vagrant session:
git config --global user.email <your email address>
git config --global user.name <your github id>
git config --global core.eol lf
git config --global core.autocrlf input
For each lesson, you will have a starter github repository. Fork that repository, creating a public fork of it in your own github account. You do this by clicking on the fork button that appears on the upper right in the github page for the starter repository.
After the fork is created, you need to clone it onto your workstation. The github page for your fork has a green code download button. Click on that. You will use https to clone your fork. You will see a clipboard icon next to the https link so that you can copy the URL of your fork. Then, within the directory where you are keeping the code you develop, type:
git clone <url>
where the url is the one you copied to the clipboard. This will copy the github repository into a directory. The directory will have the same name as the starter repository. Be sure you clone your fork, and not the starter repository. Then, cd to the directory created by the cloning process. You now need to create a branch of the repository to contain the changes you make for the lesson. This is done as follows:
git checkout -b lesson
Some lessons have you create branches with other names. Next, you make your changes to the code that was downloaded using your editor. When your changes are complete, the following steps will stage, commit, and push those changes, so that they are stored in your github repository:
git status
git add -A
git commit -m "put a useful commit message here"
git push origin lesson
except that for some lessons, the name of the branch will be something other than lesson, as described in the instructions for that lesson. You can stage, commit, and push your changes at any time. The git status command will tell you if you have uncommitted or unstaged changes. The push operation requires that you enter your git userid and a git personal access token. If you do not have a git personal access token, you can create one using the steps described here. Your git client will save this token, so that you do not have to enter it every time.
When you are done with the lesson and want it reviewed, you create a pull request. This is done when you are logged into github. You open up the repository containing your fork of the lesson. You will see a little button that says “main”. (For older repositories, it may say “master”.) This is your main branch. If you click on that button, you will see that you can switch to your lesson branch. Do so. Then click on “pull request”. You will see a button that allows you to choose the base repository. You want the to specify your own repository, not the Code-The-Dream-School one. You are creating the pull request with, as the base, the main branch of your own forked repository. Click on the button that says “Create Pull Request”.
This is enough to get you started with git. There are additional courses on Treehouse to help you learn git completely. If you get stuck with any of these steps, put a question into the slack channel.
Steps for Submitting Assignments via GitHub
- Fork the lesson workspace (in github).
- Git clone your fork (from the terminal command line).
- Create a git branch for the lesson.
- Edit a file within the lesson.
- Test your changes.
- Git stage the files. (To stage all changed or added files, do “git add -A”.
- Git commit the files. (git commit -m “some message about what you did”).
- Git push the branch back to your fork. (git push -u origin <branchname>)
- Create a pull request on github, from your working branch to your main branch.
- Submit a link to your pull request using the homework assignment submission form. (Classes starting before October 2022 use this form.)
If you can’t get the code working correctly, you can still submit your homework, but you should try to get it working, with mentor help as needed.
Reviewers will post comments via Github. After you have received their comments, you can merge your assignment branch into your main branch. It’s best to do this after your assignment has been reviewed, as your reviewer may recommend changes.
Creating a New Repository
Some assignments do not start with a repository. In particular, the final class project for many classes does not have a starter repository. Instead, you create your own, with the following steps:
- Create the project directory. Don’t have spaces in the directory name.
- Change to the project directory.
- Create at least one file that will belong to the project. Recommended: create a README.md file, a text file that describes the project.
- Do: git init
- Do: git add -A
- Do: git commit -m “first commit”
- Log on to github. The plus sign in the upper right allows you to create a new repository. Create it. Do not create a .gitignore or a README.md in this step.
- Once you have created the repository, click on the little copy icon next to the link. This copies the URL of the repository to the clipboard, used in the next step.
- On your workstation in your project directory, do: git remote add origin <URL>
- Do: git push origin main
You should do all subsequent work in branches, pushing those branches to github periodically. When you are satisfied with a branch, you can do a pull request on github and then merge the pull request into main.
A Couple More Tips
Try to remember not to make changes to the main branch. You want to make your changes to working branches, push them to github, make a pull request, and then merge the pull request.
If you forget, and start working on the main branch, but have not committed anything, you can do git stash, then create or check out your working branch, then do git stash apply. If you commit to the main branch, you can still recover the previous state. RIght after your commit, with the main branch still active, create the branch you want to work in. This new branch will then have the changes you have committed to main. Be sure to do this first, or your work will be lost! Then checkout main. Then do:
git fetch origin
git reset --hard origin/main
git clean -f
Finally, try not to make this bad mistake. Suppose you have a project directory called project1. While you are in the project1 directory, you do a git clone to create a directory for project2. You will then have two git repositories, one nested inside the other. This is bad. Git operations will do different things with your files, depending on which directory is active. If you make this mistake and you haven’t yet committed anything, you can delete the nested directory. If you have already committed something, you have a mess and you may need help from a mentor.
Here is a page you should look at next: