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.
git clone [repostitor url]
git add [files you have added or modified]
git commit -m "Commit message here"
git push
You only ever need to clone once. Repeat steps 2-4 as needed to submit assignments.
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 GitHub). 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]
NOTE: The $
is just to indicate we are working in a terminal. The command you type is everything AFTER the $
.
Git repository URLs from GitHub look like this:
https://github.com/dtg3/cs110-examples-s23.git
To clone the above repository, the command would look like this:
$ git clone https://github.com/dtg3/cs110-examples-s23.git
Since we installed and setup gh
you should not need login credentials for public or private GitHub repostiories. As long as the URL is correct, 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.
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:
git add
git commit -m
All the commits you make to a git repository are only stored locally on your machine. They are not immediately synchonized with the server. You’ll need to push
your commits to GitHub to backup your work and submit assignments. 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 GitHub.
Since we use GitHub, you can always make sure that your code it up-to-date by checking your repository link. You can submit your work as many times as necessary before the due date which is HIGHLY recommended as this will also ensure that your work is backed-up on the server in the event that your computer suffers an unfortunate issue.
Here are some tips to help:
git status
command is your friend and will tell you about:
Everything up-to-date
but you know you’ve made changes, you probably forgot to commit
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
!