Added section on rename and copy detection
[gitmagic/gitmagic.git] / clone.txt
blob4e9bab42d86c5e984efb175f15a4f37823e4320e
1 = Cloning Around =
3 In older version control systems, the standard way to get a saved state was checkout. You'd checkout a bunch of files in the requested saved state.
5 In Git and other distributed version control systems, cloning is the standard operation. To get files you create a clone of the entire repository. In other words, you practically create a mirror of the central server. So anything the main repository can do, you can do.
7 == Sync Computers ==
9 This is the reason I first used Git. I can tolerate making tarballs or using *rsync* for backups and basic syncing. The problem was sometimes I'd edit on my laptop, other times on my desktop, and they may not have talked to each other in between.
11 Initialize a Git repository and commit your files as above on one machine. Then on the other:
13  $ git-clone other.computer:/path/to/files
15 to create a second copy of the files and Git respository. From now on,
17  $ git-commit -a
18  $ git-pull other.computer:/path/to/files
20 will pull in the state of the files on the other computer into the one you're working on. If you've recently made conflicting edits in the same file, Git will let you know and you should commit again after resolving them.
22 If you have not committed any changes on the other computer, you may type
24  $ git-push other.computer:/path/to/files
26 to push the current state to the other computer. Next time you're logged in to the other computer, run
28  $ git-checkout HEAD .
30 to update the files before working on them. Be aware that you'll lose any uncommitted changes when you do this.
32 == Classic Source Control ==
34 Initialize a Git repository for your files: 
36  $ git-init-db
37  $ git add .
38  $ git commit -m "Initial commit"
40 On the central server, initialize an empty Git repository with some name:
42  $ GIT_DIR=proj.git git-init-db
44 and start the Git daemon if necessary:
46  $ git-daemon --detach
48 Some public hosts, such as [[http://repo.or.cz][repo.or.cz]], will have a different method for setting up the initially empty Git repository, such as filling in a form on a webpage.
50 Push your project to the central server with:
52  $ git-push git://central.server/path/to/proj.git HEAD
54 We're ready. To check out source, a developer types
56  $ git clone git://central.server/path/to/proj.git
58 After making changes, the code is checked in to the main server by:
60  $ git commit -a
61  $ git push
63 If the main server has been updated, the latest version needs to be checked out before the push. To sync to the latest version:
65  $ git commit -a
66  $ git pull
68 A popular misconception is that Git is ill-suited for projects requiring an official central repository (see for example [[http://www.subversionary.org/martintomes/git][this blog post]]). Nothing could be further from the truth. Photographing someone does not cause their soul to be stolen. Similarly, cloning the master repository does not make its importance diminish.
70 A good first approximation is that anything a centralized version control system can do, Git can do better. While not strictly true, one is less likely to make erroneous comparisons with this rule of thumb.
72 You might not use most of Git's features in a small project, but there is nothing wrong with this as there is negligible overhead. Moreover, your project may grow beyond your original expectations. It's like using a Swiss army knife to mostly open bottles. On the day you desperately need a screwdriver you'll be glad you have it with you.
74 == Forking a Project ==
76 Sick of the way a project is being run? Think you could do a better job? Then on your server:
78  $ git clone git://main.server/path/to/files
80 Next tell everyone to check out your fork of the project at your server.
82 At any later time, you can merge in the changes from the original project with:
84  $ git pull
86 == Ultimate Backups ==
88 Want numerous tamper-proof geographically diverse redundant archives? If your project has many developers, don't do anything! Every clone of your code is effectively a backup. Not just of the current state of the project, but of your project's entire history. Thanks to cryptographic hashing, if anyone's clone becomes corrupted, it will be spotted as soon as they try to communicate with others.
90 If your project is not so popular, find as many servers as you can to host clones.
92 The truly paranoid should always write down the latest 20-byte SHA1 hash of the HEAD somewhere safe. It has to be safe, not private. For example, publishing it in a newspaper would work well, because it's hard for an attacker to alter every copy of a newspaper.
94 == Light-Speed Multitask ==
96 Say you want to work on several features in parallel. Then after committing your project:
98  $ git-clone --local --shared . /some/new/directory
100 The options can be written as *-l -s* instead, which I like to think of as an acronym for "Light Speed"!
102 Git exploits hard links and file sharing as much as possible to create this clone. Subsequently, this clone is unsuitable as a backup. However, it's ready in a flash, and you can now work on two independent features simultaneously. For example, you can edit one clone while the other is compiling.
104 At any time, you can commit and pull changes the other clone.
106  $ git-pull /the/other/clone
108 == Guerilla Version Control ==
110 Are you working on a project that uses some other version control system, and you sorely miss Git? Then initialize a Git repository in your working directory:
112  $ git-init-db
113  $ git add .
114  $ git commit -m "Initial commit"
116 then clone it:
118  $ git-clone -l -s  . /some/new/directory
120 Now go to the new directory and work here instead, using Git to your heart's content. Once in a while, you'll want to sync with everyone else, in which case go to the original directory, sync using the other version control system, and type:
122  $ git add .
123  $ git commit -m "sync with everyone else"
125 Then go to the new directory and run:
127  $ git commit -a -m "description of my changes"
128  $ git pull
130 On the other hand, the procedure for giving your changes to everyone else varies depending on the other version control system. The new directory contains the files with your changes. Run whatever commands of the other version control system are neeeded to upload them to the central repository.