Update the "Automatic compression" entry to reflect the new git gc behaviour
[gitmagic.git] / basic.txt
blob55a4640bedd9011871c14bc065500a20d7d2bf83
1 == Basic Tricks ==
3 Rather than diving into a sea of Git commands, use these elementary examples to get your feet wet. Despite their simplicity, each of them are useful in real life.
5 === Saving State ===
7 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.
9  $ git init
10  $ git add .
11  $ git commit -m "My first backup"
13 to take a snapshot of all files in the current directory.
15 The above sequence of commands should be memorized, or placed in a script, as they will be reused frequently.
17 Then if something goes wrong, run:
19  $ git reset --hard
21 to go back to where you were. To save the state again:
23  $ git commit -a -m "Another backup"
25 ==== Add, Delete, Rename ====
27 The above will only keep track of the files that were present when you first ran *git add*. If you add new files or subdirectories, you'll have to tell Git:
29  $ git add NEWFILES...
31 Similarly, if you want Git to forget about certain files, maybe because you've deleted them
33  $ git rm OLDFILES...
35 Renaming a file is the same as removing the old name and adding the new name. There's also the shortcut *git mv* which has the same syntax as the *mv* command. For example:
37  $ git mv OLDFILE NEWFILE
39 === Advanced Undo/Redo ===
41 Running:
43  $ git log
45 shows you a list of recent commits, and their SHA1 hashes. Then:
47  $ git commit -a
48  $ git revert SHA1_HASH
50 will load the previous state with the given hash.
51 Don't like cutting and pasting hashes? Then use:
53  $ git revert "@{10 minutes ago}" .
55 Other time specifications work too. Or you can ask for the 5th-last saved state:
57  $ git revert "@{5}" .
59 Running *git log* reveals that the fact that you loaded an old saved state has been recorded as new commit. In other words, Git tracks your undo and redo operations.
61 Sometimes you just want to go back and forget about every change past a certain point because they're all wrong. Then type:
63  $ git reset --hard SHA1_HASH
65 which restores the state to a given commit and also erases all newer commits from the record permanently.
67 Using *git checkout* is a third way to load an old state, but it's slightly more complicated because it involves branches, and will be discussed in detail later.
69 To take the computer game analogy again:
71 - *git revert* is like loading a game and recording this fact as a new saved game,
73 - *git reset --hard* is like loading an old save and deleting all saved games newer than the one just loaded, and
75 - *git checkout* is like loading an old game, but if you play on, the game state will deviate from the newer saves you made the first time around. Any saved games you make now will end up in a separate branch representing the alternate reality you have entered. We describe how to deal with this later.
77 For reset and checkout, you can choose only to restore particular files and subdirectories by appending them after the command.
79 === Dowloading Files ===
81 If a project uses Git to manage its files, you can get a copy with:
83  $ git clone git://server/path/to/files
85 For example, to get all the files I used to create this site:
87  $ git clone git://git.or.cz/gitmagic.git
89 We'll have a lot more to say about the *clone* command later.
91 === The Bleeding Edge ===
93 If a project uses Git to manage its files, and you've already downloaded a copy using *git clone*, you can upgrade to the latest version with:
95  $ git pull
97 === Instant Publishing ===
99 Let's say you've written a script you'd like to share with others. You could just tell them to download from your computer, but if they do so while you're improving the script or making experimental changes, they could wind up in trouble.  Of course, this is why release cycles exist. Code is written, and at certain points in time, when it's suitable for others, it gets released.
101 To do this with Git, in the directory where your script resides:
103  $ git init
104  $ git add .
105  $ git commit -m "First release"
107 Then tell your users to type
109  $ git clone your.computer:/path/to/script
111 to download your script. This assumes they have ssh access. If not, run *git daemon* and tell your users to type
113  $ git clone git://your.computer/path/to/script
115 From now on, every time your script is ready for release, type:
117  $ git commit -a -m "Next release"
119 and your users can upgrade their version by changing to the directory containing your script and typing.
121  $ git pull
123 Your users will never end up with a version of your script you don't want them to see. Obviously this trick works for anything, not just scripts.
125 === What Have I Done? ===
127 Find out what changes you've made since the last commit with:
129  $ git diff
131 Or since yesterday:
133  $ git diff "@{yesterday}"
135 Or between a particular version and 2 versions ago:
137  $ git diff SHA1_HASH "@{2}"