| 1 | --- |
| 2 | layout: reference |
| 3 | --- |
| 4 | |
| 5 | <div class="box"> |
| 6 | <h2>Getting and Creating Projects</h2> |
| 7 | <div class="block"> |
| 8 | <p> |
| 9 | In order to do anything in Git, you need to have a Git repository. This |
| 10 | is where Git stores the data for the snapshots you are saving. |
| 11 | </p> |
| 12 | |
| 13 | <p> |
| 14 | There are two main ways to get a Git repository. One way is to simply |
| 15 | initialize a new one from an existing directory, such as a new project or |
| 16 | a project new to source control. The second way is to clone one from a |
| 17 | public Git repository, as you would do if you wanted a copy or wanted to |
| 18 | work with someone on a project. We will cover both of these here. |
| 19 | </p> |
| 20 | |
| 21 | </div> |
| 22 | </div> |
| 23 | |
| 24 | <div class="box"> |
| 25 | <h2> |
| 26 | <span class="docs"> |
| 27 | <a target="new" href="http://git-scm.com/docs/git-init">docs</a> |
| 28 | <a target="new" href="http://git-scm.com/book/en/Git-Basics-Getting-a-Git-Repository#Initializing-a-Repository-in-an-Existing-Directory">book</a> |
| 29 | </span> |
| 30 | <a name="init">git init</a> |
| 31 | <span class="desc">initializes a directory as a Git repository</span> |
| 32 | </h2> |
| 33 | |
| 34 | <div class="block"> |
| 35 | <p>To create a repository from an existing directory of files, you can |
| 36 | simply run <code>git init</code> in that directory. For example, |
| 37 | let's say we have a directory with a few files in it, like this: |
| 38 | </p> |
| 39 | |
| 40 | <pre> |
| 41 | <b>$ cd konnichiwa</b> |
| 42 | <b>$ ls</b> |
| 43 | README hello.rb |
| 44 | </pre> |
| 45 | |
| 46 | <p>This is a project where we are writing examples of the "Hello World" |
| 47 | program in every language. So far, we just have Ruby, but hey, it's |
| 48 | a start. To start version controlling this with Git, we can simply |
| 49 | run <code>git init</code>. |
| 50 | </p> |
| 51 | |
| 52 | <pre> |
| 53 | <b>$ git init</b> |
| 54 | Initialized empty Git repository in /opt/konnichiwa/.git/ |
| 55 | </pre> |
| 56 | |
| 57 | <p>Now you can see that there is a <code>.git</code> subdirectory in your |
| 58 | project. This is your Git repository where all the data of your |
| 59 | project snapshots are stored. |
| 60 | </p> |
| 61 | |
| 62 | <pre> |
| 63 | <b>$ ls -a</b> |
| 64 | . .. .git README hello.rb |
| 65 | </pre> |
| 66 | |
| 67 | <p>Congratulations, you now have a skeleton Git repository and can start |
| 68 | snapshotting your project. |
| 69 | </p> |
| 70 | |
| 71 | <p class="nutshell"> |
| 72 | <strong>In a nutshell</strong>, you use <code>git init</code> to make an |
| 73 | existing directory of content into a new Git repository. You can do this |
| 74 | in any directory at any time, completely locally.</p> |
| 75 | |
| 76 | </div> |
| 77 | </div> |
| 78 | |
| 79 | <div class="box"> |
| 80 | <h2> |
| 81 | <span class="docs"> |
| 82 | <a target="new" href="http://git-scm.com/docs/git-clone">docs</a> |
| 83 | <a target="new" href="http://git-scm.com/book/en/Git-Basics-Getting-a-Git-Repository#Cloning-an-Existing-Repository">book</a> |
| 84 | </span> |
| 85 | <a name="clone">git clone</a> |
| 86 | <span class="desc">copy a git repository so you can add to it</span> |
| 87 | </h2> |
| 88 | <div class="block"> |
| 89 | <p> |
| 90 | If you need to collaborate with someone on a project, or if you want to |
| 91 | get a copy of a project so you can look at or use the code, you will |
| 92 | clone it. You simply run the <code>git clone [url]</code> command with |
| 93 | the URL of the project you want to copy. |
| 94 | </p> |
| 95 | |
| 96 | <pre> |
| 97 | <b>$ git clone git://github.com/schacon/simplegit.git</b> |
| 98 | Initialized empty Git repository in /private/tmp/simplegit/.git/ |
| 99 | remote: Counting objects: 100, done. |
| 100 | remote: Compressing objects: 100% (86/86), done. |
| 101 | remote: Total 100 (delta 35), reused 0 (delta 0) |
| 102 | Receiving objects: 100% (100/100), 9.51 KiB, done. |
| 103 | Resolving deltas: 100% (35/35), done. |
| 104 | <b>$ cd simplegit/</b> |
| 105 | <b>$ ls</b> |
| 106 | README Rakefile <span class="blue">lib</span> |
| 107 | </pre> |
| 108 | |
| 109 | <p> |
| 110 | This will copy the entire history of that project so you have it locally |
| 111 | and it will give you a working directory of the main branch of that project |
| 112 | so you can look at the code or start editing it. If you change into the |
| 113 | new directory, you can see the <code>.git</code> subdirectory - that is |
| 114 | where all the project data is. |
| 115 | </p> |
| 116 | |
| 117 | <pre> |
| 118 | <b>$ ls -a</b> |
| 119 | . .. <span class="blue">.git</span> README Rakefile <span class="blue">lib</span> |
| 120 | <b>$ cd .git</b> |
| 121 | <b>$ ls</b> |
| 122 | HEAD description <span class="blue">info</span> packed-refs |
| 123 | <span class="blue">branches hooks logs refs</span> |
| 124 | config index <span class="blue">objects</span> |
| 125 | </pre> |
| 126 | |
| 127 | <p> |
| 128 | By default, Git will create a directory that is the same name as the |
| 129 | project in the URL you give it - basically whatever is after the last slash |
| 130 | of the URL. If you want something different, you can just put it at the |
| 131 | end of the command, after the URL. |
| 132 | </p> |
| 133 | |
| 134 | <p class="nutshell"> |
| 135 | <strong>In a nutshell</strong>, you use <code>git clone</code> to get a |
| 136 | local copy of a Git repository so you can look at it or start modifying |
| 137 | it.</p> |
| 138 | |
| 139 | </div> |
| 140 | </div> |
| 141 | |
| 142 | <p><a class="page-button next-page" href="{{ site.baseurl }}/basic">On to Basic Snapshotting »</a></p> |