Junio C Hamano | dfccbb0 | 2008-05-26 01:16:14 | [diff] [blame] | 1 | gittutorial(7) |
| 2 | ============== |
| 3 | |
| 4 | NAME |
| 5 | ---- |
| 6 | gittutorial - A tutorial introduction to git (for version 1.5.1 or newer) |
| 7 | |
| 8 | SYNOPSIS |
| 9 | -------- |
| 10 | git * |
| 11 | |
| 12 | DESCRIPTION |
| 13 | ----------- |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 14 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 15 | This tutorial explains how to import a new project into git, make |
| 16 | changes to it, and share changes with other developers. |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 17 | |
Junio C Hamano | ed7f4f6 | 2007-05-20 09:09:09 | [diff] [blame] | 18 | If you are instead primarily interested in using git to fetch a project, |
| 19 | for example, to test the latest version, you may prefer to start with |
| 20 | the first two chapters of link:user-manual.html[The Git User's Manual]. |
| 21 | |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 22 | First, note that you can get documentation for a command such as |
| 23 | `git log --graph` with: |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 24 | |
| 25 | ------------------------------------------------ |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 26 | $ man git-log |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 27 | ------------------------------------------------ |
| 28 | |
Junio C Hamano | edd2b0a | 2007-01-15 06:12:45 | [diff] [blame] | 29 | It is a good idea to introduce yourself to git with your name and |
| 30 | public email address before doing any operation. The easiest |
| 31 | way to do so is: |
Junio C Hamano | 699660b | 2006-11-29 20:40:10 | [diff] [blame] | 32 | |
| 33 | ------------------------------------------------ |
Junio C Hamano | 7ad22dc | 2007-01-29 02:55:48 | [diff] [blame] | 34 | $ git config --global user.name "Your Name Comes Here" |
| 35 | $ git config --global user.email you@yourdomain.example.com |
Junio C Hamano | 699660b | 2006-11-29 20:40:10 | [diff] [blame] | 36 | ------------------------------------------------ |
| 37 | |
| 38 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 39 | Importing a new project |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 40 | ----------------------- |
| 41 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 42 | Assume you have a tarball project.tar.gz with your initial work. You |
| 43 | can place it under git revision control as follows. |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 44 | |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 45 | ------------------------------------------------ |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 46 | $ tar xzf project.tar.gz |
| 47 | $ cd project |
Junio C Hamano | fc4d38c | 2007-01-08 06:53:32 | [diff] [blame] | 48 | $ git init |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 49 | ------------------------------------------------ |
| 50 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 51 | Git will reply |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 52 | |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 53 | ------------------------------------------------ |
Junio C Hamano | 7d23f5e | 2006-12-16 07:44:04 | [diff] [blame] | 54 | Initialized empty Git repository in .git/ |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 55 | ------------------------------------------------ |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 56 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 57 | You've now initialized the working directory--you may notice a new |
Junio C Hamano | ed7f4f6 | 2007-05-20 09:09:09 | [diff] [blame] | 58 | directory created, named ".git". |
| 59 | |
| 60 | Next, tell git to take a snapshot of the contents of all files under the |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 61 | current directory (note the '.'), with `git-add`: |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 62 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 63 | ------------------------------------------------ |
| 64 | $ git add . |
| 65 | ------------------------------------------------ |
| 66 | |
Junio C Hamano | ed7f4f6 | 2007-05-20 09:09:09 | [diff] [blame] | 67 | This snapshot is now stored in a temporary staging area which git calls |
| 68 | the "index". You can permanently store the contents of the index in the |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 69 | repository with `git-commit`: |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 70 | |
| 71 | ------------------------------------------------ |
Junio C Hamano | 699660b | 2006-11-29 20:40:10 | [diff] [blame] | 72 | $ git commit |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 73 | ------------------------------------------------ |
| 74 | |
Junio C Hamano | ed7f4f6 | 2007-05-20 09:09:09 | [diff] [blame] | 75 | This will prompt you for a commit message. You've now stored the first |
| 76 | version of your project in git. |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 77 | |
Junio C Hamano | 79770b6 | 2007-01-07 07:43:58 | [diff] [blame] | 78 | Making changes |
| 79 | -------------- |
| 80 | |
Junio C Hamano | ed7f4f6 | 2007-05-20 09:09:09 | [diff] [blame] | 81 | Modify some files, then add their updated contents to the index: |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 82 | |
| 83 | ------------------------------------------------ |
Junio C Hamano | 79770b6 | 2007-01-07 07:43:58 | [diff] [blame] | 84 | $ git add file1 file2 file3 |
Junio C Hamano | ed7f4f6 | 2007-05-20 09:09:09 | [diff] [blame] | 85 | ------------------------------------------------ |
| 86 | |
| 87 | You are now ready to commit. You can see what is about to be committed |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 88 | using `git-diff` with the --cached option: |
Junio C Hamano | ed7f4f6 | 2007-05-20 09:09:09 | [diff] [blame] | 89 | |
| 90 | ------------------------------------------------ |
| 91 | $ git diff --cached |
| 92 | ------------------------------------------------ |
| 93 | |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 94 | (Without --cached, `git-diff` will show you any changes that |
Junio C Hamano | ed7f4f6 | 2007-05-20 09:09:09 | [diff] [blame] | 95 | you've made but not yet added to the index.) You can also get a brief |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 96 | summary of the situation with `git-status`: |
Junio C Hamano | ed7f4f6 | 2007-05-20 09:09:09 | [diff] [blame] | 97 | |
| 98 | ------------------------------------------------ |
| 99 | $ git status |
| 100 | # On branch master |
| 101 | # Changes to be committed: |
| 102 | # (use "git reset HEAD <file>..." to unstage) |
| 103 | # |
| 104 | # modified: file1 |
| 105 | # modified: file2 |
| 106 | # modified: file3 |
| 107 | # |
| 108 | ------------------------------------------------ |
| 109 | |
| 110 | If you need to make any further adjustments, do so now, and then add any |
| 111 | newly modified content to the index. Finally, commit your changes with: |
| 112 | |
| 113 | ------------------------------------------------ |
Junio C Hamano | eb69295 | 2007-01-03 22:02:12 | [diff] [blame] | 114 | $ git commit |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 115 | ------------------------------------------------ |
| 116 | |
Junio C Hamano | 6fb124c | 2008-06-13 10:04:01 | [diff] [blame] | 117 | This will again prompt you for a message describing the change, and then |
Junio C Hamano | ed7f4f6 | 2007-05-20 09:09:09 | [diff] [blame] | 118 | record a new version of the project. |
Junio C Hamano | 79770b6 | 2007-01-07 07:43:58 | [diff] [blame] | 119 | |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 120 | Alternatively, instead of running `git-add` beforehand, you can use |
Junio C Hamano | 699660b | 2006-11-29 20:40:10 | [diff] [blame] | 121 | |
| 122 | ------------------------------------------------ |
| 123 | $ git commit -a |
| 124 | ------------------------------------------------ |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 125 | |
Junio C Hamano | ed7f4f6 | 2007-05-20 09:09:09 | [diff] [blame] | 126 | which will automatically notice any modified (but not new) files, add |
| 127 | them to the index, and commit, all in one step. |
Junio C Hamano | 79770b6 | 2007-01-07 07:43:58 | [diff] [blame] | 128 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 129 | A note on commit messages: Though not required, it's a good idea to |
| 130 | begin the commit message with a single short (less than 50 character) |
| 131 | line summarizing the change, followed by a blank line and then a more |
| 132 | thorough description. Tools that turn commits into email, for |
Junio C Hamano | eb69295 | 2007-01-03 22:02:12 | [diff] [blame] | 133 | example, use the first line on the Subject: line and the rest of the |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 134 | commit in the body. |
| 135 | |
Junio C Hamano | e7935c4 | 2006-12-13 21:32:17 | [diff] [blame] | 136 | Git tracks content not files |
| 137 | ---------------------------- |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 138 | |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 139 | Many revision control systems provide an `add` command that tells the |
| 140 | system to start tracking changes to a new file. Git's `add` command |
| 141 | does something simpler and more powerful: `git-add` is used both for new |
Junio C Hamano | ed7f4f6 | 2007-05-20 09:09:09 | [diff] [blame] | 142 | and newly modified files, and in both cases it takes a snapshot of the |
| 143 | given files and stages that content in the index, ready for inclusion in |
| 144 | the next commit. |
Junio C Hamano | e7935c4 | 2006-12-13 21:32:17 | [diff] [blame] | 145 | |
Junio C Hamano | f614c64 | 2007-06-11 01:21:54 | [diff] [blame] | 146 | Viewing project history |
| 147 | ----------------------- |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 148 | |
| 149 | At any point you can view the history of your changes using |
| 150 | |
| 151 | ------------------------------------------------ |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 152 | $ git log |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 153 | ------------------------------------------------ |
| 154 | |
| 155 | If you also want to see complete diffs at each step, use |
| 156 | |
| 157 | ------------------------------------------------ |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 158 | $ git log -p |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 159 | ------------------------------------------------ |
| 160 | |
Junio C Hamano | eb69295 | 2007-01-03 22:02:12 | [diff] [blame] | 161 | Often the overview of the change is useful to get a feel of |
| 162 | each step |
| 163 | |
| 164 | ------------------------------------------------ |
| 165 | $ git log --stat --summary |
| 166 | ------------------------------------------------ |
| 167 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 168 | Managing branches |
| 169 | ----------------- |
| 170 | |
| 171 | A single git repository can maintain multiple branches of |
| 172 | development. To create a new branch named "experimental", use |
| 173 | |
| 174 | ------------------------------------------------ |
| 175 | $ git branch experimental |
| 176 | ------------------------------------------------ |
| 177 | |
| 178 | If you now run |
| 179 | |
| 180 | ------------------------------------------------ |
| 181 | $ git branch |
| 182 | ------------------------------------------------ |
| 183 | |
| 184 | you'll get a list of all existing branches: |
| 185 | |
| 186 | ------------------------------------------------ |
| 187 | experimental |
| 188 | * master |
| 189 | ------------------------------------------------ |
| 190 | |
| 191 | The "experimental" branch is the one you just created, and the |
| 192 | "master" branch is a default branch that was created for you |
| 193 | automatically. The asterisk marks the branch you are currently on; |
| 194 | type |
| 195 | |
| 196 | ------------------------------------------------ |
| 197 | $ git checkout experimental |
| 198 | ------------------------------------------------ |
| 199 | |
| 200 | to switch to the experimental branch. Now edit a file, commit the |
| 201 | change, and switch back to the master branch: |
| 202 | |
| 203 | ------------------------------------------------ |
| 204 | (edit file) |
| 205 | $ git commit -a |
| 206 | $ git checkout master |
| 207 | ------------------------------------------------ |
| 208 | |
| 209 | Check that the change you made is no longer visible, since it was |
| 210 | made on the experimental branch and you're back on the master branch. |
| 211 | |
| 212 | You can make a different change on the master branch: |
| 213 | |
| 214 | ------------------------------------------------ |
| 215 | (edit file) |
| 216 | $ git commit -a |
| 217 | ------------------------------------------------ |
| 218 | |
| 219 | at this point the two branches have diverged, with different changes |
Junio C Hamano | 0df3434 | 2006-11-22 08:28:50 | [diff] [blame] | 220 | made in each. To merge the changes made in experimental into master, run |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 221 | |
| 222 | ------------------------------------------------ |
Junio C Hamano | edd2b0a | 2007-01-15 06:12:45 | [diff] [blame] | 223 | $ git merge experimental |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 224 | ------------------------------------------------ |
| 225 | |
| 226 | If the changes don't conflict, you're done. If there are conflicts, |
| 227 | markers will be left in the problematic files showing the conflict; |
| 228 | |
| 229 | ------------------------------------------------ |
| 230 | $ git diff |
| 231 | ------------------------------------------------ |
| 232 | |
| 233 | will show this. Once you've edited the files to resolve the |
| 234 | conflicts, |
| 235 | |
| 236 | ------------------------------------------------ |
| 237 | $ git commit -a |
| 238 | ------------------------------------------------ |
| 239 | |
| 240 | will commit the result of the merge. Finally, |
| 241 | |
| 242 | ------------------------------------------------ |
| 243 | $ gitk |
| 244 | ------------------------------------------------ |
| 245 | |
| 246 | will show a nice graphical representation of the resulting history. |
| 247 | |
Junio C Hamano | eb69295 | 2007-01-03 22:02:12 | [diff] [blame] | 248 | At this point you could delete the experimental branch with |
| 249 | |
| 250 | ------------------------------------------------ |
| 251 | $ git branch -d experimental |
| 252 | ------------------------------------------------ |
| 253 | |
| 254 | This command ensures that the changes in the experimental branch are |
| 255 | already in the current branch. |
| 256 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 257 | If you develop on a branch crazy-idea, then regret it, you can always |
| 258 | delete the branch with |
| 259 | |
| 260 | ------------------------------------- |
| 261 | $ git branch -D crazy-idea |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 262 | ------------------------------------- |
| 263 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 264 | Branches are cheap and easy, so this is a good way to try something |
| 265 | out. |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 266 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 267 | Using git for collaboration |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 268 | --------------------------- |
| 269 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 270 | Suppose that Alice has started a new project with a git repository in |
| 271 | /home/alice/project, and that Bob, who has a home directory on the |
| 272 | same machine, wants to contribute. |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 273 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 274 | Bob begins with: |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 275 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 276 | ------------------------------------------------ |
| 277 | $ git clone /home/alice/project myrepo |
| 278 | ------------------------------------------------ |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 279 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 280 | This creates a new directory "myrepo" containing a clone of Alice's |
| 281 | repository. The clone is on an equal footing with the original |
Junio C Hamano | 341071d | 2006-06-04 07:24:48 | [diff] [blame] | 282 | project, possessing its own copy of the original project's history. |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 283 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 284 | Bob then makes some changes and commits them: |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 285 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 286 | ------------------------------------------------ |
| 287 | (edit files) |
| 288 | $ git commit -a |
| 289 | (repeat as necessary) |
| 290 | ------------------------------------------------ |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 291 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 292 | When he's ready, he tells Alice to pull changes from the repository |
| 293 | at /home/bob/myrepo. She does this with: |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 294 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 295 | ------------------------------------------------ |
| 296 | $ cd /home/alice/project |
Junio C Hamano | f98fd88 | 2006-11-26 07:28:29 | [diff] [blame] | 297 | $ git pull /home/bob/myrepo master |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 298 | ------------------------------------------------ |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 299 | |
Junio C Hamano | f98fd88 | 2006-11-26 07:28:29 | [diff] [blame] | 300 | This merges the changes from Bob's "master" branch into Alice's |
| 301 | current branch. If Alice has made her own changes in the meantime, |
| 302 | then she may need to manually fix any conflicts. (Note that the |
| 303 | "master" argument in the above command is actually unnecessary, as it |
| 304 | is the default.) |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 305 | |
Junio C Hamano | f98fd88 | 2006-11-26 07:28:29 | [diff] [blame] | 306 | The "pull" command thus performs two operations: it fetches changes |
| 307 | from a remote branch, then merges them into the current branch. |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 308 | |
Junio C Hamano | 35bd025 | 2007-01-17 05:40:22 | [diff] [blame] | 309 | When you are working in a small closely knit group, it is not |
| 310 | unusual to interact with the same repository over and over |
| 311 | again. By defining 'remote' repository shorthand, you can make |
| 312 | it easier: |
| 313 | |
| 314 | ------------------------------------------------ |
| 315 | $ git remote add bob /home/bob/myrepo |
| 316 | ------------------------------------------------ |
| 317 | |
Junio C Hamano | 2a8f6dc | 2007-07-09 08:48:38 | [diff] [blame] | 318 | With this, Alice can perform the first operation alone using the |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 319 | `git-fetch` command without merging them with her own branch, |
Junio C Hamano | 35bd025 | 2007-01-17 05:40:22 | [diff] [blame] | 320 | using: |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 321 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 322 | ------------------------------------- |
Junio C Hamano | 35bd025 | 2007-01-17 05:40:22 | [diff] [blame] | 323 | $ git fetch bob |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 324 | ------------------------------------- |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 325 | |
Junio C Hamano | 35bd025 | 2007-01-17 05:40:22 | [diff] [blame] | 326 | Unlike the longhand form, when Alice fetches from Bob using a |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 327 | remote repository shorthand set up with `git-remote`, what was |
Junio C Hamano | 35bd025 | 2007-01-17 05:40:22 | [diff] [blame] | 328 | fetched is stored in a remote tracking branch, in this case |
| 329 | `bob/master`. So after this: |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 330 | |
| 331 | ------------------------------------- |
Junio C Hamano | 35bd025 | 2007-01-17 05:40:22 | [diff] [blame] | 332 | $ git log -p master..bob/master |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 333 | ------------------------------------- |
| 334 | |
| 335 | shows a list of all the changes that Bob made since he branched from |
| 336 | Alice's master branch. |
| 337 | |
Junio C Hamano | 35bd025 | 2007-01-17 05:40:22 | [diff] [blame] | 338 | After examining those changes, Alice |
Junio C Hamano | edd2b0a | 2007-01-15 06:12:45 | [diff] [blame] | 339 | could merge the changes into her master branch: |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 340 | |
| 341 | ------------------------------------- |
Junio C Hamano | 35bd025 | 2007-01-17 05:40:22 | [diff] [blame] | 342 | $ git merge bob/master |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 343 | ------------------------------------- |
| 344 | |
Junio C Hamano | 35bd025 | 2007-01-17 05:40:22 | [diff] [blame] | 345 | This `merge` can also be done by 'pulling from her own remote |
| 346 | tracking branch', like this: |
Junio C Hamano | f98fd88 | 2006-11-26 07:28:29 | [diff] [blame] | 347 | |
| 348 | ------------------------------------- |
Junio C Hamano | 35bd025 | 2007-01-17 05:40:22 | [diff] [blame] | 349 | $ git pull . remotes/bob/master |
Junio C Hamano | f98fd88 | 2006-11-26 07:28:29 | [diff] [blame] | 350 | ------------------------------------- |
| 351 | |
Junio C Hamano | 35bd025 | 2007-01-17 05:40:22 | [diff] [blame] | 352 | Note that git pull always merges into the current branch, |
Junio C Hamano | a638742 | 2007-08-25 03:54:27 | [diff] [blame] | 353 | regardless of what else is given on the command line. |
Junio C Hamano | f98fd88 | 2006-11-26 07:28:29 | [diff] [blame] | 354 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 355 | Later, Bob can update his repo with Alice's latest changes using |
| 356 | |
| 357 | ------------------------------------- |
| 358 | $ git pull |
| 359 | ------------------------------------- |
| 360 | |
| 361 | Note that he doesn't need to give the path to Alice's repository; |
| 362 | when Bob cloned Alice's repository, git stored the location of her |
Junio C Hamano | d3361ad | 2007-01-01 03:20:24 | [diff] [blame] | 363 | repository in the repository configuration, and that location is |
| 364 | used for pulls: |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 365 | |
| 366 | ------------------------------------- |
Junio C Hamano | 7ad22dc | 2007-01-29 02:55:48 | [diff] [blame] | 367 | $ git config --get remote.origin.url |
Junio C Hamano | 330aae6 | 2007-07-06 17:01:58 | [diff] [blame] | 368 | /home/alice/project |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 369 | ------------------------------------- |
| 370 | |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 371 | (The complete configuration created by `git-clone` is visible using |
| 372 | `git config -l`, and the linkgit:git-config[1] man page |
Junio C Hamano | d3361ad | 2007-01-01 03:20:24 | [diff] [blame] | 373 | explains the meaning of each option.) |
| 374 | |
| 375 | Git also keeps a pristine copy of Alice's master branch under the |
| 376 | name "origin/master": |
| 377 | |
| 378 | ------------------------------------- |
| 379 | $ git branch -r |
| 380 | origin/master |
| 381 | ------------------------------------- |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 382 | |
| 383 | If Bob later decides to work from a different host, he can still |
| 384 | perform clones and pulls using the ssh protocol: |
| 385 | |
| 386 | ------------------------------------- |
| 387 | $ git clone alice.org:/home/alice/project myrepo |
| 388 | ------------------------------------- |
| 389 | |
| 390 | Alternatively, git has a native protocol, or can use rsync or http; |
Junio C Hamano | 35738e8 | 2008-01-07 07:55:46 | [diff] [blame] | 391 | see linkgit:git-pull[1] for details. |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 392 | |
| 393 | Git can also be used in a CVS-like mode, with a central repository |
Junio C Hamano | 35738e8 | 2008-01-07 07:55:46 | [diff] [blame] | 394 | that various users push changes to; see linkgit:git-push[1] and |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 395 | linkgit:gitcvs-migration[7]. |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 396 | |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 397 | Exploring history |
| 398 | ----------------- |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 399 | |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 400 | Git history is represented as a series of interrelated commits. We |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 401 | have already seen that the `git-log` command can list those commits. |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 402 | Note that first line of each git log entry also gives a name for the |
| 403 | commit: |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 404 | |
| 405 | ------------------------------------- |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 406 | $ git log |
| 407 | commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7 |
| 408 | Author: Junio C Hamano <junkio@cox.net> |
| 409 | Date: Tue May 16 17:18:22 2006 -0700 |
| 410 | |
| 411 | merge-base: Clarify the comments on post processing. |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 412 | ------------------------------------- |
| 413 | |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 414 | We can give this name to `git-show` to see the details about this |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 415 | commit. |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 416 | |
| 417 | ------------------------------------- |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 418 | $ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7 |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 419 | ------------------------------------- |
| 420 | |
Junio C Hamano | eb69295 | 2007-01-03 22:02:12 | [diff] [blame] | 421 | But there are other ways to refer to commits. You can use any initial |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 422 | part of the name that is long enough to uniquely identify the commit: |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 423 | |
| 424 | ------------------------------------- |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 425 | $ git show c82a22c39c # the first few characters of the name are |
| 426 | # usually enough |
| 427 | $ git show HEAD # the tip of the current branch |
| 428 | $ git show experimental # the tip of the "experimental" branch |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 429 | ------------------------------------- |
| 430 | |
Junio C Hamano | eb69295 | 2007-01-03 22:02:12 | [diff] [blame] | 431 | Every commit usually has one "parent" commit |
| 432 | which points to the previous state of the project: |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 433 | |
| 434 | ------------------------------------- |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 435 | $ git show HEAD^ # to see the parent of HEAD |
| 436 | $ git show HEAD^^ # to see the grandparent of HEAD |
| 437 | $ git show HEAD~4 # to see the great-great grandparent of HEAD |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 438 | ------------------------------------- |
| 439 | |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 440 | Note that merge commits may have more than one parent: |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 441 | |
| 442 | ------------------------------------- |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 443 | $ git show HEAD^1 # show the first parent of HEAD (same as HEAD^) |
| 444 | $ git show HEAD^2 # show the second parent of HEAD |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 445 | ------------------------------------- |
| 446 | |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 447 | You can also give commits names of your own; after running |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 448 | |
| 449 | ------------------------------------- |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 450 | $ git tag v2.5 1b2e1d63ff |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 451 | ------------------------------------- |
| 452 | |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 453 | you can refer to 1b2e1d63ff by the name "v2.5". If you intend to |
| 454 | share this name with other people (for example, to identify a release |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 455 | version), you should create a "tag" object, and perhaps sign it; see |
Junio C Hamano | 35738e8 | 2008-01-07 07:55:46 | [diff] [blame] | 456 | linkgit:git-tag[1] for details. |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 457 | |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 458 | Any git command that needs to know a commit can take any of these |
| 459 | names. For example: |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 460 | |
| 461 | ------------------------------------- |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 462 | $ git diff v2.5 HEAD # compare the current HEAD to v2.5 |
| 463 | $ git branch stable v2.5 # start a new branch named "stable" based |
| 464 | # at v2.5 |
| 465 | $ git reset --hard HEAD^ # reset your current branch and working |
Junio C Hamano | 33db437 | 2006-06-07 19:51:45 | [diff] [blame] | 466 | # directory to its state at HEAD^ |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 467 | ------------------------------------- |
| 468 | |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 469 | Be careful with that last command: in addition to losing any changes |
| 470 | in the working directory, it will also remove all later commits from |
| 471 | this branch. If this branch is the only branch containing those |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 472 | commits, they will be lost. Also, don't use `git-reset` on a |
Junio C Hamano | ee1e428 | 2007-02-04 08:32:04 | [diff] [blame] | 473 | publicly-visible branch that other developers pull from, as it will |
| 474 | force needless merges on other developers to clean up the history. |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 475 | If you need to undo changes that you have pushed, use `git-revert` |
Junio C Hamano | 16cf158 | 2007-02-05 07:21:48 | [diff] [blame] | 476 | instead. |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 477 | |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 478 | The `git-grep` command can search for strings in any version of your |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 479 | project, so |
| 480 | |
| 481 | ------------------------------------- |
| 482 | $ git grep "hello" v2.5 |
| 483 | ------------------------------------- |
| 484 | |
Junio C Hamano | 341071d | 2006-06-04 07:24:48 | [diff] [blame] | 485 | searches for all occurrences of "hello" in v2.5. |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 486 | |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 487 | If you leave out the commit name, `git-grep` will search any of the |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 488 | files it manages in your current directory. So |
| 489 | |
| 490 | ------------------------------------- |
| 491 | $ git grep "hello" |
| 492 | ------------------------------------- |
| 493 | |
| 494 | is a quick way to search just the files that are tracked by git. |
| 495 | |
| 496 | Many git commands also take sets of commits, which can be specified |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 497 | in a number of ways. Here are some examples with `git-log`: |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 498 | |
| 499 | ------------------------------------- |
| 500 | $ git log v2.5..v2.6 # commits between v2.5 and v2.6 |
| 501 | $ git log v2.5.. # commits since v2.5 |
| 502 | $ git log --since="2 weeks ago" # commits from the last 2 weeks |
| 503 | $ git log v2.5.. Makefile # commits since v2.5 which modify |
| 504 | # Makefile |
| 505 | ------------------------------------- |
| 506 | |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 507 | You can also give `git-log` a "range" of commits where the first is not |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 508 | necessarily an ancestor of the second; for example, if the tips of |
| 509 | the branches "stable-release" and "master" diverged from a common |
| 510 | commit some time ago, then |
| 511 | |
| 512 | ------------------------------------- |
| 513 | $ git log stable..experimental |
| 514 | ------------------------------------- |
| 515 | |
| 516 | will list commits made in the experimental branch but not in the |
| 517 | stable branch, while |
| 518 | |
| 519 | ------------------------------------- |
| 520 | $ git log experimental..stable |
| 521 | ------------------------------------- |
| 522 | |
| 523 | will show the list of commits made on the stable branch but not |
| 524 | the experimental branch. |
| 525 | |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 526 | The `git-log` command has a weakness: it must present commits in a |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 527 | list. When the history has lines of development that diverged and |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 528 | then merged back together, the order in which `git-log` presents |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 529 | those commits is meaningless. |
| 530 | |
| 531 | Most projects with multiple contributors (such as the linux kernel, |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 532 | or git itself) have frequent merges, and `gitk` does a better job of |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 533 | visualizing their history. For example, |
| 534 | |
| 535 | ------------------------------------- |
| 536 | $ gitk --since="2 weeks ago" drivers/ |
| 537 | ------------------------------------- |
| 538 | |
| 539 | allows you to browse any commits from the last 2 weeks of commits |
Junio C Hamano | bb8fb05 | 2006-05-30 07:21:12 | [diff] [blame] | 540 | that modified files under the "drivers" directory. (Note: you can |
| 541 | adjust gitk's fonts by holding down the control key while pressing |
| 542 | "-" or "+".) |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 543 | |
| 544 | Finally, most commands that take filenames will optionally allow you |
| 545 | to precede any filename by a commit, to specify a particular version |
Junio C Hamano | bb8fb05 | 2006-05-30 07:21:12 | [diff] [blame] | 546 | of the file: |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 547 | |
| 548 | ------------------------------------- |
| 549 | $ git diff v2.5:Makefile HEAD:Makefile.in |
| 550 | ------------------------------------- |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 551 | |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 552 | You can also use `git-show` to see any such file: |
Junio C Hamano | bb8fb05 | 2006-05-30 07:21:12 | [diff] [blame] | 553 | |
| 554 | ------------------------------------- |
Junio C Hamano | eb69295 | 2007-01-03 22:02:12 | [diff] [blame] | 555 | $ git show v2.5:Makefile |
Junio C Hamano | bb8fb05 | 2006-05-30 07:21:12 | [diff] [blame] | 556 | ------------------------------------- |
| 557 | |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 558 | Next Steps |
| 559 | ---------- |
| 560 | |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 561 | This tutorial should be enough to perform basic distributed revision |
| 562 | control for your projects. However, to fully understand the depth |
| 563 | and power of git you need to understand two simple ideas on which it |
| 564 | is based: |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 565 | |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 566 | * The object database is the rather elegant system used to |
| 567 | store the history of your project--files, directories, and |
| 568 | commits. |
| 569 | |
| 570 | * The index file is a cache of the state of a directory tree, |
| 571 | used to create commits, check out working directories, and |
| 572 | hold the various trees involved in a merge. |
| 573 | |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 574 | Part two of this tutorial explains the object |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 575 | database, the index file, and a few other odds and ends that you'll |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 576 | need to make the most of git. You can find it at linkgit:gittutorial-2[7]. |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 577 | |
Junio C Hamano | ed7f4f6 | 2007-05-20 09:09:09 | [diff] [blame] | 578 | If you don't want to continue with that right away, a few other |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 579 | digressions that may be interesting at this point are: |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 580 | |
Junio C Hamano | 35738e8 | 2008-01-07 07:55:46 | [diff] [blame] | 581 | * linkgit:git-format-patch[1], linkgit:git-am[1]: These convert |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 582 | series of git commits into emailed patches, and vice versa, |
| 583 | useful for projects such as the linux kernel which rely heavily |
| 584 | on emailed patches. |
| 585 | |
Junio C Hamano | 35738e8 | 2008-01-07 07:55:46 | [diff] [blame] | 586 | * linkgit:git-bisect[1]: When there is a regression in your |
Junio C Hamano | c2b0a49 | 2006-01-23 07:54:36 | [diff] [blame] | 587 | project, one way to track down the bug is by searching through |
| 588 | the history to find the exact commit that's to blame. Git bisect |
| 589 | can help you perform a binary search for that commit. It is |
| 590 | smart enough to perform a close-to-optimal search even in the |
| 591 | case of complex non-linear history with lots of merged branches. |
| 592 | |
Junio C Hamano | 341071d | 2006-06-04 07:24:48 | [diff] [blame] | 593 | * link:everyday.html[Everyday GIT with 20 Commands Or So] |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 594 | |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame^] | 595 | * linkgit:gitcvs-migration[7]: Git for CVS users. |
Junio C Hamano | dfccbb0 | 2008-05-26 01:16:14 | [diff] [blame] | 596 | |
| 597 | SEE ALSO |
| 598 | -------- |
| 599 | linkgit:gittutorial-2[7], |
| 600 | linkgit:gitcvs-migration[7], |
Junio C Hamano | 9e1793f | 2008-06-02 07:31:16 | [diff] [blame] | 601 | linkgit:gitcore-tutorial[7], |
| 602 | linkgit:gitglossary[7], |
Junio C Hamano | dfccbb0 | 2008-05-26 01:16:14 | [diff] [blame] | 603 | link:everyday.html[Everyday git], |
| 604 | link:user-manual.html[The Git User's Manual] |
| 605 | |
| 606 | GIT |
| 607 | --- |
Junio C Hamano | f7c042d | 2008-06-06 22:50:53 | [diff] [blame] | 608 | Part of the linkgit:git[1] suite. |