Mention index.
[gitmagic/gitmagic.git] / basic.txt
blobd6d868b837df8bff1a910eebda3d0fe9990e18ca
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.
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 Take a snapshot of all files in the current directory with:
11  $ git init
12  $ git add .
13  $ git commit -m "My first backup"
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 Sometimes you just want to go back and forget about every change past a certain point because they're all wrong.
43 Then
45  $ git log
47 shows you a list of recent commits, and their SHA1 hashes. Next, type
49  $ git reset --hard SHA1_HASH
51 to restore the state to a given commit and erase all newer commits from the record permanently.
53 Other times you want to hop to an old state briefly. In this case, type:
55  $ git checkout SHA1_HASH
57 This takes you back in time, while preserving newer commits. However, like time travel in a science-fiction movie, if you now edit and commit, you will be in an alternate reality, because your actions are different to what they were the first time around.
59 This alternate reality is called a ''branch'', and <<branch,we'll have more to say about this later>>. For now, just remember that
61  $ git checkout master
63 will take you back to the present.
65 Uncommitted changes travel in time with you when you run checkout.
67 To take the computer game analogy again:
69 - *`git reset \--hard`*: load an old save and delete all saved games newer than the one just loaded.
71 - *`git checkout`*: load 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. <<branch,We deal with this later>>.
73 You can choose only to restore particular files and subdirectories by appending them after the command.
75 Don't like cutting and pasting hashes? Then use:
77  $ git checkout "@{10 minutes ago}" .
79 Other time specifications work too. For example, you can ask for the 5th-last saved state:
81  $ git checkout "@{5}" .
83 ==== Reverting ====
85 In a court of law, events can be stricken from the record. Likewise, you can pick specific commits to undo.
87  $ git commit -a
88  $ git revert SHA1_HASH
90 will undo just the commit with the given hash. Running *git log* reveals the revert is recorded as a new commit.
92 === Dowloading Files ===
94 Get a copy of a project managed with Git by typing:
96  $ git clone git://server/path/to/files
98 For example, to get all the files I used to create this site:
100  $ git clone git://git.or.cz/gitmagic.git
102 We'll have much to say about the *clone* command soon.
104 === The Bleeding Edge ===
106 If you've already downloaded a copy of a project using *git clone*, you can upgrade to the latest version with:
108  $ git pull
110 === Instant Publishing ===
112 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. Developers work on code, and when they feel it's suitable for others, they release the code.
114 To do this with Git, in the directory where your script resides:
116  $ git init
117  $ git add .
118  $ git commit -m "First release"
120 Then tell your users to run:
122  $ git clone your.computer:/path/to/script
124 to download your script. This assumes they have ssh access. If not, run *git daemon* and tell your users to instead run:
126  $ git clone git://your.computer/path/to/script
128 From now on, every time your script is ready for release, execute:
130  $ git commit -a -m "Next release"
132 and your users can upgrade their version by changing to the directory containing your script and typing:
134  $ git pull
136 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.
138 === What Have I Done? ===
140 Find out what changes you've made since the last commit with:
142  $ git diff
144 Or since yesterday:
146  $ git diff "@{yesterday}"
148 Or between a particular version and 2 versions ago:
150  $ git diff SHA1_HASH "@{2}"