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