Made some examples look more realistic.
[gitmagic/gitmagic.git] / en / basic.txt
blobda8c95a5a331326e36baff80b51caed48e1d32c0
1 == Basic Tricks ==
3 Rather than diving into a sea of Git commands, use these elementary examples to
4 get your feet wet. Despite their simplicity, each of them are useful.
5 Indeed, in my first months with Git I never ventured beyond the material in this chapter.
7 === Saving State ===
9 About to attempt something drastic? Before you do, take a snapshot of all files
10 in the current directory with:
12  $ git init
13  $ git add .
14  $ git commit -m "My first backup"
16 Now if your new edits go awry, restore the pristine version:
18  $ git reset --hard
20 To save the state again:
22  $ git commit -a -m "Another backup"
24 ==== Add, Delete, Rename ====
26 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:
28  $ git add readme.txt Documentation
30 Similarly, if you want Git to forget about certain files, maybe because you've deleted them:
32  $ git rm kludge.h obsolete.c
33  $ git rm -r incriminating/evidence/
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 bug.c feature.c
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. Then:
43  $ git log
45 shows you a list of recent commits, and their SHA1 hashes:
47 ----------------------------------
48 commit 766f9881690d240ba334153047649b8b8f11c664
49 Author: Bob <bob@example.com>
50 Date:   Tue Mar 14 01:59:26 2000 -0800
52     Replace printf() with write().
54 commit 82f5ea346a2e651544956a8653c0f58dc151275c
55 Author: Alice <alice@example.com>
56 Date:   Thu Jan 1 00:00:00 1970 +0000
58     Initial commit.
59 ----------------------------------
61 The first few characters of the hash are enough to specify the commit;
62 alternatively, copy and paste the entire hash. Type:
64  $ git reset --hard 766f
66 to restore the state to a given commit and erase all newer commits from the record permanently.
68 Other times you want to hop to an old state briefly. In this case, type:
70  $ git checkout 82f5
72 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.
74 This alternate reality is called a 'branch', and <<branch,we'll have more to say about this later>>. For now, just remember that
76  $ git checkout master
78 will take you back to the present. Also, to stop Git complaining, always
79 commit or reset your changes before running checkout.
81 To take the computer game analogy again:
83 - *`git reset --hard`*: load an old save and delete all saved games newer than the one just loaded.
85 - *`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>>.
87 You can choose only to restore particular files and subdirectories by appending them after the command:
89  $ git checkout 82f5 some.file another.file
91 Take care, as this form of *checkout* can silently overwrite files. To
92 prevent accidents, commit before running any checkout command, especially when
93 first learning Git. In general, whenever you feel unsure about any operation, Git command or not, first run *git commit -a*.
95 Don't like cutting and pasting hashes? Then use:
97  $ git checkout :/"My first b"
99 to jump to the commit that starts with a given message.
100 You can also ask for the 5th-last saved state:
102  $ git checkout master~5
104 ==== Reverting ====
106 In a court of law, events can be stricken from the record. Likewise, you can pick specific commits to undo.
108  $ git commit -a
109  $ git revert SHA1_HASH
111 will undo just the commit with the given hash. The revert is recorded as a new
112 commit, which you can confirm by running *git log*.
114 === Changelog Generation ===
116 Some projects require a http://en.wikipedia.org/wiki/Changelog[changelog].
117 Generate one by typing:
119  $ git log > ChangeLog
121 === Downloading Files ===
123 Get a copy of a project managed with Git by typing:
125  $ git clone git://server/path/to/files
127 For example, to get all the files I used to create this site:
129  $ git clone git://git.or.cz/gitmagic.git
131 We'll have much to say about the *clone* command soon.
133 === The Bleeding Edge ===
135 If you've already downloaded a copy of a project using *git clone*, you can upgrade to the latest version with:
137  $ git pull
139 === Instant Publishing ===
141 Suppose 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 may work on a project frequently, but they only make the code available when they feel it is presentable.
143 To do this with Git, in the directory where your script resides:
145  $ git init
146  $ git add .
147  $ git commit -m "First release"
149 Then tell your users to run:
151  $ git clone your.computer:/path/to/script
153 to download your script. This assumes they have ssh access. If not, run *git daemon* and tell your users to instead run:
155  $ git clone git://your.computer/path/to/script
157 From now on, every time your script is ready for release, execute:
159  $ git commit -a -m "Next release"
161 and your users can upgrade their version by changing to the directory containing your script and typing:
163  $ git pull
165 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.
167 === What Have I Done? ===
169 Find out what changes you've made since the last commit with:
171  $ git diff
173 Or since yesterday:
175  $ git diff "@{yesterday}"
177 Or between a particular version and 2 versions ago:
179  $ git diff SHA1_HASH "master~2"
181 In each case the output is a patch that can be applied with *git apply*.
182 Try also:
184  $ git whatchanged --since="2 weeks ago"
186 Often I'll browse history with http://sourceforge.net/projects/qgit[qgit]
187 instead, due to its slick photogenic interface, or
188 http://jonas.nitro.dk/tig/[tig], a text-mode interface that works well over
189 slow connections. Alternatively, install a web server, run *git instaweb* and
190 fire up any web browser.
192 === Exercise ===
194 Let A, B, C, D be four successive commits where B is the same as A except some files have been removed. We want to add the files back at D and not at B. How can we do this?
196 There are at least three solutions. Assuming we are at D:
198   1. The difference between A and B are the removed files. We can create a patch representing this difference and apply it:
200    $ git diff B A | git apply
202   2. Since we saved the files back at A, we can retrieve them:
204    $ git checkout A FILES...
206   3. We can view going from A to B as a change we want to undo:
208    $ git revert B
210 Which choice is best? Whichever you prefer most. It is easy to get what you want with Git, and often there are many ways to get it.