Link Search Menu Expand Document

Git Crash Course

Git has a lot of neat features for software development (we will discuss some of those in class), but here we are going to just talk about the basics.

If you need to install git, visit the course setup page and then come back!

Git is primarily a command line program, but GUI applications do exist. The instructions will cover using git from the Terminal app on MacOS or Git-Bash on Windows.

TLDR

  1. git clone [repostitory url]
  2. git add [files you have added or modified]
  3. git commit -m "Commit message here"
  4. git push

You only ever need to clone once. Repeat steps 2-4 as needed to submit assignments.

Cloning a repository

We use git repositories to hold and submit our assignments in class. For now, think of a repository as a special folder that has the ability to synchronize with a server (in our case git-keeper). When we “clone” it we make a copy from the server on our computer.

The first thing we need to do is open our terminal in the directory where we want to clone the repository. MacOS users can open the Terminal app and use cd commands to navigate to the correct directory. Windows users should have the ability to right-click any folder in Windows Explorer and choose to open Git-Bash at that location.

With your terminal window open clone a repository using the following command:

git clone [URL of the repository here]

The clone URL is that entire string, except your username will replace mine in some locations and the repository name will be different per assignment.

For me, the command would look like this:

$ git clone git@github.com:CS-232-Spring-2023/markdown-syntak-check-mirceaionescu.git

From there you will might be prompted to use a password to access the repository. The password will NOT show in the terminal, so you will need to just trust that you are typing it correctly and use the force. When you have your password typed, just hit enter. If you typed the password correctly, you should see some messages about the information git is downloading and no errors.

You only need to clone a repository once as long as it is on your computer.

Interacting with a repository

Once you have a repository cloned to your computer, git can be used to montior activity in that repository directory. To see the status of any files in a git repository you can use the command:

git status

If nothing is new in your repository, you might see a message like Your branch is up to date. If you have added new files to your repository it might say you have “untracked files” and them in red text. This means that there are files in your repository, but git is not being used to monitor or track any changes you make to them. If you would like those files to be tracked by git, you can add them to the repository.

The command is:

git add [name of file]

So if I wanted to make sure git knows about a file in my repository folder called test.md, I would use the command.

git add test.md

Running the git status command again will now show that I have “Changes to be committed” and lists the files in green text. To save that file and it’s changes, I now run the command:

git commit -m "This is a commit message"

This command tell git that I’m ready to commit (save) the changes or new file to my repository. The -m associates a message with a git commit and the message is required. Best practice is to describe what you did briefly in present tense.

For example:

$ git commit -m "Add test.md with git demo content"

If all goes well you should not see any errors but you will see some strange hash strings (combinations of letter and numbers) which is good.

This same process applied to files you have modified. So just remember:

  1. git add
  2. git commit -m

Sending Your Commits to the Server

All the commits you make to a git repository are only stored locally on your machine. They are not immediately synchronized with the server. You’ll need to push your commits to the server if you want to submit assignments with GitHub classroom or send your work to a git server. The command is:

$ git push

The command prompt should display some information about transmitting the content to the server. If no errors are mentioned, your work should be submitted to the server.

You can submit your work as many times as necessary before the due date. This will also ensure that your work is backed-up on the server in the untimely event that your computer suffers an unfortunate issue.

IT IS VERY EASY TO FORGET TO ADD A FILE, REMEMBER TO COMMIT, OR FORGET TO PUSH!

Here are some tips to help:

  • The git status command is your friend and will tell you about:
    • Untracked files (new files)
    • Modified files (files you’ve changed)
    • Changes to be committed (changes or file additions/removals ready to be a part of a commit)
  • If you push and git says Everything up-to-date but you know you’ve made changes, you probably forgot to commit

Oops

Sometimes bad things happen. We mess up a file and undo/redo can’t help or delete something accidentally…and empty the trash. It’s okay, git is here to help! Git can recover the most recent version of anything you have commited. This is why it’s very very VERY good to commit often. To recover the most recently commited version of a specific file, you can run the command:

git checkout [name of the file]

NOTE: This will give you the version of the file from the last time you committed (it doesn’t matter if you have pushed). If you haven’t commited since you started you work and delete the file….it’s gone…

Commits are “free”! Get a bit of work done? commit! Want to take a coffee break? Find a good stopping place and commit!

Other resources

Using Git source control in VS Code

Git Tutorials

Git in case of fire