Second chapter
[gitmagic.git] / basic.txt
blob83172524c3f27749862d95e2a44ac4ad69142c12
1 = Basic Git Tricks =
3 == Instant Backup ==
5 When I'm about to attempt something drastic I like to save the current state, so I can go back and try again should things go awry.
7  $ git-init
8  $ git-add .
9  $ git-commit -m "Initial commit"
11 to take a snapshot of all files in the current directory.
12 Then if something goes wrong type
14  $ git-reset --hard
16 to go back to where you were. To save the state again, type <command>git-commit -a</command> and provide a description.
18 One benefit to doing this instead of simply copying files is that with Git's hash chaining, you can tell if a backup gets corrupted.
20 == Undo/Redo History ==
22 More generally, you can do the above, and every so often "save the game" by typing
24  $ git-commit -a -m "description of current state"
26 Note if you want to keep track of newly added files or forget about deleted files you'll need to first run <command>git-add</command> or <command>git-delete</command> accordingly.
28 Typing git-log shows you a list of recent commits, and their SHA1 hashes. Then typing
30  $ git-commit -a
31  $ git-revert SHA1_HASH
33 will restore the state to the commit with the given hash. You might like to use something like the following instead:
35  $ git-revert "@{10 minutes ago}"
37 You can undo the undo: type git-log and you'll see that the other commits you made are still there.
39 Typing
41  $ git-checkout SHA1_HASH .
43 loads a saved state without recording the fact that you've gone back to an old state. This is sometimes what you want.
45 To take the computer game analogy again, git-checkout is like loading a game, git-revert is like loading a game and recording this fact as another saved game, and git-reset --hard is like loading an old save and deleting all saved games newer than the one just loaded.
47 == Synchronize Files Between Computers ==
49 This is the reason I first used Git. I can make tarballs or use rsync to do backups. 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.
51 Initialize a Git repository and commit your files as above on one machine. Then on the other:
53  $ git-clone git+ssh://other.computer/directory
55 to get a second copy. From now on,
57  $ git-commit -a
58  $ git-pull git+ssh://other.computer/directory
60 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 resolve them with another edit and commit.
62 If you have not committed any changes on the other computer, you may type
64  $ git-push git+ssh://other.computer/directory
66 to push the current state to the other computer. Next time you're there, run git-reset --hard or git-checkout HEAD . to update the files.
68 That Git does not do the last step automatically is good in case you were in the middle of some uncommitted changes.