Innit text file
[gitmagic/gitmagic.git] / vi / basic.txt
bloba52ec8c9f331031f83079bad4711ab31cba101b4
1 == Basic Tricks ==
3 Thay vì lao vào cả "biển lệnh" của Git, hãy sử dụng các ví dụ cơ bản để bắt đầu.
4 Mặc dù chúng rất đơn giản, nhưng tất cả chúng đều rất hữu dụng.
5 Quả thực là vậy, trong tháng đầu tiên sử dụng Git Tôi chưa bao giờ vượt qua những gì nói trong chương này.
7 === Ghi lại State ===
9 About to attempt something drastic? Trước khi làm điều đó, thực hiện với tất cả các tệp tin trong thư mục hiện hành bằng cách:
11  $ git init
12  $ git add .
13  $ git commit -m "My first backup"
15 Now if your new edits go awry, restore the pristine version:
17  $ git reset --hard
19 To save the state again:
21  $ git commit -a -m "Another backup"
23 === Thêm, Xóa, Đổi tên ===
25 The above only keeps 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:
27  $ git add readme.txt Documentation
29 Tương tự như vậy, nếu bạn muốn Git bỏ qua các tệp tin nào đó:
31  $ git rm kludge.h obsolete.c
32  $ git rm -r incriminating/evidence/
34 Git xóa bỏ những tệp tin nếu như bạn chưa làm.
36 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. Ví dụ:
38  $ git mv bug.c feature.c
40 === Advanced Undo/Redo ===
42 Đôi khi bạn chỉ muốn quay trở lại và bỏ đi những thay đổi trong quá khứ tại một thời điểm nào đó bởi vì chúng tất cả đã sai. Thì:
44  $ git log
46 hiển thị cho bạn danh sách các lần commit gần đây cùng với giá trị băm SHA1:
48 ----------------------------------
49 commit 766f9881690d240ba334153047649b8b8f11c664
50 Author: Bob <bob@example.com>
51 Date:   Tue Mar 14 01:59:26 2000 -0800
53     Replace printf() with write().
55 commit 82f5ea346a2e651544956a8653c0f58dc151275c
56 Author: Alice <alice@example.com>
57 Date:   Thu Jan 1 00:00:00 1970 +0000
59     Initial commit.
60 ----------------------------------
62 Chỉ vài ký tự của giá trị băm là đủ để chỉ ra một commit;
63 một cách khác là chép và dán giá trị băm. Gõ:
65  $ git reset --hard 766f
67 to restore the state to a given commit and erase all newer commits from the record permanently.
69 Other times you want to hop to an old state briefly. In this case, type:
71  $ git checkout 82f5
73 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.
75 This alternate reality is called a 'branch', and <<branch,we'll have more to say about this later>>. For now, just remember that
77  $ git checkout master
79 will take you back to the present. Also, to stop Git complaining, always
80 commit or reset your changes before running checkout.
82 To take the computer game analogy again:
84 - *`git reset --hard`*: load an old save and delete all saved games newer than the one just loaded.
86 - *`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>>.
88 You can choose only to restore particular files and subdirectories by appending them after the command:
90  $ git checkout 82f5 some.file another.file
92 Take care, as this form of *checkout* can silently overwrite files. To
93 prevent accidents, commit before running any checkout command, especially when
94 first learning Git. In general, whenever you feel unsure about any operation, Git command or not, first run *git commit -a*.
96 Bạn không thích việc cắt dán ư? Hãy sử dụng:
98  $ git checkout :/"My first b"
100 to jump to the commit that starts with a given message.
101 You can also ask for the 5th-last saved state:
103  $ git checkout master~5
105 === Reverting ===
107 In a court of law, events can be stricken from the record. Likewise, you can pick specific commits to undo.
109  $ git commit -a
110  $ git revert 1b6d
112 will undo just the commit with the given hash. The revert is recorded as a new
113 commit, which you can confirm by running *git log*.
115 === Changelog Generation ===
117 Some projects require a http://en.wikipedia.org/wiki/Changelog[changelog].
118 Generate one by typing:
120  $ git log > ChangeLog
122 === Tải về các Tệp tin ===
124 Lấy về một bản sao của một dự án quản lý bằng Git bằng cách gõ:
126  $ git clone git://server/path/to/files
128 Ví dụ, để lấy tất cả các tệp tin mà tôi đã dùng để tạo ra cho trang mạng này là:
130  $ git clone git://git.or.cz/gitmagic.git
132 Chúng ta sẽ có nhiều điều để nói về lệnh *clone* sớm thôi.
134 === The Bleeding Edge ===
136 Nếu bạn đã tải về một bản sao của một dự án sử dụng *git clone*, bạn có thể nâng cấp lên phiên bản cuối cùng với lệnh:
138  $ git pull
140 === Instant Publishing ===
142 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.
144 Thực hiện điều này với Git, trong thư mục nơi script của bạn nằm trong:
146  $ git init
147  $ git add .
148  $ git commit -m "First release"
150 Sau đó nói với những người sử dụng hãy chạy:
152  $ git clone your.computer:/path/to/script
154 để tải script về. This assumes they have ssh access. If not, run *git daemon* and tell your users to instead run:
156  $ git clone git://your.computer/path/to/script
158 From now on, every time your script is ready for release, execute:
160  $ git commit -a -m "Next release"
162 and your users can upgrade their version by changing to the directory containing your script and typing:
164  $ git pull
166 Your users will never end up with a version of your script you don't want them to see.
168 === What Have I Done? ===
170 Find out what changes you've made since the last commit with:
172  $ git diff
174 Hay từ hôm qua:
176  $ git diff "@{yesterday}"
178 Or between a particular version and 2 versions ago:
180  $ git diff 1b6d "master~2"
182 In each case the output is a patch that can be applied with *git apply*.
183 Try also:
185  $ git whatchanged --since="2 weeks ago"
187 Often I'll browse history with http://sourceforge.net/projects/qgit[qgit]
188 instead, due to its slick photogenic interface, or
189 http://jonas.nitro.dk/tig/[tig], a text-mode interface that works well over
190 slow connections. Alternatively, install a web server, run *git instaweb* and
191 fire up any web browser.
193 === Bài tập===
195 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. How can we do this?
197 There are at least three solutions. Assuming we are at D:
199   1. The difference between A and B are the removed files. We can create a patch representing this difference and apply it:
201    $ git diff B A | git apply
203   2. Since we saved the files back at A, we can retrieve them:
205    $ git checkout A foo.c bar.h
207   3. We can view going from A to B as a change we want to undo:
209    $ git revert B
211 Lựa chọn nào là tốt nhất? Cái nào bạn thích nhất. It is easy to get what you want with Git, and often there are many ways to get it.