|
2 | 2 | title: "Intro to Git"
|
3 | 3 | ---
|
4 | 4 |
|
| 5 | +Git is a version control system. It allows you to keep the entire history of your code, and makes it easy to share and collaborate with others. |
| 6 | + |
| 7 | +To use git you need to create a **Git repository**. You can think of a git repository like this: |
| 8 | + |
5 | 9 | 
|
6 | 10 |
|
7 |
| -Git is a version control system. It allows you to keep the entire history of your code, and makes it easy to share and collaborate with others. |
| 11 | +- A repository contains lots of **commits**. Each commit is essentially a different version of your files. |
| 12 | +- Commits can be organised into **branches**. A repository can contain many different branches. Branches are often used to develop new application features - that way the main codebase still works while the feature is being developed, and the new code can be *merged* in when it's working. |
| 13 | +- Branches have names, so you can keep track of them. When you first set up a repository, you'll just have one branch and it will have the default name - **master**. |
8 | 14 |
|
9 |
| -### Setting up a git repo |
| 15 | +### What does a git repository look like? |
10 | 16 |
|
11 |
| -Git works on a folder level. To set up a folder to work with git you need to open up the github app and select `File > Add Local Repository ...`. You then need to select the folder you want to control with git. |
| 17 | +On your laptop, a git repository is just a **special type of folder**. Any folder can be made into a git repository (but not all folders should - e.g. don't make your entire documents directory into a git repository, you'll run into problems). You'll normally make a new git repository for each coding project you work on. |
12 | 18 |
|
13 |
| -### Excluding some files |
| 19 | +If you look inside the folder, you won't see all the different versions stored there (they are actually there - just stored inside a hidden folder called `.git`) - you'll only see the files that are in your **working copy**. Normally this working copy will contain the files from the last commit on the master branch, along with any changes you've made. |
14 | 20 |
|
15 |
| -There are some other hidden files (e.g. those used by your operating system) that you don't want to be version controlled. An example of these is the `.DS_Store` file on macs. We'll talk more about this later - for now we'll just quickly exclude them from our repository. |
| 21 | +### Making a commit |
16 | 22 |
|
17 |
| -To do this you need to change the settings on your repository. |
| 23 | +To save some changes into the repository you need to create a commit. When you create a commit you get to write a message, to help remind yourself (and others) what changes you made (and why you made them). Once you've created a commit, you'll be able to come back to this version of your code at any point in the future. |
18 | 24 |
|
19 |
| -1. Open the settings tab of your repository. |
20 |
| -2. Add `.DS_Store` on the first line of the 'Ignored files' section, and press 'Save Changes'. |
| 25 | +Adding files to a git repository is actually a two-stage process: first you have to **add** them to something called the **index** and then you have to **commit** them. This is useful when you get more advanced, but we'll usually do those things together for the time being. |
21 | 26 |
|
22 |
| -If you're on Windows you don't need to worry about this. |
| 27 | + |
23 | 28 |
|
24 |
| -### Committing files to the repository |
| 29 | +{% exercise %} |
| 30 | +1. Set up your `first_site` folder as a git repository. |
| 31 | +2. **Add** your work to the index. |
| 32 | +3. **Commit** your work to the repository, with a message e.g. "Initial import" |
| 33 | +4. Make some change to index.html (in Sublime Text) |
| 34 | +5. **Add** your changes to the index. |
| 35 | +6. **Commit** them to the repository. |
| 36 | +{% endexercise %} |
25 | 37 |
|
26 |
| -Suppose we've now created a file called `index.html` in the folder. It's in the folder but currently isn't being tracked by git. |
27 | 38 |
|
28 |
| -Adding files to a git repository is actually a two-stage process: you have to **add** them and then **commit** them. This is useful when you get more advanced, but we'll usually do those things together for the time being. |
| 39 | +### Git with SourceTree |
29 | 40 |
|
| 41 | +#### Creating a git repository |
30 | 42 |
|
31 |
| -{% exercise %} |
32 |
| -1. Set up your `first_site` folder as a git repository: open up the github app, select `File > Add Local Repository ...` and select the `first_site` folder. |
33 |
| -2. **If you are on a mac**, tell git to ignore your `.DS_Store` files: in the settings panel of the github app, add the line `.DS_Store` in the 'Ignored files' section. |
34 |
| -3. Commit your work to the repository: in the 'Changes' panel, click 'Select all', write a 'Commit Summary' in the box (e.g. "Created index.html") and click 'Commit'. |
35 |
| -6. Make some change to index.html (in Sublime Text) |
36 |
| -7. Go back to the 'Changes' panel. Select and commit your changes, with a 'Commit Summary' describing what you did. |
37 |
| -{% endexercise %} |
| 43 | +1. Open SourceTree |
| 44 | +2. Select File > New / Clone |
| 45 | +3. Select the 'New' option |
| 46 | +4. Navigate to the folder you want to make into a git repository. |
| 47 | + |
| 48 | +#### Adding to the index |
| 49 | + |
| 50 | +1. Select all the files in the working tree section. |
| 51 | +2. Click the "Add" button at the top. |
| 52 | +3. You should see the files move to the 'Index' section |
| 53 | + |
| 54 | +#### Committing to the repository |
| 55 | + |
| 56 | +1. Once you have files in your 'Index' section, click on 'Commit'. |
| 57 | +2. Write a commit message explain what you've done (and maybe why). |
| 58 | +3. Click "Commit" |
| 59 | + |
| 60 | +### Git with the command line |
| 61 | + |
| 62 | +#### Creating a git repository |
| 63 | + |
| 64 | +1. Using the command line, navigate to the folder you want to make into a repository. |
| 65 | +2. **Inside the target folder** run the command |
| 66 | + |
| 67 | + git init |
| 68 | + |
| 69 | +3. To check this worked run the command |
| 70 | + |
| 71 | + git status |
| 72 | + |
| 73 | +#### Adding to the index |
| 74 | + |
| 75 | +1. Inside the folder, run the command |
| 76 | + |
| 77 | + git add --all |
| 78 | + |
| 79 | +3. To check this worked run the command |
| 80 | + |
| 81 | + git status |
| 82 | + |
| 83 | +#### Committing to the repository |
| 84 | + |
| 85 | +1. Inside the folder, run the command |
| 86 | + |
| 87 | + git commit -m "write your message here" |
| 88 | + |
| 89 | +3. To check this worked run the command |
| 90 | + |
| 91 | + git status |
0 commit comments