<style>
.markdown-body h1:first-of-type {
margin-top: 24px;
}
.markdown-body h1 {
margin-top: 64px;
}
.markdown-body h1 + h2 {
margin-top: 32px;
}
.markdown-body h2 {
margin-top: 48px;
}
.markdown-body h2.topics {
font-size: 1.8em;
border-bottom: none;
}
.markdown-body h2 {
color: #5a57b8;
}
.markdown-body h3 {
color: cornflowerblue;
}
.markdown-body h4 {
color: cornflowerblue;
}
.exercise {
font-size: 150%;
font-weight: bold;
color: rgb(227,112,183);
}
.note {
color: red;
}
.markdown-body em {
color: #e000e0;
}
.markdown-body strong {
color: #e000e0;
font-weight: bold;
}
.markdown-body p a,
.markdown-body td a {
border-bottom: 1px solid;
}
.markdown-body a:hover {
border-bottom: none;
text-decoration: none;
}
</style>
# Git Reference
Git is 'version-control' software that allows you to:
- Backup your files to avoid losing work
- Record changes to your files over time
- Switch between different versions of your files, and
- Collaborate efficiently with other developers
(and more) using a small number of terminal commands.
Git revolves around the idea of 'repositories' - hidden folders created on your filesystem that contain histories of your projects. At any point in time you can create a snapshot (called a 'commit') of how your files look, make a backup of those files (by 'pushing' to a remote URL), and much more.
In ACIT 1515 we will only need a subset of the available git commands: `clone`, `add`, `commit`, and `push`.
## Cloning a remote repository
For each assignment in this course you will be provided with starter code that is stored on Github - an online host for git repositories. A Python script has been provided that allows you to automate the process of downloading a copy of ('cloning') the repository, but if need be you can manually download a copy using the `git clone` command.
`git clone` requires at least one argument: the URL of the remote repository. For this course the URL will look something like `git@github.com:CIT-BCIT/assignmentX.git`, but the actual URL will be given to you each week by your instructor.
The example below includes a second argument: the name of the folder that the repo (repository) will be stored in. This will be required for all assignments in ACIT 1515 so that the starter code works correctly.
```shell
git clone <url> <foldername>
```
## Staging files
Imagine that you are working on assignment 1 in ACIT1515. You are in the terminal, having navigated to the folder (named assignment1) that contains all the files, and you begin to make changes to a file named assignment1.py.
Once you have made enough changes to necessitate creating a commit - for example you have completed one of the requirements for the assignment and want to save your progress - you need to first notify Git that the file has changed. This is referred to as 'staging' the file: adding it to a list of things that will be included in the next commit.
To 'stage' a single file, you can use the `git add` command:
```shell
git add <filename>
```
or you can stage all new, modified, or deleted files using:
```shell
git add --all
```
## Committing changes
Once a file/files have been staged, a commit can be created using:
```shell
git commit -m "Add a description of the changes here"
```
and once the commit is complete, you now have a complete snapshot of the files so that you can safely continue working, knowing that you can fall back to a working version if necessary.
## Pushing changes
The final step in the cycle of working on the code, adding files, and creating commits is uploading (called 'pushing') those commits to the remote Github repository. This is the equivalent of submitting your assignment, and can be done as many times as necessary during the development process.
Use the following command to push your changes:
```shell
git push origin main
```
Note that you do not need to specify a URL! Once the repository has been cloned, Git remembers where it was downloaded from. `origin` is a reference to that Github repository (and `main` is the default branch of that repository)
## Summing up
### clone
`git clone <url> <foldername>` is used to download a copy of the repository. This only needs to be done once
### add
`git add <filename>` tells Git that some changes have been made, and those changes should be included in the next commit
### commit
`git commit -m "Description here please!"` creates a commit - a snapshot of all the changes that occurred since the last commit
### push
`git push origin main` uploads and backs up your commit to the remote Github repository
<!--
## Next steps: creating an SSH key
See the SSH reference on the Learning Hub under Content > Week 1 for how to create an SSH key. This will allow to perform the steps above without constantly needing to enter your password!
-->