Revision | 9f55b90e9831821d572fd927a3bd008aa3ba3c14 (tree) |
---|---|
Zeit | 2020-10-25 09:39:14 |
Autor | matsuand <30614168+matsuand@user...> |
Commiter | matsuand |
修正。20201025結果追加。
@@ -0,0 +1,20598 @@ | ||
1 | +diff -au ../../git/progit2/progit2/A-git-in-other-environments.asc /tmp/A-git-in-other-environments.asc.tmp | |
2 | +--- ../../git/progit2/progit2/A-git-in-other-environments.asc 2020-08-29 09:58:24.046195489 +0900 | |
3 | ++++ /tmp/A-git-in-other-environments.asc.tmp 2020-10-25 09:24:25.507959693 +0900 | |
4 | +@@ -1,4 +1,4 @@ | |
5 | +-[[A-git-in-other-environments]] | |
6 | ++[#A-git-in-other-environments] | |
7 | + [appendix] | |
8 | + == Git in Other Environments | |
9 | + | |
10 | +@@ -7,22 +7,6 @@ | |
11 | + But the story doesn't end there; Git is usually used as part of a larger ecosystem, and the terminal isn't always the best way to work with it. | |
12 | + Now we'll take a look at some of the other kinds of environments where Git can be useful, and how other applications (including yours) work alongside Git. | |
13 | + | |
14 | +-include::book/A-git-in-other-environments/sections/guis.asc[] | |
15 | +- | |
16 | +-include::book/A-git-in-other-environments/sections/visualstudio.asc[] | |
17 | +- | |
18 | +-include::book/A-git-in-other-environments/sections/visualstudiocode.asc[] | |
19 | +- | |
20 | +-include::book/A-git-in-other-environments/sections/jetbrainsides.asc[] | |
21 | +- | |
22 | +-include::book/A-git-in-other-environments/sections/sublimetext.asc[] | |
23 | +- | |
24 | +-include::book/A-git-in-other-environments/sections/bash.asc[] | |
25 | +- | |
26 | +-include::book/A-git-in-other-environments/sections/zsh.asc[] | |
27 | +- | |
28 | +-include::book/A-git-in-other-environments/sections/powershell.asc[] | |
29 | +- | |
30 | + === Summary | |
31 | + | |
32 | + You've learned how to harness Git's power from inside the tools that you use during your everyday work, and also how to access Git repositories from your own programs. | |
33 | +Warning: file not found: ../../git/progit2/progit2-ja/B-embedding-git-in-your-applications.asc | |
34 | +diff -au ../../git/progit2/progit2/C-git-commands.asc /tmp/C-git-commands.asc.tmp | |
35 | +--- ../../git/progit2/progit2/C-git-commands.asc 2020-08-17 16:38:51.074694961 +0900 | |
36 | ++++ /tmp/C-git-commands.asc.tmp 2020-10-25 09:24:25.545961483 +0900 | |
37 | +@@ -1,4 +1,4 @@ | |
38 | +-[[C-git-commands]] | |
39 | ++[#C-git-commands] | |
40 | + [appendix] | |
41 | + == Git Commands | |
42 | + | |
43 | +@@ -8,20 +8,10 @@ | |
44 | + In this appendix, we'll go through all the Git commands we addressed throughout the book, grouped roughly by what they're used for. | |
45 | + We'll talk about what each command very generally does and then point out where in the book you can find us having used it. | |
46 | + | |
47 | +-[TIP] | |
48 | +-==== | |
49 | +-You can abbreviate long options. | |
50 | +-For example, you can type in `git commit --a`, which acts as if you typed `git commit --amend`. | |
51 | +-This only works when the letters after `--` are unique for one option. | |
52 | +-Do use the full option when writing scripts. | |
53 | +-==== | |
54 | +- | |
55 | + === Setup and Config | |
56 | + | |
57 | + There are two commands that are used quite a lot, from the first invocations of Git to common every day tweaking and referencing, the `config` and `help` commands. | |
58 | + | |
59 | +-==== git config | |
60 | +- | |
61 | + Git has a default way of doing hundreds of things. | |
62 | + For a lot of these things, you can tell Git to default to doing them a different way, or set your preferences. | |
63 | + This involves everything from telling Git what your name is to specific terminal color preferences or what editor you use. | |
64 | +@@ -29,251 +19,178 @@ | |
65 | + | |
66 | + The `git config` command has been used in nearly every chapter of the book. | |
67 | + | |
68 | +-In <<ch01-getting-started#_first_time>> we used it to specify our name, email address and editor preference before we even got started using Git. | |
69 | ++In <<ch01-introduction#r_first_time>> we used it to specify our name, email address and editor preference before we even got started using Git. | |
70 | + | |
71 | +-In <<ch02-git-basics-chapter#_git_aliases>> we showed how you could use it to create shorthand commands that expand to long option sequences so you don't have to type them every time. | |
72 | ++In <<ch02-git-basics#r_git_aliases>> we showed how you could use it to create shorthand commands that expand to long option sequences so you don't have to type them every time. | |
73 | + | |
74 | +-In <<ch03-git-branching#_rebasing>> we used it to make `--rebase` the default when you run `git pull`. | |
75 | ++In <<ch03-git-branching#r_rebasing>> we used it to make `--rebase` the default when you run `git pull`. | |
76 | + | |
77 | +-In <<ch07-git-tools#_credential_caching>> we used it to set up a default store for your HTTP passwords. | |
78 | ++In <<ch07-git-tools#r_credential_caching>> we used it to set up a default store for your HTTP passwords. | |
79 | + | |
80 | +-In <<ch08-customizing-git#_keyword_expansion>> we showed how to set up smudge and clean filters on content coming in and out of Git. | |
81 | +- | |
82 | +-Finally, basically the entirety of <<ch08-customizing-git#_git_config>> is dedicated to the command. | |
83 | +- | |
84 | +-[[_core_editor]] | |
85 | +-==== git config core.editor commands | |
86 | +- | |
87 | +-Accompanying the configuration instructions in <<ch01-getting-started#_editor>>, many editors can be set as follows: | |
88 | +- | |
89 | +-.Exhaustive list of `core.editor` configuration commands | |
90 | +-[cols="1,2",options="header"] | |
91 | +-|============================== | |
92 | +-|Editor | Configuration command | |
93 | +-|Atom |`git config --global core.editor "atom --wait"` | |
94 | +-|BBEdit (Mac, with command line tools) |`git config --global core.editor "bbedit -w"` | |
95 | +-|Emacs |`git config --global core.editor emacs` | |
96 | +-|Gedit (Linux) |`git config --global core.editor "gedit --wait --new-window"` | |
97 | +-|Gvim (Windows 64-bit) |`git config --global core.editor "'C:\Program Files\Vim\vim72\gvim.exe' --nofork '%*'"` (Also see note below) | |
98 | +-|Kate (Linux) |`git config --global core.editor "kate"` | |
99 | +-|nano |`git config --global core.editor "nano -w"` | |
100 | +-|Notepad (Windows 64-bit) |`git config core.editor notepad` | |
101 | +-|Notepad++ (Windows 64-bit) |`git config --global core.editor "'C:\Program Files\Notepad++\notepad++.exe' -multiInst -notabbar -nosession -noPlugin"` (Also see note below) | |
102 | +-|Scratch (Linux)|`git config --global core.editor "scratch-text-editor"` | |
103 | +-|Sublime Text (macOS) |`git config --global core.editor "/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl --new-window --wait"` | |
104 | +-|Sublime Text (Windows 64-bit) |`git config --global core.editor "'C:\Program Files\Sublime Text 3\sublime_text.exe' -w"` (Also see note below) | |
105 | +-|TextEdit (macOS)|`git config --global --add core.editor "open -W -n"` | |
106 | +-|Textmate |`git config --global core.editor "mate -w"` | |
107 | +-|Textpad (Windows 64-bit) |`git config --global core.editor "'C:\Program Files\TextPad 5\TextPad.exe' -m` (Also see note below) | |
108 | +-|UltraEdit (Windows 64-bit) | `git config --global core.editor Uedit32` | |
109 | +-|Vim |`git config --global core.editor "vim"` | |
110 | +-|Visual Studio Code |`git config --global core.editor "code --wait"` | |
111 | +-|VSCodium (Free/Libre Open Source Software Binaries of VSCode) | `git config --global core.editor "codium --wait"` | |
112 | +-|WordPad |`git config --global core.editor '"C:\Program Files\Windows NT\Accessories\wordpad.exe"'"` | |
113 | +-|Xi | `git config --global core.editor "xi --wait"` | |
114 | +-|============================== | |
115 | +- | |
116 | +-[NOTE] | |
117 | +-==== | |
118 | +-If you have a 32-bit editor on a Windows 64-bit system, the program will be installed in `C:\Program Files (x86)\` rather than `C:\Program Files\` as in the table above. | |
119 | +-==== | |
120 | ++In <<ch08-customizing-git#r_keyword_expansion>> we showed how to set up smudge and clean filters on content coming in and out of Git. | |
121 | + | |
122 | +-==== git help | |
123 | ++Finally, basically the entirety of <<ch08-customizing-git#r_git_config>> is dedicated to the command. | |
124 | + | |
125 | + The `git help` command is used to show you all the documentation shipped with Git about any command. | |
126 | + While we're giving a rough overview of most of the more popular ones in this appendix, for a full listing of all of the possible options and flags for every command, you can always run `git help <command>`. | |
127 | + | |
128 | +-We introduced the `git help` command in <<ch01-getting-started#_git_help>> and showed you how to use it to find more information about the `git shell` in <<ch04-git-on-the-server#_setting_up_server>>. | |
129 | +- | |
130 | ++We introduced the `git help` command in <<ch01-introduction#r_git_help>> and showed you how to use it to find more information about the `git shell` in <<ch04-git-server#r_setting_up_server>>. | |
131 | + | |
132 | + === Getting and Creating Projects | |
133 | + | |
134 | + There are two ways to get a Git repository. | |
135 | + One is to copy it from an existing repository on the network or elsewhere and the other is to create a new one in an existing directory. | |
136 | + | |
137 | +-==== git init | |
138 | +- | |
139 | + To take a directory and turn it into a new Git repository so you can start version controlling it, you can simply run `git init`. | |
140 | + | |
141 | +-We first introduce this in <<ch02-git-basics-chapter#_getting_a_repo>>, where we show creating a brand new repository to start working with. | |
142 | ++We first introduce this in <<ch02-git-basics#r_getting_a_repo>>, where we show creating a brand new repository to start working with. | |
143 | + | |
144 | +-We talk briefly about how you can change the default branch name from ``master'' in <<ch03-git-branching#_remote_branches>>. | |
145 | ++We talk briefly about how you can change the default branch from ``master'' in <<ch03-git-branching#r_remote_branches>>. | |
146 | + | |
147 | +-We use this command to create an empty bare repository for a server in <<ch04-git-on-the-server#_bare_repo>>. | |
148 | ++We use this command to create an empty bare repository for a server in <<ch04-git-server#r_bare_repo>>. | |
149 | + | |
150 | +-Finally, we go through some of the details of what it actually does behind the scenes in <<ch10-git-internals#_plumbing_porcelain>>. | |
151 | +- | |
152 | +-==== git clone | |
153 | ++Finally, we go through some of the details of what it actually does behind the scenes in <<ch10-git-internals#r_plumbing_porcelain>>. | |
154 | + | |
155 | + The `git clone` command is actually something of a wrapper around several other commands. | |
156 | + It creates a new directory, goes into it and runs `git init` to make it an empty Git repository, adds a remote (`git remote add`) to the URL that you pass it (by default named `origin`), runs a `git fetch` from that remote repository and then checks out the latest commit into your working directory with `git checkout`. | |
157 | + | |
158 | + The `git clone` command is used in dozens of places throughout the book, but we'll just list a few interesting places. | |
159 | + | |
160 | +-It's basically introduced and explained in <<ch02-git-basics-chapter#_git_cloning>>, where we go through a few examples. | |
161 | ++It's basically introduced and explained in <<ch02-git-basics#r_git_cloning>>, where we go through a few examples. | |
162 | + | |
163 | +-In <<ch04-git-on-the-server#_getting_git_on_a_server>> we look at using the `--bare` option to create a copy of a Git repository with no working directory. | |
164 | ++In <<ch04-git-server#r_git_on_the_server>> we look at using the `--bare` option to create a copy of a Git repository with no working directory. | |
165 | + | |
166 | +-In <<ch07-git-tools#_bundling>> we use it to unbundle a bundled Git repository. | |
167 | ++In <<ch07-git-tools#r_bundling>> we use it to unbundle a bundled Git repository. | |
168 | + | |
169 | +-Finally, in <<ch07-git-tools#_cloning_submodules>> we learn the `--recurse-submodules` option to make cloning a repository with submodules a little simpler. | |
170 | ++Finally, in <<ch07-git-tools#r_cloning_submodules>> we learn the `--recursive` option to make cloning a repository with submodules a little simpler. | |
171 | + | |
172 | + Though it's used in many other places through the book, these are the ones that are somewhat unique or where it is used in ways that are a little different. | |
173 | + | |
174 | +- | |
175 | + === Basic Snapshotting | |
176 | + | |
177 | + For the basic workflow of staging content and committing it to your history, there are only a few basic commands. | |
178 | + | |
179 | +-==== git add | |
180 | +- | |
181 | + The `git add` command adds content from the working directory into the staging area (or ``index'') for the next commit. | |
182 | + When the `git commit` command is run, by default it only looks at this staging area, so `git add` is used to craft what exactly you would like your next commit snapshot to look like. | |
183 | + | |
184 | + This command is an incredibly important command in Git and is mentioned or used dozens of times in this book. | |
185 | + We'll quickly cover some of the unique uses that can be found. | |
186 | + | |
187 | +-We first introduce and explain `git add` in detail in <<ch02-git-basics-chapter#_tracking_files>>. | |
188 | ++We first introduce and explain `git add` in detail in <<ch02-git-basics#r_tracking_files>>. | |
189 | + | |
190 | +-We mention how to use it to resolve merge conflicts in <<ch03-git-branching#_basic_merge_conflicts>>. | |
191 | ++We mention how to use it to resolve merge conflicts in <<ch03-git-branching#r_basic_merge_conflicts>>. | |
192 | + | |
193 | +-We go over using it to interactively stage only specific parts of a modified file in <<ch07-git-tools#_interactive_staging>>. | |
194 | ++We go over using it to interactively stage only specific parts of a modified file in <<ch07-git-tools#r_interactive_staging>>. | |
195 | + | |
196 | +-Finally, we emulate it at a low level in <<ch10-git-internals#_tree_objects>>, so you can get an idea of what it's doing behind the scenes. | |
197 | +- | |
198 | +-==== git status | |
199 | ++Finally, we emulate it at a low level in <<ch10-git-internals#r_tree_objects>>, so you can get an idea of what it's doing behind the scenes. | |
200 | + | |
201 | + The `git status` command will show you the different states of files in your working directory and staging area. | |
202 | + Which files are modified and unstaged and which are staged but not yet committed. | |
203 | + In its normal form, it also will show you some basic hints on how to move files between these stages. | |
204 | + | |
205 | +-We first cover `status` in <<ch02-git-basics-chapter#_checking_status>>, both in its basic and simplified forms. | |
206 | ++We first cover `status` in <<ch02-git-basics#r_checking_status>>, both in its basic and simplified forms. | |
207 | + While we use it throughout the book, pretty much everything you can do with the `git status` command is covered there. | |
208 | + | |
209 | +-==== git diff | |
210 | +- | |
211 | + The `git diff` command is used when you want to see differences between any two trees. | |
212 | + This could be the difference between your working environment and your staging area (`git diff` by itself), between your staging area and your last commit (`git diff --staged`), or between two commits (`git diff master branchB`). | |
213 | + | |
214 | +-We first look at the basic uses of `git diff` in <<ch02-git-basics-chapter#_git_diff_staged>>, where we show how to see what changes are staged and which are not yet staged. | |
215 | +- | |
216 | +-We use it to look for possible whitespace issues before committing with the `--check` option in <<ch05-distributed-git#_commit_guidelines>>. | |
217 | ++We first look at the basic uses of `git diff` in <<ch02-git-basics#r_git_diff_staged>>, where we show how to see what changes are staged and which are not yet staged. | |
218 | + | |
219 | +-We see how to check the differences between branches more effectively with the `git diff A...B` syntax in <<ch05-distributed-git#_what_is_introduced>>. | |
220 | ++We use it to look for possible whitespace issues before committing with the `--check` option in <<ch05-distributed-git#r_commit_guidelines>>. | |
221 | + | |
222 | +-We use it to filter out whitespace differences with `-b` and how to compare different stages of conflicted files with `--theirs`, `--ours` and `--base` in <<ch07-git-tools#_advanced_merging>>. | |
223 | ++We see how to check the differences between branches more effectively with the `git diff A...B` syntax in <<ch05-distributed-git#r_what_is_introduced>>. | |
224 | + | |
225 | +-Finally, we use it to effectively compare submodule changes with `--submodule` in <<ch07-git-tools#_starting_submodules>>. | |
226 | ++We use it to filter out whitespace differences with `-b` and how to compare different stages of conflicted files with `--theirs`, `--ours` and `--base` in <<ch07-git-tools#r_advanced_merging>>. | |
227 | + | |
228 | +-==== git difftool | |
229 | ++Finally, we use it to effectively compare submodule changes with `--submodule` in <<ch07-git-tools#r_starting_submodules>>. | |
230 | + | |
231 | + The `git difftool` command simply launches an external tool to show you the difference between two trees in case you want to use something other than the built in `git diff` command. | |
232 | + | |
233 | +-We only briefly mention this in <<ch02-git-basics-chapter#_git_diff_staged>>. | |
234 | +- | |
235 | +-==== git commit | |
236 | ++We only briefly mention this in <<ch02-git-basics#r_git_diff_staged>>. | |
237 | + | |
238 | + The `git commit` command takes all the file contents that have been staged with `git add` and records a new permanent snapshot in the database and then moves the branch pointer on the current branch up to it. | |
239 | + | |
240 | +-We first cover the basics of committing in <<ch02-git-basics-chapter#_committing_changes>>. | |
241 | ++We first cover the basics of committing in <<ch02-git-basics#r_committing_changes>>. | |
242 | + There we also demonstrate how to use the `-a` flag to skip the `git add` step in daily workflows and how to use the `-m` flag to pass a commit message in on the command line instead of firing up an editor. | |
243 | + | |
244 | +-In <<ch02-git-basics-chapter#_undoing>> we cover using the `--amend` option to redo the most recent commit. | |
245 | +- | |
246 | +-In <<ch03-git-branching#_git_branches_overview>>, we go into much more detail about what `git commit` does and why it does it like that. | |
247 | ++In <<ch02-git-basics#r_undoing>> we cover using the `--amend` option to redo the most recent commit. | |
248 | + | |
249 | +-We looked at how to sign commits cryptographically with the `-S` flag in <<ch07-git-tools#_signing_commits>>. | |
250 | ++In <<ch03-git-branching#r_git_branches_overview>>, we go into much more detail about what `git commit` does and why it does it like that. | |
251 | + | |
252 | +-Finally, we take a look at what the `git commit` command does in the background and how it's actually implemented in <<ch10-git-internals#_git_commit_objects>>. | |
253 | ++We looked at how to sign commits cryptographically with the `-S` flag in <<ch07-git-tools#r_signing_commits>>. | |
254 | + | |
255 | +-==== git reset | |
256 | ++Finally, we take a look at what the `git commit` command does in the background and how it's actually implemented in <<ch10-git-internals#r_git_commit_objects>>. | |
257 | + | |
258 | + The `git reset` command is primarily used to undo things, as you can possibly tell by the verb. | |
259 | + It moves around the `HEAD` pointer and optionally changes the `index` or staging area and can also optionally change the working directory if you use `--hard`. | |
260 | + This final option makes it possible for this command to lose your work if used incorrectly, so make sure you understand it before using it. | |
261 | + | |
262 | +-We first effectively cover the simplest use of `git reset` in <<ch02-git-basics-chapter#_unstaging>>, where we use it to unstage a file we had run `git add` on. | |
263 | +- | |
264 | +-We then cover it in quite some detail in <<ch07-git-tools#_git_reset>>, which is entirely devoted to explaining this command. | |
265 | ++We first effectively cover the simplest use of `git reset` in <<ch02-git-basics#r_unstaging>>, where we use it to unstage a file we had run `git add` on. | |
266 | + | |
267 | +-We use `git reset --hard` to abort a merge in <<ch07-git-tools#_abort_merge>>, where we also use `git merge --abort`, which is a bit of a wrapper for the `git reset` command. | |
268 | ++We then cover it in quite some detail in <<ch07-git-tools#r_git_reset>>, which is entirely devoted to explaining this command. | |
269 | + | |
270 | +-==== git rm | |
271 | ++We use `git reset --hard` to abort a merge in <<ch07-git-tools#r_abort_merge>>, where we also use `git merge --abort`, which is a bit of a wrapper for the `git reset` command. | |
272 | + | |
273 | + The `git rm` command is used to remove files from the staging area and working directory for Git. | |
274 | + It is similar to `git add` in that it stages a removal of a file for the next commit. | |
275 | + | |
276 | +-We cover the `git rm` command in some detail in <<ch02-git-basics-chapter#_removing_files>>, including recursively removing files and only removing files from the staging area but leaving them in the working directory with `--cached`. | |
277 | ++We cover the `git rm` command in some detail in <<ch02-git-basics#r_removing_files>>, including recursively removing files and only removing files from the staging area but leaving them in the working directory with `--cached`. | |
278 | + | |
279 | +-The only other differing use of `git rm` in the book is in <<ch10-git-internals#_removing_objects>> where we briefly use and explain the `--ignore-unmatch` when running `git filter-branch`, which simply makes it not error out when the file we are trying to remove doesn't exist. | |
280 | ++The only other differing use of `git rm` in the book is in <<ch10-git-internals#r_removing_objects>> where we briefly use and explain the `--ignore-unmatch` when running `git filter-branch`, which simply makes it not error out when the file we are trying to remove doesn't exist. | |
281 | + This can be useful for scripting purposes. | |
282 | + | |
283 | +-==== git mv | |
284 | +- | |
285 | + The `git mv` command is a thin convenience command to move a file and then run `git add` on the new file and `git rm` on the old file. | |
286 | + | |
287 | +-We only briefly mention this command in <<ch02-git-basics-chapter#_git_mv>>. | |
288 | +- | |
289 | +-==== git clean | |
290 | ++We only briefly mention this command in <<ch02-git-basics#r_git_mv>>. | |
291 | + | |
292 | + The `git clean` command is used to remove unwanted files from your working directory. | |
293 | + This could include removing temporary build artifacts or merge conflict files. | |
294 | + | |
295 | +-We cover many of the options and scenarios in which you might used the clean command in <<ch07-git-tools#_git_clean>>. | |
296 | ++We cover many of the options and scenarios in which you might used the clean command in <<ch07-git-tools#r_git_clean>>. | |
297 | + | |
298 | + === Branching and Merging | |
299 | + | |
300 | + There are just a handful of commands that implement most of the branching and merging functionality in Git. | |
301 | + | |
302 | +-==== git branch | |
303 | +- | |
304 | + The `git branch` command is actually something of a branch management tool. | |
305 | + It can list the branches you have, create a new branch, delete branches and rename branches. | |
306 | + | |
307 | + Most of <<ch03-git-branching#ch03-git-branching>> is dedicated to the `branch` command and it's used throughout the entire chapter. | |
308 | +-We first introduce it in <<ch03-git-branching#_create_new_branch>> and we go through most of its other features (listing and deleting) in <<ch03-git-branching#_branch_management>>. | |
309 | +- | |
310 | +-In <<ch03-git-branching#_tracking_branches>> we use the `git branch -u` option to set up a tracking branch. | |
311 | ++We first introduce it in <<ch03-git-branching#r_create_new_branch>> and we go through most of its other features (listing and deleting) in <<ch03-git-branching#r_branch_management>>. | |
312 | + | |
313 | +-Finally, we go through some of what it does in the background in <<ch10-git-internals#_git_refs>>. | |
314 | ++In <<ch03-git-branching#r_tracking_branches>> we use the `git branch -u` option to set up a tracking branch. | |
315 | + | |
316 | +-==== git checkout | |
317 | ++Finally, we go through some of what it does in the background in <<ch10-git-internals#r_git_refs>>. | |
318 | + | |
319 | + The `git checkout` command is used to switch branches and check content out into your working directory. | |
320 | + | |
321 | +-We first encounter the command in <<ch03-git-branching#_switching_branches>> along with the `git branch` command. | |
322 | ++We first encounter the command in <<ch03-git-branching#r_switching_branches>> along with the `git branch` command. | |
323 | + | |
324 | +-We see how to use it to start tracking branches with the `--track` flag in <<ch03-git-branching#_tracking_branches>>. | |
325 | ++We see how to use it to start tracking branches with the `--track` flag in <<ch03-git-branching#r_tracking_branches>>. | |
326 | + | |
327 | +-We use it to reintroduce file conflicts with `--conflict=diff3` in <<ch07-git-tools#_checking_out_conflicts>>. | |
328 | ++We use it to reintroduce file conflicts with `--conflict=diff3` in <<ch07-git-tools#r_checking_out_conflicts>>. | |
329 | + | |
330 | +-We go into closer detail on its relationship with `git reset` in <<ch07-git-tools#_git_reset>>. | |
331 | ++We go into closer detail on its relationship with `git reset` in <<ch07-git-tools#r_git_reset>>. | |
332 | + | |
333 | +-Finally, we go into some implementation detail in <<ch10-git-internals#ref_the_ref>>. | |
334 | +- | |
335 | +-==== git merge | |
336 | ++Finally, we go into some implementation detail in <<ch10-git-internals#r_the_head>>. | |
337 | + | |
338 | + The `git merge` tool is used to merge one or more branches into the branch you have checked out. | |
339 | + It will then advance the current branch to the result of the merge. | |
340 | + | |
341 | +-The `git merge` command was first introduced in <<ch03-git-branching#_basic_branching>>. | |
342 | ++The `git merge` command was first introduced in <<ch03-git-branching#r_basic_branching>>. | |
343 | + Though it is used in various places in the book, there are very few variations of the `merge` command -- generally just `git merge <branch>` with the name of the single branch you want to merge in. | |
344 | + | |
345 | +-We covered how to do a squashed merge (where Git merges the work but pretends like it's just a new commit without recording the history of the branch you're merging in) at the very end of <<ch05-distributed-git#_public_project>>. | |
346 | +- | |
347 | +-We went over a lot about the merge process and command, including the `-Xignore-space-change` command and the `--abort` flag to abort a problem merge in <<ch07-git-tools#_advanced_merging>>. | |
348 | ++We covered how to do a squashed merge (where Git merges the work but pretends like it's just a new commit without recording the history of the branch you're merging in) at the very end of <<ch05-distributed-git#r_public_project>>. | |
349 | + | |
350 | +-We learned how to verify signatures before merging if your project is using GPG signing in <<ch07-git-tools#_signing_commits>>. | |
351 | ++We went over a lot about the merge process and command, including the `-Xignore-space-change` command and the `--abort` flag to abort a problem merge in <<ch07-git-tools#r_advanced_merging>>. | |
352 | + | |
353 | +-Finally, we learned about Subtree merging in <<ch07-git-tools#_subtree_merge>>. | |
354 | ++We learned how to verify signatures before merging if your project is using GPG signing in <<ch07-git-tools#r_signing_commits>>. | |
355 | + | |
356 | +-==== git mergetool | |
357 | ++Finally, we learned about Subtree merging in <<ch07-git-tools#r_subtree_merge>>. | |
358 | + | |
359 | + The `git mergetool` command simply launches an external merge helper in case you have issues with a merge in Git. | |
360 | + | |
361 | +-We mention it quickly in <<ch03-git-branching#_basic_merge_conflicts>> and go into detail on how to implement your own external merge tool in <<ch08-customizing-git#_external_merge_tools>>. | |
362 | +- | |
363 | +-==== git log | |
364 | ++We mention it quickly in <<ch03-git-branching#r_basic_merge_conflicts>> and go into detail on how to implement your own external merge tool in <<ch08-customizing-git#r_external_merge_tools>>. | |
365 | + | |
366 | + The `git log` command is used to show the reachable recorded history of a project from the most recent commit snapshot backwards. | |
367 | + By default it will only show the history of the branch you're currently on, but can be given different or even multiple heads or branches from which to traverse. | |
368 | +@@ -281,305 +198,244 @@ | |
369 | + | |
370 | + This command is used in nearly every chapter of the book to demonstrate the history of a project. | |
371 | + | |
372 | +-We introduce the command and cover it in some depth in <<ch02-git-basics-chapter#_viewing_history>>. | |
373 | ++We introduce the command and cover it in some depth in <<ch02-git-basics#r_viewing_history>>. | |
374 | + There we look at the `-p` and `--stat` option to get an idea of what was introduced in each commit and the `--pretty` and `--oneline` options to view the history more concisely, along with some simple date and author filtering options. | |
375 | + | |
376 | +-In <<ch03-git-branching#_create_new_branch>> we use it with the `--decorate` option to easily visualize where our branch pointers are located and we also use the `--graph` option to see what divergent histories look like. | |
377 | ++In <<ch03-git-branching#r_create_new_branch>> we use it with the `--decorate` option to easily visualize where our branch pointers are located and we also use the `--graph` option to see what divergent histories look like. | |
378 | + | |
379 | +-In <<ch05-distributed-git#_private_team>> and <<ch07-git-tools#_commit_ranges>> we cover the `branchA..branchB` syntax to use the `git log` command to see what commits are unique to a branch relative to another branch. | |
380 | +-In <<ch07-git-tools#_commit_ranges>> we go through this fairly extensively. | |
381 | ++In <<ch05-distributed-git#r_private_team>> and <<ch07-git-tools#r_commit_ranges>> we cover the `branchA..branchB` syntax to use the `git log` command to see what commits are unique to a branch relative to another branch. | |
382 | ++In <<ch07-git-tools#r_commit_ranges>> we go through this fairly extensively. | |
383 | + | |
384 | +-In <<ch07-git-tools#_merge_log>> and <<ch07-git-tools#_triple_dot>> we cover using the `branchA...branchB` format and the `--left-right` syntax to see what is in one branch or the other but not in both. | |
385 | +-In <<ch07-git-tools#_merge_log>> we also look at how to use the `--merge` option to help with merge conflict debugging as well as using the `--cc` option to look at merge commit conflicts in your history. | |
386 | ++In <<ch07-git-tools#r_merge_log>> and <<ch07-git-tools#r_triple_dot>> we cover using the `branchA...branchB` format and the `--left-right` syntax to see what is in one branch or the other but not in both. | |
387 | ++In <<ch07-git-tools#r_merge_log>> we also look at how to use the `--merge` option to help with merge conflict debugging as well as using the `--cc` option to look at merge commit conflicts in your history. | |
388 | + | |
389 | +-In <<ch07-git-tools#_git_reflog>> we use the `-g` option to view the Git reflog through this tool instead of doing branch traversal. | |
390 | ++In <<ch07-git-tools#r_git_reflog>> we use the `-g` option to view the Git reflog through this tool instead of doing branch traversal. | |
391 | + | |
392 | +-In <<ch07-git-tools#_searching>> we look at using the `-S` and `-L` options to do fairly sophisticated searches for something that happened historically in the code such as seeing the history of a function. | |
393 | ++In <<ch07-git-tools#r_searching>> we look at using the `-S` and `-L` options to do fairly sophisticated searches for something that happened historically in the code such as seeing the history of a function. | |
394 | + | |
395 | +-In <<ch07-git-tools#_signing_commits>> we see how to use `--show-signature` to add a validation string to each commit in the `git log` output based on if it was validly signed or not. | |
396 | +- | |
397 | +-==== git stash | |
398 | ++In <<ch07-git-tools#r_signing_commits>> we see how to use `--show-signature` to add a validation string to each commit in the `git log` output based on if it was validly signed or not. | |
399 | + | |
400 | + The `git stash` command is used to temporarily store uncommitted work in order to clean out your working directory without having to commit unfinished work on a branch. | |
401 | + | |
402 | +-This is basically entirely covered in <<ch07-git-tools#_git_stashing>>. | |
403 | +- | |
404 | +-==== git tag | |
405 | ++This is basically entirely covered in <<ch07-git-tools#r_git_stashing>>. | |
406 | + | |
407 | + The `git tag` command is used to give a permanent bookmark to a specific point in the code history. | |
408 | + Generally this is used for things like releases. | |
409 | + | |
410 | +-This command is introduced and covered in detail in <<ch02-git-basics-chapter#_git_tagging>> and we use it in practice in <<ch05-distributed-git#_tagging_releases>>. | |
411 | +- | |
412 | +-We also cover how to create a GPG signed tag with the `-s` flag and verify one with the `-v` flag in <<ch07-git-tools#_signing>>. | |
413 | ++This command is introduced and covered in detail in <<ch02-git-basics#r_git_tagging>> and we use it in practice in <<ch05-distributed-git#r_tagging_releases>>. | |
414 | + | |
415 | ++We also cover how to create a GPG signed tag with the `-s` flag and verify one with the `-v` flag in <<ch07-git-tools#r_signing>>. | |
416 | + | |
417 | + === Sharing and Updating Projects | |
418 | + | |
419 | + There are not very many commands in Git that access the network, nearly all of the commands operate on the local database. | |
420 | + When you are ready to share your work or pull changes from elsewhere, there are a handful of commands that deal with remote repositories. | |
421 | + | |
422 | +-==== git fetch | |
423 | +- | |
424 | + The `git fetch` command communicates with a remote repository and fetches down all the information that is in that repository that is not in your current one and stores it in your local database. | |
425 | + | |
426 | +-We first look at this command in <<ch02-git-basics-chapter#_fetching_and_pulling>> and we continue to see examples of its use in <<ch03-git-branching#_remote_branches>>. | |
427 | +- | |
428 | +-We also use it in several of the examples in <<ch05-distributed-git#_contributing_project>>. | |
429 | ++We first look at this command in <<ch02-git-basics#r_fetching_and_pulling>> and we continue to see examples of it use in <<ch03-git-branching#r_remote_branches>>. | |
430 | + | |
431 | +-We use it to fetch a single specific reference that is outside of the default space in <<ch06-github#_pr_refs>> and we see how to fetch from a bundle in <<ch07-git-tools#_bundling>>. | |
432 | ++We also use it in several of the examples in <<ch05-distributed-git#r_contributing_project>>. | |
433 | + | |
434 | +-We set up highly custom refspecs in order to make `git fetch` do something a little different than the default in <<ch10-git-internals#_refspec>>. | |
435 | ++We use it to fetch a single specific reference that is outside of the default space in <<ch06-github#r_pr_refs>> and we see how to fetch from a bundle in <<ch07-git-tools#r_bundling>>. | |
436 | + | |
437 | +-==== git pull | |
438 | ++We set up highly custom refspecs in order to make `git fetch` do something a little different than the default in <<ch10-git-internals#r_refspec>>. | |
439 | + | |
440 | + The `git pull` command is basically a combination of the `git fetch` and `git merge` commands, where Git will fetch from the remote you specify and then immediately try to merge it into the branch you're on. | |
441 | + | |
442 | +-We introduce it quickly in <<ch02-git-basics-chapter#_fetching_and_pulling>> and show how to see what it will merge if you run it in <<ch02-git-basics-chapter#_inspecting_remote>>. | |
443 | +- | |
444 | +-We also see how to use it to help with rebasing difficulties in <<ch03-git-branching#_rebase_rebase>>. | |
445 | ++We introduce it quickly in <<ch02-git-basics#r_fetching_and_pulling>> and show how to see what it will merge if you run it in <<ch02-git-basics#r_inspecting_remote>>. | |
446 | + | |
447 | +-We show how to use it with a URL to pull in changes in a one-off fashion in <<ch05-distributed-git#_checking_out_remotes>>. | |
448 | ++We also see how to use it to help with rebasing difficulties in <<ch03-git-branching#r_rebase_rebase>>. | |
449 | + | |
450 | +-Finally, we very quickly mention that you can use the `--verify-signatures` option to it in order to verify that commits you are pulling have been GPG signed in <<ch07-git-tools#_signing_commits>>. | |
451 | ++We show how to use it with a URL to pull in changes in a one-off fashion in <<ch05-distributed-git#r_checking_out_remotes>>. | |
452 | + | |
453 | +-==== git push | |
454 | ++Finally, we very quickly mention that you can use the `--verify-signatures` option to it in order to verify that commits you are pulling have been GPG signed in <<ch07-git-tools#r_signing_commits>>. | |
455 | + | |
456 | + The `git push` command is used to communicate with another repository, calculate what your local database has that the remote one does not, and then pushes the difference into the other repository. | |
457 | + It requires write access to the other repository and so normally is authenticated somehow. | |
458 | + | |
459 | +-We first look at the `git push` command in <<ch02-git-basics-chapter#_pushing_remotes>>. | |
460 | ++We first look at the `git push` command in <<ch02-git-basics#r_pushing_remotes>>. | |
461 | + Here we cover the basics of pushing a branch to a remote repository. | |
462 | +-In <<ch03-git-branching#_pushing_branches>> we go a little deeper into pushing specific branches and in <<ch03-git-branching#_tracking_branches>> we see how to set up tracking branches to automatically push to. | |
463 | +-In <<ch03-git-branching#_delete_branches>> we use the `--delete` flag to delete a branch on the server with `git push`. | |
464 | ++In <<ch03-git-branching#r_pushing_branches>> we go a little deeper into pushing specific branches and in <<ch03-git-branching#r_tracking_branches>> we see how to set up tracking branches to automatically push to. | |
465 | ++In <<ch03-git-branching#r_delete_branches>> we use the `--delete` flag to delete a branch on the server with `git push`. | |
466 | + | |
467 | +-Throughout <<ch05-distributed-git#_contributing_project>> we see several examples of using `git push` to share work on branches through multiple remotes. | |
468 | ++Throughout <<ch05-distributed-git#r_contributing_project>> we see several examples of using `git push` to share work on branches through multiple remotes. | |
469 | + | |
470 | +-We see how to use it to share tags that you have made with the `--tags` option in <<ch02-git-basics-chapter#_sharing_tags>>. | |
471 | ++We see how to use it to share tags that you have made with the `--tags` option in <<ch02-git-basics#r_sharing_tags>>. | |
472 | + | |
473 | +-In <<ch07-git-tools#_publishing_submodules>> we use the `--recurse-submodules` option to check that all of our submodules work has been published before pushing the superproject, which can be really helpful when using submodules. | |
474 | ++In <<ch07-git-tools#r_publishing_submodules>> we use the `--recurse-submodules` option to check that all of our submodules work has been published before pushing the superproject, which can be really helpful when using submodules. | |
475 | + | |
476 | +-In <<ch08-customizing-git#_other_client_hooks>> we talk briefly about the `pre-push` hook, which is a script we can setup to run before a push completes to verify that it should be allowed to push. | |
477 | ++In <<ch08-customizing-git#r_other_client_hooks>> we talk briefly about the `pre-push` hook, which is a script we can setup to run before a push completes to verify that it should be allowed to push. | |
478 | + | |
479 | +-Finally, in <<ch10-git-internals#_pushing_refspecs>> we look at pushing with a full refspec instead of the general shortcuts that are normally used. | |
480 | ++Finally, in <<ch10-git-internals#r_pushing_refspecs>> we look at pushing with a full refspec instead of the general shortcuts that are normally used. | |
481 | + This can help you be very specific about what work you wish to share. | |
482 | + | |
483 | +-==== git remote | |
484 | +- | |
485 | + The `git remote` command is a management tool for your record of remote repositories. | |
486 | + It allows you to save long URLs as short handles, such as ``origin'' so you don't have to type them out all the time. | |
487 | + You can have several of these and the `git remote` command is used to add, change and delete them. | |
488 | + | |
489 | +-This command is covered in detail in <<ch02-git-basics-chapter#_remote_repos>>, including listing, adding, removing and renaming them. | |
490 | ++This command is covered in detail in <<ch02-git-basics#r_remote_repos>>, including listing, adding, removing and renaming them. | |
491 | + | |
492 | + It is used in nearly every subsequent chapter in the book too, but always in the standard `git remote add <name> <url>` format. | |
493 | + | |
494 | +-==== git archive | |
495 | +- | |
496 | + The `git archive` command is used to create an archive file of a specific snapshot of the project. | |
497 | + | |
498 | +-We use `git archive` to create a tarball of a project for sharing in <<ch05-distributed-git#_preparing_release>>. | |
499 | +- | |
500 | +-==== git submodule | |
501 | ++We use `git archive` to create a tarball of a project for sharing in <<ch05-distributed-git#r_preparing_release>>. | |
502 | + | |
503 | + The `git submodule` command is used to manage external repositories within a normal repositories. | |
504 | + This could be for libraries or other types of shared resources. | |
505 | + The `submodule` command has several sub-commands (`add`, `update`, `sync`, etc) for managing these resources. | |
506 | + | |
507 | +-This command is only mentioned and entirely covered in <<ch07-git-tools#_git_submodules>>. | |
508 | ++This command is only mentioned and entirely covered in <<ch07-git-tools#r_git_submodules>>. | |
509 | + | |
510 | + === Inspection and Comparison | |
511 | + | |
512 | +-==== git show | |
513 | +- | |
514 | + The `git show` command can show a Git object in a simple and human readable way. | |
515 | + Normally you would use this to show the information about a tag or a commit. | |
516 | + | |
517 | +-We first use it to show annotated tag information in <<ch02-git-basics-chapter#_annotated_tags>>. | |
518 | ++We first use it to show annotated tag information in <<ch02-git-basics#r_annotated_tags>>. | |
519 | + | |
520 | +-Later we use it quite a bit in <<ch07-git-tools#_revision_selection>> to show the commits that our various revision selections resolve to. | |
521 | ++Later we use it quite a bit in <<ch07-git-tools#r_revision_selection>> to show the commits that our various revision selections resolve to. | |
522 | + | |
523 | +-One of the more interesting things we do with `git show` is in <<ch07-git-tools#_manual_remerge>> to extract specific file contents of various stages during a merge conflict. | |
524 | +- | |
525 | +-==== git shortlog | |
526 | ++One of the more interesting things we do with `git show` is in <<ch07-git-tools#r_manual_remerge>> to extract specific file contents of various stages during a merge conflict. | |
527 | + | |
528 | + The `git shortlog` command is used to summarize the output of `git log`. | |
529 | + It will take many of the same options that the `git log` command will but instead of listing out all of the commits it will present a summary of the commits grouped by author. | |
530 | + | |
531 | +-We showed how to use it to create a nice changelog in <<ch05-distributed-git#_the_shortlog>>. | |
532 | +- | |
533 | +-==== git describe | |
534 | ++We showed how to use it to create a nice changelog in <<ch05-distributed-git#r_the_shortlog>>. | |
535 | + | |
536 | + The `git describe` command is used to take anything that resolves to a commit and produces a string that is somewhat human-readable and will not change. | |
537 | + It's a way to get a description of a commit that is as unambiguous as a commit SHA-1 but more understandable. | |
538 | + | |
539 | +-We use `git describe` in <<ch05-distributed-git#_build_number>> and <<ch05-distributed-git#_preparing_release>> to get a string to name our release file after. | |
540 | +- | |
541 | ++We use `git describe` in <<ch05-distributed-git#r_build_number>> and <<ch05-distributed-git#r_preparing_release>> to get a string to name our release file after. | |
542 | + | |
543 | + === Debugging | |
544 | + | |
545 | + Git has a couple of commands that are used to help debug an issue in your code. | |
546 | + This ranges from figuring out where something was introduced to figuring out who introduced it. | |
547 | + | |
548 | +-==== git bisect | |
549 | +- | |
550 | + The `git bisect` tool is an incredibly helpful debugging tool used to find which specific commit was the first one to introduce a bug or problem by doing an automatic binary search. | |
551 | + | |
552 | +-It is fully covered in <<ch07-git-tools#_binary_search>> and is only mentioned in that section. | |
553 | +- | |
554 | +-==== git blame | |
555 | ++It is fully covered in <<ch07-git-tools#r_binary_search>> and is only mentioned in that section. | |
556 | + | |
557 | + The `git blame` command annotates the lines of any file with which commit was the last one to introduce a change to each line of the file and what person authored that commit. | |
558 | + This is helpful in order to find the person to ask for more information about a specific section of your code. | |
559 | + | |
560 | +-It is covered in <<ch07-git-tools#_file_annotation>> and is only mentioned in that section. | |
561 | +- | |
562 | +-==== git grep | |
563 | ++It is covered in <<ch07-git-tools#r_file_annotation>> and is only mentioned in that section. | |
564 | + | |
565 | + The `git grep` command can help you find any string or regular expression in any of the files in your source code, even older versions of your project. | |
566 | + | |
567 | +-It is covered in <<ch07-git-tools#_git_grep>> and is only mentioned in that section. | |
568 | ++It is covered in <<ch07-git-tools#r_git_grep>> and is only mentioned in that section. | |
569 | + | |
570 | + === Patching | |
571 | + | |
572 | + A few commands in Git are centered around the concept of thinking of commits in terms of the changes they introduce, as though the commit series is a series of patches. | |
573 | + These commands help you manage your branches in this manner. | |
574 | + | |
575 | +-==== git cherry-pick | |
576 | +- | |
577 | + The `git cherry-pick` command is used to take the change introduced in a single Git commit and try to re-introduce it as a new commit on the branch you're currently on. | |
578 | + This can be useful to only take one or two commits from a branch individually rather than merging in the branch which takes all the changes. | |
579 | + | |
580 | +-Cherry picking is described and demonstrated in <<ch05-distributed-git#_rebase_cherry_pick>>. | |
581 | +- | |
582 | +-==== git rebase | |
583 | ++Cherry picking is described and demonstrated in <<ch05-distributed-git#r_rebase_cherry_pick>>. | |
584 | + | |
585 | + The `git rebase` command is basically an automated `cherry-pick`. | |
586 | + It determines a series of commits and then cherry-picks them one by one in the same order somewhere else. | |
587 | + | |
588 | +-Rebasing is covered in detail in <<ch03-git-branching#_rebasing>>, including covering the collaborative issues involved with rebasing branches that are already public. | |
589 | +- | |
590 | +-We use it in practice during an example of splitting your history into two separate repositories in <<ch07-git-tools#_replace>>, using the `--onto` flag as well. | |
591 | ++Rebasing is covered in detail in <<ch03-git-branching#r_rebasing>>, including covering the collaborative issues involved with rebasing branches that are already public. | |
592 | + | |
593 | +-We go through running into a merge conflict during rebasing in <<ch07-git-tools#ref_rerere>>. | |
594 | ++We use it in practice during an example of splitting your history into two separate repositories in <<ch07-git-tools#r_replace>>, using the `--onto` flag as well. | |
595 | + | |
596 | +-We also use it in an interactive scripting mode with the `-i` option in <<ch07-git-tools#_changing_multiple>>. | |
597 | ++We go through running into a merge conflict during rebasing in <<ch07-git-tools#r_rerere>>. | |
598 | + | |
599 | +-==== git revert | |
600 | ++We also use it in an interactive scripting mode with the `-i` option in <<ch07-git-tools#r_changing_multiple>>. | |
601 | + | |
602 | + The `git revert` command is essentially a reverse `git cherry-pick`. | |
603 | + It creates a new commit that applies the exact opposite of the change introduced in the commit you're targeting, essentially undoing or reverting it. | |
604 | + | |
605 | +-We use this in <<ch07-git-tools#_reverse_commit>> to undo a merge commit. | |
606 | ++We use this in <<ch07-git-tools#r_reverse_commit>> to undo a merge commit. | |
607 | + | |
608 | ||
609 | + | |
610 | + Many Git projects, including Git itself, are entirely maintained over mailing lists. | |
611 | + Git has a number of tools built into it that help make this process easier, from generating patches you can easily email to applying those patches from an email box. | |
612 | + | |
613 | +-==== git apply | |
614 | +- | |
615 | + The `git apply` command applies a patch created with the `git diff` or even GNU diff command. | |
616 | + It is similar to what the `patch` command might do with a few small differences. | |
617 | + | |
618 | +-We demonstrate using it and the circumstances in which you might do so in <<ch05-distributed-git#_patches_from_email>>. | |
619 | +- | |
620 | +-==== git am | |
621 | ++We demonstrate using it and the circumstances in which you might do so in <<ch05-distributed-git#r_patches_from_email>>. | |
622 | + | |
623 | + The `git am` command is used to apply patches from an email inbox, specifically one that is mbox formatted. | |
624 | + This is useful for receiving patches over email and applying them to your project easily. | |
625 | + | |
626 | +-We covered usage and workflow around `git am` in <<ch05-distributed-git#_git_am>> including using the `--resolved`, `-i` and `-3` options. | |
627 | ++We covered usage and workflow around `git am` in <<ch05-distributed-git#r_git_am>> including using the `--resolved`, `-i` and `-3` options. | |
628 | + | |
629 | +-There are also a number of hooks you can use to help with the workflow around `git am` and they are all covered in <<ch08-customizing-git#_email_hooks>>. | |
630 | ++There are also a number of hooks you can use to help with the workflow around `git am` and they are all covered in <<ch08-customizing-git#r_email_hooks>>. | |
631 | + | |
632 | +-We also use it to apply patch formatted GitHub Pull Request changes in <<ch06-github#_email_notifications>>. | |
633 | +- | |
634 | +-==== git format-patch | |
635 | ++We also use it to apply patch formatted GitHub Pull Request changes in <<ch06-github#r_email_notifications>>. | |
636 | + | |
637 | + The `git format-patch` command is used to generate a series of patches in mbox format that you can use to send to a mailing list properly formatted. | |
638 | + | |
639 | +-We go through an example of contributing to a project using the `git format-patch` tool in <<ch05-distributed-git#_project_over_email>>. | |
640 | +- | |
641 | +-==== git imap-send | |
642 | ++We go through an example of contributing to a project using the `git format-patch` tool in <<ch05-distributed-git#r_project_over_email>>. | |
643 | + | |
644 | + The `git imap-send` command uploads a mailbox generated with `git format-patch` into an IMAP drafts folder. | |
645 | + | |
646 | +-We go through an example of contributing to a project by sending patches with the `git imap-send` tool in <<ch05-distributed-git#_project_over_email>>. | |
647 | +- | |
648 | +-==== git send-email | |
649 | ++We go through an example of contributing to a project by sending patches with the `git imap-send` tool in <<ch05-distributed-git#r_project_over_email>>. | |
650 | + | |
651 | + The `git send-email` command is used to send patches that are generated with `git format-patch` over email. | |
652 | + | |
653 | +-We go through an example of contributing to a project by sending patches with the `git send-email` tool in <<ch05-distributed-git#_project_over_email>>. | |
654 | +- | |
655 | +-==== git request-pull | |
656 | ++We go through an example of contributing to a project by sending patches with the `git send-email` tool in <<ch05-distributed-git#r_project_over_email>>. | |
657 | + | |
658 | + The `git request-pull` command is simply used to generate an example message body to email to someone. | |
659 | + If you have a branch on a public server and want to let someone know how to integrate those changes without sending the patches over email, you can run this command and send the output to the person you want to pull the changes in. | |
660 | + | |
661 | +-We demonstrate how to use `git request-pull` to generate a pull message in <<ch05-distributed-git#_public_project>>. | |
662 | ++We demonstrate how to use `git request-pull` to generate a pull message in <<ch05-distributed-git#r_public_project>>. | |
663 | + | |
664 | + === External Systems | |
665 | + | |
666 | + Git comes with a few commands to integrate with other version control systems. | |
667 | + | |
668 | +-==== git svn | |
669 | +- | |
670 | + The `git svn` command is used to communicate with the Subversion version control system as a client. | |
671 | + This means you can use Git to checkout from and commit to a Subversion server. | |
672 | + | |
673 | +-This command is covered in depth in <<ch09-git-and-other-systems#_git_svn>>. | |
674 | +- | |
675 | +-==== git fast-import | |
676 | ++This command is covered in depth in <<ch09-git-and-other-scms#r_git_svn>>. | |
677 | + | |
678 | + For other version control systems or importing from nearly any format, you can use `git fast-import` to quickly map the other format to something Git can easily record. | |
679 | + | |
680 | +-This command is covered in depth in <<ch09-git-and-other-systems#_custom_importer>>. | |
681 | ++This command is covered in depth in <<ch09-git-and-other-scms#r_custom_importer>>. | |
682 | + | |
683 | + === Administration | |
684 | + | |
685 | + If you're administering a Git repository or need to fix something in a big way, Git provides a number of administrative commands to help you out. | |
686 | + | |
687 | +-==== git gc | |
688 | +- | |
689 | + The `git gc` command runs ``garbage collection'' on your repository, removing unnecessary files in your database and packing up the remaining files into a more efficient format. | |
690 | + | |
691 | + This command normally runs in the background for you, though you can manually run it if you wish. | |
692 | +-We go over some examples of this in <<ch10-git-internals#_git_gc>>. | |
693 | +- | |
694 | +-==== git fsck | |
695 | ++We go over some examples of this in <<ch10-git-internals#r_git_gc>>. | |
696 | + | |
697 | + The `git fsck` command is used to check the internal database for problems or inconsistencies. | |
698 | + | |
699 | +-We only quickly use this once in <<ch10-git-internals#_data_recovery>> to search for dangling objects. | |
700 | +- | |
701 | +-==== git reflog | |
702 | ++We only quickly use this once in <<ch10-git-internals#r_data_recovery>> to search for dangling objects. | |
703 | + | |
704 | + The `git reflog` command goes through a log of where all the heads of your branches have been as you work to find commits you may have lost through rewriting histories. | |
705 | + | |
706 | +-We cover this command mainly in <<ch07-git-tools#_git_reflog>>, where we show normal usage to and how to use `git log -g` to view the same information with `git log` output. | |
707 | +- | |
708 | +-We also go through a practical example of recovering such a lost branch in <<ch10-git-internals#_data_recovery>>. | |
709 | ++We cover this command mainly in <<ch07-git-tools#r_git_reflog>>, where we show normal usage to and how to use `git log -g` to view the same information with `git log` output. | |
710 | + | |
711 | +-==== git filter-branch | |
712 | ++We also go through a practical example of recovering such a lost branch in <<ch10-git-internals#r_data_recovery>>. | |
713 | + | |
714 | + The `git filter-branch` command is used to rewrite loads of commits according to certain patterns, like removing a file everywhere or filtering the entire repository down to a single subdirectory for extracting a project. | |
715 | + | |
716 | +-In <<ch07-git-tools#_removing_file_every_commit>> we explain the command and explore several different options such as `--commit-filter`, `--subdirectory-filter` and `--tree-filter`. | |
717 | +- | |
718 | +-In <<ch09-git-and-other-systems#_git_p4>> we use it to fix up imported external repositories. | |
719 | ++In <<ch07-git-tools#r_removing_file_every_commit>> we explain the command and explore several different options such as `--commit-filter`, `--subdirectory-filter` and `--tree-filter`. | |
720 | + | |
721 | ++In <<ch09-git-and-other-scms#r_git_p4>> and <<ch09-git-and-other-scms#r_git_tfs>> we use it to fix up imported external repositories. | |
722 | + | |
723 | + === Plumbing Commands | |
724 | + | |
725 | + There were also quite a number of lower level plumbing commands that we encountered in the book. | |
726 | + | |
727 | +-The first one we encounter is `ls-remote` in <<ch06-github#_pr_refs>> which we use to look at the raw references on the server. | |
728 | ++The first one we encounter is `ls-remote` in <<ch06-github#r_pr_refs>> which we use to look at the raw references on the server. | |
729 | + | |
730 | +-We use `ls-files` in <<ch07-git-tools#_manual_remerge>>, <<ch07-git-tools#ref_rerere>> and <<ch07-git-tools#_the_index>> to take a more raw look at what your staging area looks like. | |
731 | ++We use `ls-files` in <<ch07-git-tools#r_manual_remerge>>, <<ch07-git-tools#r_rerere>> and <<ch07-git-tools#r_the_index>> to take a more raw look at what your staging area looks like. | |
732 | + | |
733 | +-We also mention `rev-parse` in <<ch07-git-tools#_branch_references>> to take just about any string and turn it into an object SHA-1. | |
734 | ++We also mention `rev-parse` in <<ch07-git-tools#r_branch_references>> to take just about any string and turn it into an object SHA-1. | |
735 | + | |
736 | + However, most of the low level plumbing commands we cover are in <<ch10-git-internals#ch10-git-internals>>, which is more or less what the chapter is focused on. | |
737 | + We tried to avoid use of them throughout most of the rest of the book. | |
738 | +diff -au ../../git/progit2/progit2/LICENSE.asc /tmp/LICENSE.asc.tmp | |
739 | +--- ../../git/progit2/progit2/LICENSE.asc 2020-06-28 16:44:50.900011670 +0900 | |
740 | ++++ /tmp/LICENSE.asc.tmp 2020-10-25 09:24:25.552961813 +0900 | |
741 | +@@ -1,2 +0,0 @@ | |
742 | +-This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. | |
743 | +-To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/3.0 or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. | |
744 | +diff -au ../../git/progit2/progit2/README.asc /tmp/README.asc.tmp | |
745 | +--- ../../git/progit2/progit2/README.asc 2020-09-07 09:33:11.246683703 +0900 | |
746 | ++++ /tmp/README.asc.tmp 2020-10-25 09:24:25.563962332 +0900 | |
747 | +@@ -1,22 +1,51 @@ | |
748 | ++= Pro Git 第2版 日本語翻訳 | |
749 | ++ | |
750 | ++このリポジトリを使って、Pro Git第2版の日本語翻訳作業を進めています。 | |
751 | ++ | |
752 | ++- 翻訳を手伝ってみたい | |
753 | ++- 書いてある技術的内容or日本語が間違ってるんだけど | |
754 | ++- どうやって電子書籍データを生成してるんですか? | |
755 | ++ | |
756 | ++などなど、興味を持っていただいた方を歓迎しています。何かありましたら https://github.com/progit/progit2-ja/issues/[Issue] をたてていただくか、メンテナの https://twitter.com/harupong[@harupong] までご連絡いただければ幸いです。 | |
757 | ++ | |
758 | ++== 翻訳作業のお約束 | |
759 | ++ | |
760 | ++翻訳のお手伝いは大歓迎です。以下の2点を踏まえ、ぜひご協力いただければと思っています。 | |
761 | ++ | |
762 | ++1. 作業を始めるときは意思表示をしてください | |
763 | ++1. 既訳を尊重してください | |
764 | ++ | |
765 | ++具体的な作業手順など、詳しいことは link:TRANSLATION_NOTES.asc[TRANSLATION NOTES] を確認してください。 | |
766 | ++ | |
767 | ++'''''''''''''''''''''''''' | |
768 | ++(Below is the translation of original README.asc in English. 以下は英語READMEの翻訳です。) | |
769 | ++ | |
770 | + = Pro Git, Second Edition | |
771 | + | |
772 | + Welcome to the second edition of the Pro Git book. | |
773 | + | |
774 | +-You can find this book online at: https://git-scm.com/book | |
775 | ++You can find this book online at: http://git-scm.com/book | |
776 | + | |
777 | + Like the first edition, the second edition of Pro Git is open source under a Creative Commons license. | |
778 | + | |
779 | + A couple of things have changed since open sourcing the first edition. | |
780 | +-For one, we've moved from Markdown to the amazing AsciiDoc format for the text of the book; here's an https://asciidoctor.org/docs/asciidoc-syntax-quick-reference/[AsciiDoc quick reference]. | |
781 | ++For one, we've moved from Markdown to the amazing Asciidoc format for the text of the book. | |
782 | ++We've also moved to using O'Reilly's https://atlas.oreilly.com[Atlas platform] for generating continuous builds of the book so all major formats are always available in every language. | |
783 | + | |
784 | + We've also moved to keeping the translations in separate repositories rather than subdirectories of the English repository. | |
785 | +-See link:TRANSLATING.md[the translating document] for more information. | |
786 | ++See link:CONTRIBUTING.md[the Contributing document] for more information. | |
787 | + | |
788 | + == How To Generate the Book | |
789 | + | |
790 | +-You can generate the e-book files manually with Asciidoctor. | |
791 | +-We used to be able to build .mobi files (Kindle), but cannot do so now, see #1496 for more information. | |
792 | +-If you run the following you _may_ actually get HTML, Epub and PDF output files: | |
793 | ++There are two ways to generate e-book content from this source code. | |
794 | ++ | |
795 | ++The easiest way is simply to let us do it. | |
796 | ++A robot is standing by to look for new work on the main branch and automatically build it for everyone. | |
797 | ++ | |
798 | ++You can find the current builds on http://git-scm.com/book[] and more information about the builds available at https://progit.org[]. | |
799 | ++ | |
800 | ++The other way to generate e-book files is to do so manually with Asciidoctor. | |
801 | ++If you run the following you _may_ actually get HTML, Epub, Mobi and PDF output files: | |
802 | + | |
803 | + ---- | |
804 | + $ bundle install | |
805 | +@@ -25,17 +54,20 @@ | |
806 | + -- HTML output at progit.html | |
807 | + Converting to EPub... | |
808 | + -- Epub output at progit.epub | |
809 | ++Converting to Mobi (kf8)... | |
810 | ++ -- Mobi output at progit.mobi | |
811 | + Converting to PDF... | |
812 | +- -- PDF output at progit.pdf | |
813 | ++ -- PDF output at progit.pdf | |
814 | + ---- | |
815 | + | |
816 | ++This uses the `asciidoctor`, `asciidoctor-pdf` and `asciidoctor-epub` projects. | |
817 | ++ | |
818 | + == Signaling an Issue | |
819 | + | |
820 | + Before signaling an issue, please check that there isn't already a similar one in the bug tracking system. | |
821 | + | |
822 | +-Also, if this issue has been spotted on the git-scm.com site, please cross-check that it is still present in this repo. | |
823 | +-The issue may have already been corrected, but the changes have not been deployed yet. | |
824 | ++Also, if this issue has been spotted on the git-scm.com site, please cross-check that it is still present in the pdf version. The issue may have already been corrected, but the changes have not been deployed yet. | |
825 | + | |
826 | + == Contributing | |
827 | + | |
828 | +-If you'd like to help out by making a change, take a look at the link:CONTRIBUTING.md[contributor's guide]. | |
829 | ++If you'd like to help out by making a change or contributing a translation, take a look at the link:CONTRIBUTING.md[contributor's guide]. | |
830 | +diff -au ../../git/progit2/progit2/TRANSLATION_NOTES.asc /tmp/TRANSLATION_NOTES.asc.tmp | |
831 | +--- ../../git/progit2/progit2/TRANSLATION_NOTES.asc 2020-06-28 16:44:50.919014468 +0900 | |
832 | ++++ /tmp/TRANSLATION_NOTES.asc.tmp 2020-10-25 09:24:25.577962992 +0900 | |
833 | +@@ -1,11 +0,0 @@ | |
834 | +-== Translation Notes | |
835 | +- | |
836 | +-After forking this repository to translate the work, this file is where the notes for coordinating the translation work would go. | |
837 | +-Things like standardizing on words and expressions so that the work is consistent or notes on how the contributing process is to be handled. | |
838 | +- | |
839 | +-As a translation maintainer, also feel free to modify or completely rewrite the README file to contain instructions specific to your translation. | |
840 | +- | |
841 | +-=== Translation Status | |
842 | +- | |
843 | +-As the work is translated, please update the `status.json` file to indicate the rough percentage complete each file is. | |
844 | +-This will be shown on various pages to let people know how much work is left to be done. | |
845 | +diff -au ../../git/progit2/progit2/book/01-introduction/sections/about-version-control.asc /tmp/book/01-introduction/sections/about-version-control.asc.tmp | |
846 | +--- ../../git/progit2/progit2/book/01-introduction/sections/about-version-control.asc 2020-07-04 16:36:46.718685392 +0900 | |
847 | ++++ /tmp/book/01-introduction/sections/about-version-control.asc.tmp 2020-10-25 09:24:25.587963463 +0900 | |
848 | +@@ -1,60 +1,57 @@ | |
849 | + === About Version Control | |
850 | + | |
851 | +-(((version control))) | |
852 | +-What is ``version control'', and why should you care? | |
853 | ++What is "version control", and why should you care? | |
854 | + Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. | |
855 | +-For the examples in this book, you will use software source code as the files being version controlled, though in reality you can do this with nearly any type of file on a computer. | |
856 | ++For the examples in this book you will use software source code as the files being version controlled, though in reality you can do this with nearly any type of file on a computer. | |
857 | + | |
858 | + If you are a graphic or web designer and want to keep every version of an image or layout (which you would most certainly want to), a Version Control System (VCS) is a very wise thing to use. | |
859 | +-It allows you to revert selected files back to a previous state, revert the entire project back to a previous state, compare changes over time, see who last modified something that might be causing a problem, who introduced an issue and when, and more. | |
860 | ++It allows you to revert files back to a previous state, revert the entire project back to a previous state, compare changes over time, see who last modified something that might be causing a problem, who introduced an issue and when, and more. | |
861 | + Using a VCS also generally means that if you screw things up or lose files, you can easily recover. | |
862 | + In addition, you get all this for very little overhead. | |
863 | + | |
864 | + ==== Local Version Control Systems | |
865 | + | |
866 | +-(((version control,local))) | |
867 | + Many people's version-control method of choice is to copy files into another directory (perhaps a time-stamped directory, if they're clever). | |
868 | + This approach is very common because it is so simple, but it is also incredibly error prone. | |
869 | + It is easy to forget which directory you're in and accidentally write to the wrong file or copy over files you don't mean to. | |
870 | + | |
871 | + To deal with this issue, programmers long ago developed local VCSs that had a simple database that kept all the changes to files under revision control. | |
872 | + | |
873 | +-.Local version control | |
874 | ++.Local version control. | |
875 | + image::images/local.png[Local version control diagram] | |
876 | + | |
877 | +-One of the most popular VCS tools was a system called RCS, which is still distributed with many computers today. | |
878 | +-https://www.gnu.org/software/rcs/[RCS] works by keeping patch sets (that is, the differences between files) in a special format on disk; it can then re-create what any file looked like at any point in time by adding up all the patches. | |
879 | ++One of the more popular VCS tools was a system called RCS, which is still distributed with many computers today. | |
880 | ++Even the popular Mac OS X operating system includes the `rcs` command when you install the Developer Tools. | |
881 | ++RCS works by keeping patch sets (that is, the differences between files) in a special format on disk; it can then re-create what any file looked like at any point in time by adding up all the patches. | |
882 | + | |
883 | + ==== Centralized Version Control Systems | |
884 | + | |
885 | +-(((version control,centralized))) | |
886 | + The next major issue that people encounter is that they need to collaborate with developers on other systems. | |
887 | + To deal with this problem, Centralized Version Control Systems (CVCSs) were developed. | |
888 | +-These systems (such as CVS, Subversion, and Perforce) have a single server that contains all the versioned files, and a number of clients that check out files from that central place. (((CVS)))(((Subversion)))(((Perforce))) | |
889 | ++These systems, such as CVS, Subversion, and Perforce, have a single server that contains all the versioned files, and a number of clients that check out files from that central place. (((CVS)))(((Subversion)))(((Perforce))) | |
890 | + For many years, this has been the standard for version control. | |
891 | + | |
892 | +-.Centralized version control | |
893 | ++.Centralized version control. | |
894 | + image::images/centralized.png[Centralized version control diagram] | |
895 | + | |
896 | + This setup offers many advantages, especially over local VCSs. | |
897 | + For example, everyone knows to a certain degree what everyone else on the project is doing. | |
898 | +-Administrators have fine-grained control over who can do what, and it's far easier to administer a CVCS than it is to deal with local databases on every client. | |
899 | ++Administrators have fine-grained control over who can do what; and it's far easier to administer a CVCS than it is to deal with local databases on every client. | |
900 | + | |
901 | + However, this setup also has some serious downsides. | |
902 | + The most obvious is the single point of failure that the centralized server represents. | |
903 | + If that server goes down for an hour, then during that hour nobody can collaborate at all or save versioned changes to anything they're working on. | |
904 | +-If the hard disk the central database is on becomes corrupted, and proper backups haven't been kept, you lose absolutely everything -- the entire history of the project except whatever single snapshots people happen to have on their local machines. | |
905 | +-Local VCS systems suffer from this same problem -- whenever you have the entire history of the project in a single place, you risk losing everything. | |
906 | ++If the hard disk the central database is on becomes corrupted, and proper backups haven't been kept, you lose absolutely everything – the entire history of the project except whatever single snapshots people happen to have on their local machines. | |
907 | ++Local VCS systems suffer from this same problem – whenever you have the entire history of the project in a single place, you risk losing everything. | |
908 | + | |
909 | + ==== Distributed Version Control Systems | |
910 | + | |
911 | +-(((version control,distributed))) | |
912 | + This is where Distributed Version Control Systems (DVCSs) step in. | |
913 | +-In a DVCS (such as Git, Mercurial, Bazaar or Darcs), clients don't just check out the latest snapshot of the files; rather, they fully mirror the repository, including its full history. | |
914 | +-Thus, if any server dies, and these systems were collaborating via that server, any of the client repositories can be copied back up to the server to restore it. | |
915 | ++In a DVCS (such as Git, Mercurial, Bazaar or Darcs), clients don't just check out the latest snapshot of the files: they fully mirror the repository. | |
916 | ++Thus if any server dies, and these systems were collaborating via it, any of the client repositories can be copied back up to the server to restore it. | |
917 | + Every clone is really a full backup of all the data. | |
918 | + | |
919 | +-.Distributed version control | |
920 | ++.Distributed version control. | |
921 | + image::images/distributed.png[Distributed version control diagram] | |
922 | + | |
923 | + Furthermore, many of these systems deal pretty well with having several remote repositories they can work with, so you can collaborate with different groups of people in different ways simultaneously within the same project. | |
924 | +diff -au ../../git/progit2/progit2/book/01-introduction/sections/command-line.asc /tmp/book/01-introduction/sections/command-line.asc.tmp | |
925 | +--- ../../git/progit2/progit2/book/01-introduction/sections/command-line.asc 2020-06-28 16:44:50.920014615 +0900 | |
926 | ++++ /tmp/book/01-introduction/sections/command-line.asc.tmp 2020-10-25 09:24:25.591963652 +0900 | |
927 | +@@ -1,11 +1,11 @@ | |
928 | + === The Command Line | |
929 | + | |
930 | + There are a lot of different ways to use Git. | |
931 | +-There are the original command-line tools, and there are many graphical user interfaces of varying capabilities. | |
932 | ++There are the original command line tools, and there are many graphical user interfaces of varying capabilities. | |
933 | + For this book, we will be using Git on the command line. | |
934 | +-For one, the command line is the only place you can run _all_ Git commands -- most of the GUIs implement only a partial subset of Git functionality for simplicity. | |
935 | +-If you know how to run the command-line version, you can probably also figure out how to run the GUI version, while the opposite is not necessarily true. | |
936 | ++For one, the command line is the only place you can run *all* Git commands – most of the GUIs only implement some subset of Git functionality for simplicity. | |
937 | ++If you know how to run the command line version, you can probably also figure out how to run the GUI version, while the opposite is not necessarily true. | |
938 | + Also, while your choice of graphical client is a matter of personal taste, _all_ users will have the command-line tools installed and available. | |
939 | + | |
940 | +-So we will expect you to know how to open Terminal in macOS or Command Prompt or PowerShell in Windows. | |
941 | ++So we will expect you to know how to open Terminal in Mac or Command Prompt or Powershell in Windows. | |
942 | + If you don't know what we're talking about here, you may need to stop and research that quickly so that you can follow the rest of the examples and descriptions in this book. | |
943 | +diff -au ../../git/progit2/progit2/book/01-introduction/sections/first-time-setup.asc /tmp/book/01-introduction/sections/first-time-setup.asc.tmp | |
944 | +--- ../../git/progit2/progit2/book/01-introduction/sections/first-time-setup.asc 2020-08-10 10:02:15.168231103 +0900 | |
945 | ++++ /tmp/book/01-introduction/sections/first-time-setup.asc.tmp 2020-10-25 09:24:25.597963934 +0900 | |
946 | +@@ -1,4 +1,4 @@ | |
947 | +-[[_first_time]] | |
948 | ++[[r_first_time]] | |
949 | + === First-Time Git Setup | |
950 | + | |
951 | + Now that you have Git on your system, you'll want to do a few things to customize your Git environment. | |
952 | +@@ -8,129 +8,58 @@ | |
953 | + Git comes with a tool called `git config` that lets you get and set configuration variables that control all aspects of how Git looks and operates.(((git commands, config))) | |
954 | + These variables can be stored in three different places: | |
955 | + | |
956 | +-1. `[path]/etc/gitconfig` file: Contains values applied to every user on the system and all their repositories. | |
957 | ++1. `/etc/gitconfig` file: Contains values for every user on the system and all their repositories. | |
958 | + If you pass the option `--system` to `git config`, it reads and writes from this file specifically. | |
959 | +- Because this is a system configuration file, you would need administrative or superuser privilege to make changes to it. | |
960 | +-2. `~/.gitconfig` or `~/.config/git/config` file: Values specific personally to you, the user. | |
961 | +- You can make Git read and write to this file specifically by passing the `--global` option, and this affects _all_ of the repositories you work with on your system. | |
962 | ++2. `~/.gitconfig` or `~/.config/git/config` file: Specific to your user. | |
963 | ++ You can make Git read and write to this file specifically by passing the `--global` option. | |
964 | + 3. `config` file in the Git directory (that is, `.git/config`) of whatever repository you're currently using: Specific to that single repository. | |
965 | +- You can force Git to read from and write to this file with the `--local` option, but that is in fact the default. | |
966 | +- Unsurprisingly, you need to be located somewhere in a Git repository for this option to work properly. | |
967 | + | |
968 | +-Each level overrides values in the previous level, so values in `.git/config` trump those in `[path]/etc/gitconfig`. | |
969 | ++Each level overrides values in the previous level, so values in `.git/config` trump those in `/etc/gitconfig`. | |
970 | + | |
971 | + On Windows systems, Git looks for the `.gitconfig` file in the `$HOME` directory (`C:\Users\$USER` for most people). | |
972 | +-It also still looks for `[path]/etc/gitconfig`, although it's relative to the MSys root, which is wherever you decide to install Git on your Windows system when you run the installer. | |
973 | +-If you are using version 2.x or later of Git for Windows, there is also a system-level config file at | |
974 | ++It also still looks for `/etc/gitconfig`, although it's relative to the MSys root, which is wherever you decide to install Git on your Windows system when you run the installer. | |
975 | ++If you are using Git for Windows 2.x or later, there is also a system-level config file at | |
976 | + `C:\Documents and Settings\All Users\Application Data\Git\config` on Windows XP, and in `C:\ProgramData\Git\config` on Windows Vista and newer. | |
977 | + This config file can only be changed by `git config -f <file>` as an admin. | |
978 | + | |
979 | +-You can view all of your settings and where they are coming from using: | |
980 | +- | |
981 | +-[source,console] | |
982 | +----- | |
983 | +-$ git config --list --show-origin | |
984 | +----- | |
985 | +- | |
986 | + ==== Your Identity | |
987 | + | |
988 | + The first thing you should do when you install Git is to set your user name and email address. | |
989 | + This is important because every Git commit uses this information, and it's immutably baked into the commits you start creating: | |
990 | + | |
991 | +-[source,console] | |
992 | +----- | |
993 | +-$ git config --global user.name "John Doe" | |
994 | +-$ git config --global user.email johndoe@example.com | |
995 | +----- | |
996 | +- | |
997 | + Again, you need to do this only once if you pass the `--global` option, because then Git will always use that information for anything you do on that system. | |
998 | ++<<<<<<< HEAD | |
999 | + If you want to override this with a different name or email address for specific projects, you can run the command without the `--global` option when you're in that project. | |
1000 | + | |
1001 | + Many of the GUI tools will help you do this when you first run them. | |
1002 | + | |
1003 | +-[[_editor]] | |
1004 | + ==== Your Editor | |
1005 | + | |
1006 | + Now that your identity is set up, you can configure the default text editor that will be used when Git needs you to type in a message. | |
1007 | + If not configured, Git uses your system's default editor. | |
1008 | +- | |
1009 | + If you want to use a different text editor, such as Emacs, you can do the following: | |
1010 | + | |
1011 | +-[source,console] | |
1012 | +----- | |
1013 | +-$ git config --global core.editor emacs | |
1014 | +----- | |
1015 | +- | |
1016 | +-On a Windows system, if you want to use a different text editor, you must specify the full path to its executable file. | |
1017 | +-This can be different depending on how your editor is packaged. | |
1018 | +- | |
1019 | +-In the case of Notepad++, a popular programming editor, you are likely to want to use the 32-bit version, since at the time of writing the 64-bit version doesn't support all plug-ins. | |
1020 | +-If you are on a 32-bit Windows system, or you have a 64-bit editor on a 64-bit system, you'll type something like this: | |
1021 | +- | |
1022 | +-[source,console] | |
1023 | +----- | |
1024 | +-$ git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin" | |
1025 | +----- | |
1026 | ++While on a Windows system, if you want to use a different text editor, such as Notepad++, you can do the following: | |
1027 | ++ | |
1028 | ++On a x86 system | |
1029 | ++ | |
1030 | ++On a x64 system | |
1031 | + | |
1032 | +-[NOTE] | |
1033 | + ==== | |
1034 | +-Vim, Emacs and Notepad++ are popular text editors often used by developers on Unix-based systems like Linux and macOS or a Windows system. | |
1035 | +-If you are using another editor, or a 32-bit version, please find specific instructions for how to set up your favorite editor with Git in <<C-git-commands#_core_editor>>. | |
1036 | ++Vim, Emacs and Notepad++ are popular text editors often used by developers on Unix based systems like Linux and OS X or a Windows system. | |
1037 | ++If you are not familiar with either of these editors, you may need to search for specific instructions for how to set up your favorite editor with Git. | |
1038 | + ==== | |
1039 | + | |
1040 | +-[WARNING] | |
1041 | + ==== | |
1042 | +-You may find, if you don't setup your editor like this, you get into a really confusing state when Git attempts to launch it. | |
1043 | +-An example on a Windows system may include a prematurely terminated Git operation during a Git initiated edit. | |
1044 | ++You may find, if you don't setup an editor like this, you will likely get into a really confusing state when they are launched. | |
1045 | ++Such example on a Windows system may include a prematurely terminated Git operation during a Git initiated edit. | |
1046 | + ==== | |
1047 | + | |
1048 | +-==== Your default branch name | |
1049 | +- | |
1050 | +-By default Git will create a branch called _master_ when you create a new repository with `git init`. | |
1051 | +-From Git version 2.28 onwards, you can set a different name for the initial branch. | |
1052 | +- | |
1053 | +-To set _main_ as the default branch name do: | |
1054 | +- | |
1055 | +-[source,console] | |
1056 | +----- | |
1057 | +-$ git config --global init.defaultBranch main | |
1058 | +----- | |
1059 | +- | |
1060 | + ==== Checking Your Settings | |
1061 | + | |
1062 | +-If you want to check your configuration settings, you can use the `git config --list` command to list all the settings Git can find at that point: | |
1063 | ++If you want to check your settings, you can use the `git config --list` command to list all the settings Git can find at that point: | |
1064 | + | |
1065 | +-[source,console] | |
1066 | +----- | |
1067 | +-$ git config --list | |
1068 | +-user.name=John Doe | |
1069 | +-user.email=johndoe@example.com | |
1070 | +-color.status=auto | |
1071 | +-color.branch=auto | |
1072 | +-color.interactive=auto | |
1073 | +-color.diff=auto | |
1074 | +-... | |
1075 | +----- | |
1076 | +- | |
1077 | +-You may see keys more than once, because Git reads the same key from different files (`[path]/etc/gitconfig` and `~/.gitconfig`, for example). | |
1078 | ++You may see keys more than once, because Git reads the same key from different files (`/etc/gitconfig` and `~/.gitconfig`, for example). | |
1079 | + In this case, Git uses the last value for each unique key it sees. | |
1080 | + | |
1081 | + You can also check what Git thinks a specific key's value is by typing `git config <key>`:(((git commands, config))) | |
1082 | +- | |
1083 | +-[source,console] | |
1084 | +----- | |
1085 | +-$ git config user.name | |
1086 | +-John Doe | |
1087 | +----- | |
1088 | +- | |
1089 | +-[NOTE] | |
1090 | +-==== | |
1091 | +-Since Git might read the same configuration variable value from more than one file, it's possible that you have an unexpected value for one of these values and you don't know why. | |
1092 | +-In cases like that, you can query Git as to the _origin_ for that value, and it will tell you which configuration file had the final say in setting that value: | |
1093 | +- | |
1094 | +-[source,console] | |
1095 | +----- | |
1096 | +-$ git config --show-origin rerere.autoUpdate | |
1097 | +-file:/home/johndoe/.gitconfig false | |
1098 | +----- | |
1099 | +-==== | |
1100 | +diff -au ../../git/progit2/progit2/book/01-introduction/sections/help.asc /tmp/book/01-introduction/sections/help.asc.tmp | |
1101 | +--- ../../git/progit2/progit2/book/01-introduction/sections/help.asc 2020-07-04 16:36:46.718685392 +0900 | |
1102 | ++++ /tmp/book/01-introduction/sections/help.asc.tmp 2020-10-25 09:24:25.601964123 +0900 | |
1103 | +@@ -1,48 +1,10 @@ | |
1104 | +-[[_git_help]] | |
1105 | ++[[r_git_help]] | |
1106 | + === Getting Help | |
1107 | + | |
1108 | +-If you ever need help while using Git, there are three equivalent ways to get the comprehensive manual page (manpage) help for any of the Git commands: | |
1109 | ++If you ever need help while using Git, there are three ways to get the manual page (manpage) help for any of the Git commands: | |
1110 | + | |
1111 | +-[source,console] | |
1112 | +----- | |
1113 | +-$ git help <verb> | |
1114 | +-$ git <verb> --help | |
1115 | +-$ man git-<verb> | |
1116 | +----- | |
1117 | +- | |
1118 | +-For example, you can get the manpage help for the `git config` command by running this:(((git commands, help))) | |
1119 | +- | |
1120 | +-[source,console] | |
1121 | +----- | |
1122 | +-$ git help config | |
1123 | +----- | |
1124 | ++For example, you can get the manpage help for the config command by running(((git commands, help))) | |
1125 | + | |
1126 | + These commands are nice because you can access them anywhere, even offline. | |
1127 | +-If the manpages and this book aren't enough and you need in-person help, you can try the `#git` or `#github` channel on the Freenode IRC server, which can be found at https://freenode.net[]. | |
1128 | ++If the manpages and this book aren't enough and you need in-person help, you can try the `#git` or `#github` channel on the Freenode IRC server (irc.freenode.net). | |
1129 | + These channels are regularly filled with hundreds of people who are all very knowledgeable about Git and are often willing to help.(((IRC))) | |
1130 | +- | |
1131 | +-In addition, if you don't need the full-blown manpage help, but just need a quick refresher on the available options for a Git command, you can ask for the more concise ``help'' output with the `-h` option, as in: | |
1132 | +- | |
1133 | +-[source,console] | |
1134 | +----- | |
1135 | +-$ git add -h | |
1136 | +-usage: git add [<options>] [--] <pathspec>... | |
1137 | +- | |
1138 | +- -n, --dry-run dry run | |
1139 | +- -v, --verbose be verbose | |
1140 | +- | |
1141 | +- -i, --interactive interactive picking | |
1142 | +- -p, --patch select hunks interactively | |
1143 | +- -e, --edit edit current diff and apply | |
1144 | +- -f, --force allow adding otherwise ignored files | |
1145 | +- -u, --update update tracked files | |
1146 | +- --renormalize renormalize EOL of tracked files (implies -u) | |
1147 | +- -N, --intent-to-add record only the fact that the path will be added later | |
1148 | +- -A, --all add changes from all tracked and untracked files | |
1149 | +- --ignore-removal ignore paths removed in the working tree (same as --no-all) | |
1150 | +- --refresh don't add, only refresh the index | |
1151 | +- --ignore-errors just skip files which cannot be added because of errors | |
1152 | +- --ignore-missing check if - even missing - files are ignored in dry run | |
1153 | +- --chmod (+|-)x override the executable bit of the listed files | |
1154 | +----- | |
1155 | +- | |
1156 | +diff -au ../../git/progit2/progit2/book/01-introduction/sections/history.asc /tmp/book/01-introduction/sections/history.asc.tmp | |
1157 | +--- ../../git/progit2/progit2/book/01-introduction/sections/history.asc 2020-06-28 16:44:50.921014763 +0900 | |
1158 | ++++ /tmp/book/01-introduction/sections/history.asc.tmp 2020-10-25 09:24:25.607964406 +0900 | |
1159 | +@@ -17,4 +17,4 @@ | |
1160 | + * Able to handle large projects like the Linux kernel efficiently (speed and data size) | |
1161 | + | |
1162 | + Since its birth in 2005, Git has evolved and matured to be easy to use and yet retain these initial qualities. | |
1163 | +-It's amazingly fast, it's very efficient with large projects, and it has an incredible branching system for non-linear development (See <<ch03-git-branching#ch03-git-branching>>). | |
1164 | ++It's incredibly fast, it's very efficient with large projects, and it has an incredible branching system for non-linear development (See <<ch03-git-branching#ch03-git-branching>>). | |
1165 | +diff -au ../../git/progit2/progit2/book/01-introduction/sections/installing.asc /tmp/book/01-introduction/sections/installing.asc.tmp | |
1166 | +--- ../../git/progit2/progit2/book/01-introduction/sections/installing.asc 2020-07-04 16:36:46.718685392 +0900 | |
1167 | ++++ /tmp/book/01-introduction/sections/installing.asc.tmp 2020-10-25 09:24:25.611964594 +0900 | |
1168 | +@@ -4,144 +4,69 @@ | |
1169 | + Even if it's already installed, it's probably a good idea to update to the latest version. | |
1170 | + You can either install it as a package or via another installer, or download the source code and compile it yourself. | |
1171 | + | |
1172 | +-[NOTE] | |
1173 | + ==== | |
1174 | +-This book was written using Git version *2.8.0*. | |
1175 | ++This book was written using Git version *2.0.0*. | |
1176 | + Though most of the commands we use should work even in ancient versions of Git, some of them might not or might act slightly differently if you're using an older version. | |
1177 | +-Since Git is quite excellent at preserving backwards compatibility, any version after 2.8 should work just fine. | |
1178 | ++Since Git is quite excellent at preserving backwards compatibility, any version after 2.0 should work just fine. | |
1179 | + ==== | |
1180 | + | |
1181 | + ==== Installing on Linux | |
1182 | + | |
1183 | +-(((Linux, installing))) | |
1184 | +-If you want to install the basic Git tools on Linux via a binary installer, you can generally do so through the package management tool that comes with your distribution. | |
1185 | +-If you're on Fedora (or any closely-related RPM-based distribution, such as RHEL or CentOS), you can use `dnf`: | |
1186 | ++If you want to install the basic Git tools on Linux via a binary installer, you can generally do so through the basic package-management tool that comes with your distribution. | |
1187 | ++If you're on Fedora for example, you can use yum: | |
1188 | + | |
1189 | +-[source,console] | |
1190 | +----- | |
1191 | +-$ sudo dnf install git-all | |
1192 | +----- | |
1193 | ++If you're on a Debian-based distribution like Ubuntu, try apt-get: | |
1194 | + | |
1195 | +-If you're on a Debian-based distribution, such as Ubuntu, try `apt`: | |
1196 | ++For more options, there are instructions for installing on several different Unix flavors on the Git website, at http://git-scm.com/download/linux[]. | |
1197 | + | |
1198 | +-[source,console] | |
1199 | +----- | |
1200 | +-$ sudo apt install git-all | |
1201 | +----- | |
1202 | ++==== Installing on Mac | |
1203 | + | |
1204 | +-For more options, there are instructions for installing on several different Unix distributions on the Git website, at https://git-scm.com/download/linux[]. | |
1205 | +- | |
1206 | +-==== Installing on macOS | |
1207 | +- | |
1208 | +-(((macOS, installing))) | |
1209 | + There are several ways to install Git on a Mac. | |
1210 | + The easiest is probably to install the Xcode Command Line Tools.(((Xcode))) | |
1211 | +-On Mavericks (10.9) or above you can do this simply by trying to run `git` from the Terminal the very first time. | |
1212 | +- | |
1213 | +-[source,console] | |
1214 | +----- | |
1215 | +-$ git --version | |
1216 | +----- | |
1217 | +- | |
1218 | ++On Mavericks (10.9) or above you can do this simply by trying to run 'git' from the Terminal the very first time. | |
1219 | + If you don't have it installed already, it will prompt you to install it. | |
1220 | + | |
1221 | + If you want a more up to date version, you can also install it via a binary installer. | |
1222 | +-A macOS Git installer is maintained and available for download at the Git website, at https://git-scm.com/download/mac[]. | |
1223 | ++An OSX Git installer is maintained and available for download at the Git website, at http://git-scm.com/download/mac[]. | |
1224 | + | |
1225 | +-.Git macOS Installer | |
1226 | +-image::images/git-osx-installer.png[Git macOS installer] | |
1227 | ++.Git OS X Installer. | |
1228 | ++image::images/git-osx-installer.png[Git OS X installer.] | |
1229 | + | |
1230 | +-You can also install it as part of the GitHub for macOS install. | |
1231 | ++You can also install it as part of the GitHub for Mac install. | |
1232 | + Their GUI Git tool has an option to install command line tools as well. | |
1233 | +-You can download that tool from the GitHub for macOS website, at https://desktop.github.com[]. | |
1234 | ++You can download that tool from the GitHub for Mac website, at http://mac.github.com[]. | |
1235 | + | |
1236 | + ==== Installing on Windows | |
1237 | + | |
1238 | + There are also a few ways to install Git on Windows.(((Windows, installing))) | |
1239 | + The most official build is available for download on the Git website. | |
1240 | +-Just go to https://git-scm.com/download/win[] and the download will start automatically. | |
1241 | +-Note that this is a project called Git for Windows, which is separate from Git itself; for more information on it, go to https://gitforwindows.org[]. | |
1242 | +- | |
1243 | +-To get an automated installation you can use the https://chocolatey.org/packages/git[Git Chocolatey package]. | |
1244 | +-Note that the Chocolatey package is community maintained. | |
1245 | ++Just go to http://git-scm.com/download/win[] and the download will start automatically. | |
1246 | ++Note that this is a project called Git for Windows, which is separate from Git itself; for more information on it, go to https://git-for-windows.github.io/[]. | |
1247 | + | |
1248 | +-Another easy way to get Git installed is by installing GitHub Desktop. | |
1249 | ++Another easy way to get Git installed is by installing GitHub for Windows. | |
1250 | + The installer includes a command line version of Git as well as the GUI. | |
1251 | +-It also works well with PowerShell, and sets up solid credential caching and sane CRLF settings.(((PowerShell)))(((CRLF)))(((credential caching))) | |
1252 | ++It also works well with Powershell, and sets up solid credential caching and sane CRLF settings.(((Powershell)))(((CRLF)))(((credential caching))) | |
1253 | + We'll learn more about those things a little later, but suffice it to say they're things you want. | |
1254 | +-You can download this from the https://desktop.github.com[GitHub Desktop website]. | |
1255 | ++You can download this from the GitHub for Windows website, at http://windows.github.com[]. | |
1256 | + | |
1257 | + ==== Installing from Source | |
1258 | + | |
1259 | + Some people may instead find it useful to install Git from source, because you'll get the most recent version. | |
1260 | + The binary installers tend to be a bit behind, though as Git has matured in recent years, this has made less of a difference. | |
1261 | + | |
1262 | +-If you do want to install Git from source, you need to have the following libraries that Git depends on: autotools, curl, zlib, openssl, expat, and libiconv. | |
1263 | +-For example, if you're on a system that has `dnf` (such as Fedora) or `apt-get` (such as a Debian-based system), you can use one of these commands to install the minimal dependencies for compiling and installing the Git binaries: | |
1264 | ++If you do want to install Git from source, you need to have the following libraries that Git depends on: curl, zlib, openssl, expat, and libiconv. | |
1265 | ++For example, if you're on a system that has yum (such as Fedora) or apt-get (such as a Debian based system), you can use one of these commands to install the minimal dependencies for compiling and installing the Git binaries: | |
1266 | + | |
1267 | +-[source,console] | |
1268 | +----- | |
1269 | +-$ sudo dnf install dh-autoreconf curl-devel expat-devel gettext-devel \ | |
1270 | +- openssl-devel perl-devel zlib-devel | |
1271 | +-$ sudo apt-get install dh-autoreconf libcurl4-gnutls-dev libexpat1-dev \ | |
1272 | +- gettext libz-dev libssl-dev | |
1273 | +----- | |
1274 | +- | |
1275 | +-In order to be able to add the documentation in various formats (doc, html, info), these additional dependencies are required: | |
1276 | +- | |
1277 | +-[source,console] | |
1278 | +----- | |
1279 | +-$ sudo dnf install asciidoc xmlto docbook2X | |
1280 | +-$ sudo apt-get install asciidoc xmlto docbook2x | |
1281 | +----- | |
1282 | ++In order to be able to add the documentation in various formats (doc, html, info), these additional dependencies are required (Note: users of RHEL and RHEL-derivatives like CentOS and Scientific Linux will have to https://fedoraproject.org/wiki/EPEL#How_can_I_use_these_extra_packages.3F[enable the EPEL repository] to download the `docbook2X` package): | |
1283 | + | |
1284 | +-[NOTE] | |
1285 | +-==== | |
1286 | +-Users of RHEL and RHEL-derivatives like CentOS and Scientific Linux will have to https://fedoraproject.org/wiki/EPEL#How_can_I_use_these_extra_packages.3F[enable the EPEL repository] to download the `docbook2X` package. | |
1287 | +-==== | |
1288 | +- | |
1289 | +-If you're using a Debian-based distribution (Debian/Ubuntu/Ubuntu-derivatives), you also need the `install-info` package: | |
1290 | +- | |
1291 | +-[source,console] | |
1292 | +----- | |
1293 | +-$ sudo apt-get install install-info | |
1294 | +----- | |
1295 | +- | |
1296 | +-If you're using a RPM-based distribution (Fedora/RHEL/RHEL-derivatives), you also need the `getopt` package (which is already installed on a Debian-based distro): | |
1297 | +- | |
1298 | +-[source,console] | |
1299 | +----- | |
1300 | +-$ sudo dnf install getopt | |
1301 | +----- | |
1302 | +- | |
1303 | +-Additionally, if you're using Fedora/RHEL/RHEL-derivatives, you need to do this: | |
1304 | +- | |
1305 | +-[source,console] | |
1306 | +----- | |
1307 | +-$ sudo ln -s /usr/bin/db2x_docbook2texi /usr/bin/docbook2x-texi | |
1308 | +----- | |
1309 | ++Additionally, if you're using Fedora/RHEL/RHEL-derivatives, you need to do this | |
1310 | + | |
1311 | + due to binary name differences. | |
1312 | + | |
1313 | + When you have all the necessary dependencies, you can go ahead and grab the latest tagged release tarball from several places. | |
1314 | +-You can get it via the kernel.org site, at https://www.kernel.org/pub/software/scm/git[], or the mirror on the GitHub website, at https://github.com/git/git/releases[]. | |
1315 | ++You can get it via the Kernel.org site, at https://www.kernel.org/pub/software/scm/git[], or the mirror on the GitHub web site, at https://github.com/git/git/releases[]. | |
1316 | + It's generally a little clearer what the latest version is on the GitHub page, but the kernel.org page also has release signatures if you want to verify your download. | |
1317 | + | |
1318 | + Then, compile and install: | |
1319 | + | |
1320 | +-[source,console] | |
1321 | +----- | |
1322 | +-$ tar -zxf git-2.8.0.tar.gz | |
1323 | +-$ cd git-2.8.0 | |
1324 | +-$ make configure | |
1325 | +-$ ./configure --prefix=/usr | |
1326 | +-$ make all doc info | |
1327 | +-$ sudo make install install-doc install-html install-info | |
1328 | +----- | |
1329 | +- | |
1330 | + After this is done, you can also get Git via Git itself for updates: | |
1331 | +- | |
1332 | +-[source,console] | |
1333 | +----- | |
1334 | +-$ git clone git://git.kernel.org/pub/scm/git/git.git | |
1335 | +----- | |
1336 | +Warning: file not found: ../../git/progit2/progit2-ja/book/01-introduction/sections/what-is-git.asc | |
1337 | +diff -au ../../git/progit2/progit2/book/02-git-basics/sections/aliases.asc /tmp/book/02-git-basics/sections/aliases.asc.tmp | |
1338 | +--- ../../git/progit2/progit2/book/02-git-basics/sections/aliases.asc 2020-07-03 19:43:27.508489967 +0900 | |
1339 | ++++ /tmp/book/02-git-basics/sections/aliases.asc.tmp 2020-10-25 09:24:25.617964877 +0900 | |
1340 | +@@ -1,70 +1,28 @@ | |
1341 | +-[[_git_aliases]] | |
1342 | ++[[r_git_aliases]] | |
1343 | + === Git Aliases | |
1344 | + | |
1345 | +-(((aliases))) | |
1346 | +-Before we move on to the next chapter, we want to introduce a feature that can make your Git experience simpler, easier, and more familiar: aliases. | |
1347 | +-For clarity's sake, we won't be using them anywhere else in this book, but if you go on to use Git with any regularity, aliases are something you should know about. | |
1348 | ++Before we finish this chapter on basic Git, there's just one little tip that can make your Git experience simpler, easier, and more familiar: aliases. | |
1349 | ++We won't refer to them or assume you've used them later in the book, but you should probably know how to use them. | |
1350 | + | |
1351 | + Git doesn't automatically infer your command if you type it in partially. | |
1352 | + If you don't want to type the entire text of each of the Git commands, you can easily set up an alias for each command using `git config`.(((git commands, config))) | |
1353 | + Here are a couple of examples you may want to set up: | |
1354 | + | |
1355 | +-[source,console] | |
1356 | +----- | |
1357 | +-$ git config --global alias.co checkout | |
1358 | +-$ git config --global alias.br branch | |
1359 | +-$ git config --global alias.ci commit | |
1360 | +-$ git config --global alias.st status | |
1361 | +----- | |
1362 | +- | |
1363 | + This means that, for example, instead of typing `git commit`, you just need to type `git ci`. | |
1364 | + As you go on using Git, you'll probably use other commands frequently as well; don't hesitate to create new aliases. | |
1365 | + | |
1366 | + This technique can also be very useful in creating commands that you think should exist. | |
1367 | + For example, to correct the usability problem you encountered with unstaging a file, you can add your own unstage alias to Git: | |
1368 | + | |
1369 | +-[source,console] | |
1370 | +----- | |
1371 | +-$ git config --global alias.unstage 'reset HEAD --' | |
1372 | +----- | |
1373 | +- | |
1374 | + This makes the following two commands equivalent: | |
1375 | + | |
1376 | +-[source,console] | |
1377 | +----- | |
1378 | +-$ git unstage fileA | |
1379 | +-$ git reset HEAD -- fileA | |
1380 | +----- | |
1381 | +- | |
1382 | + This seems a bit clearer. | |
1383 | + It's also common to add a `last` command, like this: | |
1384 | + | |
1385 | +-[source,console] | |
1386 | +----- | |
1387 | +-$ git config --global alias.last 'log -1 HEAD' | |
1388 | +----- | |
1389 | +- | |
1390 | + This way, you can see the last commit easily: | |
1391 | + | |
1392 | +-[source,console] | |
1393 | +----- | |
1394 | +-$ git last | |
1395 | +-commit 66938dae3329c7aebe598c2246a8e6af90d04646 | |
1396 | +-Author: Josh Goebel <dreamer3@example.com> | |
1397 | +-Date: Tue Aug 26 19:48:51 2008 +0800 | |
1398 | +- | |
1399 | +- Test for current head | |
1400 | +- | |
1401 | +- Signed-off-by: Scott Chacon <schacon@example.com> | |
1402 | +----- | |
1403 | +- | |
1404 | + As you can tell, Git simply replaces the new command with whatever you alias it for. | |
1405 | + However, maybe you want to run an external command, rather than a Git subcommand. | |
1406 | + In that case, you start the command with a `!` character. | |
1407 | + This is useful if you write your own tools that work with a Git repository. | |
1408 | + We can demonstrate by aliasing `git visual` to run `gitk`: | |
1409 | +- | |
1410 | +-[source,console] | |
1411 | +----- | |
1412 | +-$ git config --global alias.visual '!gitk' | |
1413 | +----- | |
1414 | +diff -au ../../git/progit2/progit2/book/02-git-basics/sections/getting-a-repository.asc /tmp/book/02-git-basics/sections/getting-a-repository.asc.tmp | |
1415 | +--- ../../git/progit2/progit2/book/02-git-basics/sections/getting-a-repository.asc 2020-07-01 10:48:37.610009640 +0900 | |
1416 | ++++ /tmp/book/02-git-basics/sections/getting-a-repository.asc.tmp 2020-10-25 09:24:25.621965066 +0900 | |
1417 | +@@ -1,87 +1,41 @@ | |
1418 | +-[[_getting_a_repo]] | |
1419 | ++[[r_getting_a_repo]] | |
1420 | + === Getting a Git Repository | |
1421 | + | |
1422 | +-You typically obtain a Git repository in one of two ways: | |
1423 | +- | |
1424 | +-1. You can take a local directory that is currently not under version control, and turn it into a Git repository, or | |
1425 | +-2. You can _clone_ an existing Git repository from elsewhere. | |
1426 | +- | |
1427 | +-In either case, you end up with a Git repository on your local machine, ready for work. | |
1428 | ++You can get a Git project using two main approaches. | |
1429 | ++The first takes an existing project or directory and imports it into Git. | |
1430 | ++The second clones an existing Git repository from another server. | |
1431 | + | |
1432 | + ==== Initializing a Repository in an Existing Directory | |
1433 | + | |
1434 | +-If you have a project directory that is currently not under version control and you want to start controlling it with Git, you first need to go to that project's directory. | |
1435 | +-If you've never done this, it looks a little different depending on which system you're running: | |
1436 | ++If you're starting to track an existing project in Git, you need to go to the project's directory and type: | |
1437 | + | |
1438 | +-for Linux: | |
1439 | +-[source,console] | |
1440 | +----- | |
1441 | +-$ cd /home/user/my_project | |
1442 | +----- | |
1443 | +-for macOS: | |
1444 | +-[source,console] | |
1445 | +----- | |
1446 | +-$ cd /Users/user/my_project | |
1447 | +----- | |
1448 | +-for Windows: | |
1449 | +-[source,console] | |
1450 | +----- | |
1451 | +-$ cd C:/Users/user/my_project | |
1452 | +----- | |
1453 | +- | |
1454 | +-and type: | |
1455 | +- | |
1456 | +-[source,console] | |
1457 | +----- | |
1458 | +-$ git init | |
1459 | +----- | |
1460 | +- | |
1461 | +-This creates a new subdirectory named `.git` that contains all of your necessary repository files -- a Git repository skeleton. | |
1462 | ++This creates a new subdirectory named `.git` that contains all of your necessary repository files – a Git repository skeleton. | |
1463 | + At this point, nothing in your project is tracked yet. | |
1464 | +-See <<ch10-git-internals#ch10-git-internals>> for more information about exactly what files are contained in the `.git` directory you just created.(((git commands, init))) | |
1465 | ++(See <<ch10-git-internals#ch10-git-internals>> for more information about exactly what files are contained in the `.git` directory you just created.)(((git commands, init))) | |
1466 | + | |
1467 | + If you want to start version-controlling existing files (as opposed to an empty directory), you should probably begin tracking those files and do an initial commit. | |
1468 | + You can accomplish that with a few `git add` commands that specify the files you want to track, followed by a `git commit`: | |
1469 | + | |
1470 | +-[source,console] | |
1471 | +----- | |
1472 | +-$ git add *.c | |
1473 | +-$ git add LICENSE | |
1474 | +-$ git commit -m 'Initial project version' | |
1475 | +----- | |
1476 | +- | |
1477 | + We'll go over what these commands do in just a minute. | |
1478 | + At this point, you have a Git repository with tracked files and an initial commit. | |
1479 | + | |
1480 | +-[[_git_cloning]] | |
1481 | + ==== Cloning an Existing Repository | |
1482 | + | |
1483 | +-If you want to get a copy of an existing Git repository -- for example, a project you'd like to contribute to -- the command you need is `git clone`. | |
1484 | ++If you want to get a copy of an existing Git repository – for example, a project you'd like to contribute to – the command you need is `git clone`. | |
1485 | + If you're familiar with other VCS systems such as Subversion, you'll notice that the command is "clone" and not "checkout". | |
1486 | +-This is an important distinction -- instead of getting just a working copy, Git receives a full copy of nearly all data that the server has. | |
1487 | ++This is an important distinction – instead of getting just a working copy, Git receives a full copy of nearly all data that the server has. | |
1488 | + Every version of every file for the history of the project is pulled down by default when you run `git clone`. | |
1489 | +-In fact, if your server disk gets corrupted, you can often use nearly any of the clones on any client to set the server back to the state it was in when it was cloned (you may lose some server-side hooks and such, but all the versioned data would be there -- see <<ch04-git-on-the-server#_getting_git_on_a_server>> for more details). | |
1490 | +- | |
1491 | +-You clone a repository with `git clone <url>`.(((git commands, clone))) | |
1492 | +-For example, if you want to clone the Git linkable library called `libgit2`, you can do so like this: | |
1493 | +- | |
1494 | +-[source,console] | |
1495 | +----- | |
1496 | +-$ git clone https://github.com/libgit2/libgit2 | |
1497 | +----- | |
1498 | +- | |
1499 | +-That creates a directory named `libgit2`, initializes a `.git` directory inside it, pulls down all the data for that repository, and checks out a working copy of the latest version. | |
1500 | +-If you go into the new `libgit2` directory that was just created, you'll see the project files in there, ready to be worked on or used. | |
1501 | ++In fact, if your server disk gets corrupted, you can often use nearly any of the clones on any client to set the server back to the state it was in when it was cloned (you may lose some server-side hooks and such, but all the versioned data would be there – see <<ch04-git-server#r_git_on_the_server>> for more details). | |
1502 | + | |
1503 | +-If you want to clone the repository into a directory named something other than `libgit2`, you can specify the new directory name as an additional argument: | |
1504 | ++You clone a repository with `git clone [url]`.(((git commands, clone))) | |
1505 | ++For example, if you want to clone the Git linkable library called libgit2, you can do so like this: | |
1506 | + | |
1507 | +-[source,console] | |
1508 | +----- | |
1509 | +-$ git clone https://github.com/libgit2/libgit2 mylibgit | |
1510 | +----- | |
1511 | ++That creates a directory named ``libgit2'', initializes a `.git` directory inside it, pulls down all the data for that repository, and checks out a working copy of the latest version. | |
1512 | ++If you go into the new `libgit2` directory, you'll see the project files in there, ready to be worked on or used. | |
1513 | ++If you want to clone the repository into a directory named something other than ``libgit2'', you can specify that as the next command-line option: | |
1514 | + | |
1515 | + That command does the same thing as the previous one, but the target directory is called `mylibgit`. | |
1516 | + | |
1517 | + Git has a number of different transfer protocols you can use. | |
1518 | + The previous example uses the `https://` protocol, but you may also see `git://` or `user@server:path/to/repo.git`, which uses the SSH transfer protocol. | |
1519 | +-<<ch04-git-on-the-server#_getting_git_on_a_server>> will introduce all of the available options the server can set up to access your Git repository and the pros and cons of each. | |
1520 | ++<<ch04-git-server#r_git_on_the_server>> will introduce all of the available options the server can set up to access your Git repository and the pros and cons of each. | |
1521 | +diff -au ../../git/progit2/progit2/book/02-git-basics/sections/recording-changes.asc /tmp/book/02-git-basics/sections/recording-changes.asc.tmp | |
1522 | +--- ../../git/progit2/progit2/book/02-git-basics/sections/recording-changes.asc 2020-07-04 16:36:46.720685416 +0900 | |
1523 | ++++ /tmp/book/02-git-basics/sections/recording-changes.asc.tmp 2020-10-25 09:24:25.627965349 +0900 | |
1524 | +@@ -1,91 +1,48 @@ | |
1525 | + === Recording Changes to the Repository | |
1526 | + | |
1527 | +-At this point, you should have a _bona fide_ Git repository on your local machine, and a checkout or _working copy_ of all of its files in front of you. | |
1528 | +-Typically, you'll want to start making changes and committing snapshots of those changes into your repository each time the project reaches a state you want to record. | |
1529 | ++You have a bona fide Git repository and a checkout or working copy of the files for that project. | |
1530 | ++You need to make some changes and commit snapshots of those changes into your repository each time the project reaches a state you want to record. | |
1531 | + | |
1532 | +-Remember that each file in your working directory can be in one of two states: _tracked_ or _untracked_. | |
1533 | ++Remember that each file in your working directory can be in one of two states: tracked or untracked. | |
1534 | + Tracked files are files that were in the last snapshot; they can be unmodified, modified, or staged. | |
1535 | +-In short, tracked files are files that Git knows about. | |
1536 | +- | |
1537 | +-Untracked files are everything else -- any files in your working directory that were not in your last snapshot and are not in your staging area. | |
1538 | +-When you first clone a repository, all of your files will be tracked and unmodified because Git just checked them out and you haven't edited anything. | |
1539 | ++Untracked files are everything else – any files in your working directory that were not in your last snapshot and are not in your staging area. | |
1540 | ++When you first clone a repository, all of your files will be tracked and unmodified because you just checked them out and haven't edited anything. | |
1541 | + | |
1542 | + As you edit files, Git sees them as modified, because you've changed them since your last commit. | |
1543 | +-As you work, you selectively stage these modified files and then commit all those staged changes, and the cycle repeats. | |
1544 | ++You stage these modified files and then commit all your staged changes, and the cycle repeats. | |
1545 | + | |
1546 | +-.The lifecycle of the status of your files | |
1547 | +-image::images/lifecycle.png[The lifecycle of the status of your files] | |
1548 | ++.The lifecycle of the status of your files. | |
1549 | ++image::images/lifecycle.png[The lifecycle of the status of your files.] | |
1550 | + | |
1551 | +-[[_checking_status]] | |
1552 | + ==== Checking the Status of Your Files | |
1553 | + | |
1554 | + The main tool you use to determine which files are in which state is the `git status` command.(((git commands, status))) | |
1555 | + If you run this command directly after a clone, you should see something like this: | |
1556 | + | |
1557 | +-[source,console] | |
1558 | +----- | |
1559 | +-$ git status | |
1560 | +-On branch master | |
1561 | +-Your branch is up-to-date with 'origin/master'. | |
1562 | +-nothing to commit, working directory clean | |
1563 | +----- | |
1564 | +- | |
1565 | +-This means you have a clean working directory; in other words, none of your tracked files are modified. | |
1566 | ++This means you have a clean working directory – in other words, there are no tracked and modified files. | |
1567 | + Git also doesn't see any untracked files, or they would be listed here. | |
1568 | + Finally, the command tells you which branch you're on and informs you that it has not diverged from the same branch on the server. | |
1569 | +-For now, that branch is always `master`, which is the default; you won't worry about it here. | |
1570 | +-<<ch03-git-branching#ch03-git-branching>> will go over branches and references in detail. | |
1571 | ++For now, that branch is always ``master'', which is the default; you won't worry about it here. | |
1572 | ++<<ch03-git-branching#ch03-git-branching>> will go over branches and references in detail. | |
1573 | + | |
1574 | +-Let's say you add a new file to your project, a simple `README` file. | |
1575 | ++Let's say you add a new file to your project, a simple README file. | |
1576 | + If the file didn't exist before, and you run `git status`, you see your untracked file like so: | |
1577 | + | |
1578 | +-[source,console] | |
1579 | +----- | |
1580 | +-$ echo 'My Project' > README | |
1581 | +-$ git status | |
1582 | +-On branch master | |
1583 | +-Your branch is up-to-date with 'origin/master'. | |
1584 | +-Untracked files: | |
1585 | +- (use "git add <file>..." to include in what will be committed) | |
1586 | +- | |
1587 | +- README | |
1588 | +- | |
1589 | +-nothing added to commit but untracked files present (use "git add" to track) | |
1590 | +----- | |
1591 | +- | |
1592 | +-You can see that your new `README` file is untracked, because it's under the ``Untracked files'' heading in your status output. | |
1593 | ++You can see that your new README file is untracked, because it's under the ``Untracked files'' heading in your status output. | |
1594 | + Untracked basically means that Git sees a file you didn't have in the previous snapshot (commit); Git won't start including it in your commit snapshots until you explicitly tell it to do so. | |
1595 | + It does this so you don't accidentally begin including generated binary files or other files that you did not mean to include. | |
1596 | +-You do want to start including `README`, so let's start tracking the file. | |
1597 | ++You do want to start including README, so let's start tracking the file. | |
1598 | + | |
1599 | +-[[_tracking_files]] | |
1600 | + ==== Tracking New Files | |
1601 | + | |
1602 | + In order to begin tracking a new file, you use the command `git add`.(((git commands, add))) | |
1603 | +-To begin tracking the `README` file, you can run this: | |
1604 | +- | |
1605 | +-[source,console] | |
1606 | +----- | |
1607 | +-$ git add README | |
1608 | +----- | |
1609 | +- | |
1610 | +-If you run your status command again, you can see that your `README` file is now tracked and staged to be committed: | |
1611 | +- | |
1612 | +-[source,console] | |
1613 | +----- | |
1614 | +-$ git status | |
1615 | +-On branch master | |
1616 | +-Your branch is up-to-date with 'origin/master'. | |
1617 | +-Changes to be committed: | |
1618 | +- (use "git restore --staged <file>..." to unstage) | |
1619 | ++To begin tracking the README file, you can run this: | |
1620 | + | |
1621 | +- new file: README | |
1622 | +- | |
1623 | +----- | |
1624 | ++If you run your status command again, you can see that your README file is now tracked and staged to be committed: | |
1625 | + | |
1626 | + You can tell that it's staged because it's under the ``Changes to be committed'' heading. | |
1627 | +-If you commit at this point, the version of the file at the time you ran `git add` is what will be in the subsequent historical snapshot. | |
1628 | +-You may recall that when you ran `git init` earlier, you then ran `git add <files>` -- that was to begin tracking files in your directory.(((git commands, init)))(((git commands, add))) | |
1629 | ++If you commit at this point, the version of the file at the time you ran `git add` is what will be in the historical snapshot. | |
1630 | ++You may recall that when you ran `git init` earlier, you then ran `git add (files)` – that was to begin tracking files in your directory.(((git commands, init)))(((git commands, add))) | |
1631 | + The `git add` command takes a path name for either a file or a directory; if it's a directory, the command adds all the files in that directory recursively. | |
1632 | + | |
1633 | + ==== Staging Modified Files | |
1634 | +@@ -93,69 +50,17 @@ | |
1635 | + Let's change a file that was already tracked. | |
1636 | + If you change a previously tracked file called `CONTRIBUTING.md` and then run your `git status` command again, you get something that looks like this: | |
1637 | + | |
1638 | +-[source,console] | |
1639 | +----- | |
1640 | +-$ git status | |
1641 | +-On branch master | |
1642 | +-Your branch is up-to-date with 'origin/master'. | |
1643 | +-Changes to be committed: | |
1644 | +- (use "git reset HEAD <file>..." to unstage) | |
1645 | +- | |
1646 | +- new file: README | |
1647 | +- | |
1648 | +-Changes not staged for commit: | |
1649 | +- (use "git add <file>..." to update what will be committed) | |
1650 | +- (use "git checkout -- <file>..." to discard changes in working directory) | |
1651 | +- | |
1652 | +- modified: CONTRIBUTING.md | |
1653 | +- | |
1654 | +----- | |
1655 | +- | |
1656 | +-The `CONTRIBUTING.md` file appears under a section named ``Changes not staged for commit'' -- which means that a file that is tracked has been modified in the working directory but not yet staged. | |
1657 | ++The `CONTRIBUTING.md` file appears under a section named ``Changes not staged for commit'' – which means that a file that is tracked has been modified in the working directory but not yet staged. | |
1658 | + To stage it, you run the `git add` command. | |
1659 | +-`git add` is a multipurpose command -- you use it to begin tracking new files, to stage files, and to do other things like marking merge-conflicted files as resolved. | |
1660 | +-It may be helpful to think of it more as ``add precisely this content to the next commit'' rather than ``add this file to the project''.(((git commands, add))) | |
1661 | ++`git add` is a multipurpose command – you use it to begin tracking new files, to stage files, and to do other things like marking merge-conflicted files as resolved. | |
1662 | ++It may be helpful to think of it more as ``add this content to the next commit'' rather than ``add this file to the project''.(((git commands, add))) | |
1663 | + Let's run `git add` now to stage the `CONTRIBUTING.md` file, and then run `git status` again: | |
1664 | + | |
1665 | +-[source,console] | |
1666 | +----- | |
1667 | +-$ git add CONTRIBUTING.md | |
1668 | +-$ git status | |
1669 | +-On branch master | |
1670 | +-Your branch is up-to-date with 'origin/master'. | |
1671 | +-Changes to be committed: | |
1672 | +- (use "git reset HEAD <file>..." to unstage) | |
1673 | +- | |
1674 | +- new file: README | |
1675 | +- modified: CONTRIBUTING.md | |
1676 | +- | |
1677 | +----- | |
1678 | +- | |
1679 | + Both files are staged and will go into your next commit. | |
1680 | + At this point, suppose you remember one little change that you want to make in `CONTRIBUTING.md` before you commit it. | |
1681 | + You open it again and make that change, and you're ready to commit. | |
1682 | + However, let's run `git status` one more time: | |
1683 | + | |
1684 | +-[source,console] | |
1685 | +----- | |
1686 | +-$ vim CONTRIBUTING.md | |
1687 | +-$ git status | |
1688 | +-On branch master | |
1689 | +-Your branch is up-to-date with 'origin/master'. | |
1690 | +-Changes to be committed: | |
1691 | +- (use "git reset HEAD <file>..." to unstage) | |
1692 | +- | |
1693 | +- new file: README | |
1694 | +- modified: CONTRIBUTING.md | |
1695 | +- | |
1696 | +-Changes not staged for commit: | |
1697 | +- (use "git add <file>..." to update what will be committed) | |
1698 | +- (use "git checkout -- <file>..." to discard changes in working directory) | |
1699 | +- | |
1700 | +- modified: CONTRIBUTING.md | |
1701 | +- | |
1702 | +----- | |
1703 | +- | |
1704 | + What the heck? | |
1705 | + Now `CONTRIBUTING.md` is listed as both staged _and_ unstaged. | |
1706 | + How is that possible? | |
1707 | +@@ -163,41 +68,17 @@ | |
1708 | + If you commit now, the version of `CONTRIBUTING.md` as it was when you last ran the `git add` command is how it will go into the commit, not the version of the file as it looks in your working directory when you run `git commit`. | |
1709 | + If you modify a file after you run `git add`, you have to run `git add` again to stage the latest version of the file: | |
1710 | + | |
1711 | +-[source,console] | |
1712 | +----- | |
1713 | +-$ git add CONTRIBUTING.md | |
1714 | +-$ git status | |
1715 | +-On branch master | |
1716 | +-Your branch is up-to-date with 'origin/master'. | |
1717 | +-Changes to be committed: | |
1718 | +- (use "git reset HEAD <file>..." to unstage) | |
1719 | +- | |
1720 | +- new file: README | |
1721 | +- modified: CONTRIBUTING.md | |
1722 | +----- | |
1723 | +- | |
1724 | + ==== Short Status | |
1725 | + | |
1726 | + While the `git status` output is pretty comprehensive, it's also quite wordy. | |
1727 | + Git also has a short status flag so you can see your changes in a more compact way. | |
1728 | + If you run `git status -s` or `git status --short` you get a far more simplified output from the command: | |
1729 | + | |
1730 | +-[source,console] | |
1731 | +----- | |
1732 | +-$ git status -s | |
1733 | +- M README | |
1734 | +-MM Rakefile | |
1735 | +-A lib/git.rb | |
1736 | +-M lib/simplegit.rb | |
1737 | +-?? LICENSE.txt | |
1738 | +----- | |
1739 | +- | |
1740 | + New files that aren't tracked have a `??` next to them, new files that have been added to the staging area have an `A`, modified files have an `M` and so on. | |
1741 | +-There are two columns to the output -- the left-hand column indicates the status of the staging area and the right-hand column indicates the status of the working tree. | |
1742 | ++There are two columns to the output - the left hand column indicates that the file is staged and the right hand column indicates that it's modified. | |
1743 | + So for example in that output, the `README` file is modified in the working directory but not yet staged, while the `lib/simplegit.rb` file is modified and staged. | |
1744 | + The `Rakefile` was modified, staged and then modified again, so there are changes to it that are both staged and unstaged. | |
1745 | + | |
1746 | +-[[_ignoring]] | |
1747 | + ==== Ignoring Files | |
1748 | + | |
1749 | + Often, you'll have a class of files that you don't want Git to automatically add or even show you as being untracked. | |
1750 | +@@ -205,198 +86,57 @@ | |
1751 | + In such cases, you can create a file listing patterns to match them named `.gitignore`.(((ignoring files))) | |
1752 | + Here is an example `.gitignore` file: | |
1753 | + | |
1754 | +-[source,console] | |
1755 | +----- | |
1756 | +-$ cat .gitignore | |
1757 | +-*.[oa] | |
1758 | +-*~ | |
1759 | +----- | |
1760 | +- | |
1761 | +-The first line tells Git to ignore any files ending in ``.o'' or ``.a'' -- object and archive files that may be the product of building your code. | |
1762 | +-The second line tells Git to ignore all files whose names end with a tilde (`~`), which is used by many text editors such as Emacs to mark temporary files. | |
1763 | ++The first line tells Git to ignore any files ending in ``.o'' or ``.a'' – object and archive files that may be the product of building your code. | |
1764 | ++The second line tells Git to ignore all files that end with a tilde (`~`), which is used by many text editors such as Emacs to mark temporary files. | |
1765 | + You may also include a log, tmp, or pid directory; automatically generated documentation; and so on. | |
1766 | +-Setting up a `.gitignore` file for your new repository before you get going is generally a good idea so you don't accidentally commit files that you really don't want in your Git repository. | |
1767 | ++Setting up a `.gitignore` file before you get going is generally a good idea so you don't accidentally commit files that you really don't want in your Git repository. | |
1768 | + | |
1769 | + The rules for the patterns you can put in the `.gitignore` file are as follows: | |
1770 | + | |
1771 | + * Blank lines or lines starting with `#` are ignored. | |
1772 | +-* Standard glob patterns work, and will be applied recursively throughout the entire working tree. | |
1773 | ++* Standard glob patterns work. | |
1774 | + * You can start patterns with a forward slash (`/`) to avoid recursivity. | |
1775 | + * You can end patterns with a forward slash (`/`) to specify a directory. | |
1776 | + * You can negate a pattern by starting it with an exclamation point (`!`). | |
1777 | + | |
1778 | + Glob patterns are like simplified regular expressions that shells use. | |
1779 | +-An asterisk (`*`) matches zero or more characters; `[abc]` matches any character inside the brackets (in this case a, b, or c); a question mark (`?`) matches a single character; and brackets enclosing characters separated by a hyphen (`[0-9]`) matches any character between them (in this case 0 through 9). | |
1780 | ++An asterisk (`*`) matches zero or more characters; `[abc]` matches any character inside the brackets (in this case a, b, or c); a question mark (`?`) matches a single character; and brackets enclosing characters separated by a hyphen(`[0-9]`) matches any character between them (in this case 0 through 9). | |
1781 | + You can also use two asterisks to match nested directories; `a/**/z` would match `a/z`, `a/b/z`, `a/b/c/z`, and so on. | |
1782 | + | |
1783 | +-Here is another example `.gitignore` file: | |
1784 | +- | |
1785 | +-[source] | |
1786 | +----- | |
1787 | +-# ignore all .a files | |
1788 | +-*.a | |
1789 | +- | |
1790 | +-# but do track lib.a, even though you're ignoring .a files above | |
1791 | +-!lib.a | |
1792 | +- | |
1793 | +-# only ignore the TODO file in the current directory, not subdir/TODO | |
1794 | +-/TODO | |
1795 | ++Here is another example .gitignore file: | |
1796 | + | |
1797 | +-# ignore all files in any directory named build | |
1798 | +-build/ | |
1799 | +- | |
1800 | +-# ignore doc/notes.txt, but not doc/server/arch.txt | |
1801 | +-doc/*.txt | |
1802 | +- | |
1803 | +-# ignore all .pdf files in the doc/ directory and any of its subdirectories | |
1804 | +-doc/**/*.pdf | |
1805 | +----- | |
1806 | +- | |
1807 | +-[TIP] | |
1808 | + ==== | |
1809 | + GitHub maintains a fairly comprehensive list of good `.gitignore` file examples for dozens of projects and languages at https://github.com/github/gitignore[] if you want a starting point for your project. | |
1810 | + ==== | |
1811 | + | |
1812 | +-[NOTE] | |
1813 | +-==== | |
1814 | +-In the simple case, a repository might have a single `.gitignore` file in its root directory, which applies recursively to the entire repository. | |
1815 | +-However, it is also possible to have additional `.gitignore` files in subdirectories. | |
1816 | +-The rules in these nested `.gitignore` files apply only to the files under the directory where they are located. | |
1817 | +-The Linux kernel source repository has 206 `.gitignore` files. | |
1818 | +- | |
1819 | +-It is beyond the scope of this book to get into the details of multiple `.gitignore` files; see `man gitignore` for the details. | |
1820 | +-==== | |
1821 | +- | |
1822 | +-[[_git_diff_staged]] | |
1823 | + ==== Viewing Your Staged and Unstaged Changes | |
1824 | + | |
1825 | +-If the `git status` command is too vague for you -- you want to know exactly what you changed, not just which files were changed -- you can use the `git diff` command.(((git commands, diff))) | |
1826 | ++If the `git status` command is too vague for you – you want to know exactly what you changed, not just which files were changed – you can use the `git diff` command.(((git commands, diff))) | |
1827 | + We'll cover `git diff` in more detail later, but you'll probably use it most often to answer these two questions: What have you changed but not yet staged? | |
1828 | + And what have you staged that you are about to commit? | |
1829 | +-Although `git status` answers those questions very generally by listing the file names, `git diff` shows you the exact lines added and removed -- the patch, as it were. | |
1830 | ++Although `git status` answers those questions very generally by listing the file names, `git diff` shows you the exact lines added and removed – the patch, as it were. | |
1831 | + | |
1832 | + Let's say you edit and stage the `README` file again and then edit the `CONTRIBUTING.md` file without staging it. | |
1833 | + If you run your `git status` command, you once again see something like this: | |
1834 | + | |
1835 | +-[source,console] | |
1836 | +----- | |
1837 | +-$ git status | |
1838 | +-On branch master | |
1839 | +-Your branch is up-to-date with 'origin/master'. | |
1840 | +-Changes to be committed: | |
1841 | +- (use "git reset HEAD <file>..." to unstage) | |
1842 | +- | |
1843 | +- modified: README | |
1844 | +- | |
1845 | +-Changes not staged for commit: | |
1846 | +- (use "git add <file>..." to update what will be committed) | |
1847 | +- (use "git checkout -- <file>..." to discard changes in working directory) | |
1848 | +- | |
1849 | +- modified: CONTRIBUTING.md | |
1850 | +----- | |
1851 | +- | |
1852 | + To see what you've changed but not yet staged, type `git diff` with no other arguments: | |
1853 | + | |
1854 | +-[source,console] | |
1855 | +----- | |
1856 | +-$ git diff | |
1857 | +-diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md | |
1858 | +-index 8ebb991..643e24f 100644 | |
1859 | +---- a/CONTRIBUTING.md | |
1860 | +-+++ b/CONTRIBUTING.md | |
1861 | +-@@ -65,7 +65,8 @@ branch directly, things can get messy. | |
1862 | +- Please include a nice description of your changes when you submit your PR; | |
1863 | +- if we have to read the whole diff to figure out why you're contributing | |
1864 | +- in the first place, you're less likely to get feedback and have your change | |
1865 | +--merged in. | |
1866 | +-+merged in. Also, split your changes into comprehensive chunks if your patch is | |
1867 | +-+longer than a dozen lines. | |
1868 | +- | |
1869 | +- If you are starting to work on a particular area, feel free to submit a PR | |
1870 | +- that highlights your work in progress (and note in the PR title that it's | |
1871 | +----- | |
1872 | +- | |
1873 | + That command compares what is in your working directory with what is in your staging area. | |
1874 | + The result tells you the changes you've made that you haven't yet staged. | |
1875 | + | |
1876 | + If you want to see what you've staged that will go into your next commit, you can use `git diff --staged`. | |
1877 | + This command compares your staged changes to your last commit: | |
1878 | + | |
1879 | +-[source,console] | |
1880 | +----- | |
1881 | +-$ git diff --staged | |
1882 | +-diff --git a/README b/README | |
1883 | +-new file mode 100644 | |
1884 | +-index 0000000..03902a1 | |
1885 | +---- /dev/null | |
1886 | +-+++ b/README | |
1887 | +-@@ -0,0 +1 @@ | |
1888 | +-+My Project | |
1889 | +----- | |
1890 | +- | |
1891 | +-It's important to note that `git diff` by itself doesn't show all changes made since your last commit -- only changes that are still unstaged. | |
1892 | +-If you've staged all of your changes, `git diff` will give you no output. | |
1893 | ++It's important to note that `git diff` by itself doesn't show all changes made since your last commit – only changes that are still unstaged. | |
1894 | ++This can be confusing, because if you've staged all of your changes, `git diff` will give you no output. | |
1895 | + | |
1896 | + For another example, if you stage the `CONTRIBUTING.md` file and then edit it, you can use `git diff` to see the changes in the file that are staged and the changes that are unstaged. | |
1897 | + If our environment looks like this: | |
1898 | + | |
1899 | +-[source,console] | |
1900 | +----- | |
1901 | +-$ git add CONTRIBUTING.md | |
1902 | +-$ echo '# test line' >> CONTRIBUTING.md | |
1903 | +-$ git status | |
1904 | +-On branch master | |
1905 | +-Your branch is up-to-date with 'origin/master'. | |
1906 | +-Changes to be committed: | |
1907 | +- (use "git reset HEAD <file>..." to unstage) | |
1908 | +- | |
1909 | +- modified: CONTRIBUTING.md | |
1910 | +- | |
1911 | +-Changes not staged for commit: | |
1912 | +- (use "git add <file>..." to update what will be committed) | |
1913 | +- (use "git checkout -- <file>..." to discard changes in working directory) | |
1914 | +- | |
1915 | +- modified: CONTRIBUTING.md | |
1916 | +----- | |
1917 | +- | |
1918 | + Now you can use `git diff` to see what is still unstaged: | |
1919 | + | |
1920 | +-[source,console] | |
1921 | +----- | |
1922 | +-$ git diff | |
1923 | +-diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md | |
1924 | +-index 643e24f..87f08c8 100644 | |
1925 | +---- a/CONTRIBUTING.md | |
1926 | +-+++ b/CONTRIBUTING.md | |
1927 | +-@@ -119,3 +119,4 @@ at the | |
1928 | +- ## Starter Projects | |
1929 | +- | |
1930 | +- See our [projects list](https://github.com/libgit2/libgit2/blob/development/PROJECTS.md). | |
1931 | +-+# test line | |
1932 | +----- | |
1933 | +- | |
1934 | + and `git diff --cached` to see what you've staged so far (`--staged` and `--cached` are synonyms): | |
1935 | + | |
1936 | +-[source,console] | |
1937 | +----- | |
1938 | +-$ git diff --cached | |
1939 | +-diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md | |
1940 | +-index 8ebb991..643e24f 100644 | |
1941 | +---- a/CONTRIBUTING.md | |
1942 | +-+++ b/CONTRIBUTING.md | |
1943 | +-@@ -65,7 +65,8 @@ branch directly, things can get messy. | |
1944 | +- Please include a nice description of your changes when you submit your PR; | |
1945 | +- if we have to read the whole diff to figure out why you're contributing | |
1946 | +- in the first place, you're less likely to get feedback and have your change | |
1947 | +--merged in. | |
1948 | +-+merged in. Also, split your changes into comprehensive chunks if your patch is | |
1949 | +-+longer than a dozen lines. | |
1950 | +- | |
1951 | +- If you are starting to work on a particular area, feel free to submit a PR | |
1952 | +- that highlights your work in progress (and note in the PR title that it's | |
1953 | +----- | |
1954 | +- | |
1955 | +-[NOTE] | |
1956 | + .Git Diff in an External Tool | |
1957 | + ==== | |
1958 | + We will continue to use the `git diff` command in various ways throughout the rest of the book. | |
1959 | +@@ -405,68 +145,27 @@ | |
1960 | + Run `git difftool --tool-help` to see what is available on your system. | |
1961 | + ==== | |
1962 | + | |
1963 | +-[[_committing_changes]] | |
1964 | + ==== Committing Your Changes | |
1965 | + | |
1966 | + Now that your staging area is set up the way you want it, you can commit your changes. | |
1967 | +-Remember that anything that is still unstaged -- any files you have created or modified that you haven't run `git add` on since you edited them -- won't go into this commit. | |
1968 | ++Remember that anything that is still unstaged – any files you have created or modified that you haven't run `git add` on since you edited them – won't go into this commit. | |
1969 | + They will stay as modified files on your disk. | |
1970 | + In this case, let's say that the last time you ran `git status`, you saw that everything was staged, so you're ready to commit your changes.(((git commands, status))) | |
1971 | + The simplest way to commit is to type `git commit`:(((git commands, commit))) | |
1972 | + | |
1973 | +-[source,console] | |
1974 | +----- | |
1975 | +-$ git commit | |
1976 | +----- | |
1977 | +- | |
1978 | + Doing so launches your editor of choice. | |
1979 | +- | |
1980 | +-[NOTE] | |
1981 | +-==== | |
1982 | +-This is set by your shell's `EDITOR` environment variable -- usually vim or emacs, although you can configure it with whatever you want using the `git config --global core.editor` command as you saw in <<ch01-getting-started#ch01-getting-started>>.(((editor, changing default)))(((git commands, config))) | |
1983 | +-==== | |
1984 | ++(This is set by your shell's `$EDITOR` environment variable – usually vim or emacs, although you can configure it with whatever you want using the `git config --global core.editor` command as you saw in <<ch01-introduction#ch01-introduction>>).(((editor, changing default)))(((git commands, config))) | |
1985 | + | |
1986 | + The editor displays the following text (this example is a Vim screen): | |
1987 | + | |
1988 | +-[source] | |
1989 | +----- | |
1990 | +- | |
1991 | +-# Please enter the commit message for your changes. Lines starting | |
1992 | +-# with '#' will be ignored, and an empty message aborts the commit. | |
1993 | +-# On branch master | |
1994 | +-# Your branch is up-to-date with 'origin/master'. | |
1995 | +-# | |
1996 | +-# Changes to be committed: | |
1997 | +-# new file: README | |
1998 | +-# modified: CONTRIBUTING.md | |
1999 | +-# | |
2000 | +-~ | |
2001 | +-~ | |
2002 | +-~ | |
2003 | +-".git/COMMIT_EDITMSG" 9L, 283C | |
2004 | +----- | |
2005 | +- | |
2006 | + You can see that the default commit message contains the latest output of the `git status` command commented out and one empty line on top. | |
2007 | + You can remove these comments and type your commit message, or you can leave them there to help you remember what you're committing. | |
2008 | +- | |
2009 | +-[NOTE] | |
2010 | +-==== | |
2011 | +-For an even more explicit reminder of what you've modified, you can pass the `-v` option to `git commit`. | |
2012 | +-Doing so also puts the diff of your change in the editor so you can see exactly what changes you're committing. | |
2013 | +-==== | |
2014 | +- | |
2015 | ++(For an even more explicit reminder of what you've modified, you can pass the `-v` option to `git commit`. | |
2016 | ++Doing so also puts the diff of your change in the editor so you can see exactly what changes you're committing.) | |
2017 | + When you exit the editor, Git creates your commit with that commit message (with the comments and diff stripped out). | |
2018 | + | |
2019 | + Alternatively, you can type your commit message inline with the `commit` command by specifying it after a `-m` flag, like this: | |
2020 | + | |
2021 | +-[source,console] | |
2022 | +----- | |
2023 | +-$ git commit -m "Story 182: fix benchmarks for speed" | |
2024 | +-[master 463dc4f] Story 182: fix benchmarks for speed | |
2025 | +- 2 files changed, 2 insertions(+) | |
2026 | +- create mode 100644 README | |
2027 | +----- | |
2028 | +- | |
2029 | + Now you've created your first commit! | |
2030 | + You can see that the commit has given you some output about itself: which branch you committed to (`master`), what SHA-1 checksum the commit has (`463dc4f`), how many files were changed, and statistics about lines added and removed in the commit. | |
2031 | + | |
2032 | +@@ -476,73 +175,25 @@ | |
2033 | + | |
2034 | + ==== Skipping the Staging Area | |
2035 | + | |
2036 | +-(((staging area, skipping))) | |
2037 | + Although it can be amazingly useful for crafting commits exactly how you want them, the staging area is sometimes a bit more complex than you need in your workflow. | |
2038 | + If you want to skip the staging area, Git provides a simple shortcut. | |
2039 | + Adding the `-a` option to the `git commit` command makes Git automatically stage every file that is already tracked before doing the commit, letting you skip the `git add` part: | |
2040 | + | |
2041 | +-[source,console] | |
2042 | +----- | |
2043 | +-$ git status | |
2044 | +-On branch master | |
2045 | +-Your branch is up-to-date with 'origin/master'. | |
2046 | +-Changes not staged for commit: | |
2047 | +- (use "git add <file>..." to update what will be committed) | |
2048 | +- (use "git checkout -- <file>..." to discard changes in working directory) | |
2049 | +- | |
2050 | +- modified: CONTRIBUTING.md | |
2051 | +- | |
2052 | +-no changes added to commit (use "git add" and/or "git commit -a") | |
2053 | +-$ git commit -a -m 'Add new benchmarks' | |
2054 | +-[master 83e38c7] Add new benchmarks | |
2055 | +- 1 file changed, 5 insertions(+), 0 deletions(-) | |
2056 | +----- | |
2057 | +- | |
2058 | + Notice how you don't have to run `git add` on the `CONTRIBUTING.md` file in this case before you commit. | |
2059 | + That's because the `-a` flag includes all changed files. | |
2060 | + This is convenient, but be careful; sometimes this flag will cause you to include unwanted changes. | |
2061 | + | |
2062 | +-[[_removing_files]] | |
2063 | + ==== Removing Files | |
2064 | + | |
2065 | +-(((files, removing))) | |
2066 | + To remove a file from Git, you have to remove it from your tracked files (more accurately, remove it from your staging area) and then commit. | |
2067 | + The `git rm` command does that, and also removes the file from your working directory so you don't see it as an untracked file the next time around. | |
2068 | + | |
2069 | +-If you simply remove the file from your working directory, it shows up under the ``Changes not staged for commit'' (that is, _unstaged_) area of your `git status` output: | |
2070 | +- | |
2071 | +-[source,console] | |
2072 | +----- | |
2073 | +-$ rm PROJECTS.md | |
2074 | +-$ git status | |
2075 | +-On branch master | |
2076 | +-Your branch is up-to-date with 'origin/master'. | |
2077 | +-Changes not staged for commit: | |
2078 | +- (use "git add/rm <file>..." to update what will be committed) | |
2079 | +- (use "git checkout -- <file>..." to discard changes in working directory) | |
2080 | +- | |
2081 | +- deleted: PROJECTS.md | |
2082 | +- | |
2083 | +-no changes added to commit (use "git add" and/or "git commit -a") | |
2084 | +----- | |
2085 | ++If you simply remove the file from your working directory, it shows up under the ``Changed but not updated'' (that is, _unstaged_) area of your `git status` output: | |
2086 | + | |
2087 | + Then, if you run `git rm`, it stages the file's removal: | |
2088 | + | |
2089 | +-[source,console] | |
2090 | +----- | |
2091 | +-$ git rm PROJECTS.md | |
2092 | +-rm 'PROJECTS.md' | |
2093 | +-$ git status | |
2094 | +-On branch master | |
2095 | +-Your branch is up-to-date with 'origin/master'. | |
2096 | +-Changes to be committed: | |
2097 | +- (use "git reset HEAD <file>..." to unstage) | |
2098 | +- | |
2099 | +- deleted: PROJECTS.md | |
2100 | +----- | |
2101 | +- | |
2102 | + The next time you commit, the file will be gone and no longer tracked. | |
2103 | +-If you modified the file or had already added it to the staging area, you must force the removal with the `-f` option. | |
2104 | ++If you modified the file and added it to the index already, you must force the removal with the `-f` option. | |
2105 | + This is a safety feature to prevent accidental removal of data that hasn't yet been recorded in a snapshot and that can't be recovered from Git. | |
2106 | + | |
2107 | + Another useful thing you may want to do is to keep the file in your working tree but remove it from your staging area. | |
2108 | +@@ -550,71 +201,30 @@ | |
2109 | + This is particularly useful if you forgot to add something to your `.gitignore` file and accidentally staged it, like a large log file or a bunch of `.a` compiled files. | |
2110 | + To do this, use the `--cached` option: | |
2111 | + | |
2112 | +-[source,console] | |
2113 | +----- | |
2114 | +-$ git rm --cached README | |
2115 | +----- | |
2116 | +- | |
2117 | + You can pass files, directories, and file-glob patterns to the `git rm` command. | |
2118 | + That means you can do things such as: | |
2119 | + | |
2120 | +-[source,console] | |
2121 | +----- | |
2122 | +-$ git rm log/\*.log | |
2123 | +----- | |
2124 | +- | |
2125 | + Note the backslash (`\`) in front of the `*`. | |
2126 | + This is necessary because Git does its own filename expansion in addition to your shell's filename expansion. | |
2127 | + This command removes all files that have the `.log` extension in the `log/` directory. | |
2128 | + Or, you can do something like this: | |
2129 | + | |
2130 | +-[source,console] | |
2131 | +----- | |
2132 | +-$ git rm \*~ | |
2133 | +----- | |
2134 | ++This command removes all files that end with `~`. | |
2135 | + | |
2136 | +-This command removes all files whose names end with a `~`. | |
2137 | +- | |
2138 | +-[[_git_mv]] | |
2139 | + ==== Moving Files | |
2140 | + | |
2141 | +-(((files, moving))) | |
2142 | + Unlike many other VCS systems, Git doesn't explicitly track file movement. | |
2143 | + If you rename a file in Git, no metadata is stored in Git that tells it you renamed the file. | |
2144 | +-However, Git is pretty smart about figuring that out after the fact -- we'll deal with detecting file movement a bit later. | |
2145 | ++However, Git is pretty smart about figuring that out after the fact – we'll deal with detecting file movement a bit later. | |
2146 | + | |
2147 | + Thus it's a bit confusing that Git has a `mv` command. | |
2148 | + If you want to rename a file in Git, you can run something like: | |
2149 | + | |
2150 | +-[source,console] | |
2151 | +----- | |
2152 | +-$ git mv file_from file_to | |
2153 | +----- | |
2154 | +- | |
2155 | + and it works fine. | |
2156 | + In fact, if you run something like this and look at the status, you'll see that Git considers it a renamed file: | |
2157 | + | |
2158 | +-[source,console] | |
2159 | +----- | |
2160 | +-$ git mv README.md README | |
2161 | +-$ git status | |
2162 | +-On branch master | |
2163 | +-Your branch is up-to-date with 'origin/master'. | |
2164 | +-Changes to be committed: | |
2165 | +- (use "git reset HEAD <file>..." to unstage) | |
2166 | +- | |
2167 | +- renamed: README.md -> README | |
2168 | +----- | |
2169 | +- | |
2170 | + However, this is equivalent to running something like this: | |
2171 | + | |
2172 | +-[source,console] | |
2173 | +----- | |
2174 | +-$ mv README.md README | |
2175 | +-$ git rm README.md | |
2176 | +-$ git add README | |
2177 | +----- | |
2178 | +- | |
2179 | + Git figures out that it's a rename implicitly, so it doesn't matter if you rename a file that way or with the `mv` command. | |
2180 | +-The only real difference is that `git mv` is one command instead of three -- it's a convenience function. | |
2181 | ++The only real difference is that `mv` is one command instead of three – it's a convenience function. | |
2182 | + More importantly, you can use any tool you like to rename a file, and address the add/rm later, before you commit. | |
2183 | +diff -au ../../git/progit2/progit2/book/02-git-basics/sections/remotes.asc /tmp/book/02-git-basics/sections/remotes.asc.tmp | |
2184 | +--- ../../git/progit2/progit2/book/02-git-basics/sections/remotes.asc 2020-07-17 09:06:42.261936072 +0900 | |
2185 | ++++ /tmp/book/02-git-basics/sections/remotes.asc.tmp 2020-10-25 09:24:25.633965632 +0900 | |
2186 | +@@ -1,4 +1,4 @@ | |
2187 | +-[[_remote_repos]] | |
2188 | ++[[r_remote_repos]] | |
2189 | + === Working with Remotes | |
2190 | + | |
2191 | + To be able to collaborate on any Git project, you need to know how to manage your remote repositories. | |
2192 | +@@ -8,233 +8,81 @@ | |
2193 | + Managing remote repositories includes knowing how to add remote repositories, remove remotes that are no longer valid, manage various remote branches and define them as being tracked or not, and more. | |
2194 | + In this section, we'll cover some of these remote-management skills. | |
2195 | + | |
2196 | +-[NOTE] | |
2197 | +-.Remote repositories can be on your local machine. | |
2198 | +-==== | |
2199 | +-It is entirely possible that you can be working with a ``remote'' repository that is, in fact, on the same host you are. | |
2200 | +-The word ``remote'' does not necessarily imply that the repository is somewhere else on the network or Internet, only that it is elsewhere. | |
2201 | +-Working with such a remote repository would still involve all the standard pushing, pulling and fetching operations as with any other remote. | |
2202 | +-==== | |
2203 | +- | |
2204 | + ==== Showing Your Remotes | |
2205 | + | |
2206 | + To see which remote servers you have configured, you can run the `git remote` command.(((git commands, remote))) | |
2207 | + It lists the shortnames of each remote handle you've specified. | |
2208 | +-If you've cloned your repository, you should at least see `origin` -- that is the default name Git gives to the server you cloned from: | |
2209 | +- | |
2210 | +-[source,console] | |
2211 | +----- | |
2212 | +-$ git clone https://github.com/schacon/ticgit | |
2213 | +-Cloning into 'ticgit'... | |
2214 | +-remote: Reusing existing pack: 1857, done. | |
2215 | +-remote: Total 1857 (delta 0), reused 0 (delta 0) | |
2216 | +-Receiving objects: 100% (1857/1857), 374.35 KiB | 268.00 KiB/s, done. | |
2217 | +-Resolving deltas: 100% (772/772), done. | |
2218 | +-Checking connectivity... done. | |
2219 | +-$ cd ticgit | |
2220 | +-$ git remote | |
2221 | +-origin | |
2222 | +----- | |
2223 | ++If you've cloned your repository, you should at least see origin – that is the default name Git gives to the server you cloned from: | |
2224 | + | |
2225 | + You can also specify `-v`, which shows you the URLs that Git has stored for the shortname to be used when reading and writing to that remote: | |
2226 | + | |
2227 | +-[source,console] | |
2228 | +----- | |
2229 | +-$ git remote -v | |
2230 | +-origin https://github.com/schacon/ticgit (fetch) | |
2231 | +-origin https://github.com/schacon/ticgit (push) | |
2232 | +----- | |
2233 | +- | |
2234 | + If you have more than one remote, the command lists them all. | |
2235 | + For example, a repository with multiple remotes for working with several collaborators might look something like this. | |
2236 | + | |
2237 | +-[source,console] | |
2238 | +----- | |
2239 | +-$ cd grit | |
2240 | +-$ git remote -v | |
2241 | +-bakkdoor https://github.com/bakkdoor/grit (fetch) | |
2242 | +-bakkdoor https://github.com/bakkdoor/grit (push) | |
2243 | +-cho45 https://github.com/cho45/grit (fetch) | |
2244 | +-cho45 https://github.com/cho45/grit (push) | |
2245 | +-defunkt https://github.com/defunkt/grit (fetch) | |
2246 | +-defunkt https://github.com/defunkt/grit (push) | |
2247 | +-koke git://github.com/koke/grit.git (fetch) | |
2248 | +-koke git://github.com/koke/grit.git (push) | |
2249 | +-origin git@github.com:mojombo/grit.git (fetch) | |
2250 | +-origin git@github.com:mojombo/grit.git (push) | |
2251 | +----- | |
2252 | +- | |
2253 | + This means we can pull contributions from any of these users pretty easily. | |
2254 | + We may additionally have permission to push to one or more of these, though we can't tell that here. | |
2255 | + | |
2256 | +-Notice that these remotes use a variety of protocols; we'll cover more about this in <<ch04-git-on-the-server#_getting_git_on_a_server>>. | |
2257 | ++Notice that these remotes use a variety of protocols; we'll cover more about this in <<ch04-git-server#r_git_on_the_server>>. | |
2258 | + | |
2259 | + ==== Adding Remote Repositories | |
2260 | + | |
2261 | +-We've mentioned and given some demonstrations of how the `git clone` command implicitly adds the `origin` remote for you. | |
2262 | +-Here's how to add a new remote explicitly.(((git commands, remote))) | |
2263 | ++We've mentioned and given some demonstrations of adding remote repositories in previous sections, but here is how to do it explicitly.(((git commands, remote))) | |
2264 | + To add a new remote Git repository as a shortname you can reference easily, run `git remote add <shortname> <url>`: | |
2265 | + | |
2266 | +-[source,console] | |
2267 | +----- | |
2268 | +-$ git remote | |
2269 | +-origin | |
2270 | +-$ git remote add pb https://github.com/paulboone/ticgit | |
2271 | +-$ git remote -v | |
2272 | +-origin https://github.com/schacon/ticgit (fetch) | |
2273 | +-origin https://github.com/schacon/ticgit (push) | |
2274 | +-pb https://github.com/paulboone/ticgit (fetch) | |
2275 | +-pb https://github.com/paulboone/ticgit (push) | |
2276 | +----- | |
2277 | +- | |
2278 | + Now you can use the string `pb` on the command line in lieu of the whole URL. | |
2279 | + For example, if you want to fetch all the information that Paul has but that you don't yet have in your repository, you can run `git fetch pb`: | |
2280 | + | |
2281 | +-[source,console] | |
2282 | +----- | |
2283 | +-$ git fetch pb | |
2284 | +-remote: Counting objects: 43, done. | |
2285 | +-remote: Compressing objects: 100% (36/36), done. | |
2286 | +-remote: Total 43 (delta 10), reused 31 (delta 5) | |
2287 | +-Unpacking objects: 100% (43/43), done. | |
2288 | +-From https://github.com/paulboone/ticgit | |
2289 | +- * [new branch] master -> pb/master | |
2290 | +- * [new branch] ticgit -> pb/ticgit | |
2291 | +----- | |
2292 | +- | |
2293 | +-Paul's `master` branch is now accessible locally as `pb/master` -- you can merge it into one of your branches, or you can check out a local branch at that point if you want to inspect it. | |
2294 | +-We'll go over what branches are and how to use them in much more detail in <<ch03-git-branching#ch03-git-branching>>. | |
2295 | ++Paul's master branch is now accessible locally as `pb/master` – you can merge it into one of your branches, or you can check out a local branch at that point if you want to inspect it. | |
2296 | ++(We'll go over what branches are and how to use them in much more detail in <<ch03-git-branching#ch03-git-branching>>.) | |
2297 | + | |
2298 | +-[[_fetching_and_pulling]] | |
2299 | + ==== Fetching and Pulling from Your Remotes | |
2300 | + | |
2301 | + As you just saw, to get data from your remote projects, you can run:(((git commands, fetch))) | |
2302 | + | |
2303 | +-[source,console] | |
2304 | +----- | |
2305 | +-$ git fetch <remote> | |
2306 | +----- | |
2307 | +- | |
2308 | + The command goes out to that remote project and pulls down all the data from that remote project that you don't have yet. | |
2309 | + After you do this, you should have references to all the branches from that remote, which you can merge in or inspect at any time. | |
2310 | + | |
2311 | + If you clone a repository, the command automatically adds that remote repository under the name ``origin''. | |
2312 | + So, `git fetch origin` fetches any new work that has been pushed to that server since you cloned (or last fetched from) it. | |
2313 | +-It's important to note that the `git fetch` command only downloads the data to your local repository -- it doesn't automatically merge it with any of your work or modify what you're currently working on. | |
2314 | ++It's important to note that the `git fetch` command pulls the data to your local repository – it doesn't automatically merge it with any of your work or modify what you're currently working on. | |
2315 | + You have to merge it manually into your work when you're ready. | |
2316 | + | |
2317 | +-If your current branch is set up to track a remote branch (see the next section and <<ch03-git-branching#ch03-git-branching>> for more information), you can use the `git pull` command to automatically fetch and then merge that remote branch into your current branch.(((git commands, pull))) | |
2318 | +-This may be an easier or more comfortable workflow for you; and by default, the `git clone` command automatically sets up your local `master` branch to track the remote `master` branch (or whatever the default branch is called) on the server you cloned from. | |
2319 | ++If you have a branch set up to track a remote branch (see the next section and <<ch03-git-branching#ch03-git-branching>> for more information), you can use the `git pull` command to automatically fetch and then merge a remote branch into your current branch.(((git commands, pull))) | |
2320 | ++This may be an easier or more comfortable workflow for you; and by default, the `git clone` command automatically sets up your local master branch to track the remote master branch (or whatever the default branch is called) on the server you cloned from. | |
2321 | + Running `git pull` generally fetches data from the server you originally cloned from and automatically tries to merge it into the code you're currently working on. | |
2322 | + | |
2323 | +-[NOTE] | |
2324 | +-==== | |
2325 | +-From git version 2.27 onward, `git pull` will give a warning if the `pull.rebase` variable is not set. | |
2326 | +-Git will keep warning you until you set the variable. | |
2327 | +- | |
2328 | +-If you want the default behavior of git (fast-forward if possible, else create a merge commit): | |
2329 | +-`git config --global pull.rebase "false"` | |
2330 | +- | |
2331 | +-If you want to rebase when pulling: | |
2332 | +-`git config --global pull.rebase "true"` | |
2333 | +-==== | |
2334 | +- | |
2335 | +-[[_pushing_remotes]] | |
2336 | + ==== Pushing to Your Remotes | |
2337 | + | |
2338 | + When you have your project at a point that you want to share, you have to push it upstream. | |
2339 | +-The command for this is simple: `git push <remote> <branch>`.(((git commands, push))) | |
2340 | +-If you want to push your `master` branch to your `origin` server (again, cloning generally sets up both of those names for you automatically), then you can run this to push any commits you've done back up to the server: | |
2341 | +- | |
2342 | +-[source,console] | |
2343 | +----- | |
2344 | +-$ git push origin master | |
2345 | +----- | |
2346 | ++The command for this is simple: `git push [remote-name] [branch-name]`.(((git commands, push))) | |
2347 | ++If you want to push your master branch to your `origin` server (again, cloning generally sets up both of those names for you automatically), then you can run this to push any commits you've done back up to the server: | |
2348 | + | |
2349 | + This command works only if you cloned from a server to which you have write access and if nobody has pushed in the meantime. | |
2350 | + If you and someone else clone at the same time and they push upstream and then you push upstream, your push will rightly be rejected. | |
2351 | +-You'll have to fetch their work first and incorporate it into yours before you'll be allowed to push. | |
2352 | ++You'll have to pull down their work first and incorporate it into yours before you'll be allowed to push. | |
2353 | + See <<ch03-git-branching#ch03-git-branching>> for more detailed information on how to push to remote servers. | |
2354 | + | |
2355 | +-[[_inspecting_remote]] | |
2356 | + ==== Inspecting a Remote | |
2357 | + | |
2358 | +-If you want to see more information about a particular remote, you can use the `git remote show <remote>` command.(((git commands, remote))) | |
2359 | ++If you want to see more information about a particular remote, you can use the `git remote show [remote-name]` command.(((git commands, remote))) | |
2360 | + If you run this command with a particular shortname, such as `origin`, you get something like this: | |
2361 | + | |
2362 | +-[source,console] | |
2363 | +----- | |
2364 | +-$ git remote show origin | |
2365 | +-* remote origin | |
2366 | +- Fetch URL: https://github.com/schacon/ticgit | |
2367 | +- Push URL: https://github.com/schacon/ticgit | |
2368 | +- HEAD branch: master | |
2369 | +- Remote branches: | |
2370 | +- master tracked | |
2371 | +- dev-branch tracked | |
2372 | +- Local branch configured for 'git pull': | |
2373 | +- master merges with remote master | |
2374 | +- Local ref configured for 'git push': | |
2375 | +- master pushes to master (up to date) | |
2376 | +----- | |
2377 | +- | |
2378 | + It lists the URL for the remote repository as well as the tracking branch information. | |
2379 | +-The command helpfully tells you that if you're on the `master` branch and you run `git pull`, it will automatically merge in the `master` branch on the remote after it fetches all the remote references. | |
2380 | ++The command helpfully tells you that if you're on the master branch and you run `git pull`, it will automatically merge in the master branch on the remote after it fetches all the remote references. | |
2381 | + It also lists all the remote references it has pulled down. | |
2382 | + | |
2383 | + That is a simple example you're likely to encounter. | |
2384 | + When you're using Git more heavily, however, you may see much more information from `git remote show`: | |
2385 | + | |
2386 | +-[source,console] | |
2387 | +----- | |
2388 | +-$ git remote show origin | |
2389 | +-* remote origin | |
2390 | +- URL: https://github.com/my-org/complex-project | |
2391 | +- Fetch URL: https://github.com/my-org/complex-project | |
2392 | +- Push URL: https://github.com/my-org/complex-project | |
2393 | +- HEAD branch: master | |
2394 | +- Remote branches: | |
2395 | +- master tracked | |
2396 | +- dev-branch tracked | |
2397 | +- markdown-strip tracked | |
2398 | +- issue-43 new (next fetch will store in remotes/origin) | |
2399 | +- issue-45 new (next fetch will store in remotes/origin) | |
2400 | +- refs/remotes/origin/issue-11 stale (use 'git remote prune' to remove) | |
2401 | +- Local branches configured for 'git pull': | |
2402 | +- dev-branch merges with remote dev-branch | |
2403 | +- master merges with remote master | |
2404 | +- Local refs configured for 'git push': | |
2405 | +- dev-branch pushes to dev-branch (up to date) | |
2406 | +- markdown-strip pushes to markdown-strip (up to date) | |
2407 | +- master pushes to master (up to date) | |
2408 | +----- | |
2409 | +- | |
2410 | + This command shows which branch is automatically pushed to when you run `git push` while on certain branches. | |
2411 | +-It also shows you which remote branches on the server you don't yet have, which remote branches you have that have been removed from the server, and multiple local branches that are able to merge automatically with their remote-tracking branch when you run `git pull`. | |
2412 | ++It also shows you which remote branches on the server you don't yet have, which remote branches you have that have been removed from the server, and multiple branches that are automatically merged when you run `git pull`. | |
2413 | + | |
2414 | +-==== Renaming and Removing Remotes | |
2415 | ++==== Removing and Renaming Remotes | |
2416 | + | |
2417 | +-You can run `git remote rename` to change a remote's shortname.(((git commands, remote))) | |
2418 | ++If you want to rename a reference you can run `git remote rename` to change a remote's shortname.(((git commands, remote))) | |
2419 | + For instance, if you want to rename `pb` to `paul`, you can do so with `git remote rename`: | |
2420 | + | |
2421 | +-[source,console] | |
2422 | +----- | |
2423 | +-$ git remote rename pb paul | |
2424 | +-$ git remote | |
2425 | +-origin | |
2426 | +-paul | |
2427 | +----- | |
2428 | +- | |
2429 | +-It's worth mentioning that this changes all your remote-tracking branch names, too. | |
2430 | ++It's worth mentioning that this changes your remote branch names, too. | |
2431 | + What used to be referenced at `pb/master` is now at `paul/master`. | |
2432 | + | |
2433 | +-If you want to remove a remote for some reason -- you've moved the server or are no longer using a particular mirror, or perhaps a contributor isn't contributing anymore -- you can either use `git remote remove` or `git remote rm`: | |
2434 | +- | |
2435 | +-[source,console] | |
2436 | +----- | |
2437 | +-$ git remote remove paul | |
2438 | +-$ git remote | |
2439 | +-origin | |
2440 | +----- | |
2441 | +- | |
2442 | +-Once you delete the reference to a remote this way, all remote-tracking branches and configuration settings associated with that remote are also deleted. | |
2443 | ++If you want to remove a remote for some reason – you've moved the server or are no longer using a particular mirror, or perhaps a contributor isn't contributing anymore – you can use `git remote rm`: | |
2444 | +diff -au ../../git/progit2/progit2/book/02-git-basics/sections/tagging.asc /tmp/book/02-git-basics/sections/tagging.asc.tmp | |
2445 | +--- ../../git/progit2/progit2/book/02-git-basics/sections/tagging.asc 2020-08-29 09:58:24.048195629 +0900 | |
2446 | ++++ /tmp/book/02-git-basics/sections/tagging.asc.tmp 2020-10-25 09:24:25.639965914 +0900 | |
2447 | +@@ -1,299 +1,77 @@ | |
2448 | +-[[_git_tagging]] | |
2449 | ++[[r_git_tagging]] | |
2450 | + === Tagging | |
2451 | + | |
2452 | +-(((tags))) | |
2453 | +-Like most VCSs, Git has the ability to tag specific points in a repository's history as being important. | |
2454 | +-Typically, people use this functionality to mark release points (`v1.0`, `v2.0` and so on). | |
2455 | +-In this section, you'll learn how to list existing tags, how to create and delete tags, and what the different types of tags are. | |
2456 | ++Like most VCSs, Git has the ability to tag specific points in history as being important. | |
2457 | ++Typically people use this functionality to mark release points (v1.0, and so on). | |
2458 | ++In this section, you'll learn how to list the available tags, how to create new tags, and what the different types of tags are. | |
2459 | + | |
2460 | + ==== Listing Your Tags | |
2461 | + | |
2462 | +-Listing the existing tags in Git is straightforward. | |
2463 | +-Just type `git tag` (with optional `-l` or `--list`):(((git commands, tag))) | |
2464 | ++Listing the available tags in Git is straightforward. | |
2465 | ++Just type `git tag`:(((git commands, tag))) | |
2466 | + | |
2467 | +-[source,console] | |
2468 | +----- | |
2469 | +-$ git tag | |
2470 | +-v1.0 | |
2471 | +-v2.0 | |
2472 | +----- | |
2473 | ++This command lists the tags in alphabetical order; the order in which they appear has no real importance. | |
2474 | + | |
2475 | +-This command lists the tags in alphabetical order; the order in which they are displayed has no real importance. | |
2476 | +- | |
2477 | +-You can also search for tags that match a particular pattern. | |
2478 | ++You can also search for tags with a particular pattern. | |
2479 | + The Git source repo, for instance, contains more than 500 tags. | |
2480 | +-If you're interested only in looking at the 1.8.5 series, you can run this: | |
2481 | +- | |
2482 | +-[source,console] | |
2483 | +----- | |
2484 | +-$ git tag -l "v1.8.5*" | |
2485 | +-v1.8.5 | |
2486 | +-v1.8.5-rc0 | |
2487 | +-v1.8.5-rc1 | |
2488 | +-v1.8.5-rc2 | |
2489 | +-v1.8.5-rc3 | |
2490 | +-v1.8.5.1 | |
2491 | +-v1.8.5.2 | |
2492 | +-v1.8.5.3 | |
2493 | +-v1.8.5.4 | |
2494 | +-v1.8.5.5 | |
2495 | +----- | |
2496 | +- | |
2497 | +-[NOTE] | |
2498 | +-.Listing tag wildcards requires `-l` or `--list` option | |
2499 | +-==== | |
2500 | +-If you want just the entire list of tags, running the command `git tag` implicitly assumes you want a listing and provides one; the use of `-l` or `--list` in this case is optional. | |
2501 | +- | |
2502 | +-If, however, you're supplying a wildcard pattern to match tag names, the use of `-l` or `--list` is mandatory. | |
2503 | +-==== | |
2504 | ++If you're only interested in looking at the 1.8.5 series, you can run this: | |
2505 | + | |
2506 | + ==== Creating Tags | |
2507 | + | |
2508 | +-Git supports two types of tags: _lightweight_ and _annotated_. | |
2509 | ++Git uses two main types of tags: lightweight and annotated. | |
2510 | + | |
2511 | +-A lightweight tag is very much like a branch that doesn't change -- it's just a pointer to a specific commit. | |
2512 | ++A lightweight tag is very much like a branch that doesn't change – it's just a pointer to a specific commit. | |
2513 | + | |
2514 | + Annotated tags, however, are stored as full objects in the Git database. | |
2515 | + They're checksummed; contain the tagger name, email, and date; have a tagging message; and can be signed and verified with GNU Privacy Guard (GPG). | |
2516 | + It's generally recommended that you create annotated tags so you can have all this information; but if you want a temporary tag or for some reason don't want to keep the other information, lightweight tags are available too. | |
2517 | + | |
2518 | +-[[_annotated_tags]] | |
2519 | + ==== Annotated Tags | |
2520 | + | |
2521 | +-(((tags, annotated))) | |
2522 | + Creating an annotated tag in Git is simple. | |
2523 | + The easiest way is to specify `-a` when you run the `tag` command:(((git commands, tag))) | |
2524 | + | |
2525 | +-[source,console] | |
2526 | +----- | |
2527 | +-$ git tag -a v1.4 -m "my version 1.4" | |
2528 | +-$ git tag | |
2529 | +-v0.1 | |
2530 | +-v1.3 | |
2531 | +-v1.4 | |
2532 | +----- | |
2533 | +- | |
2534 | + The `-m` specifies a tagging message, which is stored with the tag. | |
2535 | + If you don't specify a message for an annotated tag, Git launches your editor so you can type it in. | |
2536 | + | |
2537 | + You can see the tag data along with the commit that was tagged by using the `git show` command: | |
2538 | + | |
2539 | +-[source,console] | |
2540 | +----- | |
2541 | +-$ git show v1.4 | |
2542 | +-tag v1.4 | |
2543 | +-Tagger: Ben Straub <ben@straub.cc> | |
2544 | +-Date: Sat May 3 20:19:12 2014 -0700 | |
2545 | +- | |
2546 | +-my version 1.4 | |
2547 | +- | |
2548 | +-commit ca82a6dff817ec66f44342007202690a93763949 | |
2549 | +-Author: Scott Chacon <schacon@gee-mail.com> | |
2550 | +-Date: Mon Mar 17 21:52:11 2008 -0700 | |
2551 | +- | |
2552 | +- Change version number | |
2553 | +----- | |
2554 | +- | |
2555 | + That shows the tagger information, the date the commit was tagged, and the annotation message before showing the commit information. | |
2556 | + | |
2557 | + ==== Lightweight Tags | |
2558 | + | |
2559 | +-(((tags, lightweight))) | |
2560 | + Another way to tag commits is with a lightweight tag. | |
2561 | +-This is basically the commit checksum stored in a file -- no other information is kept. | |
2562 | +-To create a lightweight tag, don't supply any of the `-a`, `-s`, or `-m` options, just provide a tag name: | |
2563 | +- | |
2564 | +-[source,console] | |
2565 | +----- | |
2566 | +-$ git tag v1.4-lw | |
2567 | +-$ git tag | |
2568 | +-v0.1 | |
2569 | +-v1.3 | |
2570 | +-v1.4 | |
2571 | +-v1.4-lw | |
2572 | +-v1.5 | |
2573 | +----- | |
2574 | ++This is basically the commit checksum stored in a file – no other information is kept. | |
2575 | ++To create a lightweight tag, don't supply the `-a`, `-s`, or `-m` option: | |
2576 | + | |
2577 | + This time, if you run `git show` on the tag, you don't see the extra tag information.(((git commands, show))) | |
2578 | + The command just shows the commit: | |
2579 | + | |
2580 | +-[source,console] | |
2581 | +----- | |
2582 | +-$ git show v1.4-lw | |
2583 | +-commit ca82a6dff817ec66f44342007202690a93763949 | |
2584 | +-Author: Scott Chacon <schacon@gee-mail.com> | |
2585 | +-Date: Mon Mar 17 21:52:11 2008 -0700 | |
2586 | +- | |
2587 | +- Change version number | |
2588 | +----- | |
2589 | +- | |
2590 | + ==== Tagging Later | |
2591 | + | |
2592 | + You can also tag commits after you've moved past them. | |
2593 | + Suppose your commit history looks like this: | |
2594 | + | |
2595 | +-[source,console] | |
2596 | +----- | |
2597 | +-$ git log --pretty=oneline | |
2598 | +-15027957951b64cf874c3557a0f3547bd83b3ff6 Merge branch 'experiment' | |
2599 | +-a6b4c97498bd301d84096da251c98a07c7723e65 Create write support | |
2600 | +-0d52aaab4479697da7686c15f77a3d64d9165190 One more thing | |
2601 | +-6d52a271eda8725415634dd79daabbc4d9b6008e Merge branch 'experiment' | |
2602 | +-0b7434d86859cc7b8c3d5e1dddfed66ff742fcbc Add commit function | |
2603 | +-4682c3261057305bdd616e23b64b0857d832627b Add todo file | |
2604 | +-166ae0c4d3f420721acbb115cc33848dfcc2121a Create write support | |
2605 | +-9fceb02d0ae598e95dc970b74767f19372d61af8 Update rakefile | |
2606 | +-964f16d36dfccde844893cac5b347e7b3d44abbc Commit the todo | |
2607 | +-8a5cbc430f1a9c3d00faaeffd07798508422908a Update readme | |
2608 | +----- | |
2609 | +- | |
2610 | +-Now, suppose you forgot to tag the project at v1.2, which was at the ``Update rakefile'' commit. | |
2611 | ++Now, suppose you forgot to tag the project at v1.2, which was at the ``updated rakefile'' commit. | |
2612 | + You can add it after the fact. | |
2613 | + To tag that commit, you specify the commit checksum (or part of it) at the end of the command: | |
2614 | + | |
2615 | +-[source,console] | |
2616 | +----- | |
2617 | +-$ git tag -a v1.2 9fceb02 | |
2618 | +----- | |
2619 | +- | |
2620 | + You can see that you've tagged the commit:(((git commands, tag))) | |
2621 | + | |
2622 | +-[source,console] | |
2623 | +----- | |
2624 | +-$ git tag | |
2625 | +-v0.1 | |
2626 | +-v1.2 | |
2627 | +-v1.3 | |
2628 | +-v1.4 | |
2629 | +-v1.4-lw | |
2630 | +-v1.5 | |
2631 | +- | |
2632 | +-$ git show v1.2 | |
2633 | +-tag v1.2 | |
2634 | +-Tagger: Scott Chacon <schacon@gee-mail.com> | |
2635 | +-Date: Mon Feb 9 15:32:16 2009 -0800 | |
2636 | +- | |
2637 | +-version 1.2 | |
2638 | +-commit 9fceb02d0ae598e95dc970b74767f19372d61af8 | |
2639 | +-Author: Magnus Chacon <mchacon@gee-mail.com> | |
2640 | +-Date: Sun Apr 27 20:43:35 2008 -0700 | |
2641 | +- | |
2642 | +- Update rakefile | |
2643 | +-... | |
2644 | +----- | |
2645 | +- | |
2646 | +-[[_sharing_tags]] | |
2647 | + ==== Sharing Tags | |
2648 | + | |
2649 | + By default, the `git push` command doesn't transfer tags to remote servers.(((git commands, push))) | |
2650 | + You will have to explicitly push tags to a shared server after you have created them. | |
2651 | +-This process is just like sharing remote branches -- you can run `git push origin <tagname>`. | |
2652 | +- | |
2653 | +-[source,console] | |
2654 | +----- | |
2655 | +-$ git push origin v1.5 | |
2656 | +-Counting objects: 14, done. | |
2657 | +-Delta compression using up to 8 threads. | |
2658 | +-Compressing objects: 100% (12/12), done. | |
2659 | +-Writing objects: 100% (14/14), 2.05 KiB | 0 bytes/s, done. | |
2660 | +-Total 14 (delta 3), reused 0 (delta 0) | |
2661 | +-To git@github.com:schacon/simplegit.git | |
2662 | +- * [new tag] v1.5 -> v1.5 | |
2663 | +----- | |
2664 | ++This process is just like sharing remote branches – you can run `git push origin [tagname]`. | |
2665 | + | |
2666 | + If you have a lot of tags that you want to push up at once, you can also use the `--tags` option to the `git push` command. | |
2667 | + This will transfer all of your tags to the remote server that are not already there. | |
2668 | + | |
2669 | +-[source,console] | |
2670 | +----- | |
2671 | +-$ git push origin --tags | |
2672 | +-Counting objects: 1, done. | |
2673 | +-Writing objects: 100% (1/1), 160 bytes | 0 bytes/s, done. | |
2674 | +-Total 1 (delta 0), reused 0 (delta 0) | |
2675 | +-To git@github.com:schacon/simplegit.git | |
2676 | +- * [new tag] v1.4 -> v1.4 | |
2677 | +- * [new tag] v1.4-lw -> v1.4-lw | |
2678 | +----- | |
2679 | +- | |
2680 | + Now, when someone else clones or pulls from your repository, they will get all your tags as well. | |
2681 | + | |
2682 | +-[NOTE] | |
2683 | +-.`git push` pushes both types of tags | |
2684 | +-==== | |
2685 | +-`git push <remote> --tags` will push both lightweight and annotated tags. | |
2686 | +-There is currently no option to push only lightweight tags, but if you use `git push <remote> --follow-tags` only annotated tags will be pushed to the remote. | |
2687 | +-==== | |
2688 | +- | |
2689 | +-==== Deleting Tags | |
2690 | +- | |
2691 | +-To delete a tag on your local repository, you can use `git tag -d <tagname>`. | |
2692 | +-For example, we could remove our lightweight tag above as follows: | |
2693 | +- | |
2694 | +-[source,console] | |
2695 | +----- | |
2696 | +-$ git tag -d v1.4-lw | |
2697 | +-Deleted tag 'v1.4-lw' (was e7d5add) | |
2698 | +----- | |
2699 | +- | |
2700 | +-Note that this does not remove the tag from any remote servers. | |
2701 | +-There are two common variations for deleting a tag from a remote server. | |
2702 | +- | |
2703 | +-The first variation is `git push <remote> :refs/tags/<tagname>`: | |
2704 | +- | |
2705 | +-[source,console] | |
2706 | +----- | |
2707 | +-$ git push origin :refs/tags/v1.4-lw | |
2708 | +-To /git@github.com:schacon/simplegit.git | |
2709 | +- - [deleted] v1.4-lw | |
2710 | +----- | |
2711 | +- | |
2712 | +-The way to interpret the above is to read it as the null value before the colon is being pushed to the remote tag name, effectively deleting it. | |
2713 | +- | |
2714 | +-The second (and more intuitive) way to delete a remote tag is with: | |
2715 | +- | |
2716 | +-[source,console] | |
2717 | +----- | |
2718 | +-$ git push origin --delete <tagname> | |
2719 | +----- | |
2720 | +- | |
2721 | + ==== Checking out Tags | |
2722 | + | |
2723 | +-If you want to view the versions of files a tag is pointing to, you can do a `git checkout` of that tag, although this puts your repository in ``detached HEAD'' state, which has some ill side effects: | |
2724 | +- | |
2725 | +-[source,console] | |
2726 | +----- | |
2727 | +-$ git checkout v2.0.0 | |
2728 | +-Note: switching to 'v2.0.0'. | |
2729 | +- | |
2730 | +-You are in 'detached HEAD' state. You can look around, make experimental | |
2731 | +-changes and commit them, and you can discard any commits you make in this | |
2732 | +-state without impacting any branches by performing another checkout. | |
2733 | +- | |
2734 | +-If you want to create a new branch to retain commits you create, you may | |
2735 | +-do so (now or later) by using -c with the switch command. Example: | |
2736 | +- | |
2737 | +- git switch -c <new-branch-name> | |
2738 | +- | |
2739 | +-Or undo this operation with: | |
2740 | +- | |
2741 | +- git switch - | |
2742 | +- | |
2743 | +-Turn off this advice by setting config variable advice.detachedHead to false | |
2744 | +- | |
2745 | +-HEAD is now at 99ada87... Merge pull request #89 from schacon/appendix-final | |
2746 | +- | |
2747 | +-$ git checkout v2.0-beta-0.1 | |
2748 | +-Previous HEAD position was 99ada87... Merge pull request #89 from schacon/appendix-final | |
2749 | +-HEAD is now at df3f601... Add atlas.json and cover image | |
2750 | +----- | |
2751 | +- | |
2752 | +-In ``detached HEAD'' state, if you make changes and then create a commit, the tag will stay the same, but your new commit won't belong to any branch and will be unreachable, except by the exact commit hash. | |
2753 | +-Thus, if you need to make changes -- say you're fixing a bug on an older version, for instance -- you will generally want to create a branch: | |
2754 | +- | |
2755 | +-[source,console] | |
2756 | +----- | |
2757 | +-$ git checkout -b version2 v2.0.0 | |
2758 | +-Switched to a new branch 'version2' | |
2759 | +----- | |
2760 | ++You can't really check out a tag in Git, since they can't be moved around. | |
2761 | ++If you want to put a version of your repository in your working directory that looks like a specific tag, you can create a new branch at a specific tag with `git checkout -b [branchname] [tagname]`: | |
2762 | + | |
2763 | +-If you do this and make a commit, your `version2` branch will be slightly different than your `v2.0.0` tag since it will move forward with your new changes, so do be careful. | |
2764 | ++Of course if you do this and do a commit, your `version2` branch will be slightly different than your `v2.0.0` tag since it will move forward with your new changes, so do be careful. | |
2765 | +diff -au ../../git/progit2/progit2/book/02-git-basics/sections/undoing.asc /tmp/book/02-git-basics/sections/undoing.asc.tmp | |
2766 | +--- ../../git/progit2/progit2/book/02-git-basics/sections/undoing.asc 2020-10-18 09:42:35.779991668 +0900 | |
2767 | ++++ /tmp/book/02-git-basics/sections/undoing.asc.tmp 2020-10-25 09:24:25.644966150 +0900 | |
2768 | +@@ -1,4 +1,4 @@ | |
2769 | +-[[_undoing]] | |
2770 | ++[[r_undoing]] | |
2771 | + === Undoing Things | |
2772 | + | |
2773 | + At any stage, you may want to undo something. | |
2774 | +@@ -7,12 +7,7 @@ | |
2775 | + This is one of the few areas in Git where you may lose some work if you do it wrong. | |
2776 | + | |
2777 | + One of the common undos takes place when you commit too early and possibly forget to add some files, or you mess up your commit message. | |
2778 | +-If you want to redo that commit, make the additional changes you forgot, stage them, and commit again using the `--amend` option: | |
2779 | +- | |
2780 | +-[source,console] | |
2781 | +----- | |
2782 | +-$ git commit --amend | |
2783 | +----- | |
2784 | ++If you want to try that commit again, you can run commit with the `--amend` option: | |
2785 | + | |
2786 | + This command takes your staging area and uses it for the commit. | |
2787 | + If you've made no changes since your last commit (for instance, you run this command immediately after your previous commit), then your snapshot will look exactly the same, and all you'll change is your commit message. | |
2788 | +@@ -22,214 +17,50 @@ | |
2789 | + | |
2790 | + As an example, if you commit and then realize you forgot to stage the changes in a file you wanted to add to this commit, you can do something like this: | |
2791 | + | |
2792 | +-[source,console] | |
2793 | +----- | |
2794 | +-$ git commit -m 'Initial commit' | |
2795 | +-$ git add forgotten_file | |
2796 | +-$ git commit --amend | |
2797 | +----- | |
2798 | +- | |
2799 | +-You end up with a single commit -- the second commit replaces the results of the first. | |
2800 | +- | |
2801 | +-[NOTE] | |
2802 | +-==== | |
2803 | +-It's important to understand that when you're amending your last commit, you're not so much fixing it as _replacing_ it entirely with a new, improved commit that pushes the old commit out of the way and puts the new commit in its place. | |
2804 | +-Effectively, it's as if the previous commit never happened, and it won't show up in your repository history. | |
2805 | +- | |
2806 | +-The obvious value to amending commits is to make minor improvements to your last commit, without cluttering your repository history with commit messages of the form, ``Oops, forgot to add a file'' or ``Darn, fixing a typo in last commit''. | |
2807 | +-==== | |
2808 | +- | |
2809 | +-[NOTE] | |
2810 | +-==== | |
2811 | +-Only amend commits that are still local and have not been pushed somewhere. | |
2812 | +-Amending previously pushed commits and force pushing the branch will cause problems for your collaborators. | |
2813 | +-For more on what happens when you do this and how to recover if you're on the receiving end read <<_rebase_peril>>. | |
2814 | +-==== | |
2815 | ++You end up with a single commit – the second commit replaces the results of the first. | |
2816 | + | |
2817 | +-[[_unstaging]] | |
2818 | + ==== Unstaging a Staged File | |
2819 | + | |
2820 | +-The next two sections demonstrate how to work with your staging area and working directory changes. | |
2821 | ++The next two sections demonstrate how to wrangle your staging area and working directory changes. | |
2822 | + The nice part is that the command you use to determine the state of those two areas also reminds you how to undo changes to them. | |
2823 | + For example, let's say you've changed two files and want to commit them as two separate changes, but you accidentally type `git add *` and stage them both. | |
2824 | + How can you unstage one of the two? | |
2825 | + The `git status` command reminds you: | |
2826 | + | |
2827 | +-[source,console] | |
2828 | +----- | |
2829 | +-$ git add * | |
2830 | +-$ git status | |
2831 | +-On branch master | |
2832 | +-Changes to be committed: | |
2833 | +- (use "git reset HEAD <file>..." to unstage) | |
2834 | +- | |
2835 | +- renamed: README.md -> README | |
2836 | +- modified: CONTRIBUTING.md | |
2837 | +----- | |
2838 | +- | |
2839 | + Right below the ``Changes to be committed'' text, it says use `git reset HEAD <file>...` to unstage. | |
2840 | + So, let's use that advice to unstage the `CONTRIBUTING.md` file: | |
2841 | + | |
2842 | +-[source,console] | |
2843 | +----- | |
2844 | +-$ git reset HEAD CONTRIBUTING.md | |
2845 | +-Unstaged changes after reset: | |
2846 | +-M CONTRIBUTING.md | |
2847 | +-$ git status | |
2848 | +-On branch master | |
2849 | +-Changes to be committed: | |
2850 | +- (use "git reset HEAD <file>..." to unstage) | |
2851 | +- | |
2852 | +- renamed: README.md -> README | |
2853 | +- | |
2854 | +-Changes not staged for commit: | |
2855 | +- (use "git add <file>..." to update what will be committed) | |
2856 | +- (use "git checkout -- <file>..." to discard changes in working directory) | |
2857 | +- | |
2858 | +- modified: CONTRIBUTING.md | |
2859 | +----- | |
2860 | +- | |
2861 | + The command is a bit strange, but it works. | |
2862 | + The `CONTRIBUTING.md` file is modified but once again unstaged. | |
2863 | + | |
2864 | +-[NOTE] | |
2865 | + ===== | |
2866 | +-It's true that `git reset` can be a dangerous command, especially if you provide the `--hard` flag. | |
2867 | +-However, in the scenario described above, the file in your working directory is not touched, so it's relatively safe. | |
2868 | ++While `git reset` _can_ be a dangerous command if you call it with `--hard`, in this instance the file in your working directory is not touched. | |
2869 | ++Calling `git reset` without an option is not dangerous - it only touches your staging area. | |
2870 | + ===== | |
2871 | + | |
2872 | + For now this magic invocation is all you need to know about the `git reset` command. | |
2873 | +-We'll go into much more detail about what `reset` does and how to master it to do really interesting things in <<ch07-git-tools#_git_reset>>. | |
2874 | ++We'll go into much more detail about what `reset` does and how to master it to do really interesting things in <<ch07-git-tools#r_git_reset>>. | |
2875 | + | |
2876 | + ==== Unmodifying a Modified File | |
2877 | + | |
2878 | + What if you realize that you don't want to keep your changes to the `CONTRIBUTING.md` file? | |
2879 | +-How can you easily unmodify it -- revert it back to what it looked like when you last committed (or initially cloned, or however you got it into your working directory)? | |
2880 | ++How can you easily unmodify it – revert it back to what it looked like when you last committed (or initially cloned, or however you got it into your working directory)? | |
2881 | + Luckily, `git status` tells you how to do that, too. | |
2882 | + In the last example output, the unstaged area looks like this: | |
2883 | + | |
2884 | +-[source,console] | |
2885 | +----- | |
2886 | +-Changes not staged for commit: | |
2887 | +- (use "git add <file>..." to update what will be committed) | |
2888 | +- (use "git checkout -- <file>..." to discard changes in working directory) | |
2889 | +- | |
2890 | +- modified: CONTRIBUTING.md | |
2891 | +----- | |
2892 | +- | |
2893 | + It tells you pretty explicitly how to discard the changes you've made. | |
2894 | + Let's do what it says: | |
2895 | + | |
2896 | +-[source,console] | |
2897 | +----- | |
2898 | +-$ git checkout -- CONTRIBUTING.md | |
2899 | +-$ git status | |
2900 | +-On branch master | |
2901 | +-Changes to be committed: | |
2902 | +- (use "git reset HEAD <file>..." to unstage) | |
2903 | +- | |
2904 | +- renamed: README.md -> README | |
2905 | +- | |
2906 | +----- | |
2907 | +- | |
2908 | + You can see that the changes have been reverted. | |
2909 | + | |
2910 | +-[IMPORTANT] | |
2911 | + ===== | |
2912 | + It's important to understand that `git checkout -- <file>` is a dangerous command. | |
2913 | +-Any local changes you made to that file are gone -- Git just replaced that file with the most recently-committed version. | |
2914 | +-Don't ever use this command unless you absolutely know that you don't want those unsaved local changes. | |
2915 | ++Any changes you made to that file are gone – you just copied another file over it. | |
2916 | ++Don't ever use this command unless you absolutely know that you don't want the file. | |
2917 | + ===== | |
2918 | + | |
2919 | + If you would like to keep the changes you've made to that file but still need to get it out of the way for now, we'll go over stashing and branching in <<ch03-git-branching#ch03-git-branching>>; these are generally better ways to go. | |
2920 | + | |
2921 | + Remember, anything that is _committed_ in Git can almost always be recovered. | |
2922 | +-Even commits that were on branches that were deleted or commits that were overwritten with an `--amend` commit can be recovered (see <<ch10-git-internals#_data_recovery>> for data recovery). | |
2923 | ++Even commits that were on branches that were deleted or commits that were overwritten with an `--amend` commit can be recovered (see <<ch10-git-internals#r_data_recovery>> for data recovery). | |
2924 | + However, anything you lose that was never committed is likely never to be seen again. | |
2925 | +- | |
2926 | +-[[undoing_git_restore]] | |
2927 | +-==== Undoing things with git restore | |
2928 | +- | |
2929 | +-Git version 2.25.0 introduced a new command: `git restore`. | |
2930 | +-It's basically an alternative to `git reset` which we just covered. | |
2931 | +-From Git version 2.25.0 onwards, Git will use `git restore` instead of `git reset` for many undo operations. | |
2932 | +- | |
2933 | +-Let's retrace our steps, and undo things with `git restore` instead of `git reset`. | |
2934 | +- | |
2935 | +-===== Unstaging a Staged File with git restore | |
2936 | +- | |
2937 | +-The next two sections demonstrate how to work with your staging area and working directory changes with `git restore`. | |
2938 | +-The nice part is that the command you use to determine the state of those two areas also reminds you how to undo changes to them. | |
2939 | +-For example, let's say you've changed two files and want to commit them as two separate changes, but you accidentally type `git add *` and stage them both. | |
2940 | +-How can you unstage one of the two? | |
2941 | +-The `git status` command reminds you: | |
2942 | +- | |
2943 | +-[source,console] | |
2944 | +----- | |
2945 | +-$ git add * | |
2946 | +-$ git status | |
2947 | +-On branch master | |
2948 | +-Changes to be committed: | |
2949 | +- (use "git restore --staged <file>..." to unstage) | |
2950 | +- modified: CONTRIBUTING.md | |
2951 | +- renamed: README.md -> README | |
2952 | +- | |
2953 | +----- | |
2954 | +- | |
2955 | +-Right below the ``Changes to be committed'' text, it says use `git restore --staged <file>...` to unstage. | |
2956 | +-So, let's use that advice to unstage the `CONTRIBUTING.md` file: | |
2957 | +- | |
2958 | +-[source,console] | |
2959 | +----- | |
2960 | +-$ git restore --staged CONTRIBUTING.md | |
2961 | +-$ git status | |
2962 | +-On branch master | |
2963 | +-Changes to be committed: | |
2964 | +- (use "git restore --staged <file>..." to unstage) | |
2965 | +- renamed: README.md -> README | |
2966 | +- | |
2967 | +-Changes not staged for commit: | |
2968 | +- (use "git add <file>..." to update what will be committed) | |
2969 | +- (use "git restore <file>..." to discard changes in working directory) | |
2970 | +- modified: CONTRIBUTING.md | |
2971 | +- | |
2972 | +----- | |
2973 | +- | |
2974 | +-The `CONTRIBUTING.md` file is modified but once again unstaged. | |
2975 | +- | |
2976 | +-===== Unmodifying a Modified File with git restore | |
2977 | +- | |
2978 | +-What if you realize that you don't want to keep your changes to the `CONTRIBUTING.md` file? | |
2979 | +-How can you easily unmodify it -- revert it back to what it looked like when you last committed (or initially cloned, or however you got it into your working directory)? | |
2980 | +-Luckily, `git status` tells you how to do that, too. | |
2981 | +-In the last example output, the unstaged area looks like this: | |
2982 | +- | |
2983 | +-[source,console] | |
2984 | +----- | |
2985 | +-Changes not staged for commit: | |
2986 | +- (use "git add <file>..." to update what will be committed) | |
2987 | +- (use "git restore <file>..." to discard changes in working directory) | |
2988 | +- modified: CONTRIBUTING.md | |
2989 | +- | |
2990 | +----- | |
2991 | +- | |
2992 | +-It tells you pretty explicitly how to discard the changes you've made. | |
2993 | +-Let's do what it says: | |
2994 | +- | |
2995 | +-[source,console] | |
2996 | +----- | |
2997 | +-$ git restore CONTRIBUTING.md | |
2998 | +-$ git status | |
2999 | +-On branch master | |
3000 | +-Changes to be committed: | |
3001 | +- (use "git restore --staged <file>..." to unstage) | |
3002 | +- renamed: README.md -> README | |
3003 | +- | |
3004 | +----- | |
3005 | +- | |
3006 | +-[IMPORTANT] | |
3007 | +-===== | |
3008 | +-It's important to understand that `git restore <file>` is a dangerous command. | |
3009 | +-Any local changes you made to that file are gone -- Git just replaced that file with the most recently-committed version. | |
3010 | +-Don't ever use this command unless you absolutely know that you don't want those unsaved local changes. | |
3011 | +-===== | |
3012 | +diff -au ../../git/progit2/progit2/book/02-git-basics/sections/viewing-history.asc /tmp/book/02-git-basics/sections/viewing-history.asc.tmp | |
3013 | +--- ../../git/progit2/progit2/book/02-git-basics/sections/viewing-history.asc 2020-09-26 08:56:07.950296410 +0900 | |
3014 | ++++ /tmp/book/02-git-basics/sections/viewing-history.asc.tmp 2020-10-25 09:24:25.649966386 +0900 | |
3015 | +@@ -1,213 +1,76 @@ | |
3016 | +-[[_viewing_history]] | |
3017 | ++[[r_viewing_history]] | |
3018 | + === Viewing the Commit History | |
3019 | + | |
3020 | + After you have created several commits, or if you have cloned a repository with an existing commit history, you'll probably want to look back to see what has happened. | |
3021 | + The most basic and powerful tool to do this is the `git log` command. | |
3022 | + | |
3023 | + These examples use a very simple project called ``simplegit''. | |
3024 | +-To get the project, run: | |
3025 | +- | |
3026 | +-[source,console] | |
3027 | +----- | |
3028 | +-$ git clone https://github.com/schacon/simplegit-progit | |
3029 | +----- | |
3030 | ++To get the project, run | |
3031 | + | |
3032 | + When you run `git log` in this project, you should get output that looks something like this:(((git commands, log))) | |
3033 | + | |
3034 | +-[source,console] | |
3035 | +----- | |
3036 | +-$ git log | |
3037 | +-commit ca82a6dff817ec66f44342007202690a93763949 | |
3038 | +-Author: Scott Chacon <schacon@gee-mail.com> | |
3039 | +-Date: Mon Mar 17 21:52:11 2008 -0700 | |
3040 | +- | |
3041 | +- Change version number | |
3042 | +- | |
3043 | +-commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 | |
3044 | +-Author: Scott Chacon <schacon@gee-mail.com> | |
3045 | +-Date: Sat Mar 15 16:40:33 2008 -0700 | |
3046 | +- | |
3047 | +- Remove unnecessary test | |
3048 | +- | |
3049 | +-commit a11bef06a3f659402fe7563abf99ad00de2209e6 | |
3050 | +-Author: Scott Chacon <schacon@gee-mail.com> | |
3051 | +-Date: Sat Mar 15 10:31:28 2008 -0700 | |
3052 | +- | |
3053 | +- Initial commit | |
3054 | +----- | |
3055 | +- | |
3056 | +-By default, with no arguments, `git log` lists the commits made in that repository in reverse chronological order; that is, the most recent commits show up first. | |
3057 | ++By default, with no arguments, `git log` lists the commits made in that repository in reverse chronological order – that is, the most recent commits show up first. | |
3058 | + As you can see, this command lists each commit with its SHA-1 checksum, the author's name and email, the date written, and the commit message. | |
3059 | + | |
3060 | + A huge number and variety of options to the `git log` command are available to show you exactly what you're looking for. | |
3061 | + Here, we'll show you some of the most popular. | |
3062 | + | |
3063 | +-One of the more helpful options is `-p` or `--patch`, which shows the difference (the _patch_ output) introduced in each commit. | |
3064 | +-You can also limit the number of log entries displayed, such as using `-2` to show only the last two entries. | |
3065 | +- | |
3066 | +-[source,console] | |
3067 | +----- | |
3068 | +-$ git log -p -2 | |
3069 | +-commit ca82a6dff817ec66f44342007202690a93763949 | |
3070 | +-Author: Scott Chacon <schacon@gee-mail.com> | |
3071 | +-Date: Mon Mar 17 21:52:11 2008 -0700 | |
3072 | +- | |
3073 | +- Change version number | |
3074 | +- | |
3075 | +-diff --git a/Rakefile b/Rakefile | |
3076 | +-index a874b73..8f94139 100644 | |
3077 | +---- a/Rakefile | |
3078 | +-+++ b/Rakefile | |
3079 | +-@@ -5,7 +5,7 @@ require 'rake/gempackagetask' | |
3080 | +- spec = Gem::Specification.new do |s| | |
3081 | +- s.platform = Gem::Platform::RUBY | |
3082 | +- s.name = "simplegit" | |
3083 | +-- s.version = "0.1.0" | |
3084 | +-+ s.version = "0.1.1" | |
3085 | +- s.author = "Scott Chacon" | |
3086 | +- s.email = "schacon@gee-mail.com" | |
3087 | +- s.summary = "A simple gem for using Git in Ruby code." | |
3088 | +- | |
3089 | +-commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 | |
3090 | +-Author: Scott Chacon <schacon@gee-mail.com> | |
3091 | +-Date: Sat Mar 15 16:40:33 2008 -0700 | |
3092 | +- | |
3093 | +- Remove unnecessary test | |
3094 | +- | |
3095 | +-diff --git a/lib/simplegit.rb b/lib/simplegit.rb | |
3096 | +-index a0a60ae..47c6340 100644 | |
3097 | +---- a/lib/simplegit.rb | |
3098 | +-+++ b/lib/simplegit.rb | |
3099 | +-@@ -18,8 +18,3 @@ class SimpleGit | |
3100 | +- end | |
3101 | +- | |
3102 | +- end | |
3103 | +-- | |
3104 | +--if $0 == __FILE__ | |
3105 | +-- git = SimpleGit.new | |
3106 | +-- puts git.show | |
3107 | +--end | |
3108 | +----- | |
3109 | ++One of the more helpful options is `-p`, which shows the difference introduced in each commit. | |
3110 | ++You can also use `-2`, which limits the output to only the last two entries: | |
3111 | + | |
3112 | + This option displays the same information but with a diff directly following each entry. | |
3113 | + This is very helpful for code review or to quickly browse what happened during a series of commits that a collaborator has added. | |
3114 | + You can also use a series of summarizing options with `git log`. | |
3115 | + For example, if you want to see some abbreviated stats for each commit, you can use the `--stat` option: | |
3116 | + | |
3117 | +-[source,console] | |
3118 | +----- | |
3119 | +-$ git log --stat | |
3120 | +-commit ca82a6dff817ec66f44342007202690a93763949 | |
3121 | +-Author: Scott Chacon <schacon@gee-mail.com> | |
3122 | +-Date: Mon Mar 17 21:52:11 2008 -0700 | |
3123 | +- | |
3124 | +- Change version number | |
3125 | +- | |
3126 | +- Rakefile | 2 +- | |
3127 | +- 1 file changed, 1 insertion(+), 1 deletion(-) | |
3128 | +- | |
3129 | +-commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 | |
3130 | +-Author: Scott Chacon <schacon@gee-mail.com> | |
3131 | +-Date: Sat Mar 15 16:40:33 2008 -0700 | |
3132 | +- | |
3133 | +- Remove unnecessary test | |
3134 | +- | |
3135 | +- lib/simplegit.rb | 5 ----- | |
3136 | +- 1 file changed, 5 deletions(-) | |
3137 | +- | |
3138 | +-commit a11bef06a3f659402fe7563abf99ad00de2209e6 | |
3139 | +-Author: Scott Chacon <schacon@gee-mail.com> | |
3140 | +-Date: Sat Mar 15 10:31:28 2008 -0700 | |
3141 | +- | |
3142 | +- Initial commit | |
3143 | +- | |
3144 | +- README | 6 ++++++ | |
3145 | +- Rakefile | 23 +++++++++++++++++++++++ | |
3146 | +- lib/simplegit.rb | 25 +++++++++++++++++++++++++ | |
3147 | +- 3 files changed, 54 insertions(+) | |
3148 | +----- | |
3149 | +- | |
3150 | + As you can see, the `--stat` option prints below each commit entry a list of modified files, how many files were changed, and how many lines in those files were added and removed. | |
3151 | + It also puts a summary of the information at the end. | |
3152 | + | |
3153 | + Another really useful option is `--pretty`. | |
3154 | + This option changes the log output to formats other than the default. | |
3155 | +-A few prebuilt option values are available for you to use. | |
3156 | +-The `oneline` value for this option prints each commit on a single line, which is useful if you're looking at a lot of commits. | |
3157 | +-In addition, the `short`, `full`, and `fuller` values show the output in roughly the same format but with less or more information, respectively: | |
3158 | +- | |
3159 | +-[source,console] | |
3160 | +----- | |
3161 | +-$ git log --pretty=oneline | |
3162 | +-ca82a6dff817ec66f44342007202690a93763949 Change version number | |
3163 | +-085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 Remove unnecessary test | |
3164 | +-a11bef06a3f659402fe7563abf99ad00de2209e6 Initial commit | |
3165 | +----- | |
3166 | +- | |
3167 | +-The most interesting option value is `format`, which allows you to specify your own log output format. | |
3168 | +-This is especially useful when you're generating output for machine parsing -- because you specify the format explicitly, you know it won't change with updates to Git:(((log formatting))) | |
3169 | +- | |
3170 | +-[source,console] | |
3171 | +----- | |
3172 | +-$ git log --pretty=format:"%h - %an, %ar : %s" | |
3173 | +-ca82a6d - Scott Chacon, 6 years ago : Change version number | |
3174 | +-085bb3b - Scott Chacon, 6 years ago : Remove unnecessary test | |
3175 | +-a11bef0 - Scott Chacon, 6 years ago : Initial commit | |
3176 | +----- | |
3177 | ++A few prebuilt options are available for you to use. | |
3178 | ++The `oneline` option prints each commit on a single line, which is useful if you're looking at a lot of commits. | |
3179 | ++In addition, the `short`, `full`, and `fuller` options show the output in roughly the same format but with less or more information, respectively: | |
3180 | + | |
3181 | +-<<pretty_format>> lists some of the more useful specifiers that `format` takes. | |
3182 | ++The most interesting option is `format`, which allows you to specify your own log output format. | |
3183 | ++This is especially useful when you're generating output for machine parsing – because you specify the format explicitly, you know it won't change with updates to Git:(((log formatting))) | |
3184 | + | |
3185 | +-[[pretty_format]] | |
3186 | +-.Useful specifiers for `git log --pretty=format` | |
3187 | ++<<rpretty_format>> lists some of the more useful options that format takes. | |
3188 | ++ | |
3189 | ++.Useful options for `git log --pretty=format` | |
3190 | + [cols="1,4",options="header"] | |
3191 | + |================================ | |
3192 | +-| Specifier | Description of Output | |
3193 | +-| `%H` | Commit hash | |
3194 | +-| `%h` | Abbreviated commit hash | |
3195 | +-| `%T` | Tree hash | |
3196 | +-| `%t` | Abbreviated tree hash | |
3197 | +-| `%P` | Parent hashes | |
3198 | +-| `%p` | Abbreviated parent hashes | |
3199 | +-| `%an` | Author name | |
3200 | +-| `%ae` | Author email | |
3201 | +-| `%ad` | Author date (format respects the --date=option) | |
3202 | +-| `%ar` | Author date, relative | |
3203 | +-| `%cn` | Committer name | |
3204 | +-| `%ce` | Committer email | |
3205 | +-| `%cd` | Committer date | |
3206 | +-| `%cr` | Committer date, relative | |
3207 | +-| `%s` | Subject | |
3208 | ++| Option | Description of Output | |
3209 | ++| `%H` | Commit hash | |
3210 | ++| `%h` | Abbreviated commit hash | |
3211 | ++| `%T` | Tree hash | |
3212 | ++| `%t` | Abbreviated tree hash | |
3213 | ++| `%P` | Parent hashes | |
3214 | ++| `%p` | Abbreviated parent hashes | |
3215 | ++| `%an` | Author name | |
3216 | ++| `%ae` | Author email | |
3217 | ++| `%ad` | Author date (format respects the --date=option) | |
3218 | ++| `%ar` | Author date, relative | |
3219 | ++| `%cn` | Committer name | |
3220 | ++| `%ce` | Committer email | |
3221 | ++| `%cd` | Committer date | |
3222 | ++| `%cr` | Committer date, relative | |
3223 | ++| `%s` | Subject | |
3224 | + |================================ | |
3225 | + | |
3226 | + You may be wondering what the difference is between _author_ and _committer_. | |
3227 | + The author is the person who originally wrote the work, whereas the committer is the person who last applied the work. | |
3228 | +-So, if you send in a patch to a project and one of the core members applies the patch, both of you get credit -- you as the author, and the core member as the committer. | |
3229 | ++So, if you send in a patch to a project and one of the core members applies the patch, both of you get credit – you as the author, and the core member as the committer. | |
3230 | + We'll cover this distinction a bit more in <<ch05-distributed-git#ch05-distributed-git>>. | |
3231 | + | |
3232 | +-The `oneline` and `format` option values are particularly useful with another `log` option called `--graph`. | |
3233 | ++The `oneline` and `format` options are particularly useful with another `log` option called `--graph`. | |
3234 | + This option adds a nice little ASCII graph showing your branch and merge history: | |
3235 | + | |
3236 | +-[source,console] | |
3237 | +----- | |
3238 | +-$ git log --pretty=format:"%h %s" --graph | |
3239 | +-* 2d3acf9 Ignore errors from SIGCHLD on trap | |
3240 | +-* 5e3ee11 Merge branch 'master' of git://github.com/dustin/grit | |
3241 | +-|\ | |
3242 | +-| * 420eac9 Add method for getting the current branch | |
3243 | +-* | 30e367c Timeout code and tests | |
3244 | +-* | 5a09431 Add timeout protection to grit | |
3245 | +-* | e1193f8 Support for heads with slashes in them | |
3246 | +-|/ | |
3247 | +-* d6016bc Require time for xmlschema | |
3248 | +-* 11d191e Merge branch 'defunkt' into local | |
3249 | +----- | |
3250 | +- | |
3251 | + This type of output will become more interesting as we go through branching and merging in the next chapter. | |
3252 | + | |
3253 | +-Those are only some simple output-formatting options to `git log` -- there are many more. | |
3254 | +-<<log_options>> lists the options we've covered so far, as well as some other common formatting options that may be useful, along with how they change the output of the log command. | |
3255 | ++Those are only some simple output-formatting options to `git log` – there are many more. | |
3256 | ++<<rlog_options>> lists the options we've covered so far, as well as some other common formatting options that may be useful, along with how they change the output of the log command. | |
3257 | + | |
3258 | +-[[log_options]] | |
3259 | + .Common options to `git log` | |
3260 | + [cols="1,4",options="header"] | |
3261 | + |================================ | |
3262 | +@@ -220,63 +83,39 @@ | |
3263 | + | `--abbrev-commit` | Show only the first few characters of the SHA-1 checksum instead of all 40. | |
3264 | + | `--relative-date` | Display the date in a relative format (for example, ``2 weeks ago'') instead of using the full date format. | |
3265 | + | `--graph` | Display an ASCII graph of the branch and merge history beside the log output. | |
3266 | +-| `--pretty` | Show commits in an alternate format. Option values include oneline, short, full, fuller, and format (where you specify your own format). | |
3267 | +-| `--oneline` | Shorthand for `--pretty=oneline --abbrev-commit` used together. | |
3268 | ++| `--pretty` | Show commits in an alternate format. Options include oneline, short, full, fuller, and format (where you specify your own format). | |
3269 | + |================================ | |
3270 | + | |
3271 | + ==== Limiting Log Output | |
3272 | + | |
3273 | +-In addition to output-formatting options, `git log` takes a number of useful limiting options; that is, options that let you show only a subset of commits. | |
3274 | +-You've seen one such option already -- the `-2` option, which displays only the last two commits. | |
3275 | ++In addition to output-formatting options, `git log` takes a number of useful limiting options – that is, options that let you show only a subset of commits. | |
3276 | ++You've seen one such option already – the `-2` option, which show only the last two commits. | |
3277 | + In fact, you can do `-<n>`, where `n` is any integer to show the last `n` commits. | |
3278 | + In reality, you're unlikely to use that often, because Git by default pipes all output through a pager so you see only one page of log output at a time. | |
3279 | + | |
3280 | + However, the time-limiting options such as `--since` and `--until` are very useful. | |
3281 | + For example, this command gets the list of commits made in the last two weeks: | |
3282 | + | |
3283 | +-[source,console] | |
3284 | +----- | |
3285 | +-$ git log --since=2.weeks | |
3286 | +----- | |
3287 | +- | |
3288 | +-This command works with lots of formats -- you can specify a specific date like `"2008-01-15"`, or a relative date such as `"2 years 1 day 3 minutes ago"`. | |
3289 | ++This command works with lots of formats – you can specify a specific date like `"2008-01-15"`, or a relative date such as `"2 years 1 day 3 minutes ago"`. | |
3290 | + | |
3291 | + You can also filter the list to commits that match some search criteria. | |
3292 | + The `--author` option allows you to filter on a specific author, and the `--grep` option lets you search for keywords in the commit messages. | |
3293 | ++(Note that if you want to specify both author and grep options, you have to add `--all-match` or the command will match commits with either.) | |
3294 | + | |
3295 | +-[NOTE] | |
3296 | +-==== | |
3297 | +-You can specify more than one instance of both the `--author` and `--grep` search criteria, which | |
3298 | +-will limit the commit output to commits that match _any_ of the `--author` patterns and _any_ | |
3299 | +-of the `--grep` patterns; however, adding the `--all-match` option further limits the output to | |
3300 | +-just those commits that match _all_ `--grep` patterns. | |
3301 | +-==== | |
3302 | +- | |
3303 | +-Another really helpful filter is the `-S` option (colloquially referred to as Git's ``pickaxe'' option), which takes a string and shows only those commits that changed the number of occurrences of that string. | |
3304 | ++Another really helpful filter is the `-S` option which takes a string and only shows the commits that introduced a change to the code that added or removed that string. | |
3305 | + For instance, if you wanted to find the last commit that added or removed a reference to a specific function, you could call: | |
3306 | + | |
3307 | +-[source,console] | |
3308 | +----- | |
3309 | +-$ git log -S function_name | |
3310 | +----- | |
3311 | +- | |
3312 | + The last really useful option to pass to `git log` as a filter is a path. | |
3313 | + If you specify a directory or file name, you can limit the log output to commits that introduced a change to those files. | |
3314 | +-This is always the last option and is generally preceded by double dashes (`--`) to separate the paths from the options: | |
3315 | +- | |
3316 | +-[source,console] | |
3317 | +----- | |
3318 | +-$ git log -- path/to/file | |
3319 | +----- | |
3320 | ++This is always the last option and is generally preceded by double dashes (`--`) to separate the paths from the options. | |
3321 | + | |
3322 | +-In <<limit_options>> we'll list these and a few other common options for your reference. | |
3323 | ++In <<rlimit_options>> we'll list these and a few other common options for your reference. | |
3324 | + | |
3325 | +-[[limit_options]] | |
3326 | + .Options to limit the output of `git log` | |
3327 | + [cols="2,4",options="header"] | |
3328 | + |================================ | |
3329 | + | Option | Description | |
3330 | +-| `-<n>` | Show only the last n commits | |
3331 | ++| `-(n)` | Show only the last n commits | |
3332 | + | `--since`, `--after` | Limit the commits to those made after the specified date. | |
3333 | + | `--until`, `--before` | Limit the commits to those made before the specified date. | |
3334 | + | `--author` | Only show commits in which the author entry matches the specified string. | |
3335 | +@@ -285,25 +124,6 @@ | |
3336 | + | `-S` | Only show commits adding or removing code matching the string | |
3337 | + |================================ | |
3338 | + | |
3339 | +-For example, if you want to see which commits modifying test files in the Git source code history were committed by Junio Hamano in the month of October 2008 and are not merge commits, you can run something like this:(((log filtering))) | |
3340 | +- | |
3341 | +-[source,console] | |
3342 | +----- | |
3343 | +-$ git log --pretty="%h - %s" --author='Junio C Hamano' --since="2008-10-01" \ | |
3344 | +- --before="2008-11-01" --no-merges -- t/ | |
3345 | +-5610e3b - Fix testcase failure when extended attributes are in use | |
3346 | +-acd3b9e - Enhance hold_lock_file_for_{update,append}() API | |
3347 | +-f563754 - demonstrate breakage of detached checkout with symbolic link HEAD | |
3348 | +-d1a43f2 - reset --hard/read-tree --reset -u: remove unmerged new paths | |
3349 | +-51a94af - Fix "checkout --track -b newbranch" on detached HEAD | |
3350 | +-b0ad11e - pull: allow "git pull origin $something:$current_branch" into an unborn branch | |
3351 | +----- | |
3352 | ++For example, if you want to see which commits modifying test files in the Git source code history are merged and were committed by Junio Hamano in the month of October 2008, you can run something like this:(((log filtering))) | |
3353 | + | |
3354 | + Of the nearly 40,000 commits in the Git source code history, this command shows the 6 that match those criteria. | |
3355 | +- | |
3356 | +-[TIP] | |
3357 | +-.Preventing the display of merge commits | |
3358 | +-==== | |
3359 | +-Depending on the workflow used in your repository, it's possible that a sizable percentage of the commits in your log history are just merge commits, which typically aren't very informative. | |
3360 | +-To prevent the display of merge commits cluttering up your log history, simply add the log option `--no-merges`. | |
3361 | +-==== | |
3362 | +diff -au ../../git/progit2/progit2/book/03-git-branching/sections/basic-branching-and-merging.asc /tmp/book/03-git-branching/sections/basic-branching-and-merging.asc.tmp | |
3363 | +--- ../../git/progit2/progit2/book/03-git-branching/sections/basic-branching-and-merging.asc 2020-07-04 16:36:46.720685416 +0900 | |
3364 | ++++ /tmp/book/03-git-branching/sections/basic-branching-and-merging.asc.tmp 2020-10-25 09:24:25.655966668 +0900 | |
3365 | +@@ -3,8 +3,8 @@ | |
3366 | + Let's go through a simple example of branching and merging with a workflow that you might use in the real world. | |
3367 | + You'll follow these steps: | |
3368 | + | |
3369 | +-. Do some work on a website. | |
3370 | +-. Create a branch for a new user story you're working on. | |
3371 | ++. Do work on a web site. | |
3372 | ++. Create a branch for a new story you're working on. | |
3373 | + . Do some work in that branch. | |
3374 | + | |
3375 | + At this stage, you'll receive a call that another issue is critical and you need a hotfix. | |
3376 | +@@ -13,306 +13,134 @@ | |
3377 | + . Switch to your production branch. | |
3378 | + . Create a branch to add the hotfix. | |
3379 | + . After it's tested, merge the hotfix branch, and push to production. | |
3380 | +-. Switch back to your original user story and continue working. | |
3381 | ++. Switch back to your original story and continue working. | |
3382 | + | |
3383 | +-[[_basic_branching]] | |
3384 | + ==== Basic Branching | |
3385 | + | |
3386 | +-(((branches, basic workflow))) | |
3387 | +-First, let's say you're working on your project and have a couple of commits already on the `master` branch. | |
3388 | ++First, let's say you're working on your project and have a couple of commits already. | |
3389 | + | |
3390 | + .A simple commit history | |
3391 | +-image::images/basic-branching-1.png[A simple commit history] | |
3392 | ++image::images/basic-branching-1.png[A simple commit history.] | |
3393 | + | |
3394 | + You've decided that you're going to work on issue #53 in whatever issue-tracking system your company uses. | |
3395 | +-To create a new branch and switch to it at the same time, you can run the `git checkout` command with the `-b` switch: | |
3396 | +- | |
3397 | +-[source,console] | |
3398 | +----- | |
3399 | +-$ git checkout -b iss53 | |
3400 | +-Switched to a new branch "iss53" | |
3401 | +----- | |
3402 | ++To create a branch and switch to it at the same time, you can run the `git checkout` command with the `-b` switch: | |
3403 | + | |
3404 | + This is shorthand for: | |
3405 | + | |
3406 | +-[source,console] | |
3407 | +----- | |
3408 | +-$ git branch iss53 | |
3409 | +-$ git checkout iss53 | |
3410 | +----- | |
3411 | +- | |
3412 | + .Creating a new branch pointer | |
3413 | +-image::images/basic-branching-2.png[Creating a new branch pointer] | |
3414 | ++image::images/basic-branching-2.png[Creating a new branch pointer.] | |
3415 | + | |
3416 | +-You work on your website and do some commits. | |
3417 | ++You work on your web site and do some commits. | |
3418 | + Doing so moves the `iss53` branch forward, because you have it checked out (that is, your `HEAD` is pointing to it): | |
3419 | + | |
3420 | +-[source,console] | |
3421 | +----- | |
3422 | +-$ vim index.html | |
3423 | +-$ git commit -a -m 'Create new footer [issue 53]' | |
3424 | +----- | |
3425 | +- | |
3426 | +-.The `iss53` branch has moved forward with your work | |
3427 | +-image::images/basic-branching-3.png[The `iss53` branch has moved forward with your work] | |
3428 | ++.The iss53 branch has moved forward with your work | |
3429 | ++image::images/basic-branching-3.png[The iss53 branch has moved forward with your work.] | |
3430 | + | |
3431 | +-Now you get the call that there is an issue with the website, and you need to fix it immediately. | |
3432 | ++Now you get the call that there is an issue with the web site, and you need to fix it immediately. | |
3433 | + With Git, you don't have to deploy your fix along with the `iss53` changes you've made, and you don't have to put a lot of effort into reverting those changes before you can work on applying your fix to what is in production. | |
3434 | + All you have to do is switch back to your `master` branch. | |
3435 | + | |
3436 | + However, before you do that, note that if your working directory or staging area has uncommitted changes that conflict with the branch you're checking out, Git won't let you switch branches. | |
3437 | + It's best to have a clean working state when you switch branches. | |
3438 | +-There are ways to get around this (namely, stashing and commit amending) that we'll cover later on, in <<ch07-git-tools#_git_stashing>>. | |
3439 | ++There are ways to get around this (namely, stashing and commit amending) that we'll cover later on, in <<ch07-git-tools#r_git_stashing>>. | |
3440 | + For now, let's assume you've committed all your changes, so you can switch back to your `master` branch: | |
3441 | + | |
3442 | +-[source,console] | |
3443 | +----- | |
3444 | +-$ git checkout master | |
3445 | +-Switched to branch 'master' | |
3446 | +----- | |
3447 | +- | |
3448 | + At this point, your project working directory is exactly the way it was before you started working on issue #53, and you can concentrate on your hotfix. | |
3449 | + This is an important point to remember: when you switch branches, Git resets your working directory to look like it did the last time you committed on that branch. | |
3450 | + It adds, removes, and modifies files automatically to make sure your working copy is what the branch looked like on your last commit to it. | |
3451 | + | |
3452 | + Next, you have a hotfix to make. | |
3453 | +-Let's create a `hotfix` branch on which to work until it's completed: | |
3454 | +- | |
3455 | +-[source,console] | |
3456 | +----- | |
3457 | +-$ git checkout -b hotfix | |
3458 | +-Switched to a new branch 'hotfix' | |
3459 | +-$ vim index.html | |
3460 | +-$ git commit -a -m 'Fix broken email address' | |
3461 | +-[hotfix 1fb7853] Fix broken email address | |
3462 | +- 1 file changed, 2 insertions(+) | |
3463 | +----- | |
3464 | ++Let's create a hotfix branch on which to work until it's completed: | |
3465 | + | |
3466 | + .Hotfix branch based on `master` | |
3467 | +-image::images/basic-branching-4.png[Hotfix branch based on `master`] | |
3468 | ++image::images/basic-branching-4.png[Hotfix branch based on `master`.] | |
3469 | + | |
3470 | +-You can run your tests, make sure the hotfix is what you want, and finally merge the `hotfix` branch back into your `master` branch to deploy to production. | |
3471 | ++You can run your tests, make sure the hotfix is what you want, and merge it back into your `master` branch to deploy to production. | |
3472 | + You do this with the `git merge` command:(((git commands, merge))) | |
3473 | + | |
3474 | +-[source,console] | |
3475 | +----- | |
3476 | +-$ git checkout master | |
3477 | +-$ git merge hotfix | |
3478 | +-Updating f42c576..3a0874c | |
3479 | +-Fast-forward | |
3480 | +- index.html | 2 ++ | |
3481 | +- 1 file changed, 2 insertions(+) | |
3482 | +----- | |
3483 | +- | |
3484 | + You'll notice the phrase ``fast-forward'' in that merge. | |
3485 | +-Because the commit `C4` pointed to by the branch `hotfix` you merged in was directly ahead of the commit `C2` you're on, Git simply moves the pointer forward. | |
3486 | +-To phrase that another way, when you try to merge one commit with a commit that can be reached by following the first commit's history, Git simplifies things by moving the pointer forward because there is no divergent work to merge together -- this is called a ``fast-forward.'' | |
3487 | ++Because the commit pointed to by the branch you merged in was directly upstream of the commit you're on, Git simply moves the pointer forward. | |
3488 | ++To phrase that another way, when you try to merge one commit with a commit that can be reached by following the first commit's history, Git simplifies things by moving the pointer forward because there is no divergent work to merge together – this is called a ``fast-forward.'' | |
3489 | + | |
3490 | + Your change is now in the snapshot of the commit pointed to by the `master` branch, and you can deploy the fix. | |
3491 | + | |
3492 | + .`master` is fast-forwarded to `hotfix` | |
3493 | +-image::images/basic-branching-5.png[`master` is fast-forwarded to `hotfix`] | |
3494 | ++image::images/basic-branching-5.png[`master` is fast-forwarded to `hotfix`.] | |
3495 | + | |
3496 | + After your super-important fix is deployed, you're ready to switch back to the work you were doing before you were interrupted. | |
3497 | +-However, first you'll delete the `hotfix` branch, because you no longer need it -- the `master` branch points at the same place. | |
3498 | ++However, first you'll delete the `hotfix` branch, because you no longer need it – the `master` branch points at the same place. | |
3499 | + You can delete it with the `-d` option to `git branch`: | |
3500 | + | |
3501 | +-[source,console] | |
3502 | +----- | |
3503 | +-$ git branch -d hotfix | |
3504 | +-Deleted branch hotfix (3a0874c). | |
3505 | +----- | |
3506 | +- | |
3507 | + Now you can switch back to your work-in-progress branch on issue #53 and continue working on it. | |
3508 | + | |
3509 | +-[source,console] | |
3510 | +----- | |
3511 | +-$ git checkout iss53 | |
3512 | +-Switched to branch "iss53" | |
3513 | +-$ vim index.html | |
3514 | +-$ git commit -a -m 'Finish the new footer [issue 53]' | |
3515 | +-[iss53 ad82d7a] Finish the new footer [issue 53] | |
3516 | +-1 file changed, 1 insertion(+) | |
3517 | +----- | |
3518 | +- | |
3519 | + .Work continues on `iss53` | |
3520 | +-image::images/basic-branching-6.png[Work continues on `iss53`] | |
3521 | ++image::images/basic-branching-6.png[Work continues on `iss53`.] | |
3522 | + | |
3523 | + It's worth noting here that the work you did in your `hotfix` branch is not contained in the files in your `iss53` branch. | |
3524 | + If you need to pull it in, you can merge your `master` branch into your `iss53` branch by running `git merge master`, or you can wait to integrate those changes until you decide to pull the `iss53` branch back into `master` later. | |
3525 | + | |
3526 | +-[[_basic_merging]] | |
3527 | + ==== Basic Merging | |
3528 | + | |
3529 | +-(((branches, merging)))(((merging))) | |
3530 | + Suppose you've decided that your issue #53 work is complete and ready to be merged into your `master` branch. | |
3531 | + In order to do that, you'll merge your `iss53` branch into `master`, much like you merged your `hotfix` branch earlier. | |
3532 | + All you have to do is check out the branch you wish to merge into and then run the `git merge` command: | |
3533 | + | |
3534 | +-[source,console] | |
3535 | +----- | |
3536 | +-$ git checkout master | |
3537 | +-Switched to branch 'master' | |
3538 | +-$ git merge iss53 | |
3539 | +-Merge made by the 'recursive' strategy. | |
3540 | +-index.html | 1 + | |
3541 | +-1 file changed, 1 insertion(+) | |
3542 | +----- | |
3543 | +- | |
3544 | + This looks a bit different than the `hotfix` merge you did earlier. | |
3545 | + In this case, your development history has diverged from some older point. | |
3546 | + Because the commit on the branch you're on isn't a direct ancestor of the branch you're merging in, Git has to do some work. | |
3547 | + In this case, Git does a simple three-way merge, using the two snapshots pointed to by the branch tips and the common ancestor of the two. | |
3548 | + | |
3549 | + .Three snapshots used in a typical merge | |
3550 | +-image::images/basic-merging-1.png[Three snapshots used in a typical merge] | |
3551 | ++image::images/basic-merging-1.png[Three snapshots used in a typical merge.] | |
3552 | + | |
3553 | + Instead of just moving the branch pointer forward, Git creates a new snapshot that results from this three-way merge and automatically creates a new commit that points to it. | |
3554 | + This is referred to as a merge commit, and is special in that it has more than one parent. | |
3555 | + | |
3556 | + .A merge commit | |
3557 | +-image::images/basic-merging-2.png[A merge commit] | |
3558 | ++image::images/basic-merging-2.png[A merge commit.] | |
3559 | + | |
3560 | +-Now that your work is merged in, you have no further need for the `iss53` branch. | |
3561 | +-You can close the issue in your issue-tracking system, and delete the branch: | |
3562 | ++It's worth pointing out that Git determines the best common ancestor to use for its merge base; this is different than older tools like CVS or Subversion (before version 1.5), where the developer doing the merge had to figure out the best merge base for themselves. | |
3563 | ++This makes merging a heck of a lot easier in Git than in these other systems. | |
3564 | + | |
3565 | +-[source,console] | |
3566 | +----- | |
3567 | +-$ git branch -d iss53 | |
3568 | +----- | |
3569 | ++Now that your work is merged in, you have no further need for the `iss53` branch. | |
3570 | ++You can close the ticket in your ticket-tracking system, and delete the branch: | |
3571 | + | |
3572 | +-[[_basic_merge_conflicts]] | |
3573 | + ==== Basic Merge Conflicts | |
3574 | + | |
3575 | +-(((merging, conflicts))) | |
3576 | + Occasionally, this process doesn't go smoothly. | |
3577 | +-If you changed the same part of the same file differently in the two branches you're merging, Git won't be able to merge them cleanly. | |
3578 | +-If your fix for issue #53 modified the same part of a file as the `hotfix` branch, you'll get a merge conflict that looks something like this: | |
3579 | +- | |
3580 | +-[source,console] | |
3581 | +----- | |
3582 | +-$ git merge iss53 | |
3583 | +-Auto-merging index.html | |
3584 | +-CONFLICT (content): Merge conflict in index.html | |
3585 | +-Automatic merge failed; fix conflicts and then commit the result. | |
3586 | +----- | |
3587 | ++If you changed the same part of the same file differently in the two branches you're merging together, Git won't be able to merge them cleanly. | |
3588 | ++If your fix for issue #53 modified the same part of a file as the `hotfix`, you'll get a merge conflict that looks something like this: | |
3589 | + | |
3590 | + Git hasn't automatically created a new merge commit. | |
3591 | + It has paused the process while you resolve the conflict. | |
3592 | + If you want to see which files are unmerged at any point after a merge conflict, you can run `git status`: | |
3593 | + | |
3594 | +-[source,console] | |
3595 | +----- | |
3596 | +-$ git status | |
3597 | +-On branch master | |
3598 | +-You have unmerged paths. | |
3599 | +- (fix conflicts and run "git commit") | |
3600 | +- | |
3601 | +-Unmerged paths: | |
3602 | +- (use "git add <file>..." to mark resolution) | |
3603 | +- | |
3604 | +- both modified: index.html | |
3605 | +- | |
3606 | +-no changes added to commit (use "git add" and/or "git commit -a") | |
3607 | +----- | |
3608 | +- | |
3609 | + Anything that has merge conflicts and hasn't been resolved is listed as unmerged. | |
3610 | + Git adds standard conflict-resolution markers to the files that have conflicts, so you can open them manually and resolve those conflicts. | |
3611 | + Your file contains a section that looks something like this: | |
3612 | + | |
3613 | +-[source,html] | |
3614 | +----- | |
3615 | +-<<<<<<< HEAD:index.html | |
3616 | +-<div id="footer">contact : email.support@github.com</div> | |
3617 | +-======= | |
3618 | +-<div id="footer"> | |
3619 | +- please contact us at support@github.com | |
3620 | +-</div> | |
3621 | +->>>>>>> iss53:index.html | |
3622 | +----- | |
3623 | +- | |
3624 | + This means the version in `HEAD` (your `master` branch, because that was what you had checked out when you ran your merge command) is the top part of that block (everything above the `=======`), while the version in your `iss53` branch looks like everything in the bottom part. | |
3625 | + In order to resolve the conflict, you have to either choose one side or the other or merge the contents yourself. | |
3626 | + For instance, you might resolve this conflict by replacing the entire block with this: | |
3627 | + | |
3628 | +-[source,html] | |
3629 | +----- | |
3630 | +-<div id="footer"> | |
3631 | +-please contact us at email.support@github.com | |
3632 | +-</div> | |
3633 | +----- | |
3634 | +- | |
3635 | +-This resolution has a little of each section, and the `<<<<<<<`, `=======`, and `>>>>>>>` lines have been completely removed. | |
3636 | ++This resolution has a little of each section, and the `>>>>>` lines have been completely removed. | |
3637 | + After you've resolved each of these sections in each conflicted file, run `git add` on each file to mark it as resolved. | |
3638 | + Staging the file marks it as resolved in Git. | |
3639 | + | |
3640 | + If you want to use a graphical tool to resolve these issues, you can run `git mergetool`, which fires up an appropriate visual merge tool and walks you through the conflicts:(((git commands, mergetool))) | |
3641 | + | |
3642 | +-[source,console] | |
3643 | +----- | |
3644 | +-$ git mergetool | |
3645 | +- | |
3646 | +-This message is displayed because 'merge.tool' is not configured. | |
3647 | +-See 'git mergetool --tool-help' or 'git help config' for more details. | |
3648 | +-'git mergetool' will now attempt to use one of the following tools: | |
3649 | +-opendiff kdiff3 tkdiff xxdiff meld tortoisemerge gvimdiff diffuse diffmerge ecmerge p4merge araxis bc3 codecompare vimdiff emerge | |
3650 | +-Merging: | |
3651 | +-index.html | |
3652 | +- | |
3653 | +-Normal merge conflict for 'index.html': | |
3654 | +- {local}: modified file | |
3655 | +- {remote}: modified file | |
3656 | +-Hit return to start merge resolution tool (opendiff): | |
3657 | +----- | |
3658 | +- | |
3659 | + If you want to use a merge tool other than the default (Git chose `opendiff` in this case because the command was run on a Mac), you can see all the supported tools listed at the top after ``one of the following tools.'' | |
3660 | + Just type the name of the tool you'd rather use. | |
3661 | + | |
3662 | +-[NOTE] | |
3663 | + ==== | |
3664 | +-If you need more advanced tools for resolving tricky merge conflicts, we cover more on merging in <<ch07-git-tools#_advanced_merging>>. | |
3665 | ++If you need more advanced tools for resolving tricky merge conflicts, we cover more on merging in <<ch07-git-tools#r_advanced_merging>>. | |
3666 | + ==== | |
3667 | + | |
3668 | + After you exit the merge tool, Git asks you if the merge was successful. | |
3669 | + If you tell the script that it was, it stages the file to mark it as resolved for you. | |
3670 | + You can run `git status` again to verify that all conflicts have been resolved: | |
3671 | + | |
3672 | +-[source,console] | |
3673 | +----- | |
3674 | +-$ git status | |
3675 | +-On branch master | |
3676 | +-All conflicts fixed but you are still merging. | |
3677 | +- (use "git commit" to conclude merge) | |
3678 | +- | |
3679 | +-Changes to be committed: | |
3680 | +- | |
3681 | +- modified: index.html | |
3682 | +----- | |
3683 | +- | |
3684 | + If you're happy with that, and you verify that everything that had conflicts has been staged, you can type `git commit` to finalize the merge commit. | |
3685 | + The commit message by default looks something like this: | |
3686 | + | |
3687 | +-[source,console] | |
3688 | +----- | |
3689 | +-Merge branch 'iss53' | |
3690 | +- | |
3691 | +-Conflicts: | |
3692 | +- index.html | |
3693 | +-# | |
3694 | +-# It looks like you may be committing a merge. | |
3695 | +-# If this is not correct, please remove the file | |
3696 | +-# .git/MERGE_HEAD | |
3697 | +-# and try again. | |
3698 | +- | |
3699 | +- | |
3700 | +-# Please enter the commit message for your changes. Lines starting | |
3701 | +-# with '#' will be ignored, and an empty message aborts the commit. | |
3702 | +-# On branch master | |
3703 | +-# All conflicts fixed but you are still merging. | |
3704 | +-# | |
3705 | +-# Changes to be committed: | |
3706 | +-# modified: index.html | |
3707 | +-# | |
3708 | +----- | |
3709 | +- | |
3710 | +-If you think it would be helpful to others looking at this merge in the future, you can modify this commit message with details about how you resolved the merge and explain why you did the changes you made if these are not obvious. | |
3711 | ++You can modify that message with details about how you resolved the merge if you think it would be helpful to others looking at this merge in the future – why you did what you did, if it's not obvious. | |
3712 | +diff -au ../../git/progit2/progit2/book/03-git-branching/sections/branch-management.asc /tmp/book/03-git-branching/sections/branch-management.asc.tmp | |
3713 | +--- ../../git/progit2/progit2/book/03-git-branching/sections/branch-management.asc 2020-09-12 09:27:13.221663700 +0900 | |
3714 | ++++ /tmp/book/03-git-branching/sections/branch-management.asc.tmp 2020-10-25 09:24:25.660966904 +0900 | |
3715 | +@@ -1,4 +1,4 @@ | |
3716 | +-[[_branch_management]] | |
3717 | ++[[r_branch_management]] | |
3718 | + === Branch Management | |
3719 | + | |
3720 | + (((branches, managing))) | |
3721 | +@@ -22,9 +22,9 @@ | |
3722 | + [source,console] | |
3723 | + ---- | |
3724 | + $ git branch -v | |
3725 | +- iss53 93b412c Fix javascript issue | |
3726 | ++ iss53 93b412c fix javascript issue | |
3727 | + * master 7a98805 Merge branch 'iss53' | |
3728 | +- testing 782fd34 Add scott to the author list in the readme | |
3729 | ++ testing 782fd34 add scott to the author list in the readmes | |
3730 | + ---- | |
3731 | + | |
3732 | + The useful `--merged` and `--no-merged` options can filter this list to branches that you have or have not yet merged into the branch you're currently on. | |
3733 | +@@ -59,126 +59,3 @@ | |
3734 | + ---- | |
3735 | + | |
3736 | + If you really do want to delete the branch and lose that work, you can force it with `-D`, as the helpful message points out. | |
3737 | +- | |
3738 | +-[TIP] | |
3739 | +-==== | |
3740 | +-The options described above, `--merged` and `--no-merged` will, if not given a commit or branch name as an argument, show you what is, respectively, merged or not merged into your _current_ branch. | |
3741 | +- | |
3742 | +-You can always provide an additional argument to ask about the merge state with respect to some other branch without checking that other branch out first, as in, what is not merged into the `master` branch? | |
3743 | +-[source,console] | |
3744 | +----- | |
3745 | +-$ git checkout testing | |
3746 | +-$ git branch --no-merged master | |
3747 | +- topicA | |
3748 | +- featureB | |
3749 | +----- | |
3750 | +-==== | |
3751 | +- | |
3752 | +-==== Changing a branch name | |
3753 | +- | |
3754 | +-[CAUTION] | |
3755 | +-==== | |
3756 | +-Do not rename branches that are still in use by other collaborators. | |
3757 | +-Do not rename a branch like master/main/mainline without having read the section "Changing the master branch name". | |
3758 | +-==== | |
3759 | +- | |
3760 | +-Suppose you have a branch that is called _bad-branch-name_ and you want to change it to _corrected-branch-name_, while keeping all history. | |
3761 | +-You also want to change the branch name on the remote (GitHub, GitLab, other server). | |
3762 | +-How do you do this? | |
3763 | +- | |
3764 | +-Rename the branch locally with the `git branch --move` command: | |
3765 | +- | |
3766 | +-[source, console] | |
3767 | +----- | |
3768 | +-$ git branch --move bad-branch-name corrected-branch-name | |
3769 | +----- | |
3770 | +- | |
3771 | +-This replaces your bad-branch-name with corrected-branch-name, but this change is only local for now. | |
3772 | +-To let others see the corrected branch on the remote, push it: | |
3773 | +- | |
3774 | +-[source,console] | |
3775 | +----- | |
3776 | +-$ git push --set-upstream origin corrected-branch-name | |
3777 | +----- | |
3778 | +- | |
3779 | +-Now we'll take a brief look at where we are now: | |
3780 | +- | |
3781 | +-[source, console] | |
3782 | +----- | |
3783 | +-$ git branch --all | |
3784 | +-* corrected-branch-name | |
3785 | +- main | |
3786 | +- remotes/origin/bad-branch-name | |
3787 | +- remotes/origin/corrected-branch-name | |
3788 | +- remotes/origin/main | |
3789 | +----- | |
3790 | +- | |
3791 | +-Notice that you're on the branch corrected-branch-name. | |
3792 | +-The corrected branch is available on the remote. | |
3793 | +-However the bad branch is also still present on the remote. | |
3794 | +-You can delete the bad branch from the remote: | |
3795 | +- | |
3796 | +-[source,console] | |
3797 | +----- | |
3798 | +-$ git push origin --delete bad-branch-name | |
3799 | +----- | |
3800 | +- | |
3801 | +-Now the bad branch name is fully replaced with the corrected branch name. | |
3802 | +- | |
3803 | +-===== Changing the master branch name | |
3804 | +- | |
3805 | +-[WARNING] | |
3806 | +-==== | |
3807 | +-Changing the name of a branch like master/main/mainline/default will break the integrations, services, helper utilities and build/release scripts that your repository uses. | |
3808 | +-Before you do this, make sure you consult with your collaborators. | |
3809 | +-Also make sure you do a thorough search through your repo and update any references to the old branch name in your code or scripts. | |
3810 | +-==== | |
3811 | +- | |
3812 | +-Rename your local _master_ branch into _main_ with the following command | |
3813 | +- | |
3814 | +-[source,console] | |
3815 | +----- | |
3816 | +-$ git branch --move master main | |
3817 | +----- | |
3818 | +- | |
3819 | +-There's no _master_ branch locally anymore, because it's renamed to the _main_ branch. | |
3820 | +- | |
3821 | +-To let others see the new _main_ branch, you need to push it to the remote. | |
3822 | +-This makes the renamed branch available on the remote. | |
3823 | +- | |
3824 | +-[source,console] | |
3825 | +----- | |
3826 | +-$ git push --set-upstream origin main | |
3827 | +----- | |
3828 | +- | |
3829 | +-Now we end up with the following state: | |
3830 | +- | |
3831 | +-[source,console] | |
3832 | +----- | |
3833 | +-git branch --all | |
3834 | +-* main | |
3835 | +- remotes/origin/HEAD -> origin/master | |
3836 | +- remotes/origin/main | |
3837 | +- remotes/origin/master | |
3838 | +----- | |
3839 | +- | |
3840 | +-Your local _master_ branch is gone, as it's replaced with the _main_ branch. | |
3841 | +-The _main_ branch is also available on the remote. | |
3842 | +-But the remote still has a _master_ branch. | |
3843 | +-Other collaborators will continue to use the _master_ branch as the base of their work, until you make some further changes. | |
3844 | +- | |
3845 | +-Now you have a few more tasks in front of you to complete the transition: | |
3846 | +- | |
3847 | +-* Any projects that depend on this one will need to update their code and/or configuration. | |
3848 | +-* Update any test-runner configuration files. | |
3849 | +-* Adjust build and release scripts. | |
3850 | +-* Redirect settings on your repo host for things like the repo's default branch, merge rules, and other things that match branch names. | |
3851 | +-* Update references to the old branch in documentation. | |
3852 | +-* Close or merge any pull requests that target the old branch. | |
3853 | +- | |
3854 | +-After you've done all these tasks, and are certain the main branch performs just as the _master_ branch, you can delete the _master_ branch: | |
3855 | +- | |
3856 | +-[source, console] | |
3857 | +----- | |
3858 | +-$ git push origin --delete master | |
3859 | +----- | |
3860 | +diff -au ../../git/progit2/progit2/book/03-git-branching/sections/nutshell.asc /tmp/book/03-git-branching/sections/nutshell.asc.tmp | |
3861 | +--- ../../git/progit2/progit2/book/03-git-branching/sections/nutshell.asc 2020-09-26 08:56:07.950296410 +0900 | |
3862 | ++++ /tmp/book/03-git-branching/sections/nutshell.asc.tmp 2020-10-25 09:24:25.667967234 +0900 | |
3863 | +@@ -1,41 +1,34 @@ | |
3864 | +-[[_git_branches_overview]] | |
3865 | ++[[r_git_branches_overview]] | |
3866 | + === Branches in a Nutshell | |
3867 | + | |
3868 | + To really understand the way Git does branching, we need to take a step back and examine how Git stores its data. | |
3869 | + | |
3870 | +-As you may remember from <<ch01-getting-started#what_is_git_section>>, Git doesn't store data as a series of changesets or differences, but instead as a series of _snapshots_. | |
3871 | ++As you may remember from <<ch01-introduction#ch01-introduction>>, Git doesn't store data as a series of changesets or differences, but instead as a series of snapshots. | |
3872 | + | |
3873 | + When you make a commit, Git stores a commit object that contains a pointer to the snapshot of the content you staged. | |
3874 | +-This object also contains the author's name and email address, the message that you typed, and pointers to the commit or commits that directly came before this commit (its parent or parents): zero parents for the initial commit, one parent for a normal commit, and multiple parents for a commit that results from a merge of two or more branches. | |
3875 | ++This object also contains the author's name and email, the message that you typed, and pointers to the commit or commits that directly came before this commit (its parent or parents): zero parents for the initial commit, one parent for a normal commit, and multiple parents for a commit that results from a merge of two or more branches. | |
3876 | + | |
3877 | + To visualize this, let's assume that you have a directory containing three files, and you stage them all and commit. | |
3878 | +-Staging the files computes a checksum for each one (the SHA-1 hash we mentioned in <<ch01-getting-started#what_is_git_section>>), stores that version of the file in the Git repository (Git refers to them as _blobs_), and adds that checksum to the staging area: | |
3879 | ++Staging the files checksums each one (the SHA-1 hash we mentioned in <<ch01-introduction#ch01-introduction>>), stores that version of the file in the Git repository (Git refers to them as blobs), and adds that checksum to the staging area: | |
3880 | + | |
3881 | +-[source,console] | |
3882 | +----- | |
3883 | +-$ git add README test.rb LICENSE | |
3884 | +-$ git commit -m 'Initial commit' | |
3885 | +----- | |
3886 | +- | |
3887 | +-When you create the commit by running `git commit`, Git checksums each subdirectory (in this case, just the root project directory) and stores them as a tree object in the Git repository. | |
3888 | ++When you create the commit by running `git commit`, Git checksums each subdirectory (in this case, just the root project directory) and stores those tree objects in the Git repository. | |
3889 | + Git then creates a commit object that has the metadata and a pointer to the root project tree so it can re-create that snapshot when needed.(((git commands, commit))) | |
3890 | + | |
3891 | +-Your Git repository now contains five objects: three _blobs_ (each representing the contents of one of the three files), one _tree_ that lists the contents of the directory and specifies which file names are stored as which blobs, and one _commit_ with the pointer to that root tree and all the commit metadata. | |
3892 | ++Your Git repository now contains five objects: one blob for the contents of each of your three files, one tree that lists the contents of the directory and specifies which file names are stored as which blobs, and one commit with the pointer to that root tree and all the commit metadata. | |
3893 | + | |
3894 | + .A commit and its tree | |
3895 | +-image::images/commit-and-tree.png[A commit and its tree] | |
3896 | ++image::images/commit-and-tree.png[A commit and its tree.] | |
3897 | + | |
3898 | + If you make some changes and commit again, the next commit stores a pointer to the commit that came immediately before it. | |
3899 | + | |
3900 | + .Commits and their parents | |
3901 | +-image::images/commits-and-parents.png[Commits and their parents] | |
3902 | ++image::images/commits-and-parents.png[Commits and their parents.] | |
3903 | + | |
3904 | + A branch in Git is simply a lightweight movable pointer to one of these commits. | |
3905 | + The default branch name in Git is `master`. | |
3906 | + As you start making commits, you're given a `master` branch that points to the last commit you made. | |
3907 | +-Every time you commit, the `master` branch pointer moves forward automatically. | |
3908 | ++Every time you commit, it moves forward automatically. | |
3909 | + | |
3910 | +-[NOTE] | |
3911 | + ==== | |
3912 | + The ``master'' branch in Git is not a special branch.(((master))) | |
3913 | + It is exactly like any other branch. | |
3914 | +@@ -43,108 +36,62 @@ | |
3915 | + ==== | |
3916 | + | |
3917 | + .A branch and its commit history | |
3918 | +-image::images/branch-and-history.png[A branch and its commit history] | |
3919 | ++image::images/branch-and-history.png[A branch and its commit history.] | |
3920 | + | |
3921 | +-[[_create_new_branch]] | |
3922 | + ==== Creating a New Branch | |
3923 | + | |
3924 | +-(((branches, creating))) | |
3925 | +-What happens when you create a new branch? | |
3926 | ++What happens if you create a new branch? | |
3927 | + Well, doing so creates a new pointer for you to move around. | |
3928 | +-Let's say you want to create a new branch called `testing`. | |
3929 | ++Let's say you create a new branch called testing. | |
3930 | + You do this with the `git branch` command:(((git commands, branch))) | |
3931 | + | |
3932 | +-[source,console] | |
3933 | +----- | |
3934 | +-$ git branch testing | |
3935 | +----- | |
3936 | +- | |
3937 | + This creates a new pointer to the same commit you're currently on. | |
3938 | + | |
3939 | + .Two branches pointing into the same series of commits | |
3940 | +-image::images/two-branches.png[Two branches pointing into the same series of commits] | |
3941 | ++image::images/two-branches.png[Two branches pointing into the same series of commits.] | |
3942 | + | |
3943 | + How does Git know what branch you're currently on? | |
3944 | + It keeps a special pointer called `HEAD`. | |
3945 | + Note that this is a lot different than the concept of `HEAD` in other VCSs you may be used to, such as Subversion or CVS. | |
3946 | + In Git, this is a pointer to the local branch you're currently on. | |
3947 | + In this case, you're still on `master`. | |
3948 | +-The `git branch` command only _created_ a new branch -- it didn't switch to that branch. | |
3949 | ++The `git branch` command only _created_ a new branch – it didn't switch to that branch. | |
3950 | + | |
3951 | + .HEAD pointing to a branch | |
3952 | +-image::images/head-to-master.png[HEAD pointing to a branch] | |
3953 | ++image::images/head-to-master.png[HEAD pointing to a branch.] | |
3954 | + | |
3955 | + You can easily see this by running a simple `git log` command that shows you where the branch pointers are pointing. | |
3956 | + This option is called `--decorate`. | |
3957 | + | |
3958 | +-[source,console] | |
3959 | +----- | |
3960 | +-$ git log --oneline --decorate | |
3961 | +-f30ab (HEAD -> master, testing) Add feature #32 - ability to add new formats to the central interface | |
3962 | +-34ac2 Fix bug #1328 - stack overflow under certain conditions | |
3963 | +-98ca9 Initial commit | |
3964 | +----- | |
3965 | +- | |
3966 | +-You can see the `master` and `testing` branches that are right there next to the `f30ab` commit. | |
3967 | ++You can see the ``master'' and ``testing'' branches that are right there next to the `f30ab` commit. | |
3968 | + | |
3969 | +-[[_switching_branches]] | |
3970 | + ==== Switching Branches | |
3971 | + | |
3972 | +-(((branches, switching))) | |
3973 | + To switch to an existing branch, you run the `git checkout` command.(((git commands, checkout))) | |
3974 | + Let's switch to the new `testing` branch: | |
3975 | + | |
3976 | +-[source,console] | |
3977 | +----- | |
3978 | +-$ git checkout testing | |
3979 | +----- | |
3980 | +- | |
3981 | + This moves `HEAD` to point to the `testing` branch. | |
3982 | + | |
3983 | + .HEAD points to the current branch | |
3984 | +-image::images/head-to-testing.png[HEAD points to the current branch] | |
3985 | ++image::images/head-to-testing.png[HEAD points to the current branch.] | |
3986 | + | |
3987 | + What is the significance of that? | |
3988 | + Well, let's do another commit: | |
3989 | + | |
3990 | +-[source,console] | |
3991 | +----- | |
3992 | +-$ vim test.rb | |
3993 | +-$ git commit -a -m 'made a change' | |
3994 | +----- | |
3995 | +- | |
3996 | + .The HEAD branch moves forward when a commit is made | |
3997 | +-image::images/advance-testing.png[The HEAD branch moves forward when a commit is made] | |
3998 | ++image::images/advance-testing.png[The HEAD branch moves forward when a commit is made.] | |
3999 | + | |
4000 | + This is interesting, because now your `testing` branch has moved forward, but your `master` branch still points to the commit you were on when you ran `git checkout` to switch branches. | |
4001 | + Let's switch back to the `master` branch: | |
4002 | + | |
4003 | +-[source,console] | |
4004 | +----- | |
4005 | +-$ git checkout master | |
4006 | +----- | |
4007 | +- | |
4008 | +-[NOTE] | |
4009 | +-.`git log` doesn't show _all_ the branches _all_ the time | |
4010 | +-==== | |
4011 | +-If you were to run `git log` right now, you might wonder where the "testing" branch you just created went, as it would not appear in the output. | |
4012 | +- | |
4013 | +-The branch hasn't disappeared; Git just doesn't know that you're interested in that branch and it is trying to show you what it thinks you're interested in. | |
4014 | +-In other words, by default, `git log` will only show commit history below the branch you've checked out. | |
4015 | +- | |
4016 | +-To show commit history for the desired branch you have to explicitly specify it: `git log testing`. | |
4017 | +-To show all of the branches, add `--all` to your `git log` command. | |
4018 | +-==== | |
4019 | +- | |
4020 | + .HEAD moves when you checkout | |
4021 | +-image::images/checkout-master.png[HEAD moves when you checkout] | |
4022 | ++image::images/checkout-master.png[HEAD moves when you checkout.] | |
4023 | + | |
4024 | + That command did two things. | |
4025 | + It moved the HEAD pointer back to point to the `master` branch, and it reverted the files in your working directory back to the snapshot that `master` points to. | |
4026 | + This also means the changes you make from this point forward will diverge from an older version of the project. | |
4027 | + It essentially rewinds the work you've done in your `testing` branch so you can go in a different direction. | |
4028 | + | |
4029 | +-[NOTE] | |
4030 | + .Switching branches changes files in your working directory | |
4031 | + ==== | |
4032 | + It's important to note that when you switch branches in Git, files in your working directory will change. | |
4033 | +@@ -154,36 +101,18 @@ | |
4034 | + | |
4035 | + Let's make a few changes and commit again: | |
4036 | + | |
4037 | +-[source,console] | |
4038 | +----- | |
4039 | +-$ vim test.rb | |
4040 | +-$ git commit -a -m 'made other changes' | |
4041 | +----- | |
4042 | +- | |
4043 | +-Now your project history has diverged (see <<divergent_history>>). | |
4044 | ++Now your project history has diverged (see <<rdivergent_history>>). | |
4045 | + You created and switched to a branch, did some work on it, and then switched back to your main branch and did other work. | |
4046 | + Both of those changes are isolated in separate branches: you can switch back and forth between the branches and merge them together when you're ready. | |
4047 | + And you did all that with simple `branch`, `checkout`, and `commit` commands. | |
4048 | + | |
4049 | +-[[divergent_history]] | |
4050 | + .Divergent history | |
4051 | +-image::images/advance-master.png[Divergent history] | |
4052 | ++image::images/advance-master.png[Divergent history.] | |
4053 | + | |
4054 | + You can also see this easily with the `git log` command. | |
4055 | + If you run `git log --oneline --decorate --graph --all` it will print out the history of your commits, showing where your branch pointers are and how your history has diverged. | |
4056 | + | |
4057 | +-[source,console] | |
4058 | +----- | |
4059 | +-$ git log --oneline --decorate --graph --all | |
4060 | +-* c2b9e (HEAD, master) Made other changes | |
4061 | +-| * 87ab2 (testing) Made a change | |
4062 | +-|/ | |
4063 | +-* f30ab Add feature #32 - ability to add new formats to the central interface | |
4064 | +-* 34ac2 Fix bug #1328 - stack overflow under certain conditions | |
4065 | +-* 98ca9 initial commit of my project | |
4066 | +----- | |
4067 | +- | |
4068 | +-Because a branch in Git is actually a simple file that contains the 40 character SHA-1 checksum of the commit it points to, branches are cheap to create and destroy. | |
4069 | ++Because a branch in Git is in actuality a simple file that contains the 40 character SHA-1 checksum of the commit it points to, branches are cheap to create and destroy. | |
4070 | + Creating a new branch is as quick and simple as writing 41 bytes to a file (40 characters and a newline). | |
4071 | + | |
4072 | + This is in sharp contrast to the way most older VCS tools branch, which involves copying all of the project's files into a second directory. | |
4073 | +@@ -192,18 +121,3 @@ | |
4074 | + These features help encourage developers to create and use branches often. | |
4075 | + | |
4076 | + Let's see why you should do so. | |
4077 | +- | |
4078 | +-[NOTE] | |
4079 | +-.Creating a new branch and switching to it at the same time | |
4080 | +-==== | |
4081 | +-It's typical to create a new branch and want to switch to that new branch at the same time -- this can be done in one operation with `git checkout -b <newbranchname>`. | |
4082 | +-==== | |
4083 | +- | |
4084 | +-[NOTE] | |
4085 | +-==== | |
4086 | +-From Git version 2.23 onwards you can use `git switch` instead of `git checkout` to: | |
4087 | +- | |
4088 | +-- Switch to an existing branch: `git switch testing-branch`. | |
4089 | +-- Create a new branch and switch to it: `git switch -c new-branch`. The `-c` flag stands for create, you can also use the full flag: `--create`. | |
4090 | +-- Return to your previously checked out branch: `git switch -`. | |
4091 | +-==== | |
4092 | +diff -au ../../git/progit2/progit2/book/03-git-branching/sections/rebasing.asc /tmp/book/03-git-branching/sections/rebasing.asc.tmp | |
4093 | +--- ../../git/progit2/progit2/book/03-git-branching/sections/rebasing.asc 2020-08-17 16:38:51.075694960 +0900 | |
4094 | ++++ /tmp/book/03-git-branching/sections/rebasing.asc.tmp 2020-10-25 09:24:25.673967517 +0900 | |
4095 | +@@ -1,144 +1,94 @@ | |
4096 | +-[[_rebasing]] | |
4097 | ++[[r_rebasing]] | |
4098 | + === Rebasing | |
4099 | + | |
4100 | +-(((rebasing))) | |
4101 | + In Git, there are two main ways to integrate changes from one branch into another: the `merge` and the `rebase`. | |
4102 | + In this section you'll learn what rebasing is, how to do it, why it's a pretty amazing tool, and in what cases you won't want to use it. | |
4103 | + | |
4104 | + ==== The Basic Rebase | |
4105 | + | |
4106 | +-If you go back to an earlier example from <<_basic_merging>>, you can see that you diverged your work and made commits on two different branches. | |
4107 | ++If you go back to an earlier example from <<r_basic_merging>>, you can see that you diverged your work and made commits on two different branches. | |
4108 | + | |
4109 | + .Simple divergent history | |
4110 | +-image::images/basic-rebase-1.png[Simple divergent history] | |
4111 | ++image::images/basic-rebase-1.png[Simple divergent history.] | |
4112 | + | |
4113 | + The easiest way to integrate the branches, as we've already covered, is the `merge` command. | |
4114 | + It performs a three-way merge between the two latest branch snapshots (`C3` and `C4`) and the most recent common ancestor of the two (`C2`), creating a new snapshot (and commit). | |
4115 | + | |
4116 | +-[[rebasing-merging-example]] | |
4117 | + .Merging to integrate diverged work history | |
4118 | +-image::images/basic-rebase-2.png[Merging to integrate diverged work history] | |
4119 | ++image::images/basic-rebase-2.png[Merging to integrate diverged work history.] | |
4120 | + | |
4121 | + However, there is another way: you can take the patch of the change that was introduced in `C4` and reapply it on top of `C3`. | |
4122 | + In Git, this is called _rebasing_. | |
4123 | +-With the `rebase` command, you can take all the changes that were committed on one branch and replay them on a different branch.(((git commands, rebase))) | |
4124 | ++With the `rebase` command, you can take all the changes that were committed on one branch and replay them on another one.(((git commands, rebase))) | |
4125 | + | |
4126 | +-For this example, you would check out the `experiment` branch, and then rebase it onto the `master` branch as follows: | |
4127 | ++In this example, you'd run the following: | |
4128 | + | |
4129 | +-[source,console] | |
4130 | +----- | |
4131 | +-$ git checkout experiment | |
4132 | +-$ git rebase master | |
4133 | +-First, rewinding head to replay your work on top of it... | |
4134 | +-Applying: added staged command | |
4135 | +----- | |
4136 | +- | |
4137 | +-This operation works by going to the common ancestor of the two branches (the one you're on and the one you're rebasing onto), getting the diff introduced by each commit of the branch you're on, saving those diffs to temporary files, resetting the current branch to the same commit as the branch you are rebasing onto, and finally applying each change in turn. | |
4138 | ++It works by going to the common ancestor of the two branches (the one you're on and the one you're rebasing onto), getting the diff introduced by each commit of the branch you're on, saving those diffs to temporary files, resetting the current branch to the same commit as the branch you are rebasing onto, and finally applying each change in turn. | |
4139 | + | |
4140 | + .Rebasing the change introduced in `C4` onto `C3` | |
4141 | +-image::images/basic-rebase-3.png[Rebasing the change introduced in `C4` onto `C3`] | |
4142 | ++image::images/basic-rebase-3.png[Rebasing the change introduced in `C4` onto `C3`.] | |
4143 | + | |
4144 | + At this point, you can go back to the `master` branch and do a fast-forward merge. | |
4145 | + | |
4146 | +-[source,console] | |
4147 | +----- | |
4148 | +-$ git checkout master | |
4149 | +-$ git merge experiment | |
4150 | +----- | |
4151 | +- | |
4152 | +-.Fast-forwarding the `master` branch | |
4153 | +-image::images/basic-rebase-4.png[Fast-forwarding the `master` branch] | |
4154 | ++.Fast-forwarding the master branch | |
4155 | ++image::images/basic-rebase-4.png[Fast-forwarding the master branch.] | |
4156 | + | |
4157 | +-Now, the snapshot pointed to by `C4'` is exactly the same as the one that was pointed to by `C5` in <<rebasing-merging-example,the merge example>>. | |
4158 | ++Now, the snapshot pointed to by `C4'` is exactly the same as the one that was pointed to by `C5` in the merge example. | |
4159 | + There is no difference in the end product of the integration, but rebasing makes for a cleaner history. | |
4160 | + If you examine the log of a rebased branch, it looks like a linear history: it appears that all the work happened in series, even when it originally happened in parallel. | |
4161 | + | |
4162 | +-Often, you'll do this to make sure your commits apply cleanly on a remote branch -- perhaps in a project to which you're trying to contribute but that you don't maintain. | |
4163 | ++Often, you'll do this to make sure your commits apply cleanly on a remote branch – perhaps in a project to which you're trying to contribute but that you don't maintain. | |
4164 | + In this case, you'd do your work in a branch and then rebase your work onto `origin/master` when you were ready to submit your patches to the main project. | |
4165 | +-That way, the maintainer doesn't have to do any integration work -- just a fast-forward or a clean apply. | |
4166 | ++That way, the maintainer doesn't have to do any integration work – just a fast-forward or a clean apply. | |
4167 | + | |
4168 | +-Note that the snapshot pointed to by the final commit you end up with, whether it's the last of the rebased commits for a rebase or the final merge commit after a merge, is the same snapshot -- it's only the history that is different. | |
4169 | ++Note that the snapshot pointed to by the final commit you end up with, whether it's the last of the rebased commits for a rebase or the final merge commit after a merge, is the same snapshot – it's only the history that is different. | |
4170 | + Rebasing replays changes from one line of work onto another in the order they were introduced, whereas merging takes the endpoints and merges them together. | |
4171 | + | |
4172 | + ==== More Interesting Rebases | |
4173 | + | |
4174 | + You can also have your rebase replay on something other than the rebase target branch. | |
4175 | +-Take a history like <<rbdiag_e>>, for example. | |
4176 | ++Take a history like <<rrbdiag_e>>, for example. | |
4177 | + You branched a topic branch (`server`) to add some server-side functionality to your project, and made a commit. | |
4178 | + Then, you branched off that to make the client-side changes (`client`) and committed a few times. | |
4179 | + Finally, you went back to your server branch and did a few more commits. | |
4180 | + | |
4181 | +-[[rbdiag_e]] | |
4182 | + .A history with a topic branch off another topic branch | |
4183 | +-image::images/interesting-rebase-1.png[A history with a topic branch off another topic branch] | |
4184 | ++image::images/interesting-rebase-1.png[A history with a topic branch off another topic branch.] | |
4185 | + | |
4186 | + Suppose you decide that you want to merge your client-side changes into your mainline for a release, but you want to hold off on the server-side changes until it's tested further. | |
4187 | +-You can take the changes on `client` that aren't on `server` (`C8` and `C9`) and replay them on your `master` branch by using the `--onto` option of `git rebase`: | |
4188 | ++You can take the changes on client that aren't on server (`C8` and `C9`) and replay them on your `master` branch by using the `--onto` option of `git rebase`: | |
4189 | + | |
4190 | +-[source,console] | |
4191 | +----- | |
4192 | +-$ git rebase --onto master server client | |
4193 | +----- | |
4194 | +- | |
4195 | +-This basically says, ``Take the `client` branch, figure out the patches since it diverged from the `server` branch, and replay these patches in the `client` branch as if it was based directly off the `master` branch instead.'' | |
4196 | ++This basically says, ``Check out the client branch, figure out the patches from the common ancestor of the `client` and `server` branches, and then replay them onto `master`.'' | |
4197 | + It's a bit complex, but the result is pretty cool. | |
4198 | + | |
4199 | + .Rebasing a topic branch off another topic branch | |
4200 | +-image::images/interesting-rebase-2.png[Rebasing a topic branch off another topic branch] | |
4201 | ++image::images/interesting-rebase-2.png[Rebasing a topic branch off another topic branch.] | |
4202 | + | |
4203 | +-Now you can fast-forward your `master` branch (see <<rbdiag_g>>): | |
4204 | ++Now you can fast-forward your `master` branch (see <<rrbdiag_g>>): | |
4205 | + | |
4206 | +-[source,console] | |
4207 | +----- | |
4208 | +-$ git checkout master | |
4209 | +-$ git merge client | |
4210 | +----- | |
4211 | +- | |
4212 | +-[[rbdiag_g]] | |
4213 | +-.Fast-forwarding your `master` branch to include the client branch changes | |
4214 | +-image::images/interesting-rebase-3.png[Fast-forwarding your `master` branch to include the client branch changes] | |
4215 | ++.Fast-forwarding your master branch to include the client branch changes | |
4216 | ++image::images/interesting-rebase-3.png[Fast-forwarding your master branch to include the client branch changes.] | |
4217 | + | |
4218 | + Let's say you decide to pull in your server branch as well. | |
4219 | +-You can rebase the `server` branch onto the `master` branch without having to check it out first by running `git rebase <basebranch> <topicbranch>` -- which checks out the topic branch (in this case, `server`) for you and replays it onto the base branch (`master`): | |
4220 | ++You can rebase the server branch onto the `master` branch without having to check it out first by running `git rebase [basebranch] [topicbranch]` – which checks out the topic branch (in this case, `server`) for you and replays it onto the base branch (`master`): | |
4221 | ++ | |
4222 | ++This replays your `server` work on top of your `master` work, as shown in <<rrbdiag_h>>. | |
4223 | + | |
4224 | +-[source,console] | |
4225 | +----- | |
4226 | +-$ git rebase master server | |
4227 | +----- | |
4228 | +- | |
4229 | +-This replays your `server` work on top of your `master` work, as shown in <<rbdiag_h>>. | |
4230 | +- | |
4231 | +-[[rbdiag_h]] | |
4232 | +-.Rebasing your server branch on top of your `master` branch | |
4233 | +-image::images/interesting-rebase-4.png[Rebasing your server branch on top of your `master` branch] | |
4234 | ++.Rebasing your server branch on top of your master branch | |
4235 | ++image::images/interesting-rebase-4.png[Rebasing your server branch on top of your master branch.] | |
4236 | + | |
4237 | + Then, you can fast-forward the base branch (`master`): | |
4238 | + | |
4239 | +-[source,console] | |
4240 | +----- | |
4241 | +-$ git checkout master | |
4242 | +-$ git merge server | |
4243 | +----- | |
4244 | +- | |
4245 | +-You can remove the `client` and `server` branches because all the work is integrated and you don't need them anymore, leaving your history for this entire process looking like <<rbdiag_i>>: | |
4246 | +- | |
4247 | +-[source,console] | |
4248 | +----- | |
4249 | +-$ git branch -d client | |
4250 | +-$ git branch -d server | |
4251 | +----- | |
4252 | ++You can remove the `client` and `server` branches because all the work is integrated and you don't need them anymore, leaving your history for this entire process looking like <<rrbdiag_i>>: | |
4253 | + | |
4254 | +-[[rbdiag_i]] | |
4255 | + .Final commit history | |
4256 | +-image::images/interesting-rebase-5.png[Final commit history] | |
4257 | ++image::images/interesting-rebase-5.png[Final commit history.] | |
4258 | + | |
4259 | +-[[_rebase_peril]] | |
4260 | + ==== The Perils of Rebasing | |
4261 | + | |
4262 | +-(((rebasing, perils of))) | |
4263 | + Ahh, but the bliss of rebasing isn't without its drawbacks, which can be summed up in a single line: | |
4264 | + | |
4265 | +-*Do not rebase commits that exist outside your repository and that people may have based work on.* | |
4266 | ++*Do not rebase commits that exist outside your repository.* | |
4267 | + | |
4268 | + If you follow that guideline, you'll be fine. | |
4269 | + If you don't, people will hate you, and you'll be scorned by friends and family. | |
4270 | +@@ -162,22 +112,19 @@ | |
4271 | + Next, the person who pushed the merged work decides to go back and rebase their work instead; they do a `git push --force` to overwrite the history on the server. | |
4272 | + You then fetch from that server, bringing down the new commits. | |
4273 | + | |
4274 | +-[[_pre_merge_rebase_work]] | |
4275 | + .Someone pushes rebased commits, abandoning commits you've based your work on | |
4276 | + image::images/perils-of-rebasing-3.png["Someone pushes rebased commits, abandoning commits you've based your work on."] | |
4277 | + | |
4278 | + Now you're both in a pickle. | |
4279 | + If you do a `git pull`, you'll create a merge commit which includes both lines of history, and your repository will look like this: | |
4280 | + | |
4281 | +-[[_merge_rebase_work]] | |
4282 | + .You merge in the same work again into a new merge commit | |
4283 | +-image::images/perils-of-rebasing-4.png[You merge in the same work again into a new merge commit] | |
4284 | ++image::images/perils-of-rebasing-4.png[You merge in the same work again into a new merge commit.] | |
4285 | + | |
4286 | + If you run a `git log` when your history looks like this, you'll see two commits that have the same author, date, and message, which will be confusing. | |
4287 | + Furthermore, if you push this history back up to the server, you'll reintroduce all those rebased commits to the central server, which can further confuse people. | |
4288 | + It's pretty safe to assume that the other developer doesn't want `C4` and `C6` to be in the history; that's why they rebased in the first place. | |
4289 | + | |
4290 | +-[[_rebase_rebase]] | |
4291 | + ==== Rebase When You Rebase | |
4292 | + | |
4293 | + If you *do* find yourself in a situation like this, Git has some further magic that might help you out. | |
4294 | +@@ -188,20 +135,19 @@ | |
4295 | + | |
4296 | + If you pull down work that was rewritten and rebase it on top of the new commits from your partner, Git can often successfully figure out what is uniquely yours and apply them back on top of the new branch. | |
4297 | + | |
4298 | +-For instance, in the previous scenario, if instead of doing a merge when we're at <<_pre_merge_rebase_work>> we run `git rebase teamone/master`, Git will: | |
4299 | ++For instance, in the previous scenario, if instead of doing a merge when we're at <<r_pre_merge_rebase_work>> we run `git rebase teamone/master`, Git will: | |
4300 | + | |
4301 | + * Determine what work is unique to our branch (C2, C3, C4, C6, C7) | |
4302 | + * Determine which are not merge commits (C2, C3, C4) | |
4303 | + * Determine which have not been rewritten into the target branch (just C2 and C3, since C4 is the same patch as C4') | |
4304 | + * Apply those commits to the top of `teamone/master` | |
4305 | + | |
4306 | +-So instead of the result we see in <<_merge_rebase_work>>, we would end up with something more like <<_rebase_rebase_work>>. | |
4307 | ++So instead of the result we see in <<r_merge_rebase_work>>, we would end up with something more like <<r_rebase_rebase_work>>. | |
4308 | + | |
4309 | +-[[_rebase_rebase_work]] | |
4310 | +-.Rebase on top of force-pushed rebase work | |
4311 | +-image::images/perils-of-rebasing-5.png[Rebase on top of force-pushed rebase work] | |
4312 | ++.Rebase on top of force-pushed rebase work. | |
4313 | ++image::images/perils-of-rebasing-5.png[Rebase on top of force-pushed rebase work.] | |
4314 | + | |
4315 | +-This only works if `C4` and `C4'` that your partner made are almost exactly the same patch. | |
4316 | ++This only works if C4 and C4' that your partner made are almost exactly the same patch. | |
4317 | + Otherwise the rebase won't be able to tell that it's a duplicate and will add another C4-like patch (which will probably fail to apply cleanly, since the changes would already be at least somewhat there). | |
4318 | + | |
4319 | + You can also simplify this by running a `git pull --rebase` instead of a normal `git pull`. | |
4320 | +@@ -209,15 +155,13 @@ | |
4321 | + | |
4322 | + If you are using `git pull` and want to make `--rebase` the default, you can set the `pull.rebase` config value with something like `git config --global pull.rebase true`. | |
4323 | + | |
4324 | +-If you only ever rebase commits that have never left your own computer, you'll be just fine. | |
4325 | +-If you rebase commits that have been pushed, but that no one else has based commits from, you'll also be fine. | |
4326 | ++If you treat rebasing as a way to clean up and work with commits before you push them, and if you only rebase commits that have never been available publicly, then you'll be fine. | |
4327 | + If you rebase commits that have already been pushed publicly, and people may have based work on those commits, then you may be in for some frustrating trouble, and the scorn of your teammates. | |
4328 | + | |
4329 | + If you or a partner does find it necessary at some point, make sure everyone knows to run `git pull --rebase` to try to make the pain after it happens a little bit simpler. | |
4330 | + | |
4331 | + ==== Rebase vs. Merge | |
4332 | + | |
4333 | +-(((rebasing, vs. merging)))(((merging, vs. rebasing))) | |
4334 | + Now that you've seen rebasing and merging in action, you may be wondering which one is better. | |
4335 | + Before we can answer this, let's step back a bit and talk about what history means. | |
4336 | + | |
4337 | +@@ -228,13 +172,11 @@ | |
4338 | + That's how it happened, and the repository should preserve that for posterity. | |
4339 | + | |
4340 | + The opposing point of view is that the commit history is the *story of how your project was made.* | |
4341 | +-You wouldn't publish the first draft of a book, so why show your messy work? | |
4342 | +-When you're working on a project, you may need a record of all your missteps and dead-end paths, but when it's time to show your work to the world, you may want to tell a more coherent story of how to get from A to B. | |
4343 | +-People in this camp use tools like rebase and filter-branch to rewrite their commits before they're merged into the mainline branch. | |
4344 | +-They use tools like `rebase` and `filter-branch`, to tell the story in the way that's best for future readers. | |
4345 | ++You wouldn't publish the first draft of a book, and the manual for how to maintain your software deserves careful editing. | |
4346 | ++This is the camp that uses tools like rebase and filter-branch to tell the story in the way that's best for future readers. | |
4347 | + | |
4348 | + Now, to the question of whether merging or rebasing is better: hopefully you'll see that it's not that simple. | |
4349 | + Git is a powerful tool, and allows you to do many things to and with your history, but every team and every project is different. | |
4350 | + Now that you know how both of these things work, it's up to you to decide which one is best for your particular situation. | |
4351 | + | |
4352 | +-You can get the best of both worlds: rebase local changes before pushing to clean up your work, but never rebase anything that you've pushed somewhere. | |
4353 | ++In general the way to get the best of both worlds is to rebase local changes you've made but haven't shared yet before you push them in order to clean up your story, but never rebase anything you've pushed somewhere. | |
4354 | +diff -au ../../git/progit2/progit2/book/03-git-branching/sections/remote-branches.asc /tmp/book/03-git-branching/sections/remote-branches.asc.tmp | |
4355 | +--- ../../git/progit2/progit2/book/03-git-branching/sections/remote-branches.asc 2020-07-04 16:36:46.723685453 +0900 | |
4356 | ++++ /tmp/book/03-git-branching/sections/remote-branches.asc.tmp 2020-10-25 09:24:25.680967847 +0900 | |
4357 | +@@ -1,25 +1,23 @@ | |
4358 | +-[[_remote_branches]] | |
4359 | ++[[r_remote_branches]] | |
4360 | + === Remote Branches | |
4361 | + | |
4362 | +-(((branches, remote)))(((references, remote))) | |
4363 | + Remote references are references (pointers) in your remote repositories, including branches, tags, and so on. | |
4364 | +-You can get a full list of remote references explicitly with `git ls-remote <remote>`, or `git remote show <remote>` for remote branches as well as more information. | |
4365 | ++You can get a full list of remote references explicitly with `git ls-remote [remote]`, or `git remote show [remote]` for remote branches as well as more information. | |
4366 | + Nevertheless, a more common way is to take advantage of remote-tracking branches. | |
4367 | + | |
4368 | + Remote-tracking branches are references to the state of remote branches. | |
4369 | +-They're local references that you can't move; Git moves them for you whenever you do any network communication, to make sure they accurately represent the state of the remote repository. | |
4370 | +-Think of them as bookmarks, to remind you where the branches in your remote repositories were the last time you connected to them. | |
4371 | ++They're local references that you can't move; they're moved automatically for you whenever you do any network communication. | |
4372 | ++Remote-tracking branches act as bookmarks to remind you where the branches in your remote repositories were the last time you connected to them. | |
4373 | + | |
4374 | +-Remote-tracking branch names take the form `<remote>/<branch>`. | |
4375 | ++They take the form `(remote)/(branch)`. | |
4376 | + For instance, if you wanted to see what the `master` branch on your `origin` remote looked like as of the last time you communicated with it, you would check the `origin/master` branch. | |
4377 | +-If you were working on an issue with a partner and they pushed up an `iss53` branch, you might have your own local `iss53` branch, but the branch on the server would be represented by the remote-tracking branch `origin/iss53`. | |
4378 | ++If you were working on an issue with a partner and they pushed up an `iss53` branch, you might have your own local `iss53` branch; but the branch on the server would point to the commit at `origin/iss53`. | |
4379 | + | |
4380 | + This may be a bit confusing, so let's look at an example. | |
4381 | + Let's say you have a Git server on your network at `git.ourcompany.com`. | |
4382 | + If you clone from this, Git's `clone` command automatically names it `origin` for you, pulls down all its data, creates a pointer to where its `master` branch is, and names it `origin/master` locally. | |
4383 | + Git also gives you your own local `master` branch starting at the same place as origin's `master` branch, so you have something to work from. | |
4384 | + | |
4385 | +-[NOTE] | |
4386 | + .``origin'' is not special | |
4387 | + ==== | |
4388 | + Just like the branch name ``master'' does not have any special meaning in Git, neither does ``origin''. | |
4389 | +@@ -31,62 +29,47 @@ | |
4390 | + image::images/remote-branches-1.png[Server and local repositories after cloning.] | |
4391 | + | |
4392 | + If you do some work on your local `master` branch, and, in the meantime, someone else pushes to `git.ourcompany.com` and updates its `master` branch, then your histories move forward differently. | |
4393 | +-Also, as long as you stay out of contact with your `origin` server, your `origin/master` pointer doesn't move. | |
4394 | ++Also, as long as you stay out of contact with your origin server, your `origin/master` pointer doesn't move. | |
4395 | + | |
4396 | + .Local and remote work can diverge | |
4397 | +-image::images/remote-branches-2.png[Local and remote work can diverge] | |
4398 | ++image::images/remote-branches-2.png[Local and remote work can diverge.] | |
4399 | + | |
4400 | +-To synchronize your work with a given remote, you run a `git fetch <remote>` command (in our case, `git fetch origin`). | |
4401 | ++To synchronize your work, you run a `git fetch origin` command. | |
4402 | + This command looks up which server ``origin'' is (in this case, it's `git.ourcompany.com`), fetches any data from it that you don't yet have, and updates your local database, moving your `origin/master` pointer to its new, more up-to-date position. | |
4403 | + | |
4404 | +-.`git fetch` updates your remote-tracking branches | |
4405 | +-image::images/remote-branches-3.png[`git fetch` updates your remote references] | |
4406 | ++.`git fetch` updates your remote references | |
4407 | ++image::images/remote-branches-3.png[`git fetch` updates your remote references.] | |
4408 | + | |
4409 | + To demonstrate having multiple remote servers and what remote branches for those remote projects look like, let's assume you have another internal Git server that is used only for development by one of your sprint teams. | |
4410 | + This server is at `git.team1.ourcompany.com`. | |
4411 | +-You can add it as a new remote reference to the project you're currently working on by running the `git remote add` command as we covered in <<ch02-git-basics-chapter#ch02-git-basics-chapter>>. | |
4412 | ++You can add it as a new remote reference to the project you're currently working on by running the `git remote add` command as we covered in <<ch02-git-basics#ch02-git-basics>>. | |
4413 | + Name this remote `teamone`, which will be your shortname for that whole URL. | |
4414 | + | |
4415 | + .Adding another server as a remote | |
4416 | +-image::images/remote-branches-4.png[Adding another server as a remote] | |
4417 | ++image::images/remote-branches-4.png[Adding another server as a remote.] | |
4418 | + | |
4419 | + Now, you can run `git fetch teamone` to fetch everything the remote `teamone` server has that you don't have yet. | |
4420 | + Because that server has a subset of the data your `origin` server has right now, Git fetches no data but sets a remote-tracking branch called `teamone/master` to point to the commit that `teamone` has as its `master` branch. | |
4421 | + | |
4422 | +-.Remote-tracking branch for `teamone/master` | |
4423 | +-image::images/remote-branches-5.png[Remote tracking branch for `teamone/master`] | |
4424 | ++.Remote tracking branch for `teamone/master` | |
4425 | ++image::images/remote-branches-5.png[Remote tracking branch for `teamone/master`.] | |
4426 | + | |
4427 | +-[[_pushing_branches]] | |
4428 | + ==== Pushing | |
4429 | + | |
4430 | +-(((pushing))) | |
4431 | +-When you want to share a branch with the world, you need to push it up to a remote to which you have write access. | |
4432 | +-Your local branches aren't automatically synchronized to the remotes you write to -- you have to explicitly push the branches you want to share. | |
4433 | ++When you want to share a branch with the world, you need to push it up to a remote that you have write access to. | |
4434 | ++Your local branches aren't automatically synchronized to the remotes you write to – you have to explicitly push the branches you want to share. | |
4435 | + That way, you can use private branches for work you don't want to share, and push up only the topic branches you want to collaborate on. | |
4436 | + | |
4437 | + If you have a branch named `serverfix` that you want to work on with others, you can push it up the same way you pushed your first branch. | |
4438 | + Run `git push <remote> <branch>`:(((git commands, push))) | |
4439 | + | |
4440 | +-[source,console] | |
4441 | +----- | |
4442 | +-$ git push origin serverfix | |
4443 | +-Counting objects: 24, done. | |
4444 | +-Delta compression using up to 8 threads. | |
4445 | +-Compressing objects: 100% (15/15), done. | |
4446 | +-Writing objects: 100% (24/24), 1.91 KiB | 0 bytes/s, done. | |
4447 | +-Total 24 (delta 2), reused 0 (delta 0) | |
4448 | +-To https://github.com/schacon/simplegit | |
4449 | +- * [new branch] serverfix -> serverfix | |
4450 | +----- | |
4451 | +- | |
4452 | + This is a bit of a shortcut. | |
4453 | +-Git automatically expands the `serverfix` branchname out to `refs/heads/serverfix:refs/heads/serverfix`, which means, ``Take my `serverfix` local branch and push it to update the remote's `serverfix` branch.'' | |
4454 | ++Git automatically expands the `serverfix` branchname out to `refs/heads/serverfix:refs/heads/serverfix`, which means, ``Take my serverfix local branch and push it to update the remote's serverfix branch.'' | |
4455 | + We'll go over the `refs/heads/` part in detail in <<ch10-git-internals#ch10-git-internals>>, but you can generally leave it off. | |
4456 | +-You can also do `git push origin serverfix:serverfix`, which does the same thing -- it says, ``Take my serverfix and make it the remote's serverfix.'' | |
4457 | ++You can also do `git push origin serverfix:serverfix`, which does the same thing – it says, ``Take my serverfix and make it the remote's serverfix.'' | |
4458 | + You can use this format to push a local branch into a remote branch that is named differently. | |
4459 | + If you didn't want it to be called `serverfix` on the remote, you could instead run `git push origin serverfix:awesomebranch` to push your local `serverfix` branch to the `awesomebranch` branch on the remote project. | |
4460 | + | |
4461 | +-[NOTE] | |
4462 | + .Don't type your password every time | |
4463 | + ==== | |
4464 | + If you're using an HTTPS URL to push over, the Git server will ask you for your username and password for authentication. | |
4465 | +@@ -95,87 +78,38 @@ | |
4466 | + If you don't want to type it every single time you push, you can set up a ``credential cache''. | |
4467 | + The simplest is just to keep it in memory for a few minutes, which you can easily set up by running `git config --global credential.helper cache`. | |
4468 | + | |
4469 | +-For more information on the various credential caching options available, see <<ch07-git-tools#_credential_caching>>. | |
4470 | ++For more information on the various credential caching options available, see <<ch07-git-tools#r_credential_caching>>. | |
4471 | + ==== | |
4472 | + | |
4473 | + The next time one of your collaborators fetches from the server, they will get a reference to where the server's version of `serverfix` is under the remote branch `origin/serverfix`: | |
4474 | + | |
4475 | +-[source,console] | |
4476 | +----- | |
4477 | +-$ git fetch origin | |
4478 | +-remote: Counting objects: 7, done. | |
4479 | +-remote: Compressing objects: 100% (2/2), done. | |
4480 | +-remote: Total 3 (delta 0), reused 3 (delta 0) | |
4481 | +-Unpacking objects: 100% (3/3), done. | |
4482 | +-From https://github.com/schacon/simplegit | |
4483 | +- * [new branch] serverfix -> origin/serverfix | |
4484 | +----- | |
4485 | +- | |
4486 | + It's important to note that when you do a fetch that brings down new remote-tracking branches, you don't automatically have local, editable copies of them. | |
4487 | +-In other words, in this case, you don't have a new `serverfix` branch -- you have only an `origin/serverfix` pointer that you can't modify. | |
4488 | ++In other words, in this case, you don't have a new `serverfix` branch – you only have an `origin/serverfix` pointer that you can't modify. | |
4489 | + | |
4490 | + To merge this work into your current working branch, you can run `git merge origin/serverfix`. | |
4491 | + If you want your own `serverfix` branch that you can work on, you can base it off your remote-tracking branch: | |
4492 | + | |
4493 | +-[source,console] | |
4494 | +----- | |
4495 | +-$ git checkout -b serverfix origin/serverfix | |
4496 | +-Branch serverfix set up to track remote branch serverfix from origin. | |
4497 | +-Switched to a new branch 'serverfix' | |
4498 | +----- | |
4499 | +- | |
4500 | + This gives you a local branch that you can work on that starts where `origin/serverfix` is. | |
4501 | + | |
4502 | +-[[_tracking_branches]] | |
4503 | + ==== Tracking Branches | |
4504 | + | |
4505 | +-(((branches, tracking)))(((branches, upstream))) | |
4506 | + Checking out a local branch from a remote-tracking branch automatically creates what is called a ``tracking branch'' (and the branch it tracks is called an ``upstream branch''). | |
4507 | + Tracking branches are local branches that have a direct relationship to a remote branch. | |
4508 | +-If you're on a tracking branch and type `git pull`, Git automatically knows which server to fetch from and which branch to merge in. | |
4509 | ++If you're on a tracking branch and type `git pull`, Git automatically knows which server to fetch from and branch to merge into. | |
4510 | + | |
4511 | + When you clone a repository, it generally automatically creates a `master` branch that tracks `origin/master`. | |
4512 | +-However, you can set up other tracking branches if you wish -- ones that track branches on other remotes, or don't track the `master` branch. | |
4513 | +-The simple case is the example you just saw, running `git checkout -b <branch> <remote>/<branch>`. | |
4514 | +-This is a common enough operation that Git provides the `--track` shorthand: | |
4515 | +- | |
4516 | +-[source,console] | |
4517 | +----- | |
4518 | +-$ git checkout --track origin/serverfix | |
4519 | +-Branch serverfix set up to track remote branch serverfix from origin. | |
4520 | +-Switched to a new branch 'serverfix' | |
4521 | +----- | |
4522 | +- | |
4523 | +-In fact, this is so common that there's even a shortcut for that shortcut. | |
4524 | +-If the branch name you're trying to checkout (a) doesn't exist and (b) exactly matches a name on only one remote, Git will create a tracking branch for you: | |
4525 | +- | |
4526 | +-[source,console] | |
4527 | +----- | |
4528 | +-$ git checkout serverfix | |
4529 | +-Branch serverfix set up to track remote branch serverfix from origin. | |
4530 | +-Switched to a new branch 'serverfix' | |
4531 | +----- | |
4532 | ++However, you can set up other tracking branches if you wish – ones that track branches on other remotes, or don't track the `master` branch. | |
4533 | ++The simple case is the example you just saw, running `git checkout -b [branch] [remotename]/[branch]`. | |
4534 | ++This is a common enough operation that git provides the `--track` shorthand: | |
4535 | + | |
4536 | +-To set up a local branch with a different name than the remote branch, you can easily use the first version with a different local branch name: | |
4537 | ++In fact, this is so common that there's even a shortcut for that shortcut. If the branch name you're trying to checkout (a) doesn't exist and (b) exactly matches a name on only one remote, Git will create a tracking branch for you: | |
4538 | + | |
4539 | +-[source,console] | |
4540 | +----- | |
4541 | +-$ git checkout -b sf origin/serverfix | |
4542 | +-Branch sf set up to track remote branch serverfix from origin. | |
4543 | +-Switched to a new branch 'sf' | |
4544 | +----- | |
4545 | ++To set up a local branch with a different name than the remote branch, you can easily use the first version with a different local branch name: | |
4546 | + | |
4547 | + Now, your local branch `sf` will automatically pull from `origin/serverfix`. | |
4548 | + | |
4549 | + If you already have a local branch and want to set it to a remote branch you just pulled down, or want to change the upstream branch you're tracking, you can use the `-u` or `--set-upstream-to` option to `git branch` to explicitly set it at any time. | |
4550 | + | |
4551 | +-[source,console] | |
4552 | +----- | |
4553 | +-$ git branch -u origin/serverfix | |
4554 | +-Branch serverfix set up to track remote branch serverfix from origin. | |
4555 | +----- | |
4556 | +- | |
4557 | +-[NOTE] | |
4558 | + .Upstream shorthand | |
4559 | + ==== | |
4560 | + When you have a tracking branch set up, you can reference its upstream branch with the `@{upstream}` or `@{u}` shorthand. | |
4561 | +@@ -185,15 +119,6 @@ | |
4562 | + If you want to see what tracking branches you have set up, you can use the `-vv` option to `git branch`. | |
4563 | + This will list out your local branches with more information including what each branch is tracking and if your local branch is ahead, behind or both. | |
4564 | + | |
4565 | +-[source,console] | |
4566 | +----- | |
4567 | +-$ git branch -vv | |
4568 | +- iss53 7e424c3 [origin/iss53: ahead 2] Add forgotten brackets | |
4569 | +- master 1ae2a45 [origin/master] Deploy index fix | |
4570 | +-* serverfix f8674d9 [teamone/server-fix-good: ahead 3, behind 1] This should do it | |
4571 | +- testing 5ea463a Try something new | |
4572 | +----- | |
4573 | +- | |
4574 | + So here we can see that our `iss53` branch is tracking `origin/iss53` and is ``ahead'' by two, meaning that we have two commits locally that are not pushed to the server. | |
4575 | + We can also see that our `master` branch is tracking `origin/master` and is up to date. | |
4576 | + Next we can see that our `serverfix` branch is tracking the `server-fix-good` branch on our `teamone` server and is ahead by three and behind by one, meaning that there is one commit on the server we haven't merged in yet and three commits locally that we haven't pushed. | |
4577 | +@@ -202,37 +127,22 @@ | |
4578 | + It's important to note that these numbers are only since the last time you fetched from each server. | |
4579 | + This command does not reach out to the servers, it's telling you about what it has cached from these servers locally. | |
4580 | + If you want totally up to date ahead and behind numbers, you'll need to fetch from all your remotes right before running this. | |
4581 | +-You could do that like this: | |
4582 | +- | |
4583 | +-[source,console] | |
4584 | +----- | |
4585 | +-$ git fetch --all; git branch -vv | |
4586 | +----- | |
4587 | ++You could do that like this: `git fetch --all; git branch -vv` | |
4588 | + | |
4589 | + ==== Pulling | |
4590 | + | |
4591 | +-(((pulling))) | |
4592 | +-While the `git fetch` command will fetch all the changes on the server that you don't have yet, it will not modify your working directory at all. | |
4593 | ++While the `git fetch` command will fetch down all the changes on the server that you don't have yet, it will not modify your working directory at all. | |
4594 | + It will simply get the data for you and let you merge it yourself. | |
4595 | + However, there is a command called `git pull` which is essentially a `git fetch` immediately followed by a `git merge` in most cases. | |
4596 | + If you have a tracking branch set up as demonstrated in the last section, either by explicitly setting it or by having it created for you by the `clone` or `checkout` commands, `git pull` will look up what server and branch your current branch is tracking, fetch from that server and then try to merge in that remote branch. | |
4597 | + | |
4598 | + Generally it's better to simply use the `fetch` and `merge` commands explicitly as the magic of `git pull` can often be confusing. | |
4599 | + | |
4600 | +-[[_delete_branches]] | |
4601 | + ==== Deleting Remote Branches | |
4602 | + | |
4603 | +-(((branches, deleting remote))) | |
4604 | +-Suppose you're done with a remote branch -- say you and your collaborators are finished with a feature and have merged it into your remote's `master` branch (or whatever branch your stable codeline is in). | |
4605 | ++Suppose you're done with a remote branch – say you and your collaborators are finished with a feature and have merged it into your remote's `master` branch (or whatever branch your stable codeline is in). | |
4606 | + You can delete a remote branch using the `--delete` option to `git push`. | |
4607 | + If you want to delete your `serverfix` branch from the server, you run the following: | |
4608 | + | |
4609 | +-[source,console] | |
4610 | +----- | |
4611 | +-$ git push origin --delete serverfix | |
4612 | +-To https://github.com/schacon/simplegit | |
4613 | +- - [deleted] serverfix | |
4614 | +----- | |
4615 | +- | |
4616 | + Basically all this does is remove the pointer from the server. | |
4617 | + The Git server will generally keep the data there for a while until a garbage collection runs, so if it was accidentally deleted, it's often easy to recover. | |
4618 | +diff -au ../../git/progit2/progit2/book/03-git-branching/sections/workflows.asc /tmp/book/03-git-branching/sections/workflows.asc.tmp | |
4619 | +--- ../../git/progit2/progit2/book/03-git-branching/sections/workflows.asc 2020-07-04 16:36:46.723685453 +0900 | |
4620 | ++++ /tmp/book/03-git-branching/sections/workflows.asc.tmp 2020-10-25 09:24:25.686968130 +0900 | |
4621 | +@@ -1,39 +1,35 @@ | |
4622 | + === Branching Workflows | |
4623 | + | |
4624 | + Now that you have the basics of branching and merging down, what can or should you do with them? | |
4625 | +-In this section, we'll cover some common workflows that this lightweight branching makes possible, so you can decide if you would like to incorporate them into your own development cycle. | |
4626 | ++In this section, we'll cover some common workflows that this lightweight branching makes possible, so you can decide if you would like to incorporate it into your own development cycle. | |
4627 | + | |
4628 | + ==== Long-Running Branches | |
4629 | + | |
4630 | +-(((branches, long-running))) | |
4631 | + Because Git uses a simple three-way merge, merging from one branch into another multiple times over a long period is generally easy to do. | |
4632 | + This means you can have several branches that are always open and that you use for different stages of your development cycle; you can merge regularly from some of them into others. | |
4633 | + | |
4634 | +-Many Git developers have a workflow that embraces this approach, such as having only code that is entirely stable in their `master` branch -- possibly only code that has been or will be released. | |
4635 | +-They have another parallel branch named `develop` or `next` that they work from or use to test stability -- it isn't necessarily always stable, but whenever it gets to a stable state, it can be merged into `master`. | |
4636 | ++Many Git developers have a workflow that embraces this approach, such as having only code that is entirely stable in their `master` branch – possibly only code that has been or will be released. | |
4637 | ++They have another parallel branch named `develop` or `next` that they work from or use to test stability – it isn't necessarily always stable, but whenever it gets to a stable state, it can be merged into `master`. | |
4638 | + It's used to pull in topic branches (short-lived branches, like your earlier `iss53` branch) when they're ready, to make sure they pass all the tests and don't introduce bugs. | |
4639 | + | |
4640 | + In reality, we're talking about pointers moving up the line of commits you're making. | |
4641 | + The stable branches are farther down the line in your commit history, and the bleeding-edge branches are farther up the history. | |
4642 | + | |
4643 | + .A linear view of progressive-stability branching | |
4644 | +-image::images/lr-branches-1.png[A linear view of progressive-stability branching] | |
4645 | ++image::images/lr-branches-1.png[A linear view of progressive-stability branching.] | |
4646 | + | |
4647 | + It's generally easier to think about them as work silos, where sets of commits graduate to a more stable silo when they're fully tested. | |
4648 | + | |
4649 | +-[[lrbranch_b]] | |
4650 | + .A ``silo'' view of progressive-stability branching | |
4651 | +-image::images/lr-branches-2.png[A ``silo'' view of progressive-stability branching] | |
4652 | ++image::images/lr-branches-2.png[A ``silo'' view of progressive-stability branching.] | |
4653 | + | |
4654 | + You can keep doing this for several levels of stability. | |
4655 | + Some larger projects also have a `proposed` or `pu` (proposed updates) branch that has integrated branches that may not be ready to go into the `next` or `master` branch. | |
4656 | + The idea is that your branches are at various levels of stability; when they reach a more stable level, they're merged into the branch above them. | |
4657 | + Again, having multiple long-running branches isn't necessary, but it's often helpful, especially when you're dealing with very large or complex projects. | |
4658 | + | |
4659 | +-[[_topic_branch]] | |
4660 | + ==== Topic Branches | |
4661 | + | |
4662 | +-(((branches, topic))) | |
4663 | + Topic branches, however, are useful in projects of any size. | |
4664 | + A topic branch is a short-lived branch that you create and use for a single particular feature or related work. | |
4665 | + This is something you've likely never done with a VCS before because it's generally too expensive to create and merge branches. | |
4666 | +@@ -41,23 +37,23 @@ | |
4667 | + | |
4668 | + You saw this in the last section with the `iss53` and `hotfix` branches you created. | |
4669 | + You did a few commits on them and deleted them directly after merging them into your main branch. | |
4670 | +-This technique allows you to context-switch quickly and completely -- because your work is separated into silos where all the changes in that branch have to do with that topic, it's easier to see what has happened during code review and such. | |
4671 | ++This technique allows you to context-switch quickly and completely – because your work is separated into silos where all the changes in that branch have to do with that topic, it's easier to see what has happened during code review and such. | |
4672 | + You can keep the changes there for minutes, days, or months, and merge them in when they're ready, regardless of the order in which they were created or worked on. | |
4673 | + | |
4674 | + Consider an example of doing some work (on `master`), branching off for an issue (`iss91`), working on it for a bit, branching off the second branch to try another way of handling the same thing (`iss91v2`), going back to your `master` branch and working there for a while, and then branching off there to do some work that you're not sure is a good idea (`dumbidea` branch). | |
4675 | + Your commit history will look something like this: | |
4676 | + | |
4677 | + .Multiple topic branches | |
4678 | +-image::images/topic-branches-1.png[Multiple topic branches] | |
4679 | ++image::images/topic-branches-1.png[Multiple topic branches.] | |
4680 | + | |
4681 | + Now, let's say you decide you like the second solution to your issue best (`iss91v2`); and you showed the `dumbidea` branch to your coworkers, and it turns out to be genius. | |
4682 | + You can throw away the original `iss91` branch (losing commits `C5` and `C6`) and merge in the other two. | |
4683 | + Your history then looks like this: | |
4684 | + | |
4685 | + .History after merging `dumbidea` and `iss91v2` | |
4686 | +-image::images/topic-branches-2.png[History after merging `dumbidea` and `iss91v2`] | |
4687 | ++image::images/topic-branches-2.png[History after merging `dumbidea` and `iss91v2`.] | |
4688 | + | |
4689 | + We will go into more detail about the various possible workflows for your Git project in <<ch05-distributed-git#ch05-distributed-git>>, so before you decide which branching scheme your next project will use, be sure to read that chapter. | |
4690 | + | |
4691 | + It's important to remember when you're doing all this that these branches are completely local. | |
4692 | +-When you're branching and merging, everything is being done only in your Git repository -- there is no communication with the server. | |
4693 | ++When you're branching and merging, everything is being done only in your Git repository – no server communication is happening. | |
4694 | +diff -au ../../git/progit2/progit2/book/04-git-server/sections/generating-ssh-key.asc /tmp/book/04-git-server/sections/generating-ssh-key.asc.tmp | |
4695 | +--- ../../git/progit2/progit2/book/04-git-server/sections/generating-ssh-key.asc 2020-08-05 13:56:11.288918669 +0900 | |
4696 | ++++ /tmp/book/04-git-server/sections/generating-ssh-key.asc.tmp 2020-10-25 09:24:25.690968318 +0900 | |
4697 | +@@ -1,57 +1,21 @@ | |
4698 | +-[[_generate_ssh_key]] | |
4699 | ++[[r_generate_ssh_key]] | |
4700 | + === Generating Your SSH Public Key | |
4701 | + | |
4702 | +-(((SSH keys))) | |
4703 | +-Many Git servers authenticate using SSH public keys. | |
4704 | ++That being said, many Git servers authenticate using SSH public keys. | |
4705 | + In order to provide a public key, each user in your system must generate one if they don't already have one. | |
4706 | + This process is similar across all operating systems. | |
4707 | + First, you should check to make sure you don't already have a key. | |
4708 | + By default, a user's SSH keys are stored in that user's `~/.ssh` directory. | |
4709 | + You can easily check to see if you have a key already by going to that directory and listing the contents: | |
4710 | + | |
4711 | +-[source,console] | |
4712 | +----- | |
4713 | +-$ cd ~/.ssh | |
4714 | +-$ ls | |
4715 | +-authorized_keys2 id_dsa known_hosts | |
4716 | +-config id_dsa.pub | |
4717 | +----- | |
4718 | +- | |
4719 | + You're looking for a pair of files named something like `id_dsa` or `id_rsa` and a matching file with a `.pub` extension. | |
4720 | +-The `.pub` file is your public key, and the other file is the corresponding private key. | |
4721 | +-If you don't have these files (or you don't even have a `.ssh` directory), you can create them by running a program called `ssh-keygen`, which is provided with the SSH package on Linux/macOS systems and comes with Git for Windows: | |
4722 | +- | |
4723 | +-[source,console] | |
4724 | +----- | |
4725 | +-$ ssh-keygen -o | |
4726 | +-Generating public/private rsa key pair. | |
4727 | +-Enter file in which to save the key (/home/schacon/.ssh/id_rsa): | |
4728 | +-Created directory '/home/schacon/.ssh'. | |
4729 | +-Enter passphrase (empty for no passphrase): | |
4730 | +-Enter same passphrase again: | |
4731 | +-Your identification has been saved in /home/schacon/.ssh/id_rsa. | |
4732 | +-Your public key has been saved in /home/schacon/.ssh/id_rsa.pub. | |
4733 | +-The key fingerprint is: | |
4734 | +-d0:82:24:8e:d7:f1:bb:9b:33:53:96:93:49:da:9b:e3 schacon@mylaptop.local | |
4735 | +----- | |
4736 | ++The `.pub` file is your public key, and the other file is your private key. | |
4737 | ++If you don't have these files (or you don't even have a `.ssh` directory), you can create them by running a program called `ssh-keygen`, which is provided with the SSH package on Linux/Mac systems and comes with Git for Windows: | |
4738 | + | |
4739 | + First it confirms where you want to save the key (`.ssh/id_rsa`), and then it asks twice for a passphrase, which you can leave empty if you don't want to type a password when you use the key. | |
4740 | +-However, if you do use a password, make sure to add the `-o` option; it saves the private key in a format that is more resistant to brute-force password cracking than is the default format. | |
4741 | +-You can also use the `ssh-agent` tool to prevent having to enter the password each time. | |
4742 | + | |
4743 | + Now, each user that does this has to send their public key to you or whoever is administrating the Git server (assuming you're using an SSH server setup that requires public keys). | |
4744 | + All they have to do is copy the contents of the `.pub` file and email it. | |
4745 | + The public keys look something like this: | |
4746 | + | |
4747 | +-[source,console] | |
4748 | +----- | |
4749 | +-$ cat ~/.ssh/id_rsa.pub | |
4750 | +-ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSU | |
4751 | +-GPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3 | |
4752 | +-Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XA | |
4753 | +-t3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/En | |
4754 | +-mZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbx | |
4755 | +-NrRFi9wrf+M7Q== schacon@mylaptop.local | |
4756 | +----- | |
4757 | +- | |
4758 | +-For a more in-depth tutorial on creating an SSH key on multiple operating systems, see the GitHub guide on SSH keys at https://docs.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent[]. | |
4759 | ++For a more in-depth tutorial on creating an SSH key on multiple operating systems, see the GitHub guide on SSH keys at https://help.github.com/articles/generating-ssh-keys[]. | |
4760 | +diff -au ../../git/progit2/progit2/book/04-git-server/sections/git-daemon.asc /tmp/book/04-git-server/sections/git-daemon.asc.tmp | |
4761 | +--- ../../git/progit2/progit2/book/04-git-server/sections/git-daemon.asc 2020-06-28 16:44:50.925015352 +0900 | |
4762 | ++++ /tmp/book/04-git-server/sections/git-daemon.asc.tmp 2020-10-25 09:24:25.694968507 +0900 | |
4763 | +@@ -1,66 +1,33 @@ | |
4764 | + === Git Daemon | |
4765 | + | |
4766 | +-(((serving repositories, git protocol))) | |
4767 | +-Next we'll set up a daemon serving repositories using the ``Git'' protocol. | |
4768 | +-This is a common choice for fast, unauthenticated access to your Git data. | |
4769 | +-Remember that since this is not an authenticated service, anything you serve over this protocol is public within its network. | |
4770 | ++Next we'll set up a daemon serving repositories over the ``Git'' protocol. | |
4771 | ++This is common choice for fast, unauthenticated access to your Git data. | |
4772 | ++Remember that since it's not an authenticated service, anything you serve over this protocol is public within its network. | |
4773 | + | |
4774 | +-If you're running this on a server outside your firewall, it should be used only for projects that are publicly visible to the world. | |
4775 | ++If you're running this on a server outside your firewall, it should only be used for projects that are publicly visible to the world. | |
4776 | + If the server you're running it on is inside your firewall, you might use it for projects that a large number of people or computers (continuous integration or build servers) have read-only access to, when you don't want to have to add an SSH key for each. | |
4777 | + | |
4778 | + In any case, the Git protocol is relatively easy to set up. | |
4779 | + Basically, you need to run this command in a daemonized manner:(((git commands, daemon))) | |
4780 | + | |
4781 | +-[source,console] | |
4782 | +----- | |
4783 | +-$ git daemon --reuseaddr --base-path=/srv/git/ /srv/git/ | |
4784 | +----- | |
4785 | +- | |
4786 | +-The `--reuseaddr` option allows the server to restart without waiting for old connections to time out, while the `--base-path` option allows people to clone projects without specifying the entire path, and the path at the end tells the Git daemon where to look for repositories to export. | |
4787 | ++`--reuseaddr` allows the server to restart without waiting for old connections to time out, the `--base-path` option allows people to clone projects without specifying the entire path, and the path at the end tells the Git daemon where to look for repositories to export. | |
4788 | + If you're running a firewall, you'll also need to punch a hole in it at port 9418 on the box you're setting this up on. | |
4789 | + | |
4790 | + You can daemonize this process a number of ways, depending on the operating system you're running. | |
4791 | ++On an Ubuntu machine, you can use an Upstart script. | |
4792 | ++So, in the following file | |
4793 | + | |
4794 | +-Since `systemd` is the most common init system among modern Linux distributions, you can use it for that purpose. | |
4795 | +-Simply place a file in `/etc/systemd/system/git-daemon.service` with these contents: | |
4796 | +- | |
4797 | +-[source,console] | |
4798 | +----- | |
4799 | +-[Unit] | |
4800 | +-Description=Start Git Daemon | |
4801 | +- | |
4802 | +-[Service] | |
4803 | +-ExecStart=/usr/bin/git daemon --reuseaddr --base-path=/srv/git/ /srv/git/ | |
4804 | +- | |
4805 | +-Restart=always | |
4806 | +-RestartSec=500ms | |
4807 | ++you put this script: | |
4808 | + | |
4809 | +-StandardOutput=syslog | |
4810 | +-StandardError=syslog | |
4811 | +-SyslogIdentifier=git-daemon | |
4812 | ++For security reasons, it is strongly encouraged to have this daemon run as a user with read-only permissions to the repositories – you can easily do this by creating a new user 'git-ro' and running the daemon as them. | |
4813 | ++For the sake of simplicity we'll simply run it as the same 'git' user that `git-shell` is running as. | |
4814 | + | |
4815 | +-User=git | |
4816 | +-Group=git | |
4817 | ++When you restart your machine, your Git daemon will start automatically and respawn if it goes down. | |
4818 | ++To get it running without having to reboot, you can run this: | |
4819 | + | |
4820 | +-[Install] | |
4821 | +-WantedBy=multi-user.target | |
4822 | +----- | |
4823 | +- | |
4824 | +-You might have noticed that Git daemon is started here with `git` as both group and user. | |
4825 | +-Modify it to fit your needs and make sure the provided user exists on the system. | |
4826 | +-Also, check that the Git binary is indeed located at `/usr/bin/git` and change the path if necessary. | |
4827 | +- | |
4828 | +-Finally, you'll run `systemctl enable git-daemon` to automatically start the service on boot, and can start and stop the service with, respectively, `systemctl start git-daemon` and `systemctl stop git-daemon`. | |
4829 | +- | |
4830 | +-On other systems, you may want to use `xinetd`, a script in your `sysvinit` system, or something else -- as long as you get that command daemonized and watched somehow. | |
4831 | ++On other systems, you may want to use `xinetd`, a script in your `sysvinit` system, or something else – as long as you get that command daemonized and watched somehow. | |
4832 | + | |
4833 | + Next, you have to tell Git which repositories to allow unauthenticated Git server-based access to. | |
4834 | + You can do this in each repository by creating a file named `git-daemon-export-ok`. | |
4835 | + | |
4836 | +-[source,console] | |
4837 | +----- | |
4838 | +-$ cd /path/to/project.git | |
4839 | +-$ touch git-daemon-export-ok | |
4840 | +----- | |
4841 | +- | |
4842 | + The presence of that file tells Git that it's OK to serve this project without authentication. | |
4843 | +diff -au ../../git/progit2/progit2/book/04-git-server/sections/git-on-a-server.asc /tmp/book/04-git-server/sections/git-on-a-server.asc.tmp | |
4844 | +--- ../../git/progit2/progit2/book/04-git-server/sections/git-on-a-server.asc 2020-07-04 16:36:46.723685453 +0900 | |
4845 | ++++ /tmp/book/04-git-server/sections/git-on-a-server.asc.tmp 2020-10-25 09:24:25.700968789 +0900 | |
4846 | +@@ -1,74 +1,42 @@ | |
4847 | +-[[_getting_git_on_a_server]] | |
4848 | ++[[r_git_on_the_server]] | |
4849 | + === Getting Git on a Server | |
4850 | + | |
4851 | + Now we'll cover setting up a Git service running these protocols on your own server. | |
4852 | + | |
4853 | +-[NOTE] | |
4854 | + ==== | |
4855 | +-Here we'll be demonstrating the commands and steps needed to do basic, simplified installations on a Linux-based server, though it's also possible to run these services on macOS or Windows servers. | |
4856 | ++Here we'll be demonstrating the commands and steps needed to do basic, simplified installations on a Linux based server, though it's also possible to run these services on Mac or Windows servers. | |
4857 | + Actually setting up a production server within your infrastructure will certainly entail differences in security measures or operating system tools, but hopefully this will give you the general idea of what's involved. | |
4858 | + ==== | |
4859 | + | |
4860 | +-In order to initially set up any Git server, you have to export an existing repository into a new bare repository -- a repository that doesn't contain a working directory. | |
4861 | ++In order to initially set up any Git server, you have to export an existing repository into a new bare repository – a repository that doesn't contain a working directory. | |
4862 | + This is generally straightforward to do. | |
4863 | + In order to clone your repository to create a new bare repository, you run the clone command with the `--bare` option.(((git commands, clone, bare))) | |
4864 | +-By convention, bare repository directory names end with the suffix `.git`, like so: | |
4865 | +- | |
4866 | +-[source,console] | |
4867 | +----- | |
4868 | +-$ git clone --bare my_project my_project.git | |
4869 | +-Cloning into bare repository 'my_project.git'... | |
4870 | +-done. | |
4871 | +----- | |
4872 | ++By convention, bare repository directories end in `.git`, like so: | |
4873 | + | |
4874 | + You should now have a copy of the Git directory data in your `my_project.git` directory. | |
4875 | + | |
4876 | +-This is roughly equivalent to something like: | |
4877 | +- | |
4878 | +-[source,console] | |
4879 | +----- | |
4880 | +-$ cp -Rf my_project/.git my_project.git | |
4881 | +----- | |
4882 | ++This is roughly equivalent to something like | |
4883 | + | |
4884 | +-There are a couple of minor differences in the configuration file but, for your purpose, this is close to the same thing. | |
4885 | ++There are a couple of minor differences in the configuration file; but for your purpose, this is close to the same thing. | |
4886 | + It takes the Git repository by itself, without a working directory, and creates a directory specifically for it alone. | |
4887 | + | |
4888 | +-[[_bare_repo]] | |
4889 | + ==== Putting the Bare Repository on a Server | |
4890 | + | |
4891 | + Now that you have a bare copy of your repository, all you need to do is put it on a server and set up your protocols. | |
4892 | +-Let's say you've set up a server called `git.example.com` to which you have SSH access, and you want to store all your Git repositories under the `/srv/git` directory. | |
4893 | +-Assuming that `/srv/git` exists on that server, you can set up your new repository by copying your bare repository over: | |
4894 | ++Let's say you've set up a server called `git.example.com` that you have SSH access to, and you want to store all your Git repositories under the `/opt/git` directory. | |
4895 | ++Assuming that `/opt/git` exists on that server, you can set up your new repository by copying your bare repository over: | |
4896 | ++ | |
4897 | ++At this point, other users who have SSH access to the same server which has read-access to the `/opt/git` directory can clone your repository by running | |
4898 | ++ | |
4899 | ++If a user SSHs into a server and has write access to the `/opt/git/my_project.git` directory, they will also automatically have push access. | |
4900 | + | |
4901 | +-[source,console] | |
4902 | +----- | |
4903 | +-$ scp -r my_project.git user@git.example.com:/srv/git | |
4904 | +----- | |
4905 | +- | |
4906 | +-At this point, other users who have SSH-based read access to the `/srv/git` directory on that server can clone your repository by running: | |
4907 | +- | |
4908 | +-[source,console] | |
4909 | +----- | |
4910 | +-$ git clone user@git.example.com:/srv/git/my_project.git | |
4911 | +----- | |
4912 | +- | |
4913 | +-If a user SSHs into a server and has write access to the `/srv/git/my_project.git` directory, they will also automatically have push access. | |
4914 | +- | |
4915 | +-Git will automatically add group write permissions to a repository properly if you run the `git init` command with the `--shared` option. | |
4916 | +-Note that by running this command, you will not destroy any commits, refs, etc. in the process.(((git commands, init, bare))) | |
4917 | +- | |
4918 | +-[source,console] | |
4919 | +----- | |
4920 | +-$ ssh user@git.example.com | |
4921 | +-$ cd /srv/git/my_project.git | |
4922 | +-$ git init --bare --shared | |
4923 | +----- | |
4924 | ++Git will automatically add group write permissions to a repository properly if you run the `git init` command with the `--shared` option.(((git commands, init, bare))) | |
4925 | + | |
4926 | + You see how easy it is to take a Git repository, create a bare version, and place it on a server to which you and your collaborators have SSH access. | |
4927 | + Now you're ready to collaborate on the same project. | |
4928 | + | |
4929 | +-It's important to note that this is literally all you need to do to run a useful Git server to which several people have access -- just add SSH-able accounts on a server, and stick a bare repository somewhere that all those users have read and write access to. | |
4930 | +-You're ready to go -- nothing else needed. | |
4931 | ++It's important to note that this is literally all you need to do to run a useful Git server to which several people have access – just add SSH-able accounts on a server, and stick a bare repository somewhere that all those users have read and write access to. | |
4932 | ++You're ready to go – nothing else needed. | |
4933 | + | |
4934 | + In the next few sections, you'll see how to expand to more sophisticated setups. | |
4935 | + This discussion will include not having to create user accounts for each user, adding public read access to repositories, setting up web UIs and more. | |
4936 | +@@ -78,24 +46,23 @@ | |
4937 | + | |
4938 | + If you're a small outfit or are just trying out Git in your organization and have only a few developers, things can be simple for you. | |
4939 | + One of the most complicated aspects of setting up a Git server is user management. | |
4940 | +-If you want some repositories to be read-only for certain users and read/write for others, access and permissions can be a bit more difficult to arrange. | |
4941 | ++If you want some repositories to be read-only to certain users and read/write to others, access and permissions can be a bit more difficult to arrange. | |
4942 | + | |
4943 | + ===== SSH Access | |
4944 | + | |
4945 | +-(((serving repositories, SSH))) | |
4946 | + If you have a server to which all your developers already have SSH access, it's generally easiest to set up your first repository there, because you have to do almost no work (as we covered in the last section). | |
4947 | +-If you want more complex access control type permissions on your repositories, you can handle them with the normal filesystem permissions of your server's operating system. | |
4948 | ++If you want more complex access control type permissions on your repositories, you can handle them with the normal filesystem permissions of the operating system your server runs. | |
4949 | + | |
4950 | +-If you want to place your repositories on a server that doesn't have accounts for everyone on your team for whom you want to grant write access, then you must set up SSH access for them. | |
4951 | ++If you want to place your repositories on a server that doesn't have accounts for everyone on your team whom you want to have write access, then you must set up SSH access for them. | |
4952 | + We assume that if you have a server with which to do this, you already have an SSH server installed, and that's how you're accessing the server. | |
4953 | + | |
4954 | + There are a few ways you can give access to everyone on your team. | |
4955 | + The first is to set up accounts for everybody, which is straightforward but can be cumbersome. | |
4956 | +-You may not want to run `adduser` (or the possible alternative `useradd`) and have to set temporary passwords for every new user. | |
4957 | ++You may not want to run `adduser` and set temporary passwords for every user. | |
4958 | + | |
4959 | +-A second method is to create a single 'git' user account on the machine, ask every user who is to have write access to send you an SSH public key, and add that key to the `~/.ssh/authorized_keys` file of that new 'git' account. | |
4960 | +-At that point, everyone will be able to access that machine via the 'git' account. | |
4961 | +-This doesn't affect the commit data in any way -- the SSH user you connect as doesn't affect the commits you've recorded. | |
4962 | ++A second method is to create a single 'git' user on the machine, ask every user who is to have write access to send you an SSH public key, and add that key to the `~/.ssh/authorized_keys` file of your new 'git' user. | |
4963 | ++At that point, everyone will be able to access that machine via the 'git' user. | |
4964 | ++This doesn't affect the commit data in any way – the SSH user you connect as doesn't affect the commits you've recorded. | |
4965 | + | |
4966 | + Another way to do it is to have your SSH server authenticate from an LDAP server or some other centralized authentication source that you may already have set up. | |
4967 | + As long as each user can get shell access on the machine, any SSH authentication mechanism you can think of should work. | |
4968 | +diff -au ../../git/progit2/progit2/book/04-git-server/sections/gitlab.asc /tmp/book/04-git-server/sections/gitlab.asc.tmp | |
4969 | +--- ../../git/progit2/progit2/book/04-git-server/sections/gitlab.asc 2020-09-07 09:33:11.247683703 +0900 | |
4970 | ++++ /tmp/book/04-git-server/sections/gitlab.asc.tmp 2020-10-25 09:24:25.706969072 +0900 | |
4971 | +@@ -2,63 +2,60 @@ | |
4972 | + | |
4973 | + (((serving repositories, GitLab)))(((GitLab))) | |
4974 | + GitWeb is pretty simplistic though. | |
4975 | +-If you're looking for a modern, fully featured Git server, there are several open source solutions out there that you can install instead. | |
4976 | +-As GitLab is one of the popular ones, we'll cover installing and using it as an example. | |
4977 | +-This is harder than the GitWeb option and will require more maintenance, but it is a fully featured option. | |
4978 | ++If you're looking for a more modern, fully featured Git server, there are some several open source solutions out there that you can install instead. | |
4979 | ++As GitLab is one of the more popular ones, we'll cover installing and using it as an example. | |
4980 | ++This is a bit more complex than the GitWeb option and likely requires more maintenance, but it is a much more fully featured option. | |
4981 | + | |
4982 | + ==== Installation | |
4983 | + | |
4984 | +-GitLab is a database-backed web application, so its installation is more involved than some other Git servers. | |
4985 | +-Fortunately, this process is well-documented and supported. | |
4986 | +-GitLab strongly recommends installing GitLab on your server via the official Omnibus GitLab package. | |
4987 | +- | |
4988 | +-The other installation options are: | |
4989 | +- | |
4990 | +-* GitLab Helm chart, for use with Kubernetes. | |
4991 | +-* Dockerized GitLab packages for use with Docker. | |
4992 | +-* From the source files. | |
4993 | +-* Cloud provider such as AWS, Google Cloud Platform, Azure, OpenShift and Digital Ocean. | |
4994 | ++GitLab is a database-backed web application, so its installation is a bit more involved than some other git servers. | |
4995 | ++Fortunately, this process is very well-documented and supported. | |
4996 | + | |
4997 | +-For more information read the https://gitlab.com/gitlab-org/gitlab-foss/-/blob/master/README.md[GitLab Community Edition (CE) readme]. | |
4998 | ++There are a few methods you can pursue to install GitLab. | |
4999 | ++To get something up and running quickly, you can download a virtual machine image or a one-click installer from https://bitnami.com/stack/gitlab[], and tweak the configuration to match your particular environment.(((bitnami))) | |
5000 | ++One nice touch Bitnami has included is the login screen (accessed by typing alt-→); it tells you the IP address and default username and password for the installed GitLab. | |
5001 | ++ | |
5002 | ++.The Bitnami GitLab virtual machine login screen. | |
5003 | ++image::images/bitnami.png[The Bitnami GitLab virtual machine login screen.] | |
5004 | ++ | |
5005 | ++For anything else, follow the guidance in the GitLab Community Edition readme, which can be found at https://gitlab.com/gitlab-org/gitlab-ce/tree/master[]. | |
5006 | ++There you'll find assistance for installing GitLab using Chef recipes, a virtual machine on Digital Ocean, and RPM and DEB packages (which, as of this writing, are in beta). | |
5007 | ++There's also ``unofficial'' guidance on getting GitLab running with non-standard operating systems and databases, a fully-manual installation script, and many other topics. | |
5008 | + | |
5009 | + ==== Administration | |
5010 | + | |
5011 | + GitLab's administration interface is accessed over the web. | |
5012 | +-Simply point your browser to the hostname or IP address where GitLab is installed, and log in as the admin user. | |
5013 | +-The default username is `admin@local.host`, and the default password is `5iveL!fe` (which you must change right away). | |
5014 | +-After you've logged in, click the ``Admin area'' icon in the menu at the top right. | |
5015 | +- | |
5016 | +-[[gitlab_menu]] | |
5017 | +-.The ``Admin area'' item in the GitLab menu | |
5018 | +-image::images/gitlab-menu.png[The ``Admin area'' item in the GitLab menu] | |
5019 | ++Simply point your browser to the hostname or IP address where GitLab is installed, and log in as an admin user. | |
5020 | ++The default username is `admin@local.host`, and the default password is `5iveL!fe` (which you will be prompted to change as soon as you enter it). | |
5021 | ++Once logged in, click the ``Admin area'' icon in the menu at the top right. | |
5022 | ++ | |
5023 | ++.The ``Admin area'' item in the GitLab menu. | |
5024 | ++image::images/gitlab-menu.png[The ``Admin area'' item in the GitLab menu.] | |
5025 | + | |
5026 | + ===== Users | |
5027 | + | |
5028 | +-Everybody using your GitLab server must have an user account. | |
5029 | +-User accounts are quite simple, they mainly contain personal information attached to login data. | |
5030 | +-Each user account has a *namespace*, which is a logical grouping of projects that belong to that user. | |
5031 | +-If the user +jane+ had a project named +project+, that project's url would be `http://server/jane/project`. | |
5032 | +- | |
5033 | +-[[gitlab_users]] | |
5034 | +-.The GitLab user administration screen | |
5035 | +-image::images/gitlab-users.png[The GitLab user administration screen] | |
5036 | ++Users in GitLab are accounts that correspond to people. | |
5037 | ++User accounts don't have a lot of complexity; mainly it's a collection of personal information attached to login data. | |
5038 | ++Each user account comes with a *namespace*, which is a logical grouping of projects that belong to that user. | |
5039 | ++If the user +jane+ had a project named +project+, that project's url would be http://server/jane/project[]. | |
5040 | ++ | |
5041 | ++.The GitLab user administration screen. | |
5042 | ++image::images/gitlab-users.png[The GitLab user administration screen.] | |
5043 | + | |
5044 | +-You can remove a user account in two ways: | |
5045 | ++Removing a user can be done in two ways. | |
5046 | + ``Blocking'' a user prevents them from logging into the GitLab instance, but all of the data under that user's namespace will be preserved, and commits signed with that user's email address will still link back to their profile. | |
5047 | + | |
5048 | + ``Destroying'' a user, on the other hand, completely removes them from the database and filesystem. | |
5049 | + All projects and data in their namespace is removed, and any groups they own will also be removed. | |
5050 | +-This is obviously a much more permanent and destructive action, and you will rarely need it. | |
5051 | ++This is obviously a much more permanent and destructive action, and its uses are rare. | |
5052 | + | |
5053 | +-[[_gitlab_groups_section]] | |
5054 | + ===== Groups | |
5055 | + | |
5056 | +-A GitLab group is a collection of projects, along with data about how users can access those projects. | |
5057 | +-Each group has a project namespace (the same way that users do), so if the group +training+ has a project +materials+, its url would be `http://server/training/materials`. | |
5058 | ++A GitLab group is an assemblage of projects, along with data about how users can access those projects. | |
5059 | ++Each group has a project namespace (the same way that users do), so if the group +training+ has a project +materials+, its url would be http://server/training/materials[]. | |
5060 | + | |
5061 | +-[[gitlab_groups]] | |
5062 | +-.The GitLab group administration screen | |
5063 | +-image::images/gitlab-groups.png[The GitLab group administration screen] | |
5064 | ++.The GitLab group administration screen. | |
5065 | ++image::images/gitlab-groups.png[The GitLab group administration screen.] | |
5066 | + | |
5067 | + Each group is associated with a number of users, each of which has a level of permissions for the group's projects and the group itself. | |
5068 | + These range from ``Guest'' (issues and chat only) to ``Owner'' (full control of the group, its members, and its projects). | |
5069 | +@@ -66,27 +63,27 @@ | |
5070 | + | |
5071 | + ===== Projects | |
5072 | + | |
5073 | +-A GitLab project roughly corresponds to a single Git repository. | |
5074 | ++A GitLab project roughly corresponds to a single git repository. | |
5075 | + Every project belongs to a single namespace, either a user or a group. | |
5076 | +-If the project belongs to a user, the owner of the project has direct control over who has access to the project; if the project belongs to a group, the group's user-level permissions will take effect. | |
5077 | ++If the project belongs to a user, the owner of the project has direct control over who has access to the project; if the project belongs to a group, the group's user-level permissions will also take effect. | |
5078 | + | |
5079 | +-Every project has a visibility level, which controls who has read access to that project's pages and repository. | |
5080 | ++Every project also has a visibility level, which controls who has read access to that project's pages and repository. | |
5081 | + If a project is _Private_, the project's owner must explicitly grant access to specific users. | |
5082 | + An _Internal_ project is visible to any logged-in user, and a _Public_ project is visible to anyone. | |
5083 | +-Note that this controls both `git fetch` access as well as access to the web UI for that project. | |
5084 | ++Note that this controls both git ``fetch'' access as well as access to the web UI for that project. | |
5085 | + | |
5086 | + ===== Hooks | |
5087 | + | |
5088 | + GitLab includes support for hooks, both at a project or system level. | |
5089 | + For either of these, the GitLab server will perform an HTTP POST with some descriptive JSON whenever relevant events occur. | |
5090 | +-This is a great way to connect your Git repositories and GitLab instance to the rest of your development automation, such as CI servers, chat rooms, or deployment tools. | |
5091 | ++This is a great way to connect your git repositories and GitLab instance to the rest of your development automation, such as CI servers, chat rooms, or deployment tools. | |
5092 | + | |
5093 | + ==== Basic Usage | |
5094 | + | |
5095 | + The first thing you'll want to do with GitLab is create a new project. | |
5096 | +-You can do this by clicking on the ``+'' icon on the toolbar. | |
5097 | ++This is accomplished by clicking the ``+'' icon on the toolbar. | |
5098 | + You'll be asked for the project's name, which namespace it should belong to, and what its visibility level should be. | |
5099 | +-Most of what you specify here isn't permanent, and can be changed later through the settings interface. | |
5100 | ++Most of what you specify here isn't permanent, and can be re-adjusted later through the settings interface. | |
5101 | + Click ``Create Project'', and you're done. | |
5102 | + | |
5103 | + Once the project exists, you'll probably want to connect it with a local Git repository. | |
5104 | +@@ -94,31 +91,21 @@ | |
5105 | + The URLs are visible at the top of the project's home page. | |
5106 | + For an existing local repository, this command will create a remote named `gitlab` to the hosted location: | |
5107 | + | |
5108 | +-[source,console] | |
5109 | +----- | |
5110 | +-$ git remote add gitlab https://server/namespace/project.git | |
5111 | +----- | |
5112 | +- | |
5113 | + If you don't have a local copy of the repository, you can simply do this: | |
5114 | + | |
5115 | +-[source,console] | |
5116 | +----- | |
5117 | +-$ git clone https://server/namespace/project.git | |
5118 | +----- | |
5119 | +- | |
5120 | + The web UI provides access to several useful views of the repository itself. | |
5121 | + Each project's home page shows recent activity, and links along the top will lead you to views of the project's files and commit log. | |
5122 | + | |
5123 | + ==== Working Together | |
5124 | + | |
5125 | +-The simplest way of working together on a GitLab project is by giving each user direct push access to the Git repository. | |
5126 | +-You can add a user to a project by going to the ``Members'' section of that project's settings, and associating the new user with an access level (the different access levels are discussed a bit in <<_gitlab_groups_section>>). | |
5127 | +-By giving a user an access level of ``Developer'' or above, that user can push commits and branches directly to the repository. | |
5128 | ++The simplest way of working together on a GitLab project is by giving another user direct push access to the git repository. | |
5129 | ++You can add a user to a project by going to the ``Members'' section of that project's settings, and associating the new user with an access level (the different access levels are discussed a bit in <<r_gitlab_groups_section>>). | |
5130 | ++By giving a user an access level of ``Developer'' or above, that user can push commits and branches directly to the repository with impunity. | |
5131 | + | |
5132 | + Another, more decoupled way of collaboration is by using merge requests. | |
5133 | + This feature enables any user that can see a project to contribute to it in a controlled way. | |
5134 | + Users with direct access can simply create a branch, push commits to it, and open a merge request from their branch back into `master` or any other branch. | |
5135 | +-Users who don't have push permissions for a repository can ``fork'' it to create their own copy, push commits to _their_ copy, and open a merge request from their fork back to the main project. | |
5136 | ++Users who don't have push permissions for a repository can ``fork'' it (create their own copy), push commits to _that_ copy, and open a merge request from their fork back to the main project. | |
5137 | + This model allows the owner to be in full control of what goes into the repository and when, while allowing contributions from untrusted users. | |
5138 | + | |
5139 | + Merge requests and issues are the main units of long-lived discussion in GitLab. | |
5140 | +@@ -126,4 +113,4 @@ | |
5141 | + Both can be assigned to users, or organized into milestones. | |
5142 | + | |
5143 | + This section is focused mainly on the Git-related features of GitLab, but as a mature project, it provides many other features to help your team work together, such as project wikis and system maintenance tools. | |
5144 | +-One benefit to GitLab is that, once the server is set up and running, you'll rarely need to tweak a configuration file or access the server via SSH; most administration and general usage can be done through the in-browser interface. | |
5145 | ++One benefit to GitLab is that, once the server is set up and running, you'll rarely need to tweak a configuration file or access the server via SSH; most administration and general usage can be accomplished through the in-browser interface. | |
5146 | +diff -au ../../git/progit2/progit2/book/04-git-server/sections/gitweb.asc /tmp/book/04-git-server/sections/gitweb.asc.tmp | |
5147 | +--- ../../git/progit2/progit2/book/04-git-server/sections/gitweb.asc 2020-07-04 16:36:46.723685453 +0900 | |
5148 | ++++ /tmp/book/04-git-server/sections/gitweb.asc.tmp 2020-10-25 09:24:25.710969260 +0900 | |
5149 | +@@ -4,67 +4,25 @@ | |
5150 | + Now that you have basic read/write and read-only access to your project, you may want to set up a simple web-based visualizer. | |
5151 | + Git comes with a CGI script called GitWeb that is sometimes used for this. | |
5152 | + | |
5153 | +-[[gitweb]] | |
5154 | +-.The GitWeb web-based user interface | |
5155 | +-image::images/git-instaweb.png[The GitWeb web-based user interface] | |
5156 | ++.GitWeb のユーザーインターフェイス | |
5157 | ++image::images/git-instaweb.png[GitWeb のユーザーインターフェイス] | |
5158 | + | |
5159 | +-If you want to check out what GitWeb would look like for your project, Git comes with a command to fire up a temporary instance if you have a lightweight web server on your system like `lighttpd` or `webrick`. | |
5160 | ++If you want to check out what GitWeb would look like for your project, Git comes with a command to fire up a temporary instance if you have a lightweight server on your system like `lighttpd` or `webrick`. | |
5161 | + On Linux machines, `lighttpd` is often installed, so you may be able to get it to run by typing `git instaweb` in your project directory. | |
5162 | + If you're running a Mac, Leopard comes preinstalled with Ruby, so `webrick` may be your best bet. | |
5163 | + To start `instaweb` with a non-lighttpd handler, you can run it with the `--httpd` option.(((git commands, instaweb))) | |
5164 | + | |
5165 | +-[source,console] | |
5166 | +----- | |
5167 | +-$ git instaweb --httpd=webrick | |
5168 | +-[2009-02-21 10:02:21] INFO WEBrick 1.3.1 | |
5169 | +-[2009-02-21 10:02:21] INFO ruby 1.8.6 (2008-03-03) [universal-darwin9.0] | |
5170 | +----- | |
5171 | +- | |
5172 | + That starts up an HTTPD server on port 1234 and then automatically starts a web browser that opens on that page. | |
5173 | + It's pretty easy on your part. | |
5174 | + When you're done and want to shut down the server, you can run the same command with the `--stop` option: | |
5175 | + | |
5176 | +-[source,console] | |
5177 | +----- | |
5178 | +-$ git instaweb --httpd=webrick --stop | |
5179 | +----- | |
5180 | +- | |
5181 | + If you want to run the web interface on a server all the time for your team or for an open source project you're hosting, you'll need to set up the CGI script to be served by your normal web server. | |
5182 | +-Some Linux distributions have a `gitweb` package that you may be able to install via `apt` or `dnf`, so you may want to try that first. | |
5183 | ++Some Linux distributions have a `gitweb` package that you may be able to install via `apt` or `yum`, so you may want to try that first. | |
5184 | + We'll walk through installing GitWeb manually very quickly. | |
5185 | + First, you need to get the Git source code, which GitWeb comes with, and generate the custom CGI script: | |
5186 | + | |
5187 | +-[source,console] | |
5188 | +----- | |
5189 | +-$ git clone git://git.kernel.org/pub/scm/git/git.git | |
5190 | +-$ cd git/ | |
5191 | +-$ make GITWEB_PROJECTROOT="/srv/git" prefix=/usr gitweb | |
5192 | +- SUBDIR gitweb | |
5193 | +- SUBDIR ../ | |
5194 | +-make[2]: `GIT-VERSION-FILE' is up to date. | |
5195 | +- GEN gitweb.cgi | |
5196 | +- GEN static/gitweb.js | |
5197 | +-$ sudo cp -Rf gitweb /var/www/ | |
5198 | +----- | |
5199 | +- | |
5200 | + Notice that you have to tell the command where to find your Git repositories with the `GITWEB_PROJECTROOT` variable. | |
5201 | + Now, you need to make Apache use CGI for that script, for which you can add a VirtualHost: | |
5202 | + | |
5203 | +-[source,console] | |
5204 | +----- | |
5205 | +-<VirtualHost *:80> | |
5206 | +- ServerName gitserver | |
5207 | +- DocumentRoot /var/www/gitweb | |
5208 | +- <Directory /var/www/gitweb> | |
5209 | +- Options +ExecCGI +FollowSymLinks +SymLinksIfOwnerMatch | |
5210 | +- AllowOverride All | |
5211 | +- order allow,deny | |
5212 | +- Allow from all | |
5213 | +- AddHandler cgi-script cgi | |
5214 | +- DirectoryIndex gitweb.cgi | |
5215 | +- </Directory> | |
5216 | +-</VirtualHost> | |
5217 | +----- | |
5218 | +- | |
5219 | + Again, GitWeb can be served with any CGI or Perl capable web server; if you prefer to use something else, it shouldn't be difficult to set up. | |
5220 | + At this point, you should be able to visit `http://gitserver/` to view your repositories online. | |
5221 | +diff -au ../../git/progit2/progit2/book/04-git-server/sections/hosted.asc /tmp/book/04-git-server/sections/hosted.asc.tmp | |
5222 | +--- ../../git/progit2/progit2/book/04-git-server/sections/hosted.asc 2020-07-04 16:36:46.725685477 +0900 | |
5223 | ++++ /tmp/book/04-git-server/sections/hosted.asc.tmp 2020-10-25 09:24:25.715969496 +0900 | |
5224 | +@@ -5,6 +5,6 @@ | |
5225 | + Even if you set up and run your own server internally, you may still want to use a public hosting site for your open source code – it's generally easier for the open source community to find and help you with. | |
5226 | + | |
5227 | + These days, you have a huge number of hosting options to choose from, each with different advantages and disadvantages. | |
5228 | +-To see an up-to-date list, check out the GitHosting page on the main Git wiki at https://git.wiki.kernel.org/index.php/GitHosting[]. | |
5229 | ++To see an up-to-date list, check out the GitHosting page on the main Git wiki at https://git.wiki.kernel.org/index.php/GitHosting[] | |
5230 | + | |
5231 | + We'll cover using GitHub in detail in <<ch06-github#ch06-github>>, as it is the largest Git host out there and you may need to interact with projects hosted on it in any case, but there are dozens more to choose from should you not want to set up your own Git server. | |
5232 | +diff -au ../../git/progit2/progit2/book/04-git-server/sections/protocols.asc /tmp/book/04-git-server/sections/protocols.asc.tmp | |
5233 | +--- ../../git/progit2/progit2/book/04-git-server/sections/protocols.asc 2020-07-04 16:36:46.726685489 +0900 | |
5234 | ++++ /tmp/book/04-git-server/sections/protocols.asc.tmp 2020-10-25 09:24:25.720969732 +0900 | |
5235 | +@@ -1,55 +1,39 @@ | |
5236 | + === The Protocols | |
5237 | + | |
5238 | +-Git can use four distinct protocols to transfer data: Local, HTTP, Secure Shell (SSH) and Git. | |
5239 | ++Git can use four major protocols to transfer data: Local, HTTP, Secure Shell (SSH) and Git. | |
5240 | + Here we'll discuss what they are and in what basic circumstances you would want (or not want) to use them. | |
5241 | + | |
5242 | + ==== Local Protocol | |
5243 | + | |
5244 | +-(((protocols, local))) | |
5245 | +-The most basic is the _Local protocol_, in which the remote repository is in another directory on the same host. | |
5246 | +-This is often used if everyone on your team has access to a shared filesystem such as an https://en.wikipedia.org/wiki/Network_File_System[NFS] mount, or in the less likely case that everyone logs in to the same computer. | |
5247 | ++The most basic is the _Local protocol_, in which the remote repository is in another directory on disk. | |
5248 | ++This is often used if everyone on your team has access to a shared filesystem such as an NFS mount, or in the less likely case that everyone logs in to the same computer. | |
5249 | + The latter wouldn't be ideal, because all your code repository instances would reside on the same computer, making a catastrophic loss much more likely. | |
5250 | + | |
5251 | + If you have a shared mounted filesystem, then you can clone, push to, and pull from a local file-based repository. | |
5252 | +-To clone a repository like this, or to add one as a remote to an existing project, use the path to the repository as the URL. | |
5253 | ++To clone a repository like this or to add one as a remote to an existing project, use the path to the repository as the URL. | |
5254 | + For example, to clone a local repository, you can run something like this: | |
5255 | + | |
5256 | +-[source,console] | |
5257 | +----- | |
5258 | +-$ git clone /srv/git/project.git | |
5259 | +----- | |
5260 | +- | |
5261 | + Or you can do this: | |
5262 | + | |
5263 | +-[source,console] | |
5264 | +----- | |
5265 | +-$ git clone file:///srv/git/project.git | |
5266 | +----- | |
5267 | +- | |
5268 | + Git operates slightly differently if you explicitly specify `file://` at the beginning of the URL. | |
5269 | + If you just specify the path, Git tries to use hardlinks or directly copy the files it needs. | |
5270 | +-If you specify `file://`, Git fires up the processes that it normally uses to transfer data over a network, which is generally much less efficient. | |
5271 | +-The main reason to specify the `file://` prefix is if you want a clean copy of the repository with extraneous references or objects left out -- generally after an import from another VCS or something similar (see <<ch10-git-internals#ch10-git-internals>> for maintenance tasks). | |
5272 | ++If you specify `file://`, Git fires up the processes that it normally uses to transfer data over a network which is generally a lot less efficient method of transferring the data. | |
5273 | ++The main reason to specify the `file://` prefix is if you want a clean copy of the repository with extraneous references or objects left out – generally after an import from another version-control system or something similar (see <<ch10-git-internals#ch10-git-internals>> for maintenance tasks). | |
5274 | + We'll use the normal path here because doing so is almost always faster. | |
5275 | + | |
5276 | + To add a local repository to an existing Git project, you can run something like this: | |
5277 | + | |
5278 | +-[source,console] | |
5279 | +----- | |
5280 | +-$ git remote add local_proj /srv/git/project.git | |
5281 | +----- | |
5282 | +- | |
5283 | +-Then, you can push to and pull from that remote via your new remote name `local_proj` as though you were doing so over a network. | |
5284 | ++Then, you can push to and pull from that remote as though you were doing so over a network. | |
5285 | + | |
5286 | + ===== The Pros | |
5287 | + | |
5288 | + The pros of file-based repositories are that they're simple and they use existing file permissions and network access. | |
5289 | + If you already have a shared filesystem to which your whole team has access, setting up a repository is very easy. | |
5290 | + You stick the bare repository copy somewhere everyone has shared access to and set the read/write permissions as you would for any other shared directory. | |
5291 | +-We'll discuss how to export a bare repository copy for this purpose in <<ch04-git-on-the-server#_getting_git_on_a_server>>. | |
5292 | ++We'll discuss how to export a bare repository copy for this purpose in <<r_git_on_the_server>>. | |
5293 | + | |
5294 | + This is also a nice option for quickly grabbing work from someone else's working repository. | |
5295 | +-If you and a co-worker are working on the same project and they want you to check something out, running a command like `git pull /home/john/project` is often easier than them pushing to a remote server and you subsequently fetching from it. | |
5296 | ++If you and a co-worker are working on the same project and they want you to check something out, running a command like `git pull /home/john/project` is often easier than them pushing to a remote server and you pulling down. | |
5297 | + | |
5298 | + ===== The Cons | |
5299 | + | |
5300 | +@@ -61,59 +45,39 @@ | |
5301 | + A repository on NFS is often slower than the repository over SSH on the same server, allowing Git to run off local disks on each system. | |
5302 | + | |
5303 | + Finally, this protocol does not protect the repository against accidental damage. | |
5304 | +-Every user has full shell access to the ``remote'' directory, and there is nothing preventing them from changing or removing internal Git files and corrupting the repository. | |
5305 | ++Every user has full shell access to the "remote" directory, and there is nothing preventing them from changing or removing internal Git files and corrupting the repository. | |
5306 | + | |
5307 | + ==== The HTTP Protocols | |
5308 | + | |
5309 | +-Git can communicate over HTTP using two different modes. | |
5310 | +-Prior to Git 1.6.6, there was only one way it could do this which was very simple and generally read-only. | |
5311 | +-In version 1.6.6, a new, smarter protocol was introduced that involved Git being able to intelligently negotiate data transfer in a manner similar to how it does over SSH. | |
5312 | ++Git can communicate over HTTP in two different modes. | |
5313 | ++Prior to Git 1.6.6 there was only one way it could do this which was very simple and generally read-only. | |
5314 | ++In version 1.6.6 a new, smarter protocol was introduced that involved Git being able to intelligently negotiate data transfer in a manner similar to how it does over SSH. | |
5315 | + In the last few years, this new HTTP protocol has become very popular since it's simpler for the user and smarter about how it communicates. | |
5316 | +-The newer version is often referred to as the _Smart_ HTTP protocol and the older way as _Dumb_ HTTP. | |
5317 | +-We'll cover the newer Smart HTTP protocol first. | |
5318 | ++The newer version is often referred to as the ``Smart'' HTTP protocol and the older way as ``Dumb'' HTTP. | |
5319 | ++We'll cover the newer ``smart'' HTTP protocol first. | |
5320 | + | |
5321 | +-===== Smart HTTP | |
5322 | +- | |
5323 | +-(((protocols, smart HTTP))) | |
5324 | +-Smart HTTP operates very similarly to the SSH or Git protocols but runs over standard HTTPS ports and can use various HTTP authentication mechanisms, meaning it's often easier on the user than something like SSH, since you can use things like username/password authentication rather than having to set up SSH keys. | |
5325 | ++The ``smart'' HTTP protocol operates very similarly to the SSH or Git protocols but runs over standard HTTP/S ports and can use various HTTP authentication mechanisms, meaning it's often easier on the user than something like SSH, since you can use things like username/password basic authentication rather than having to set up SSH keys. | |
5326 | + | |
5327 | + It has probably become the most popular way to use Git now, since it can be set up to both serve anonymously like the `git://` protocol, and can also be pushed over with authentication and encryption like the SSH protocol. | |
5328 | + Instead of having to set up different URLs for these things, you can now use a single URL for both. | |
5329 | + If you try to push and the repository requires authentication (which it normally should), the server can prompt for a username and password. | |
5330 | + The same goes for read access. | |
5331 | + | |
5332 | +-In fact, for services like GitHub, the URL you use to view the repository online (for example, https://github.com/schacon/simplegit[]) is the same URL you can use to clone and, if you have access, push over. | |
5333 | +- | |
5334 | +-===== Dumb HTTP | |
5335 | ++In fact, for services like GitHub, the URL you use to view the repository online (for example, ``https://github.com/schacon/simplegit[]'') is the same URL you can use to clone and, if you have access, push over. | |
5336 | + | |
5337 | +-(((protocols, dumb HTTP))) | |
5338 | +-If the server does not respond with a Git HTTP smart service, the Git client will try to fall back to the simpler _Dumb_ HTTP protocol. | |
5339 | ++If the server does not respond with a Git HTTP smart service, the Git client will try to fall back to the simpler ``dumb'' HTTP protocol. | |
5340 | + The Dumb protocol expects the bare Git repository to be served like normal files from the web server. | |
5341 | +-The beauty of Dumb HTTP is the simplicity of setting it up. | |
5342 | +-Basically, all you have to do is put a bare Git repository under your HTTP document root and set up a specific `post-update` hook, and you're done (See <<ch08-customizing-git#_git_hooks>>). | |
5343 | ++The beauty of the Dumb HTTP protocol is the simplicity of setting it up. | |
5344 | ++Basically, all you have to do is put a bare Git repository under your HTTP document root and set up a specific `post-update` hook, and you're done (See <<ch08-customizing-git#r_git_hooks>>). | |
5345 | + At that point, anyone who can access the web server under which you put the repository can also clone your repository. | |
5346 | + To allow read access to your repository over HTTP, do something like this: | |
5347 | + | |
5348 | +-[source,console] | |
5349 | +----- | |
5350 | +-$ cd /var/www/htdocs/ | |
5351 | +-$ git clone --bare /path/to/git_project gitproject.git | |
5352 | +-$ cd gitproject.git | |
5353 | +-$ mv hooks/post-update.sample hooks/post-update | |
5354 | +-$ chmod a+x hooks/post-update | |
5355 | +----- | |
5356 | +- | |
5357 | + That's all.(((hooks, post-update))) | |
5358 | + The `post-update` hook that comes with Git by default runs the appropriate command (`git update-server-info`) to make HTTP fetching and cloning work properly. | |
5359 | +-This command is run when you push to this repository (over SSH perhaps); then, other people can clone via something like: | |
5360 | +- | |
5361 | +-[source,console] | |
5362 | +----- | |
5363 | +-$ git clone https://example.com/gitproject.git | |
5364 | +----- | |
5365 | ++This command is run when you push to this repository (over SSH perhaps); then, other people can clone via something like | |
5366 | + | |
5367 | +-In this particular case, we're using the `/var/www/htdocs` path that is common for Apache setups, but you can use any static web server -- just put the bare repository in its path. | |
5368 | +-The Git data is served as basic static files (see the <<ch10-git-internals#ch10-git-internals>> chapter for details about exactly how it's served). | |
5369 | ++In this particular case, we're using the `/var/www/htdocs` path that is common for Apache setups, but you can use any static web server – just put the bare repository in its path. | |
5370 | ++The Git data is served as basic static files (see <<ch10-git-internals#ch10-git-internals>> for details about exactly how it's served). | |
5371 | + | |
5372 | + Generally you would either choose to run a read/write Smart HTTP server or simply have the files accessible as read-only in the Dumb manner. | |
5373 | + It's rare to run a mix of the two services. | |
5374 | +@@ -129,69 +93,57 @@ | |
5375 | + | |
5376 | + You can also serve your repositories read-only over HTTPS, which means you can encrypt the content transfer; or you can go so far as to make the clients use specific signed SSL certificates. | |
5377 | + | |
5378 | +-Another nice thing is that HTTP and HTTPS are such commonly used protocols that corporate firewalls are often set up to allow traffic through their ports. | |
5379 | ++Another nice thing is that HTTP/S are such commonly used protocols that corporate firewalls are often set up to allow traffic through these ports. | |
5380 | + | |
5381 | + ===== The Cons | |
5382 | + | |
5383 | +-Git over HTTPS can be a little more tricky to set up compared to SSH on some servers. | |
5384 | +-Other than that, there is very little advantage that other protocols have over Smart HTTP for serving Git content. | |
5385 | ++Git over HTTP/S can be a little more tricky to set up compared to SSH on some servers. | |
5386 | ++Other than that, there is very little advantage that other protocols have over the ``Smart'' HTTP protocol for serving Git. | |
5387 | + | |
5388 | + If you're using HTTP for authenticated pushing, providing your credentials is sometimes more complicated than using keys over SSH. | |
5389 | +-There are, however, several credential caching tools you can use, including Keychain access on macOS and Credential Manager on Windows, to make this pretty painless. | |
5390 | +-Read <<ch07-git-tools#_credential_caching>> to see how to set up secure HTTP password caching on your system. | |
5391 | ++There are however several credential caching tools you can use, including Keychain access on OSX and Credential Manager on Windows, to make this pretty painless. | |
5392 | ++Read <<ch07-git-tools#r_credential_caching>> to see how to set up secure HTTP password caching on your system. | |
5393 | + | |
5394 | + ==== The SSH Protocol | |
5395 | + | |
5396 | +-(((protocols, SSH))) | |
5397 | + A common transport protocol for Git when self-hosting is over SSH. | |
5398 | +-This is because SSH access to servers is already set up in most places -- and if it isn't, it's easy to do. | |
5399 | +-SSH is also an authenticated network protocol and, because it's ubiquitous, it's generally easy to set up and use. | |
5400 | ++This is because SSH access to servers is already set up in most places – and if it isn't, it's easy to do. | |
5401 | ++SSH is also an authenticated network protocol; and because it's ubiquitous, it's generally easy to set up and use. | |
5402 | + | |
5403 | +-To clone a Git repository over SSH, you can specify an `ssh://` URL like this: | |
5404 | +- | |
5405 | +-[source,console] | |
5406 | +----- | |
5407 | +-$ git clone ssh://[user@]server/project.git | |
5408 | +----- | |
5409 | ++To clone a Git repository over SSH, you can specify ssh:// URL like this: | |
5410 | + | |
5411 | + Or you can use the shorter scp-like syntax for the SSH protocol: | |
5412 | + | |
5413 | +-[source,console] | |
5414 | +----- | |
5415 | +-$ git clone [user@]server:project.git | |
5416 | +----- | |
5417 | +- | |
5418 | +-In both cases above, if you don't specify the optional username, Git assumes the user you're currently logged in as. | |
5419 | ++You can also not specify a user, and Git assumes the user you're currently logged in as. | |
5420 | + | |
5421 | + ===== The Pros | |
5422 | + | |
5423 | + The pros of using SSH are many. | |
5424 | +-First, SSH is relatively easy to set up -- SSH daemons are commonplace, many network admins have experience with them, and many OS distributions are set up with them or have tools to manage them. | |
5425 | +-Next, access over SSH is secure -- all data transfer is encrypted and authenticated. | |
5426 | +-Last, like the HTTPS, Git and Local protocols, SSH is efficient, making the data as compact as possible before transferring it. | |
5427 | ++First, SSH is relatively easy to set up – SSH daemons are commonplace, many network admins have experience with them, and many OS distributions are set up with them or have tools to manage them. | |
5428 | ++Next, access over SSH is secure – all data transfer is encrypted and authenticated. | |
5429 | ++Last, like the HTTP/S, Git and Local protocols, SSH is efficient, making the data as compact as possible before transferring it. | |
5430 | + | |
5431 | + ===== The Cons | |
5432 | + | |
5433 | +-The negative aspect of SSH is that it doesn't support anonymous access to your Git repository. | |
5434 | +-If you're using SSH, people _must_ have SSH access to your machine, even in a read-only capacity, which doesn't make SSH conducive to open source projects for which people might simply want to clone your repository to examine it. | |
5435 | ++The negative aspect of SSH is that you can't serve anonymous access of your repository over it. | |
5436 | ++People must have access to your machine over SSH to access it, even in a read-only capacity, which doesn't make SSH access conducive to open source projects. | |
5437 | + If you're using it only within your corporate network, SSH may be the only protocol you need to deal with. | |
5438 | +-If you want to allow anonymous read-only access to your projects and also want to use SSH, you'll have to set up SSH for you to push over but something else for others to fetch from. | |
5439 | ++If you want to allow anonymous read-only access to your projects and also want to use SSH, you’ll have to set up SSH for you to push over but something else for others to fetch over. | |
5440 | + | |
5441 | + ==== The Git Protocol | |
5442 | + | |
5443 | +-(((protocols, git))) | |
5444 | +-Finally, we have the Git protocol. | |
5445 | ++Next is the Git protocol. | |
5446 | + This is a special daemon that comes packaged with Git; it listens on a dedicated port (9418) that provides a service similar to the SSH protocol, but with absolutely no authentication. | |
5447 | +-In order for a repository to be served over the Git protocol, you must create a `git-daemon-export-ok` file -- the daemon won't serve a repository without that file in it -- but, other than that, there is no security. | |
5448 | +-Either the Git repository is available for everyone to clone, or it isn't. | |
5449 | ++In order for a repository to be served over the Git protocol, you must create the `git-daemon-export-ok` file – the daemon won't serve a repository without that file in it – but other than that there is no security. | |
5450 | ++Either the Git repository is available for everyone to clone or it isn't. | |
5451 | + This means that there is generally no pushing over this protocol. | |
5452 | +-You can enable push access but, given the lack of authentication, anyone on the internet who finds your project's URL could push to that project. | |
5453 | ++You can enable push access; but given the lack of authentication, if you turn on push access, anyone on the internet who finds your project's URL could push to your project. | |
5454 | + Suffice it to say that this is rare. | |
5455 | + | |
5456 | + ===== The Pros | |
5457 | + | |
5458 | + The Git protocol is often the fastest network transfer protocol available. | |
5459 | +-If you're serving a lot of traffic for a public project or serving a very large project that doesn't require user authentication for read access, it's likely that you'll want to set up a Git daemon to serve your project. | |
5460 | ++If you’re serving a lot of traffic for a public project or serving a very large project that doesn't require user authentication for read access, it’s likely that you'll want to set up a Git daemon to serve your project. | |
5461 | + It uses the same data-transfer mechanism as the SSH protocol but without the encryption and authentication overhead. | |
5462 | + | |
5463 | + ===== The Cons | |
5464 | +@@ -200,6 +152,6 @@ | |
5465 | + It's generally undesirable for the Git protocol to be the only access to your project. | |
5466 | + Generally, you'll pair it with SSH or HTTPS access for the few developers who have push (write) access and have everyone else use `git://` for read-only access. | |
5467 | + It's also probably the most difficult protocol to set up. | |
5468 | +-It must run its own daemon, which requires `xinetd` or `systemd` configuration or the like, which isn't always a walk in the park. | |
5469 | ++It must run its own daemon, which requires `xinetd` configuration or the like, which isn't always a walk in the park. | |
5470 | + It also requires firewall access to port 9418, which isn't a standard port that corporate firewalls always allow. | |
5471 | + Behind big corporate firewalls, this obscure port is commonly blocked. | |
5472 | +diff -au ../../git/progit2/progit2/book/04-git-server/sections/setting-up-server.asc /tmp/book/04-git-server/sections/setting-up-server.asc.tmp | |
5473 | +--- ../../git/progit2/progit2/book/04-git-server/sections/setting-up-server.asc 2020-06-28 16:44:50.927015646 +0900 | |
5474 | ++++ /tmp/book/04-git-server/sections/setting-up-server.asc.tmp 2020-10-25 09:24:25.727970062 +0900 | |
5475 | +@@ -1,147 +1,40 @@ | |
5476 | +-[[_setting_up_server]] | |
5477 | ++[[r_setting_up_server]] | |
5478 | + === Setting Up the Server | |
5479 | + | |
5480 | + Let's walk through setting up SSH access on the server side. | |
5481 | + In this example, you'll use the `authorized_keys` method for authenticating your users. | |
5482 | + We also assume you're running a standard Linux distribution like Ubuntu. | |
5483 | +- | |
5484 | +-[NOTE] | |
5485 | +-==== | |
5486 | +-A good deal of what is described here can be automated by using the `ssh-copy-id` command, rather than manually copying and installing public keys. | |
5487 | +-==== | |
5488 | +- | |
5489 | +-First, you create a `git` user account and a `.ssh` directory for that user. | |
5490 | +- | |
5491 | +-[source,console] | |
5492 | +----- | |
5493 | +-$ sudo adduser git | |
5494 | +-$ su git | |
5495 | +-$ cd | |
5496 | +-$ mkdir .ssh && chmod 700 .ssh | |
5497 | +-$ touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys | |
5498 | +----- | |
5499 | ++First, you create a `git` user and a `.ssh` directory for that user. | |
5500 | + | |
5501 | + Next, you need to add some developer SSH public keys to the `authorized_keys` file for the `git` user. | |
5502 | + Let's assume you have some trusted public keys and have saved them to temporary files. | |
5503 | + Again, the public keys look something like this: | |
5504 | + | |
5505 | +-[source,console] | |
5506 | +----- | |
5507 | +-$ cat /tmp/id_rsa.john.pub | |
5508 | +-ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCB007n/ww+ouN4gSLKssMxXnBOvf9LGt4L | |
5509 | +-ojG6rs6hPB09j9R/T17/x4lhJA0F3FR1rP6kYBRsWj2aThGw6HXLm9/5zytK6Ztg3RPKK+4k | |
5510 | +-Yjh6541NYsnEAZuXz0jTTyAUfrtU3Z5E003C4oxOj6H0rfIF1kKI9MAQLMdpGW1GYEIgS9Ez | |
5511 | +-Sdfd8AcCIicTDWbqLAcU4UpkaX8KyGlLwsNuuGztobF8m72ALC/nLF6JLtPofwFBlgc+myiv | |
5512 | +-O7TCUSBdLQlgMVOFq1I2uPWQOkOWQAHukEOmfjy2jctxSDBQ220ymjaNsHT4kgtZg2AYYgPq | |
5513 | +-dAv8JggJICUvax2T9va5 gsg-keypair | |
5514 | +----- | |
5515 | +- | |
5516 | + You just append them to the `git` user's `authorized_keys` file in its `.ssh` directory: | |
5517 | + | |
5518 | +-[source,console] | |
5519 | +----- | |
5520 | +-$ cat /tmp/id_rsa.john.pub >> ~/.ssh/authorized_keys | |
5521 | +-$ cat /tmp/id_rsa.josie.pub >> ~/.ssh/authorized_keys | |
5522 | +-$ cat /tmp/id_rsa.jessica.pub >> ~/.ssh/authorized_keys | |
5523 | +----- | |
5524 | +- | |
5525 | + Now, you can set up an empty repository for them by running `git init` with the `--bare` option, which initializes the repository without a working directory:(((git commands, init, bare))) | |
5526 | + | |
5527 | +-[source,console] | |
5528 | +----- | |
5529 | +-$ cd /srv/git | |
5530 | +-$ mkdir project.git | |
5531 | +-$ cd project.git | |
5532 | +-$ git init --bare | |
5533 | +-Initialized empty Git repository in /srv/git/project.git/ | |
5534 | +----- | |
5535 | +- | |
5536 | + Then, John, Josie, or Jessica can push the first version of their project into that repository by adding it as a remote and pushing up a branch. | |
5537 | + Note that someone must shell onto the machine and create a bare repository every time you want to add a project. | |
5538 | + Let's use `gitserver` as the hostname of the server on which you've set up your `git` user and repository. | |
5539 | + If you're running it internally, and you set up DNS for `gitserver` to point to that server, then you can use the commands pretty much as is (assuming that `myproject` is an existing project with files in it): | |
5540 | + | |
5541 | +-[source,console] | |
5542 | +----- | |
5543 | +-# on John's computer | |
5544 | +-$ cd myproject | |
5545 | +-$ git init | |
5546 | +-$ git add . | |
5547 | +-$ git commit -m 'Initial commit' | |
5548 | +-$ git remote add origin git@gitserver:/srv/git/project.git | |
5549 | +-$ git push origin master | |
5550 | +----- | |
5551 | +- | |
5552 | + At this point, the others can clone it down and push changes back up just as easily: | |
5553 | + | |
5554 | +-[source,console] | |
5555 | +----- | |
5556 | +-$ git clone git@gitserver:/srv/git/project.git | |
5557 | +-$ cd project | |
5558 | +-$ vim README | |
5559 | +-$ git commit -am 'Fix for README file' | |
5560 | +-$ git push origin master | |
5561 | +----- | |
5562 | +- | |
5563 | + With this method, you can quickly get a read/write Git server up and running for a handful of developers. | |
5564 | + | |
5565 | + You should note that currently all these users can also log into the server and get a shell as the `git` user. | |
5566 | +-If you want to restrict that, you will have to change the shell to something else in the `/etc/passwd` file. | |
5567 | ++If you want to restrict that, you will have to change the shell to something else in the `passwd` file. | |
5568 | + | |
5569 | +-You can easily restrict the `git` user account to only Git-related activities with a limited shell tool called `git-shell` that comes with Git. | |
5570 | +-If you set this as the `git` user account's login shell, then that account can't have normal shell access to your server. | |
5571 | +-To use this, specify `git-shell` instead of `bash` or `csh` for that account's login shell. | |
5572 | +-To do so, you must first add the full pathname of the `git-shell` command to `/etc/shells` if it's not already there: | |
5573 | +- | |
5574 | +-[source,console] | |
5575 | +----- | |
5576 | +-$ cat /etc/shells # see if git-shell is already in there. If not... | |
5577 | +-$ which git-shell # make sure git-shell is installed on your system. | |
5578 | +-$ sudo -e /etc/shells # and add the path to git-shell from last command | |
5579 | +----- | |
5580 | +- | |
5581 | +-Now you can edit the shell for a user using `chsh <username> -s <shell>`: | |
5582 | +- | |
5583 | +-[source,console] | |
5584 | +----- | |
5585 | +-$ sudo chsh git -s $(which git-shell) | |
5586 | +----- | |
5587 | ++You can easily restrict the `git` user to only doing Git activities with a limited shell tool called `git-shell` that comes with Git. | |
5588 | ++If you set this as your `git` user's login shell, then the `git` user can't have normal shell access to your server. | |
5589 | ++To use this, specify `git-shell` instead of bash or csh for your user's login shell. | |
5590 | ++To do so, you must first add `git-shell` to `/etc/shells` if it's not already there: | |
5591 | + | |
5592 | +-Now, the `git` user can still use the SSH connection to push and pull Git repositories but can't shell onto the machine. | |
5593 | +-If you try, you'll see a login rejection like this: | |
5594 | ++Now you can edit the shell for a user using `chsh <username>`: | |
5595 | + | |
5596 | +-[source,console] | |
5597 | +----- | |
5598 | +-$ ssh git@gitserver | |
5599 | +-fatal: Interactive git shell is not enabled. | |
5600 | +-hint: ~/git-shell-commands should exist and have read and execute access. | |
5601 | +-Connection to gitserver closed. | |
5602 | +----- | |
5603 | +- | |
5604 | +-At this point, users are still able to use SSH port forwarding to access any host the git server is able to reach. | |
5605 | +-If you want to prevent that, you can edit the `authorized_keys` file and prepend the following options to each key you'd like to restrict: | |
5606 | +- | |
5607 | +-[source,console] | |
5608 | +----- | |
5609 | +-no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty | |
5610 | +----- | |
5611 | +- | |
5612 | +-The result should look like this: | |
5613 | +- | |
5614 | +-[source,console] | |
5615 | +----- | |
5616 | +-$ cat ~/.ssh/authorized_keys | |
5617 | +-no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa | |
5618 | +-AAAAB3NzaC1yc2EAAAADAQABAAABAQCB007n/ww+ouN4gSLKssMxXnBOvf9LGt4LojG6rs6h | |
5619 | +-PB09j9R/T17/x4lhJA0F3FR1rP6kYBRsWj2aThGw6HXLm9/5zytK6Ztg3RPKK+4kYjh6541N | |
5620 | +-YsnEAZuXz0jTTyAUfrtU3Z5E003C4oxOj6H0rfIF1kKI9MAQLMdpGW1GYEIgS9EzSdfd8AcC | |
5621 | +-IicTDWbqLAcU4UpkaX8KyGlLwsNuuGztobF8m72ALC/nLF6JLtPofwFBlgc+myivO7TCUSBd | |
5622 | +-LQlgMVOFq1I2uPWQOkOWQAHukEOmfjy2jctxSDBQ220ymjaNsHT4kgtZg2AYYgPqdAv8JggJ | |
5623 | +-ICUvax2T9va5 gsg-keypair | |
5624 | +- | |
5625 | +-no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa | |
5626 | +-AAAAB3NzaC1yc2EAAAADAQABAAABAQDEwENNMomTboYI+LJieaAY16qiXiH3wuvENhBG... | |
5627 | +----- | |
5628 | ++Now, the `git` user can only use the SSH connection to push and pull Git repositories and can't shell onto the machine. | |
5629 | ++If you try, you'll see a login rejection like this: | |
5630 | + | |
5631 | + Now Git network commands will still work just fine but the users won't be able to get a shell. | |
5632 | + As the output states, you can also set up a directory in the `git` user's home directory that customizes the `git-shell` command a bit. | |
5633 | +diff -au ../../git/progit2/progit2/book/04-git-server/sections/smart-http.asc /tmp/book/04-git-server/sections/smart-http.asc.tmp | |
5634 | +--- ../../git/progit2/progit2/book/04-git-server/sections/smart-http.asc 2020-06-28 16:44:50.927015646 +0900 | |
5635 | ++++ /tmp/book/04-git-server/sections/smart-http.asc.tmp 2020-10-25 09:24:25.732970297 +0900 | |
5636 | +@@ -1,62 +1,28 @@ | |
5637 | + === Smart HTTP | |
5638 | + | |
5639 | + (((serving repositories, HTTP))) | |
5640 | +-We now have authenticated access through SSH and unauthenticated access through `git://`, but there is also a protocol that can do both at the same time. | |
5641 | ++We now have authenticated access though SSH and unauthenticated access through `git://`, but there is also a protocol that can do both at the same time. | |
5642 | + Setting up Smart HTTP is basically just enabling a CGI script that is provided with Git called `git-http-backend` on the server.(((git commands, "http-backend"))) | |
5643 | + This CGI will read the path and headers sent by a `git fetch` or `git push` to an HTTP URL and determine if the client can communicate over HTTP (which is true for any client since version 1.6.6). | |
5644 | +-If the CGI sees that the client is smart, it will communicate smartly with it; otherwise it will fall back to the dumb behavior (so it is backward compatible for reads with older clients). | |
5645 | ++If the CGI sees that the client is smart, it will communicate smartly with it, otherwise it will fall back to the dumb behavior (so it is backward compatible for reads with older clients). | |
5646 | + | |
5647 | + Let's walk through a very basic setup. | |
5648 | + We'll set this up with Apache as the CGI server. | |
5649 | + If you don't have Apache setup, you can do so on a Linux box with something like this:(((Apache))) | |
5650 | + | |
5651 | +-[source,console] | |
5652 | +----- | |
5653 | +-$ sudo apt-get install apache2 apache2-utils | |
5654 | +-$ a2enmod cgi alias env | |
5655 | +----- | |
5656 | +- | |
5657 | +-This also enables the `mod_cgi`, `mod_alias`, and `mod_env` modules, which are all needed for this to work properly. | |
5658 | +- | |
5659 | +-You'll also need to set the Unix user group of the `/srv/git` directories to `www-data` so your web server can read- and write-access the repositories, because the Apache instance running the CGI script will (by default) be running as that user: | |
5660 | +- | |
5661 | +-[source,console] | |
5662 | +----- | |
5663 | +-$ chgrp -R www-data /srv/git | |
5664 | +----- | |
5665 | ++This also enables the `mod_cgi`, `mod_alias`, `mod_env`, and `mod_rewrite` modules, which are all needed for this to work properly. | |
5666 | + | |
5667 | +-Next we need to add some things to the Apache configuration to run the `git-http-backend` as the handler for anything coming into the `/git` path of your web server. | |
5668 | ++You’ll also need to set the Unix user group of the `/opt/git` directories to `www-data` so your web server can read- and write-access the repositories, because the Apache instance running the CGI script will (by default) be running as that user: | |
5669 | + | |
5670 | +-[source,console] | |
5671 | +----- | |
5672 | +-SetEnv GIT_PROJECT_ROOT /srv/git | |
5673 | +-SetEnv GIT_HTTP_EXPORT_ALL | |
5674 | +-ScriptAlias /git/ /usr/lib/git-core/git-http-backend/ | |
5675 | +----- | |
5676 | ++Next we need to add some things to the Apache configuration to run the `git-http-backend` as the handler for anything coming into the `/git` path of your web server. | |
5677 | + | |
5678 | + If you leave out `GIT_HTTP_EXPORT_ALL` environment variable, then Git will only serve to unauthenticated clients the repositories with the `git-daemon-export-ok` file in them, just like the Git daemon did. | |
5679 | + | |
5680 | + Finally you'll want to tell Apache to allow requests to `git-http-backend` and make writes be authenticated somehow, possibly with an Auth block like this: | |
5681 | + | |
5682 | +-[source,console] | |
5683 | +----- | |
5684 | +-<Files "git-http-backend"> | |
5685 | +- AuthType Basic | |
5686 | +- AuthName "Git Access" | |
5687 | +- AuthUserFile /srv/git/.htpasswd | |
5688 | +- Require expr !(%{QUERY_STRING} -strmatch '*service=git-receive-pack*' || %{REQUEST_URI} =~ m#/git-receive-pack$#) | |
5689 | +- Require valid-user | |
5690 | +-</Files> | |
5691 | +----- | |
5692 | +- | |
5693 | + That will require you to create a `.htpasswd` file containing the passwords of all the valid users. | |
5694 | + Here is an example of adding a ``schacon'' user to the file: | |
5695 | + | |
5696 | +-[source,console] | |
5697 | +----- | |
5698 | +-$ htpasswd -c /srv/git/.htpasswd schacon | |
5699 | +----- | |
5700 | +- | |
5701 | + There are tons of ways to have Apache authenticate users, you'll have to choose and implement one of them. | |
5702 | + This is just the simplest example we could come up with. | |
5703 | + You'll also almost certainly want to set this up over SSL so all this data is encrypted. | |
5704 | +@@ -66,7 +32,6 @@ | |
5705 | + It does not implement any authentication itself, but that can easily be controlled at the layer of the web server that invokes it. | |
5706 | + You can do this with nearly any CGI-capable web server, so go with the one that you know best. | |
5707 | + | |
5708 | +-[NOTE] | |
5709 | + ==== | |
5710 | +-For more information on configuring authentication in Apache, check out the Apache docs here: https://httpd.apache.org/docs/current/howto/auth.html[] | |
5711 | ++For more information on configuring authentication in Apache, check out the Apache docs here: http://httpd.apache.org/docs/current/howto/auth.html[] | |
5712 | + ==== | |
5713 | +diff -au ../../git/progit2/progit2/book/05-distributed-git/sections/contributing.asc /tmp/book/05-distributed-git/sections/contributing.asc.tmp | |
5714 | +--- ../../git/progit2/progit2/book/05-distributed-git/sections/contributing.asc 2020-08-29 09:58:24.050195768 +0900 | |
5715 | ++++ /tmp/book/05-distributed-git/sections/contributing.asc.tmp 2020-10-25 09:24:25.740970675 +0900 | |
5716 | +@@ -1,12 +1,11 @@ | |
5717 | +-[[_contributing_project]] | |
5718 | ++[[r_contributing_project]] | |
5719 | + === Contributing to a Project | |
5720 | + | |
5721 | +-(((contributing))) | |
5722 | +-The main difficulty with describing how to contribute to a project are the numerous variations on how to do that. | |
5723 | +-Because Git is very flexible, people can and do work together in many ways, and it's problematic to describe how you should contribute -- every project is a bit different. | |
5724 | ++The main difficulty with describing how to contribute to a project is that there are a huge number of variations on how it's done. | |
5725 | ++Because Git is very flexible, people can and do work together in many ways, and it's problematic to describe how you should contribute – every project is a bit different. | |
5726 | + Some of the variables involved are active contributor count, chosen workflow, your commit access, and possibly the external contribution method. | |
5727 | + | |
5728 | +-The first variable is active contributor count -- how many users are actively contributing code to this project, and how often? | |
5729 | ++The first variable is active contributor count – how many users are actively contributing code to this project, and how often? | |
5730 | + In many instances, you'll have two or three developers with a few commits a day, or possibly less for somewhat dormant projects. | |
5731 | + For larger companies or projects, the number of developers could be in the thousands, with hundreds or thousands of commits coming in each day. | |
5732 | + This is important because with more and more developers, you run into more issues with making sure your code applies cleanly or can be easily merged. | |
5733 | +@@ -20,7 +19,7 @@ | |
5734 | + Are you involved in that process? | |
5735 | + Is a lieutenant system in place, and do you have to submit your work to them first? | |
5736 | + | |
5737 | +-The next variable is your commit access. | |
5738 | ++The next issue is your commit access. | |
5739 | + The workflow required in order to contribute to a project is much different if you have write access to the project than if you don't. | |
5740 | + If you don't have write access, how does the project prefer to accept contributed work? | |
5741 | + Does it even have a policy? | |
5742 | +@@ -30,89 +29,75 @@ | |
5743 | + All these questions can affect how you contribute effectively to a project and what workflows are preferred or available to you. | |
5744 | + We'll cover aspects of each of these in a series of use cases, moving from simple to more complex; you should be able to construct the specific workflows you need in practice from these examples. | |
5745 | + | |
5746 | +-[[_commit_guidelines]] | |
5747 | + ==== Commit Guidelines | |
5748 | + | |
5749 | + Before we start looking at the specific use cases, here's a quick note about commit messages. | |
5750 | + Having a good guideline for creating commits and sticking to it makes working with Git and collaborating with others a lot easier. | |
5751 | +-The Git project provides a document that lays out a number of good tips for creating commits from which to submit patches -- you can read it in the Git source code in the `Documentation/SubmittingPatches` file. | |
5752 | ++The Git project provides a document that lays out a number of good tips for creating commits from which to submit patches – you can read it in the Git source code in the `Documentation/SubmittingPatches` file. | |
5753 | + | |
5754 | +-(((git commands, diff, check))) | |
5755 | +-First, your submissions should not contain any whitespace errors. | |
5756 | +-Git provides an easy way to check for this -- before you commit, run `git diff --check`, which identifies possible whitespace errors and lists them for you. | |
5757 | ++First, you don't want to submit any whitespace errors. | |
5758 | ++Git provides an easy way to check for this – before you commit, run `git diff --check`, which identifies possible whitespace errors and lists them for you. | |
5759 | + | |
5760 | +-.Output of `git diff --check` | |
5761 | +-image::images/git-diff-check.png[Output of `git diff --check`] | |
5762 | ++.Output of `git diff --check`. | |
5763 | ++image::images/git-diff-check.png[Output of `git diff --check`.] | |
5764 | + | |
5765 | + If you run that command before committing, you can tell if you're about to commit whitespace issues that may annoy other developers. | |
5766 | + | |
5767 | + Next, try to make each commit a logically separate changeset. | |
5768 | +-If you can, try to make your changes digestible -- don't code for a whole weekend on five different issues and then submit them all as one massive commit on Monday. | |
5769 | ++If you can, try to make your changes digestible – don't code for a whole weekend on five different issues and then submit them all as one massive commit on Monday. | |
5770 | + Even if you don't commit during the weekend, use the staging area on Monday to split your work into at least one commit per issue, with a useful message per commit. | |
5771 | +-If some of the changes modify the same file, try to use `git add --patch` to partially stage files (covered in detail in <<ch07-git-tools#_interactive_staging>>). | |
5772 | ++If some of the changes modify the same file, try to use `git add --patch` to partially stage files (covered in detail in <<ch07-git-tools#r_interactive_staging>>). | |
5773 | + The project snapshot at the tip of the branch is identical whether you do one commit or five, as long as all the changes are added at some point, so try to make things easier on your fellow developers when they have to review your changes. | |
5774 | +- | |
5775 | + This approach also makes it easier to pull out or revert one of the changesets if you need to later. | |
5776 | +-<<ch07-git-tools#_rewriting_history>> describes a number of useful Git tricks for rewriting history and interactively staging files -- use these tools to help craft a clean and understandable history before sending the work to someone else. | |
5777 | ++<<ch07-git-tools#r_rewriting_history>> describes a number of useful Git tricks for rewriting history and interactively staging files – use these tools to help craft a clean and understandable history before sending the work to someone else. | |
5778 | + | |
5779 | + The last thing to keep in mind is the commit message. | |
5780 | + Getting in the habit of creating quality commit messages makes using and collaborating with Git a lot easier. | |
5781 | + As a general rule, your messages should start with a single line that's no more than about 50 characters and that describes the changeset concisely, followed by a blank line, followed by a more detailed explanation. | |
5782 | +-The Git project requires that the more detailed explanation include your motivation for the change and contrast its implementation with previous behavior -- this is a good guideline to follow. | |
5783 | +-Write your commit message in the imperative: "Fix bug" and not "Fixed bug" or "Fixes bug." | |
5784 | +-Here is a template you can follow, which we've lightly adapted from one https://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html[originally written by Tim Pope]: | |
5785 | +- | |
5786 | +-[source,text] | |
5787 | +----- | |
5788 | +-Capitalized, short (50 chars or less) summary | |
5789 | +- | |
5790 | +-More detailed explanatory text, if necessary. Wrap it to about 72 | |
5791 | +-characters or so. In some contexts, the first line is treated as the | |
5792 | +-subject of an email and the rest of the text as the body. The blank | |
5793 | +-line separating the summary from the body is critical (unless you omit | |
5794 | +-the body entirely); tools like rebase will confuse you if you run the | |
5795 | +-two together. | |
5796 | +- | |
5797 | +-Write your commit message in the imperative: "Fix bug" and not "Fixed bug" | |
5798 | +-or "Fixes bug." This convention matches up with commit messages generated | |
5799 | +-by commands like git merge and git revert. | |
5800 | ++The Git project requires that the more detailed explanation include your motivation for the change and contrast its implementation with previous behavior – this is a good guideline to follow. | |
5801 | ++It's also a good idea to use the imperative present tense in these messages. | |
5802 | ++In other words, use commands. | |
5803 | ++Instead of ``I added tests for'' or ``Adding tests for,'' use ``Add tests for.'' | |
5804 | ++Here is a template originally written by Tim Pope: | |
5805 | ++ | |
5806 | ++---- | |
5807 | ++Short (50 chars or less) summary of changes | |
5808 | ++ | |
5809 | ++More detailed explanatory text, if necessary. Wrap it to | |
5810 | ++about 72 characters or so. In some contexts, the first | |
5811 | ++line is treated as the subject of an email and the rest of | |
5812 | ++the text as the body. The blank line separating the | |
5813 | ++summary from the body is critical (unless you omit the body | |
5814 | ++entirely); tools like rebase can get confused if you run | |
5815 | ++the two together. | |
5816 | + | |
5817 | + Further paragraphs come after blank lines. | |
5818 | + | |
5819 | +-- Bullet points are okay, too | |
5820 | +- | |
5821 | +-- Typically a hyphen or asterisk is used for the bullet, followed by a | |
5822 | +- single space, with blank lines in between, but conventions vary here | |
5823 | ++ - Bullet points are okay, too | |
5824 | + | |
5825 | +-- Use a hanging indent | |
5826 | ++ - Typically a hyphen or asterisk is used for the bullet, | |
5827 | ++ preceded by a single space, with blank lines in | |
5828 | ++ between, but conventions vary here | |
5829 | + ---- | |
5830 | + | |
5831 | +-If all your commit messages follow this model, things will be much easier for you and the developers with whom you collaborate. | |
5832 | +-The Git project has well-formatted commit messages -- try running `git log --no-merges` there to see what a nicely-formatted project-commit history looks like. | |
5833 | +- | |
5834 | +-[NOTE] | |
5835 | +-.Do as we say, not as we do. | |
5836 | +-==== | |
5837 | +-For the sake of brevity, many of the examples in this book don't have nicely-formatted commit messages like this; instead, we simply use the `-m` option to `git commit`. | |
5838 | ++If all your commit messages look like this, things will be a lot easier for you and the developers you work with. | |
5839 | ++The Git project has well-formatted commit messages – try running `git log --no-merges` there to see what a nicely formatted project-commit history looks like. | |
5840 | + | |
5841 | +-In short, do as we say, not as we do. | |
5842 | +-==== | |
5843 | ++In the following examples, and throughout most of this book, for the sake of brevity this book doesn't have nicely-formatted messages like this; instead, we use the `-m` option to `git commit`. | |
5844 | ++Do as we say, not as we do. | |
5845 | + | |
5846 | +-[[_private_team]] | |
5847 | + ==== Private Small Team | |
5848 | + | |
5849 | +-(((contributing, private small team))) | |
5850 | + The simplest setup you're likely to encounter is a private project with one or two other developers. | |
5851 | +-``Private,'' in this context, means closed-source -- not accessible to the outside world. | |
5852 | ++``Private,'' in this context, means closed-source – not accessible to the outside world. | |
5853 | + You and the other developers all have push access to the repository. | |
5854 | + | |
5855 | + In this environment, you can follow a workflow similar to what you might do when using Subversion or another centralized system. | |
5856 | + You still get the advantages of things like offline committing and vastly simpler branching and merging, but the workflow can be very similar; the main difference is that merges happen client-side rather than on the server at commit time. | |
5857 | + Let's see what it might look like when two developers start to work together with a shared repository. | |
5858 | + The first developer, John, clones the repository, makes a change, and commits locally. | |
5859 | +-The protocol messages have been replaced with `...` in these examples to shorten them somewhat. | |
5860 | ++(The protocol messages have been replaced with `...` in these examples to shorten them somewhat.) | |
5861 | + | |
5862 | +-[source,console] | |
5863 | + ---- | |
5864 | + # John's Machine | |
5865 | + $ git clone john@githost:simplegit.git | |
5866 | +@@ -120,14 +105,13 @@ | |
5867 | + ... | |
5868 | + $ cd simplegit/ | |
5869 | + $ vim lib/simplegit.rb | |
5870 | +-$ git commit -am 'Remove invalid default value' | |
5871 | +-[master 738ee87] Remove invalid default value | |
5872 | ++$ git commit -am 'removed invalid default value' | |
5873 | ++[master 738ee87] removed invalid default value | |
5874 | + 1 files changed, 1 insertions(+), 1 deletions(-) | |
5875 | + ---- | |
5876 | + | |
5877 | +-The second developer, Jessica, does the same thing -- clones the repository and commits a change: | |
5878 | ++The second developer, Jessica, does the same thing – clones the repository and commits a change: | |
5879 | + | |
5880 | +-[source,console] | |
5881 | + ---- | |
5882 | + # Jessica's Machine | |
5883 | + $ git clone jessica@githost:simplegit.git | |
5884 | +@@ -135,14 +119,13 @@ | |
5885 | + ... | |
5886 | + $ cd simplegit/ | |
5887 | + $ vim TODO | |
5888 | +-$ git commit -am 'Add reset task' | |
5889 | +-[master fbff5bc] Add reset task | |
5890 | ++$ git commit -am 'add reset task' | |
5891 | ++[master fbff5bc] add reset task | |
5892 | + 1 files changed, 1 insertions(+), 0 deletions(-) | |
5893 | + ---- | |
5894 | + | |
5895 | +-Now, Jessica pushes her work to the server, which works just fine: | |
5896 | ++Now, Jessica pushes her work up to the server: | |
5897 | + | |
5898 | +-[source,console] | |
5899 | + ---- | |
5900 | + # Jessica's Machine | |
5901 | + $ git push origin master | |
5902 | +@@ -151,14 +134,8 @@ | |
5903 | + 1edee6b..fbff5bc master -> master | |
5904 | + ---- | |
5905 | + | |
5906 | +-The last line of the output above shows a useful return message from the push operation. | |
5907 | +-The basic format is `<oldref>..<newref> fromref -> toref`, where `oldref` means the old reference, `newref` means the new reference, `fromref` is the name of the local reference being pushed, and `toref` is the name of the remote reference being updated. | |
5908 | +-You'll see similar output like this below in the discussions, so having a basic idea of the meaning will help in understanding the various states of the repositories. | |
5909 | +-More details are available in the documentation for https://git-scm.com/docs/git-push[git-push]. | |
5910 | +- | |
5911 | +-Continuing with this example, shortly afterwards, John makes some changes, commits them to his local repository, and tries to push them to the same server: | |
5912 | ++John tries to push his change up, too: | |
5913 | + | |
5914 | +-[source,console] | |
5915 | + ---- | |
5916 | + # John's Machine | |
5917 | + $ git push origin master | |
5918 | +@@ -167,65 +144,39 @@ | |
5919 | + error: failed to push some refs to 'john@githost:simplegit.git' | |
5920 | + ---- | |
5921 | + | |
5922 | +-In this case, John's push fails because of Jessica's earlier push of _her_ changes. | |
5923 | ++John isn't allowed to push because Jessica has pushed in the meantime. | |
5924 | + This is especially important to understand if you're used to Subversion, because you'll notice that the two developers didn't edit the same file. | |
5925 | +-Although Subversion automatically does such a merge on the server if different files are edited, with Git, you must _first_ merge the commits locally. | |
5926 | +-In other words, John must first fetch Jessica's upstream changes and merge them into his local repository before he will be allowed to push. | |
5927 | +- | |
5928 | +-As a first step, John fetches Jessica's work (this only _fetches_ Jessica's upstream work, it does not yet merge it into John's work): | |
5929 | +- | |
5930 | +-[source,console] | |
5931 | +----- | |
5932 | +-$ git fetch origin | |
5933 | +-... | |
5934 | +-From john@githost:simplegit | |
5935 | +- + 049d078...fbff5bc master -> origin/master | |
5936 | +----- | |
5937 | ++Although Subversion automatically does such a merge on the server if different files are edited, in Git you must merge the commits locally. | |
5938 | ++John has to fetch Jessica's changes and merge them in before he will be allowed to push: | |
5939 | + | |
5940 | + At this point, John's local repository looks something like this: | |
5941 | + | |
5942 | +-.John's divergent history | |
5943 | +-image::images/small-team-1.png[John's divergent history] | |
5944 | +- | |
5945 | +-Now John can merge Jessica's work that he fetched into his own local work: | |
5946 | +- | |
5947 | +-[source,console] | |
5948 | +----- | |
5949 | +-$ git merge origin/master | |
5950 | +-Merge made by the 'recursive' strategy. | |
5951 | +- TODO | 1 + | |
5952 | +- 1 files changed, 1 insertions(+), 0 deletions(-) | |
5953 | +----- | |
5954 | ++.John's divergent history. | |
5955 | ++image::images/small-team-1.png[John's divergent history.] | |
5956 | + | |
5957 | +-As long as that local merge goes smoothly, John's updated history will now look like this: | |
5958 | ++John has a reference to the changes Jessica pushed up, but he has to merge them into his own work before he is allowed to push: | |
5959 | + | |
5960 | +-.John's repository after merging `origin/master` | |
5961 | +-image::images/small-team-2.png[John's repository after merging `origin/master`] | |
5962 | ++The merge goes smoothly – John's commit history now looks like this: | |
5963 | + | |
5964 | +-At this point, John might want to test this new code to make sure none of Jessica's work affects any of his and, as long as everything seems fine, he can finally push the new merged work up to the server: | |
5965 | ++.John's repository after merging `origin/master`. | |
5966 | ++image::images/small-team-2.png[John's repository after merging `origin/master`.] | |
5967 | + | |
5968 | +-[source,console] | |
5969 | +----- | |
5970 | +-$ git push origin master | |
5971 | +-... | |
5972 | +-To john@githost:simplegit.git | |
5973 | +- fbff5bc..72bbc59 master -> master | |
5974 | +----- | |
5975 | ++Now, John can test his code to make sure it still works properly, and then he can push his new merged work up to the server: | |
5976 | + | |
5977 | +-In the end, John's commit history will look like this: | |
5978 | ++Finally, John's commit history looks like this: | |
5979 | + | |
5980 | +-.John's history after pushing to the `origin` server | |
5981 | +-image::images/small-team-3.png[John's history after pushing to the `origin` server] | |
5982 | ++.John's history after pushing to the `origin` server. | |
5983 | ++image::images/small-team-3.png[John's history after pushing to the `origin` server.] | |
5984 | + | |
5985 | +-In the meantime, Jessica has created a new topic branch called `issue54`, and made three commits to that branch. | |
5986 | ++In the meantime, Jessica has been working on a topic branch. | |
5987 | ++She's created a topic branch called `issue54` and done three commits on that branch. | |
5988 | + She hasn't fetched John's changes yet, so her commit history looks like this: | |
5989 | + | |
5990 | +-.Jessica's topic branch | |
5991 | +-image::images/small-team-4.png[Jessica's topic branch] | |
5992 | ++.Jessica's topic branch. | |
5993 | ++image::images/small-team-4.png[Jessica's topic branch.] | |
5994 | + | |
5995 | +-Suddenly, Jessica learns that John has pushed some new work to the server and she wants to take a look at it, so she can fetch all new content from the server that she does not yet have with: | |
5996 | ++Jessica wants to sync up with John, so she fetches: | |
5997 | + | |
5998 | +-[source,console] | |
5999 | + ---- | |
6000 | + # Jessica's Machine | |
6001 | + $ git fetch origin | |
6002 | +@@ -237,100 +188,54 @@ | |
6003 | + That pulls down the work John has pushed up in the meantime. | |
6004 | + Jessica's history now looks like this: | |
6005 | + | |
6006 | +-.Jessica's history after fetching John's changes | |
6007 | +-image::images/small-team-5.png[Jessica's history after fetching John's changes] | |
6008 | ++.Jessica's history after fetching John's changes. | |
6009 | ++image::images/small-team-5.png[Jessica's history after fetching John's changes.] | |
6010 | + | |
6011 | +-Jessica thinks her topic branch is ready, but she wants to know what part of John's fetched work she has to merge into her work so that she can push. | |
6012 | ++Jessica thinks her topic branch is ready, but she wants to know what she has to merge into her work so that she can push. | |
6013 | + She runs `git log` to find out: | |
6014 | + | |
6015 | +-[source,console] | |
6016 | +----- | |
6017 | +-$ git log --no-merges issue54..origin/master | |
6018 | +-commit 738ee872852dfaa9d6634e0dea7a324040193016 | |
6019 | +-Author: John Smith <jsmith@example.com> | |
6020 | +-Date: Fri May 29 16:01:27 2009 -0700 | |
6021 | +- | |
6022 | +- Remove invalid default value | |
6023 | +----- | |
6024 | +- | |
6025 | +-The `issue54..origin/master` syntax is a log filter that asks Git to display only those commits that are on the latter branch (in this case `origin/master`) that are not on the first branch (in this case `issue54`). | |
6026 | +-We'll go over this syntax in detail in <<ch07-git-tools#_commit_ranges>>. | |
6027 | ++The `issue54..origin/master` syntax is a log filter that asks Git to only show the list of commits that are on the latter branch (in this case `origin/master`) that are not on the first branch (in this case `issue54`). | |
6028 | ++We'll go over this syntax in detail in <<ch07-git-tools#r_commit_ranges>>. | |
6029 | + | |
6030 | +-From the above output, we can see that there is a single commit that John has made that Jessica has not merged into her local work. | |
6031 | ++For now, we can see from the output that there is a single commit that John has made that Jessica has not merged in. | |
6032 | + If she merges `origin/master`, that is the single commit that will modify her local work. | |
6033 | + | |
6034 | +-Now, Jessica can merge her topic work into her `master` branch, merge John's work (`origin/master`) into her `master` branch, and then push back to the server again. | |
6035 | +- | |
6036 | +-First (having committed all of the work on her `issue54` topic branch), Jessica switches back to her `master` branch in preparation for integrating all this work: | |
6037 | +- | |
6038 | +-[source,console] | |
6039 | +----- | |
6040 | +-$ git checkout master | |
6041 | +-Switched to branch 'master' | |
6042 | +-Your branch is behind 'origin/master' by 2 commits, and can be fast-forwarded. | |
6043 | +----- | |
6044 | +- | |
6045 | +-Jessica can merge either `origin/master` or `issue54` first -- they're both upstream, so the order doesn't matter. | |
6046 | +-The end snapshot should be identical no matter which order she chooses; only the history will be different. | |
6047 | +-She chooses to merge the `issue54` branch first: | |
6048 | +- | |
6049 | +-[source,console] | |
6050 | +----- | |
6051 | +-$ git merge issue54 | |
6052 | +-Updating fbff5bc..4af4298 | |
6053 | +-Fast forward | |
6054 | +- README | 1 + | |
6055 | +- lib/simplegit.rb | 6 +++++- | |
6056 | +- 2 files changed, 6 insertions(+), 1 deletions(-) | |
6057 | +----- | |
6058 | +- | |
6059 | +-No problems occur; as you can see it was a simple fast-forward merge. | |
6060 | +-Jessica now completes the local merging process by merging John's earlier fetched work that is sitting in the `origin/master` branch: | |
6061 | ++Now, Jessica can merge her topic work into her master branch, merge John's work (`origin/master`) into her `master` branch, and then push back to the server again. | |
6062 | ++First, she switches back to her master branch to integrate all this work: | |
6063 | + | |
6064 | +-[source,console] | |
6065 | +----- | |
6066 | +-$ git merge origin/master | |
6067 | +-Auto-merging lib/simplegit.rb | |
6068 | +-Merge made by the 'recursive' strategy. | |
6069 | +- lib/simplegit.rb | 2 +- | |
6070 | +- 1 files changed, 1 insertions(+), 1 deletions(-) | |
6071 | +----- | |
6072 | ++She can merge either `origin/master` or `issue54` first – they're both upstream, so the order doesn't matter. | |
6073 | ++The end snapshot should be identical no matter which order she chooses; only the history will be slightly different. | |
6074 | ++She chooses to merge in `issue54` first: | |
6075 | + | |
6076 | +-Everything merges cleanly, and Jessica's history now looks like this: | |
6077 | ++No problems occur; as you can see it was a simple fast-forward. | |
6078 | ++Now Jessica merges in John's work (`origin/master`): | |
6079 | + | |
6080 | +-.Jessica's history after merging John's changes | |
6081 | +-image::images/small-team-6.png[Jessica's history after merging John's changes] | |
6082 | ++Everything merges cleanly, and Jessica's history looks like this: | |
6083 | + | |
6084 | +-Now `origin/master` is reachable from Jessica's `master` branch, so she should be able to successfully push (assuming John hasn't pushed even more changes in the meantime): | |
6085 | ++.Jessica's history after merging John's changes. | |
6086 | ++image::images/small-team-6.png[Jessica's history after merging John's changes.] | |
6087 | + | |
6088 | +-[source,console] | |
6089 | +----- | |
6090 | +-$ git push origin master | |
6091 | +-... | |
6092 | +-To jessica@githost:simplegit.git | |
6093 | +- 72bbc59..8059c15 master -> master | |
6094 | +----- | |
6095 | ++Now `origin/master` is reachable from Jessica's `master` branch, so she should be able to successfully push (assuming John hasn't pushed again in the meantime): | |
6096 | + | |
6097 | + Each developer has committed a few times and merged each other's work successfully. | |
6098 | + | |
6099 | +-.Jessica's history after pushing all changes back to the server | |
6100 | +-image::images/small-team-7.png[Jessica's history after pushing all changes back to the server] | |
6101 | ++.Jessica's history after pushing all changes back to the server. | |
6102 | ++image::images/small-team-7.png[Jessica's history after pushing all changes back to the server.] | |
6103 | + | |
6104 | + That is one of the simplest workflows. | |
6105 | +-You work for a while (generally in a topic branch), and merge that work into your `master` branch when it's ready to be integrated. | |
6106 | +-When you want to share that work, you fetch and merge your `master` from `origin/master` if it has changed, and finally push to the `master` branch on the server. | |
6107 | ++You work for a while, generally in a topic branch, and merge into your master branch when it's ready to be integrated. | |
6108 | ++When you want to share that work, you merge it into your own master branch, then fetch and merge `origin/master` if it has changed, and finally push to the `master` branch on the server. | |
6109 | + The general sequence is something like this: | |
6110 | + | |
6111 | +-.General sequence of events for a simple multiple-developer Git workflow | |
6112 | +-image::images/small-team-flow.png[General sequence of events for a simple multiple-developer Git workflow] | |
6113 | ++.General sequence of events for a simple multiple-developer Git workflow. | |
6114 | ++image::images/small-team-flow.png[General sequence of events for a simple multiple-developer Git workflow.] | |
6115 | + | |
6116 | + ==== Private Managed Team | |
6117 | + | |
6118 | +-(((contributing, private managed team))) | |
6119 | + In this next scenario, you'll look at contributor roles in a larger private group. | |
6120 | +-You'll learn how to work in an environment where small groups collaborate on features, after which those team-based contributions are integrated by another party. | |
6121 | ++You'll learn how to work in an environment where small groups collaborate on features and then those team-based contributions are integrated by another party. | |
6122 | + | |
6123 | +-Let's say that John and Jessica are working together on one feature (call this ``featureA''), while Jessica and a third developer, Josie, are working on a second (say, ``featureB''). | |
6124 | ++Let's say that John and Jessica are working together on one feature, while Jessica and Josie are working on a second. | |
6125 | + In this case, the company is using a type of integration-manager workflow where the work of the individual groups is integrated only by certain engineers, and the `master` branch of the main repo can be updated only by those engineers. | |
6126 | + In this scenario, all work is done in team-based branches and pulled together by the integrators later. | |
6127 | + | |
6128 | +@@ -338,33 +243,23 @@ | |
6129 | + Assuming she already has her repository cloned, she decides to work on `featureA` first. | |
6130 | + She creates a new branch for the feature and does some work on it there: | |
6131 | + | |
6132 | +-[source,console] | |
6133 | + ---- | |
6134 | + # Jessica's Machine | |
6135 | + $ git checkout -b featureA | |
6136 | + Switched to a new branch 'featureA' | |
6137 | + $ vim lib/simplegit.rb | |
6138 | +-$ git commit -am 'Add limit to log function' | |
6139 | +-[featureA 3300904] Add limit to log function | |
6140 | ++$ git commit -am 'add limit to log function' | |
6141 | ++[featureA 3300904] add limit to log function | |
6142 | + 1 files changed, 1 insertions(+), 1 deletions(-) | |
6143 | + ---- | |
6144 | + | |
6145 | + At this point, she needs to share her work with John, so she pushes her `featureA` branch commits up to the server. | |
6146 | +-Jessica doesn't have push access to the `master` branch -- only the integrators do -- so she has to push to another branch in order to collaborate with John: | |
6147 | +- | |
6148 | +-[source,console] | |
6149 | +----- | |
6150 | +-$ git push -u origin featureA | |
6151 | +-... | |
6152 | +-To jessica@githost:simplegit.git | |
6153 | +- * [new branch] featureA -> featureA | |
6154 | +----- | |
6155 | ++Jessica doesn't have push access to the `master` branch – only the integrators do – so she has to push to another branch in order to collaborate with John: | |
6156 | + | |
6157 | + Jessica emails John to tell him that she's pushed some work into a branch named `featureA` and he can look at it now. | |
6158 | + While she waits for feedback from John, Jessica decides to start working on `featureB` with Josie. | |
6159 | + To begin, she starts a new feature branch, basing it off the server's `master` branch: | |
6160 | + | |
6161 | +-[source,console] | |
6162 | + ---- | |
6163 | + # Jessica's Machine | |
6164 | + $ git fetch origin | |
6165 | +@@ -374,254 +269,120 @@ | |
6166 | + | |
6167 | + Now, Jessica makes a couple of commits on the `featureB` branch: | |
6168 | + | |
6169 | +-[source,console] | |
6170 | +----- | |
6171 | +-$ vim lib/simplegit.rb | |
6172 | +-$ git commit -am 'Make ls-tree function recursive' | |
6173 | +-[featureB e5b0fdc] Make ls-tree function recursive | |
6174 | +- 1 files changed, 1 insertions(+), 1 deletions(-) | |
6175 | +-$ vim lib/simplegit.rb | |
6176 | +-$ git commit -am 'Add ls-files' | |
6177 | +-[featureB 8512791] Add ls-files | |
6178 | +- 1 files changed, 5 insertions(+), 0 deletions(-) | |
6179 | +----- | |
6180 | +- | |
6181 | +-Jessica's repository now looks like this: | |
6182 | +- | |
6183 | +-.Jessica's initial commit history | |
6184 | +-image::images/managed-team-1.png[Jessica's initial commit history] | |
6185 | +- | |
6186 | +-She's ready to push her work, but gets an email from Josie that a branch with some initial ``featureB'' work on it was already pushed to the server as the `featureBee` branch. | |
6187 | +-Jessica needs to merge those changes with her own before she can push her work to the server. | |
6188 | +-Jessica first fetches Josie's changes with `git fetch`: | |
6189 | +- | |
6190 | +-[source,console] | |
6191 | +----- | |
6192 | +-$ git fetch origin | |
6193 | +-... | |
6194 | +-From jessica@githost:simplegit | |
6195 | +- * [new branch] featureBee -> origin/featureBee | |
6196 | +----- | |
6197 | ++Jessica's repository looks like this: | |
6198 | + | |
6199 | +-Assuming Jessica is still on her checked-out `featureB` branch, she can now merge Josie's work into that branch with `git merge`: | |
6200 | ++.Jessica's initial commit history. | |
6201 | ++image::images/managed-team-1.png[Jessica's initial commit history.] | |
6202 | + | |
6203 | +-[source,console] | |
6204 | +----- | |
6205 | +-$ git merge origin/featureBee | |
6206 | +-Auto-merging lib/simplegit.rb | |
6207 | +-Merge made by the 'recursive' strategy. | |
6208 | +- lib/simplegit.rb | 4 ++++ | |
6209 | +- 1 files changed, 4 insertions(+), 0 deletions(-) | |
6210 | +----- | |
6211 | ++She's ready to push up her work, but gets an email from Josie that a branch with some initial work on it was already pushed to the server as `featureBee`. | |
6212 | ++Jessica first needs to merge those changes in with her own before she can push to the server. | |
6213 | ++She can then fetch Josie's changes down with `git fetch`: | |
6214 | + | |
6215 | +-At this point, Jessica wants to push all of this merged ``featureB'' work back to the server, but she doesn't want to simply push her own `featureB` branch. | |
6216 | +-Rather, since Josie has already started an upstream `featureBee` branch, Jessica wants to push to _that_ branch, which she does with: | |
6217 | ++Jessica can now merge this into the work she did with `git merge`: | |
6218 | + | |
6219 | +-[source,console] | |
6220 | +----- | |
6221 | +-$ git push -u origin featureB:featureBee | |
6222 | +-... | |
6223 | +-To jessica@githost:simplegit.git | |
6224 | +- fba9af8..cd685d1 featureB -> featureBee | |
6225 | +----- | |
6226 | ++There is a bit of a problem – she needs to push the merged work in her `featureB` branch to the `featureBee` branch on the server. | |
6227 | ++She can do so by specifying the local branch followed by a colon (:) followed by the remote branch to the `git push` command: | |
6228 | + | |
6229 | + This is called a _refspec_. | |
6230 | +-See <<ch10-git-internals#_refspec>> for a more detailed discussion of Git refspecs and different things you can do with them. | |
6231 | ++See <<ch10-git-internals#r_refspec>> for a more detailed discussion of Git refspecs and different things you can do with them. | |
6232 | + Also notice the `-u` flag; this is short for `--set-upstream`, which configures the branches for easier pushing and pulling later. | |
6233 | + | |
6234 | +-Suddenly, Jessica gets email from John, who tells her he's pushed some changes to the `featureA` branch on which they are collaborating, and he asks Jessica to take a look at them. | |
6235 | +-Again, Jessica runs a simple `git fetch` to fetch _all_ new content from the server, including (of course) John's latest work: | |
6236 | ++Next, John emails Jessica to say he's pushed some changes to the `featureA` branch and asks her to verify them. | |
6237 | ++She runs a `git fetch` to pull down those changes: | |
6238 | + | |
6239 | +-[source,console] | |
6240 | +----- | |
6241 | +-$ git fetch origin | |
6242 | +-... | |
6243 | +-From jessica@githost:simplegit | |
6244 | +- 3300904..aad881d featureA -> origin/featureA | |
6245 | +----- | |
6246 | +- | |
6247 | +-Jessica can display the log of John's new work by comparing the content of the newly-fetched `featureA` branch with her local copy of the same branch: | |
6248 | +- | |
6249 | +-[source,console] | |
6250 | +----- | |
6251 | +-$ git log featureA..origin/featureA | |
6252 | +-commit aad881d154acdaeb2b6b18ea0e827ed8a6d671e6 | |
6253 | +-Author: John Smith <jsmith@example.com> | |
6254 | +-Date: Fri May 29 19:57:33 2009 -0700 | |
6255 | +- | |
6256 | +- Increase log output to 30 from 25 | |
6257 | +----- | |
6258 | +- | |
6259 | +-If Jessica likes what she sees, she can merge John's new work into her local `featureA` branch with: | |
6260 | +- | |
6261 | +-[source,console] | |
6262 | +----- | |
6263 | +-$ git checkout featureA | |
6264 | +-Switched to branch 'featureA' | |
6265 | +-$ git merge origin/featureA | |
6266 | +-Updating 3300904..aad881d | |
6267 | +-Fast forward | |
6268 | +- lib/simplegit.rb | 10 +++++++++- | |
6269 | +-1 files changed, 9 insertions(+), 1 deletions(-) | |
6270 | +----- | |
6271 | ++Then, she can see what has been changed with `git log`: | |
6272 | + | |
6273 | +-Finally, Jessica might want to make a couple minor changes to all that merged content, so she is free to make those changes, commit them to her local `featureA` branch, and push the end result back to the server: | |
6274 | ++Finally, she merges John's work into her own `featureA` branch: | |
6275 | + | |
6276 | +-[source,console] | |
6277 | +----- | |
6278 | +-$ git commit -am 'Add small tweak to merged content' | |
6279 | +-[featureA 774b3ed] Add small tweak to merged content | |
6280 | +- 1 files changed, 1 insertions(+), 1 deletions(-) | |
6281 | +-$ git push | |
6282 | +-... | |
6283 | +-To jessica@githost:simplegit.git | |
6284 | +- 3300904..774b3ed featureA -> featureA | |
6285 | +----- | |
6286 | ++Jessica wants to tweak something, so she commits again and then pushes this back up to the server: | |
6287 | + | |
6288 | + Jessica's commit history now looks something like this: | |
6289 | + | |
6290 | +-.Jessica's history after committing on a feature branch | |
6291 | +-image::images/managed-team-2.png[Jessica's history after committing on a feature branch] | |
6292 | ++.Jessica's history after committing on a feature branch. | |
6293 | ++image::images/managed-team-2.png[Jessica's history after committing on a feature branch.] | |
6294 | + | |
6295 | +-At some point, Jessica, Josie, and John inform the integrators that the `featureA` and `featureBee` branches on the server are ready for integration into the mainline. | |
6296 | ++Jessica, Josie, and John inform the integrators that the `featureA` and `featureBee` branches on the server are ready for integration into the mainline. | |
6297 | + After the integrators merge these branches into the mainline, a fetch will bring down the new merge commit, making the history look like this: | |
6298 | + | |
6299 | +-.Jessica's history after merging both her topic branches | |
6300 | +-image::images/managed-team-3.png[Jessica's history after merging both her topic branches] | |
6301 | ++.Jessica's history after merging both her topic branches. | |
6302 | ++image::images/managed-team-3.png[Jessica's history after merging both her topic branches.] | |
6303 | + | |
6304 | + Many groups switch to Git because of this ability to have multiple teams working in parallel, merging the different lines of work late in the process. | |
6305 | + The ability of smaller subgroups of a team to collaborate via remote branches without necessarily having to involve or impede the entire team is a huge benefit of Git. | |
6306 | + The sequence for the workflow you saw here is something like this: | |
6307 | + | |
6308 | +-.Basic sequence of this managed-team workflow | |
6309 | +-image::images/managed-team-flow.png[Basic sequence of this managed-team workflow] | |
6310 | ++.Basic sequence of this managed-team workflow. | |
6311 | ++image::images/managed-team-flow.png[Basic sequence of this managed-team workflow.] | |
6312 | + | |
6313 | +-[[_public_project]] | |
6314 | + ==== Forked Public Project | |
6315 | + | |
6316 | +-(((contributing, public small project))) | |
6317 | + Contributing to public projects is a bit different. | |
6318 | + Because you don't have the permissions to directly update branches on the project, you have to get the work to the maintainers some other way. | |
6319 | + This first example describes contributing via forking on Git hosts that support easy forking. | |
6320 | +-Many hosting sites support this (including GitHub, BitBucket, repo.or.cz, and others), and many project maintainers expect this style of contribution. | |
6321 | ++Many hosting sites support this (including GitHub, BitBucket, Google Code, repo.or.cz, and others), and many project maintainers expect this style of contribution. | |
6322 | + The next section deals with projects that prefer to accept contributed patches via email. | |
6323 | + | |
6324 | + First, you'll probably want to clone the main repository, create a topic branch for the patch or patch series you're planning to contribute, and do your work there. | |
6325 | + The sequence looks basically like this: | |
6326 | + | |
6327 | +-[source,console] | |
6328 | +----- | |
6329 | +-$ git clone <url> | |
6330 | +-$ cd project | |
6331 | +-$ git checkout -b featureA | |
6332 | +- ... work ... | |
6333 | +-$ git commit | |
6334 | +- ... work ... | |
6335 | +-$ git commit | |
6336 | +----- | |
6337 | +- | |
6338 | +-[NOTE] | |
6339 | + ==== | |
6340 | +-You may want to use `rebase -i` to squash your work down to a single commit, or rearrange the work in the commits to make the patch easier for the maintainer to review -- see <<ch07-git-tools#_rewriting_history>> for more information about interactive rebasing. | |
6341 | ++You may want to use `rebase -i` to squash your work down to a single commit, or rearrange the work in the commits to make the patch easier for the maintainer to review – see <<ch07-git-tools#r_rewriting_history>> for more information about interactive rebasing. | |
6342 | + ==== | |
6343 | + | |
6344 | + When your branch work is finished and you're ready to contribute it back to the maintainers, go to the original project page and click the ``Fork'' button, creating your own writable fork of the project. | |
6345 | +-You then need to add this repository URL as a new remote of your local repository; in this example, let's call it `myfork`: | |
6346 | +- | |
6347 | +-[source,console] | |
6348 | +----- | |
6349 | +-$ git remote add myfork <url> | |
6350 | +----- | |
6351 | +- | |
6352 | +-You then need to push your new work to this repository. | |
6353 | +-It's easiest to push the topic branch you're working on to your forked repository, rather than merging that work into your `master` branch and pushing that. | |
6354 | +-The reason is that if your work isn't accepted or is cherry-picked, you don't have to rewind your `master` branch (the Git `cherry-pick` operation is covered in more detail in <<ch05-distributed-git#_rebase_cherry_pick>>). | |
6355 | +-If the maintainers `merge`, `rebase`, or `cherry-pick` your work, you'll eventually get it back via pulling from their repository anyhow. | |
6356 | +- | |
6357 | +-In any event, you can push your work with: | |
6358 | ++You then need to add in this new repository URL as a second remote, in this case named `myfork`: | |
6359 | + | |
6360 | +-[source,console] | |
6361 | +----- | |
6362 | +-$ git push -u myfork featureA | |
6363 | +----- | |
6364 | +- | |
6365 | +-(((git commands, request-pull))) | |
6366 | +-Once your work has been pushed to your fork of the repository, you need to notify the maintainers of the original project that you have work you'd like them to merge. | |
6367 | +-This is often called a _pull request_, and you typically generate such a request either via the website -- GitHub has its own ``Pull Request'' mechanism that we'll go over in <<ch06-github#ch06-github>> -- or you can run the `git request-pull` command and email the subsequent output to the project maintainer manually. | |
6368 | +- | |
6369 | +-The `git request-pull` command takes the base branch into which you want your topic branch pulled and the Git repository URL you want them to pull from, and produces a summary of all the changes you're asking to be pulled. | |
6370 | +-For instance, if Jessica wants to send John a pull request, and she's done two commits on the topic branch she just pushed, she can run this: | |
6371 | +- | |
6372 | +-[source,console] | |
6373 | +----- | |
6374 | +-$ git request-pull origin/master myfork | |
6375 | +-The following changes since commit 1edee6b1d61823a2de3b09c160d7080b8d1b3a40: | |
6376 | +-Jessica Smith (1): | |
6377 | +- Create new function | |
6378 | ++Then you need to push your work up to it. | |
6379 | ++It's easiest to push the topic branch you're working on up to your repository, rather than merging into your master branch and pushing that up. | |
6380 | ++The reason is that if the work isn't accepted or is cherry picked, you don't have to rewind your master branch. | |
6381 | ++If the maintainers merge, rebase, or cherry-pick your work, you'll eventually get it back via pulling from their repository anyhow: | |
6382 | + | |
6383 | +-are available in the git repository at: | |
6384 | ++When your work has been pushed up to your fork, you need to notify the maintainer. | |
6385 | ++This is often called a pull request, and you can either generate it via the website – GitHub has its own Pull Request mechanism that we'll go over in <<ch06-github#ch06-github>> – or you can run the `git request-pull` command and email the output to the project maintainer manually. | |
6386 | + | |
6387 | +- git://githost/simplegit.git featureA | |
6388 | ++The `request-pull` command takes the base branch into which you want your topic branch pulled and the Git repository URL you want them to pull from, and outputs a summary of all the changes you're asking to be pulled in. | |
6389 | ++For instance, if Jessica wants to send John a pull request, and she's done two commits on the topic branch she just pushed up, she can run this: | |
6390 | + | |
6391 | +-Jessica Smith (2): | |
6392 | +- Add limit to log function | |
6393 | +- Increase log output to 30 from 25 | |
6394 | +- | |
6395 | +- lib/simplegit.rb | 10 +++++++++- | |
6396 | +- 1 files changed, 9 insertions(+), 1 deletions(-) | |
6397 | +----- | |
6398 | +- | |
6399 | +-This output can be sent to the maintainer -- it tells them where the work was branched from, summarizes the commits, and identifies from where the new work is to be pulled. | |
6400 | ++The output can be sent to the maintainer – it tells them where the work was branched from, summarizes the commits, and tells where to pull this work from. | |
6401 | + | |
6402 | + On a project for which you're not the maintainer, it's generally easier to have a branch like `master` always track `origin/master` and to do your work in topic branches that you can easily discard if they're rejected. | |
6403 | + Having work themes isolated into topic branches also makes it easier for you to rebase your work if the tip of the main repository has moved in the meantime and your commits no longer apply cleanly. | |
6404 | +-For example, if you want to submit a second topic of work to the project, don't continue working on the topic branch you just pushed up -- start over from the main repository's `master` branch: | |
6405 | ++For example, if you want to submit a second topic of work to the project, don't continue working on the topic branch you just pushed up – start over from the main repository's `master` branch: | |
6406 | + | |
6407 | + [source,console] | |
6408 | + ---- | |
6409 | + $ git checkout -b featureB origin/master | |
6410 | +- ... work ... | |
6411 | ++# (work) | |
6412 | + $ git commit | |
6413 | + $ git push myfork featureB | |
6414 | +-$ git request-pull origin/master myfork | |
6415 | +- ... email generated request pull to maintainer ... | |
6416 | ++# (email maintainer) | |
6417 | + $ git fetch origin | |
6418 | + ---- | |
6419 | + | |
6420 | +-Now, each of your topics is contained within a silo -- similar to a patch queue -- that you can rewrite, rebase, and modify without the topics interfering or interdepending on each other, like so: | |
6421 | ++Now, each of your topics is contained within a silo – similar to a patch queue – that you can rewrite, rebase, and modify without the topics interfering or interdepending on each other, like so: | |
6422 | + | |
6423 | +-.Initial commit history with `featureB` work | |
6424 | +-image::images/public-small-1.png[Initial commit history with `featureB` work] | |
6425 | ++.Initial commit history with `featureB` work. | |
6426 | ++image::images/public-small-1.png[Initial commit history with `featureB` work.] | |
6427 | + | |
6428 | + Let's say the project maintainer has pulled in a bunch of other patches and tried your first branch, but it no longer cleanly merges. | |
6429 | + In this case, you can try to rebase that branch on top of `origin/master`, resolve the conflicts for the maintainer, and then resubmit your changes: | |
6430 | + | |
6431 | +-[source,console] | |
6432 | +----- | |
6433 | +-$ git checkout featureA | |
6434 | +-$ git rebase origin/master | |
6435 | +-$ git push -f myfork featureA | |
6436 | +----- | |
6437 | ++This rewrites your history to now look like <<rpsp_b>>. | |
6438 | + | |
6439 | +-This rewrites your history to now look like <<psp_b>>. | |
6440 | +- | |
6441 | +-[[psp_b]] | |
6442 | +-.Commit history after `featureA` work | |
6443 | +-image::images/public-small-2.png[Commit history after `featureA` work] | |
6444 | ++.Commit history after `featureA` work. | |
6445 | ++image::images/public-small-2.png[Commit history after `featureA` work.] | |
6446 | + | |
6447 | + Because you rebased the branch, you have to specify the `-f` to your push command in order to be able to replace the `featureA` branch on the server with a commit that isn't a descendant of it. | |
6448 | + An alternative would be to push this new work to a different branch on the server (perhaps called `featureAv2`). | |
6449 | + | |
6450 | + Let's look at one more possible scenario: the maintainer has looked at work in your second branch and likes the concept but would like you to change an implementation detail. | |
6451 | + You'll also take this opportunity to move the work to be based off the project's current `master` branch. | |
6452 | +-You start a new branch based off the current `origin/master` branch, squash the `featureB` changes there, resolve any conflicts, make the implementation change, and then push that as a new branch: | |
6453 | ++You start a new branch based off the current `origin/master` branch, squash the `featureB` changes there, resolve any conflicts, make the implementation change, and then push that up as a new branch: | |
6454 | + | |
6455 | +-(((git commands, merge, squash))) | |
6456 | +-[source,console] | |
6457 | + ---- | |
6458 | + $ git checkout -b featureBv2 origin/master | |
6459 | + $ git merge --squash featureB | |
6460 | +- ... change implementation ... | |
6461 | ++# (change implementation) | |
6462 | + $ git commit | |
6463 | + $ git push myfork featureBv2 | |
6464 | + ---- | |
6465 | +@@ -630,173 +391,61 @@ | |
6466 | + This means your future commit will have one parent only and allows you to introduce all the changes from another branch and then make more changes before recording the new commit. | |
6467 | + Also the `--no-commit` option can be useful to delay the merge commit in case of the default merge process. | |
6468 | + | |
6469 | +-At this point, you can notify the maintainer that you've made the requested changes, and that they can find those changes in your `featureBv2` branch. | |
6470 | ++Now you can send the maintainer a message that you've made the requested changes and they can find those changes in your `featureBv2` branch. | |
6471 | + | |
6472 | +-.Commit history after `featureBv2` work | |
6473 | +-image::images/public-small-3.png[Commit history after `featureBv2` work] | |
6474 | ++.Commit history after `featureBv2` work. | |
6475 | ++image::images/public-small-3.png[Commit history after `featureBv2` work.] | |
6476 | + | |
6477 | +-[[_project_over_email]] | |
6478 | + ==== Public Project over Email | |
6479 | + | |
6480 | +-(((contributing, public large project))) | |
6481 | +-Many projects have established procedures for accepting patches -- you'll need to check the specific rules for each project, because they will differ. | |
6482 | ++Many projects have established procedures for accepting patches – you'll need to check the specific rules for each project, because they will differ. | |
6483 | + Since there are several older, larger projects which accept patches via a developer mailing list, we'll go over an example of that now. | |
6484 | + | |
6485 | +-The workflow is similar to the previous use case -- you create topic branches for each patch series you work on. | |
6486 | ++The workflow is similar to the previous use case – you create topic branches for each patch series you work on. | |
6487 | + The difference is how you submit them to the project. | |
6488 | + Instead of forking the project and pushing to your own writable version, you generate email versions of each commit series and email them to the developer mailing list: | |
6489 | + | |
6490 | +-[source,console] | |
6491 | + ---- | |
6492 | + $ git checkout -b topicA | |
6493 | +- ... work ... | |
6494 | ++# (work) | |
6495 | + $ git commit | |
6496 | +- ... work ... | |
6497 | ++# (work) | |
6498 | + $ git commit | |
6499 | + ---- | |
6500 | + | |
6501 | +-(((git commands, format-patch))) | |
6502 | + Now you have two commits that you want to send to the mailing list. | |
6503 | +-You use `git format-patch` to generate the mbox-formatted files that you can email to the list -- it turns each commit into an email message with the first line of the commit message as the subject and the rest of the message plus the patch that the commit introduces as the body. | |
6504 | ++You use `git format-patch` to generate the mbox-formatted files that you can email to the list – it turns each commit into an email message with the first line of the commit message as the subject and the rest of the message plus the patch that the commit introduces as the body. | |
6505 | + The nice thing about this is that applying a patch from an email generated with `format-patch` preserves all the commit information properly. | |
6506 | + | |
6507 | +-[source,console] | |
6508 | +----- | |
6509 | +-$ git format-patch -M origin/master | |
6510 | +-0001-add-limit-to-log-function.patch | |
6511 | +-0002-increase-log-output-to-30-from-25.patch | |
6512 | +----- | |
6513 | +- | |
6514 | + The `format-patch` command prints out the names of the patch files it creates. | |
6515 | + The `-M` switch tells Git to look for renames. | |
6516 | + The files end up looking like this: | |
6517 | + | |
6518 | +-[source,console] | |
6519 | +----- | |
6520 | +-$ cat 0001-add-limit-to-log-function.patch | |
6521 | +-From 330090432754092d704da8e76ca5c05c198e71a8 Mon Sep 17 00:00:00 2001 | |
6522 | +-From: Jessica Smith <jessica@example.com> | |
6523 | +-Date: Sun, 6 Apr 2008 10:17:23 -0700 | |
6524 | +-Subject: [PATCH 1/2] Add limit to log function | |
6525 | +- | |
6526 | +-Limit log functionality to the first 20 | |
6527 | +- | |
6528 | +---- | |
6529 | +- lib/simplegit.rb | 2 +- | |
6530 | +- 1 files changed, 1 insertions(+), 1 deletions(-) | |
6531 | +- | |
6532 | +-diff --git a/lib/simplegit.rb b/lib/simplegit.rb | |
6533 | +-index 76f47bc..f9815f1 100644 | |
6534 | +---- a/lib/simplegit.rb | |
6535 | +-+++ b/lib/simplegit.rb | |
6536 | +-@@ -14,7 +14,7 @@ class SimpleGit | |
6537 | +- end | |
6538 | +- | |
6539 | +- def log(treeish = 'master') | |
6540 | +-- command("git log #{treeish}") | |
6541 | +-+ command("git log -n 20 #{treeish}") | |
6542 | +- end | |
6543 | +- | |
6544 | +- def ls_tree(treeish = 'master') | |
6545 | +--- | |
6546 | +-2.1.0 | |
6547 | +----- | |
6548 | +- | |
6549 | + You can also edit these patch files to add more information for the email list that you don't want to show up in the commit message. | |
6550 | +-If you add text between the `---` line and the beginning of the patch (the `diff --git` line), the developers can read it, but that content is ignored by the patching process. | |
6551 | ++If you add text between the `---` line and the beginning of the patch (the `diff --git` line), then developers can read it; but applying the patch excludes it. | |
6552 | + | |
6553 | + To email this to a mailing list, you can either paste the file into your email program or send it via a command-line program. | |
6554 | + Pasting the text often causes formatting issues, especially with ``smarter'' clients that don't preserve newlines and other whitespace appropriately. | |
6555 | + Luckily, Git provides a tool to help you send properly formatted patches via IMAP, which may be easier for you. | |
6556 | + We'll demonstrate how to send a patch via Gmail, which happens to be the email agent we know best; you can read detailed instructions for a number of mail programs at the end of the aforementioned `Documentation/SubmittingPatches` file in the Git source code. | |
6557 | + | |
6558 | +-(((git commands, config)))(((email))) | |
6559 | + First, you need to set up the imap section in your `~/.gitconfig` file. | |
6560 | + You can set each value separately with a series of `git config` commands, or you can add them manually, but in the end your config file should look something like this: | |
6561 | + | |
6562 | +-[source,ini] | |
6563 | +----- | |
6564 | +-[imap] | |
6565 | +- folder = "[Gmail]/Drafts" | |
6566 | +- host = imaps://imap.gmail.com | |
6567 | +- user = user@gmail.com | |
6568 | +- pass = YX]8g76G_2^sFbd | |
6569 | +- port = 993 | |
6570 | +- sslverify = false | |
6571 | +----- | |
6572 | +- | |
6573 | + If your IMAP server doesn't use SSL, the last two lines probably aren't necessary, and the host value will be `imap://` instead of `imaps://`. | |
6574 | + When that is set up, you can use `git imap-send` to place the patch series in the Drafts folder of the specified IMAP server: | |
6575 | + | |
6576 | +-[source,console] | |
6577 | +----- | |
6578 | +-$ cat *.patch |git imap-send | |
6579 | +-Resolving imap.gmail.com... ok | |
6580 | +-Connecting to [74.125.142.109]:993... ok | |
6581 | +-Logging in... | |
6582 | +-sending 2 messages | |
6583 | +-100% (2/2) done | |
6584 | +----- | |
6585 | +- | |
6586 | + At this point, you should be able to go to your Drafts folder, change the To field to the mailing list you're sending the patch to, possibly CC the maintainer or person responsible for that section, and send it off. | |
6587 | + | |
6588 | + You can also send the patches through an SMTP server. | |
6589 | + As before, you can set each value separately with a series of `git config` commands, or you can add them manually in the sendemail section in your `~/.gitconfig` file: | |
6590 | + | |
6591 | +-[source,ini] | |
6592 | +----- | |
6593 | +-[sendemail] | |
6594 | +- smtpencryption = tls | |
6595 | +- smtpserver = smtp.gmail.com | |
6596 | +- smtpuser = user@gmail.com | |
6597 | +- smtpserverport = 587 | |
6598 | +----- | |
6599 | +- | |
6600 | + After this is done, you can use `git send-email` to send your patches: | |
6601 | + | |
6602 | +-[source,console] | |
6603 | +----- | |
6604 | +-$ git send-email *.patch | |
6605 | +-0001-add-limit-to-log-function.patch | |
6606 | +-0002-increase-log-output-to-30-from-25.patch | |
6607 | +-Who should the emails appear to be from? [Jessica Smith <jessica@example.com>] | |
6608 | +-Emails will be sent from: Jessica Smith <jessica@example.com> | |
6609 | +-Who should the emails be sent to? jessica@example.com | |
6610 | +-Message-ID to be used as In-Reply-To for the first email? y | |
6611 | +----- | |
6612 | +- | |
6613 | + Then, Git spits out a bunch of log information looking something like this for each patch you're sending: | |
6614 | + | |
6615 | +-[source,text] | |
6616 | +----- | |
6617 | +-(mbox) Adding cc: Jessica Smith <jessica@example.com> from | |
6618 | +- \line 'From: Jessica Smith <jessica@example.com>' | |
6619 | +-OK. Log says: | |
6620 | +-Sendmail: /usr/sbin/sendmail -i jessica@example.com | |
6621 | +-From: Jessica Smith <jessica@example.com> | |
6622 | +-To: jessica@example.com | |
6623 | +-Subject: [PATCH 1/2] Add limit to log function | |
6624 | +-Date: Sat, 30 May 2009 13:29:15 -0700 | |
6625 | +-Message-Id: <1243715356-61726-1-git-send-email-jessica@example.com> | |
6626 | +-X-Mailer: git-send-email 1.6.2.rc1.20.g8c5b.dirty | |
6627 | +-In-Reply-To: <y> | |
6628 | +-References: <y> | |
6629 | +- | |
6630 | +-Result: OK | |
6631 | +----- | |
6632 | +- | |
6633 | +-[TIP] | |
6634 | +-==== | |
6635 | +-For help on configuring your system and email, more tips and tricks, and a sandbox to send a trial patch via email, go to [git-send-email.io](https://git-send-email.io/). | |
6636 | +-==== | |
6637 | +- | |
6638 | + ==== Summary | |
6639 | + | |
6640 | +-In this section, we covered multiple workflows, and talked about the differences between working as part of a small team on closed-source projects vs contributing to a big public project. | |
6641 | +-You know to check for white-space errors before committing, and can write a great commit message. | |
6642 | +-You learned how to format patches, and e-mail them to a developer mailing list. | |
6643 | +-Dealing with merges was also covered in the context of the different workflows. | |
6644 | +-You are now well prepared to collaborate on any project. | |
6645 | +- | |
6646 | ++This section has covered a number of common workflows for dealing with several very different types of Git projects you're likely to encounter, and introduced a couple of new tools to help you manage this process. | |
6647 | + Next, you'll see how to work the other side of the coin: maintaining a Git project. | |
6648 | + You'll learn how to be a benevolent dictator or integration manager. | |
6649 | +diff -au ../../git/progit2/progit2/book/05-distributed-git/sections/distributed-workflows.asc /tmp/book/05-distributed-git/sections/distributed-workflows.asc.tmp | |
6650 | +--- ../../git/progit2/progit2/book/05-distributed-git/sections/distributed-workflows.asc 2020-07-04 16:36:46.728685513 +0900 | |
6651 | ++++ /tmp/book/05-distributed-git/sections/distributed-workflows.asc.tmp 2020-10-25 09:24:25.747971004 +0900 | |
6652 | +@@ -1,21 +1,19 @@ | |
6653 | + === Distributed Workflows | |
6654 | + | |
6655 | +-(((workflows))) | |
6656 | +-In contrast with Centralized Version Control Systems (CVCSs), the distributed nature of Git allows you to be far more flexible in how developers collaborate on projects. | |
6657 | +-In centralized systems, every developer is a node working more or less equally with a central hub. | |
6658 | +-In Git, however, every developer is potentially both a node and a hub; that is, every developer can both contribute code to other repositories and maintain a public repository on which others can base their work and which they can contribute to. | |
6659 | +-This presents a vast range of workflow possibilities for your project and/or your team, so we'll cover a few common paradigms that take advantage of this flexibility. | |
6660 | ++Unlike Centralized Version Control Systems (CVCSs), the distributed nature of Git allows you to be far more flexible in how developers collaborate on projects. | |
6661 | ++In centralized systems, every developer is a node working more or less equally on a central hub. | |
6662 | ++In Git, however, every developer is potentially both a node and a hub – that is, every developer can both contribute code to other repositories and maintain a public repository on which others can base their work and which they can contribute to. | |
6663 | ++This opens a vast range of workflow possibilities for your project and/or your team, so we'll cover a few common paradigms that take advantage of this flexibility. | |
6664 | + We'll go over the strengths and possible weaknesses of each design; you can choose a single one to use, or you can mix and match features from each. | |
6665 | + | |
6666 | + ==== Centralized Workflow | |
6667 | + | |
6668 | +-(((workflows, centralized))) | |
6669 | +-In centralized systems, there is generally a single collaboration model -- the centralized workflow. | |
6670 | +-One central hub, or _repository_, can accept code, and everyone synchronizes their work with it. | |
6671 | +-A number of developers are nodes -- consumers of that hub -- and synchronize with that centralized location. | |
6672 | ++In centralized systems, there is generally a single collaboration model–the centralized workflow. | |
6673 | ++One central hub, or repository, can accept code, and everyone synchronizes their work to it. | |
6674 | ++A number of developers are nodes – consumers of that hub – and synchronize to that one place. | |
6675 | + | |
6676 | +-.Centralized workflow | |
6677 | +-image::images/centralized_workflow.png[Centralized workflow] | |
6678 | ++.Centralized workflow. | |
6679 | ++image::images/centralized_workflow.png[Centralized workflow.] | |
6680 | + | |
6681 | + This means that if two developers clone from the hub and both make changes, the first developer to push their changes back up can do so with no problems. | |
6682 | + The second developer must merge in the first one's work before pushing changes up, so as not to overwrite the first developer's changes. | |
6683 | +@@ -23,7 +21,6 @@ | |
6684 | + | |
6685 | + If you are already comfortable with a centralized workflow in your company or team, you can easily continue using that workflow with Git. | |
6686 | + Simply set up a single repository, and give everyone on your team push access; Git won't let users overwrite each other. | |
6687 | +- | |
6688 | + Say John and Jessica both start working at the same time. | |
6689 | + John finishes his change and pushes it to the server. | |
6690 | + Then Jessica tries to push her changes, but the server rejects them. | |
6691 | +@@ -33,68 +30,50 @@ | |
6692 | + This is also not limited to small teams. | |
6693 | + With Git's branching model, it's possible for hundreds of developers to successfully work on a single project through dozens of branches simultaneously. | |
6694 | + | |
6695 | +-[[_integration_manager]] | |
6696 | + ==== Integration-Manager Workflow | |
6697 | + | |
6698 | +-(((workflows, integration manager))) | |
6699 | + Because Git allows you to have multiple remote repositories, it's possible to have a workflow where each developer has write access to their own public repository and read access to everyone else's. | |
6700 | + This scenario often includes a canonical repository that represents the ``official'' project. | |
6701 | + To contribute to that project, you create your own public clone of the project and push your changes to it. | |
6702 | + Then, you can send a request to the maintainer of the main project to pull in your changes. | |
6703 | + The maintainer can then add your repository as a remote, test your changes locally, merge them into their branch, and push back to their repository. | |
6704 | +-The process works as follows (see <<wfdiag_b>>): | |
6705 | ++The process works as follows (see <<rwfdiag_b>>): | |
6706 | + | |
6707 | + 1. The project maintainer pushes to their public repository. | |
6708 | + 2. A contributor clones that repository and makes changes. | |
6709 | + 3. The contributor pushes to their own public copy. | |
6710 | + 4. The contributor sends the maintainer an email asking them to pull changes. | |
6711 | +-5. The maintainer adds the contributor's repository as a remote and merges locally. | |
6712 | ++5. The maintainer adds the contributor's repo as a remote and merges locally. | |
6713 | + 6. The maintainer pushes merged changes to the main repository. | |
6714 | + | |
6715 | +-[[wfdiag_b]] | |
6716 | +-.Integration-manager workflow | |
6717 | +-image::images/integration-manager.png[Integration-manager workflow] | |
6718 | ++.Integration-manager workflow. | |
6719 | ++image::images/integration-manager.png[Integration-manager workflow.] | |
6720 | + | |
6721 | +-(((forking))) | |
6722 | + This is a very common workflow with hub-based tools like GitHub or GitLab, where it's easy to fork a project and push your changes into your fork for everyone to see. | |
6723 | + One of the main advantages of this approach is that you can continue to work, and the maintainer of the main repository can pull in your changes at any time. | |
6724 | +-Contributors don't have to wait for the project to incorporate their changes -- each party can work at their own pace. | |
6725 | ++Contributors don't have to wait for the project to incorporate their changes – each party can work at their own pace. | |
6726 | + | |
6727 | + ==== Dictator and Lieutenants Workflow | |
6728 | + | |
6729 | +-(((workflows, dictator and lieutenants))) | |
6730 | + This is a variant of a multiple-repository workflow. | |
6731 | + It's generally used by huge projects with hundreds of collaborators; one famous example is the Linux kernel. | |
6732 | +-Various integration managers are in charge of certain parts of the repository; they're called _lieutenants_. | |
6733 | ++Various integration managers are in charge of certain parts of the repository; they're called lieutenants. | |
6734 | + All the lieutenants have one integration manager known as the benevolent dictator. | |
6735 | +-The benevolent dictator pushes from their directory to a reference repository from which all the collaborators need to pull. | |
6736 | +-The process works like this (see <<wfdiag_c>>): | |
6737 | ++The benevolent dictator's repository serves as the reference repository from which all the collaborators need to pull. | |
6738 | ++The process works like this (see <<rwfdiag_c>>): | |
6739 | + | |
6740 | + 1. Regular developers work on their topic branch and rebase their work on top of `master`. | |
6741 | +- The `master` branch is that of the reference repository to which the dictator pushes. | |
6742 | ++ The `master` branch is that of the dictator. | |
6743 | + 2. Lieutenants merge the developers' topic branches into their `master` branch. | |
6744 | + 3. The dictator merges the lieutenants' `master` branches into the dictator's `master` branch. | |
6745 | +-4. Finally, the dictator pushes that `master` branch to the reference repository so the other developers can rebase on it. | |
6746 | ++4. The dictator pushes their `master` to the reference repository so the other developers can rebase on it. | |
6747 | + | |
6748 | +-[[wfdiag_c]] | |
6749 | +-.Benevolent dictator workflow | |
6750 | +-image::images/benevolent-dictator.png[Benevolent dictator workflow] | |
6751 | ++.Benevolent dictator workflow. | |
6752 | ++image::images/benevolent-dictator.png[Benevolent dictator workflow.] | |
6753 | + | |
6754 | + This kind of workflow isn't common, but can be useful in very big projects, or in highly hierarchical environments. | |
6755 | + It allows the project leader (the dictator) to delegate much of the work and collect large subsets of code at multiple points before integrating them. | |
6756 | + | |
6757 | +-[[_patterns_for_managing_source_code_branches]] | |
6758 | +-==== Patterns for Managing Source Code Branches | |
6759 | +- | |
6760 | +-[NOTE] | |
6761 | +-==== | |
6762 | +-Martin Fowler has made a guide "Patterns for Managing Source Code Branches". | |
6763 | +-This guide covers all the common Git workflows, and explains how/when to use them. | |
6764 | +-There's also a section comparing high and low integration frequencies. | |
6765 | +- | |
6766 | +-https://martinfowler.com/articles/branching-patterns.html | |
6767 | +-==== | |
6768 | +- | |
6769 | + ==== Workflows Summary | |
6770 | + | |
6771 | + These are some commonly used workflows that are possible with a distributed system like Git, but you can see that many variations are possible to suit your particular real-world workflow. | |
6772 | +diff -au ../../git/progit2/progit2/book/05-distributed-git/sections/maintaining.asc /tmp/book/05-distributed-git/sections/maintaining.asc.tmp | |
6773 | +--- ../../git/progit2/progit2/book/05-distributed-git/sections/maintaining.asc 2020-07-07 09:30:48.190756402 +0900 | |
6774 | ++++ /tmp/book/05-distributed-git/sections/maintaining.asc.tmp 2020-10-25 09:24:25.755971381 +0900 | |
6775 | +@@ -1,122 +1,64 @@ | |
6776 | + === Maintaining a Project | |
6777 | + | |
6778 | +-(((maintaining a project))) | |
6779 | +-In addition to knowing how to contribute effectively to a project, you'll likely need to know how to maintain one. | |
6780 | ++In addition to knowing how to effectively contribute to a project, you'll likely need to know how to maintain one. | |
6781 | + This can consist of accepting and applying patches generated via `format-patch` and emailed to you, or integrating changes in remote branches for repositories you've added as remotes to your project. | |
6782 | + Whether you maintain a canonical repository or want to help by verifying or approving patches, you need to know how to accept work in a way that is clearest for other contributors and sustainable by you over the long run. | |
6783 | + | |
6784 | + ==== Working in Topic Branches | |
6785 | + | |
6786 | +-(((branches, topic))) | |
6787 | +-When you're thinking of integrating new work, it's generally a good idea to try it out in a _topic branch_ -- a temporary branch specifically made to try out that new work. | |
6788 | ++When you're thinking of integrating new work, it's generally a good idea to try it out in a topic branch – a temporary branch specifically made to try out that new work. | |
6789 | + This way, it's easy to tweak a patch individually and leave it if it's not working until you have time to come back to it. | |
6790 | + If you create a simple branch name based on the theme of the work you're going to try, such as `ruby_client` or something similarly descriptive, you can easily remember it if you have to abandon it for a while and come back later. | |
6791 | +-The maintainer of the Git project tends to namespace these branches as well -- such as `sc/ruby_client`, where `sc` is short for the person who contributed the work. | |
6792 | +-As you'll remember, you can create the branch based off your `master` branch like this: | |
6793 | +- | |
6794 | +-[source,console] | |
6795 | +----- | |
6796 | +-$ git branch sc/ruby_client master | |
6797 | +----- | |
6798 | ++The maintainer of the Git project tends to namespace these branches as well – such as `sc/ruby_client`, where `sc` is short for the person who contributed the work. | |
6799 | ++As you'll remember, you can create the branch based off your master branch like this: | |
6800 | + | |
6801 | + Or, if you want to also switch to it immediately, you can use the `checkout -b` option: | |
6802 | + | |
6803 | +-[source,console] | |
6804 | +----- | |
6805 | +-$ git checkout -b sc/ruby_client master | |
6806 | +----- | |
6807 | +- | |
6808 | +-Now you're ready to add the contributed work that you received into this topic branch and determine if you want to merge it into your longer-term branches. | |
6809 | ++Now you're ready to add your contributed work into this topic branch and determine if you want to merge it into your longer-term branches. | |
6810 | + | |
6811 | +-[[_patches_from_email]] | |
6812 | + ==== Applying Patches from Email | |
6813 | + | |
6814 | +-(((email, applying patches from))) | |
6815 | + If you receive a patch over email that you need to integrate into your project, you need to apply the patch in your topic branch to evaluate it. | |
6816 | + There are two ways to apply an emailed patch: with `git apply` or with `git am`. | |
6817 | + | |
6818 | + ===== Applying a Patch with apply | |
6819 | + | |
6820 | +-(((git commands, apply))) | |
6821 | +-If you received the patch from someone who generated it with `git diff` or some variation of the Unix `diff` command (which is not recommended; see the next section), you can apply it with the `git apply` command. | |
6822 | ++If you received the patch from someone who generated it with the `git diff` or a Unix `diff` command (which is not recommended; see the next section), you can apply it with the `git apply` command. | |
6823 | + Assuming you saved the patch at `/tmp/patch-ruby-client.patch`, you can apply the patch like this: | |
6824 | + | |
6825 | +-[source,console] | |
6826 | +----- | |
6827 | +-$ git apply /tmp/patch-ruby-client.patch | |
6828 | +----- | |
6829 | +- | |
6830 | + This modifies the files in your working directory. | |
6831 | + It's almost identical to running a `patch -p1` command to apply the patch, although it's more paranoid and accepts fewer fuzzy matches than patch. | |
6832 | + It also handles file adds, deletes, and renames if they're described in the `git diff` format, which `patch` won't do. | |
6833 | + Finally, `git apply` is an ``apply all or abort all'' model where either everything is applied or nothing is, whereas `patch` can partially apply patchfiles, leaving your working directory in a weird state. | |
6834 | + `git apply` is overall much more conservative than `patch`. | |
6835 | +-It won't create a commit for you -- after running it, you must stage and commit the changes introduced manually. | |
6836 | ++It won't create a commit for you – after running it, you must stage and commit the changes introduced manually. | |
6837 | + | |
6838 | +-You can also use `git apply` to see if a patch applies cleanly before you try actually applying it -- you can run `git apply --check` with the patch: | |
6839 | +- | |
6840 | +-[source,console] | |
6841 | +----- | |
6842 | +-$ git apply --check 0001-see-if-this-helps-the-gem.patch | |
6843 | +-error: patch failed: ticgit.gemspec:1 | |
6844 | +-error: ticgit.gemspec: patch does not apply | |
6845 | +----- | |
6846 | ++You can also use git apply to see if a patch applies cleanly before you try actually applying it – you can run `git apply --check` with the patch: | |
6847 | + | |
6848 | + If there is no output, then the patch should apply cleanly. | |
6849 | + This command also exits with a non-zero status if the check fails, so you can use it in scripts if you want. | |
6850 | + | |
6851 | +-[[_git_am]] | |
6852 | + ===== Applying a Patch with `am` | |
6853 | + | |
6854 | +-(((git commands, am))) | |
6855 | + If the contributor is a Git user and was good enough to use the `format-patch` command to generate their patch, then your job is easier because the patch contains author information and a commit message for you. | |
6856 | + If you can, encourage your contributors to use `format-patch` instead of `diff` to generate patches for you. | |
6857 | + You should only have to use `git apply` for legacy patches and things like that. | |
6858 | + | |
6859 | +-To apply a patch generated by `format-patch`, you use `git am` (the command is named `am` as it is used to "apply a series of patches from a mailbox"). | |
6860 | ++To apply a patch generated by `format-patch`, you use `git am`. | |
6861 | + Technically, `git am` is built to read an mbox file, which is a simple, plain-text format for storing one or more email messages in one text file. | |
6862 | + It looks something like this: | |
6863 | + | |
6864 | +-[source,console] | |
6865 | +----- | |
6866 | +-From 330090432754092d704da8e76ca5c05c198e71a8 Mon Sep 17 00:00:00 2001 | |
6867 | +-From: Jessica Smith <jessica@example.com> | |
6868 | +-Date: Sun, 6 Apr 2008 10:17:23 -0700 | |
6869 | +-Subject: [PATCH 1/2] Add limit to log function | |
6870 | +- | |
6871 | +-Limit log functionality to the first 20 | |
6872 | +----- | |
6873 | ++This is the beginning of the output of the format-patch command that you saw in the previous section. | |
6874 | ++This is also a valid mbox email format. | |
6875 | ++If someone has emailed you the patch properly using git send-email, and you download that into an mbox format, then you can point git am to that mbox file, and it will start applying all the patches it sees. | |
6876 | ++If you run a mail client that can save several emails out in mbox format, you can save entire patch series into a file and then use git am to apply them one at a time. | |
6877 | + | |
6878 | +-This is the beginning of the output of the `git format-patch` command that you saw in the previous section; it also represents a valid mbox email format. | |
6879 | +-If someone has emailed you the patch properly using `git send-email`, and you download that into an mbox format, then you can point `git am` to that mbox file, and it will start applying all the patches it sees. | |
6880 | +-If you run a mail client that can save several emails out in mbox format, you can save entire patch series into a file and then use `git am` to apply them one at a time. | |
6881 | +- | |
6882 | +-However, if someone uploaded a patch file generated via `git format-patch` to a ticketing system or something similar, you can save the file locally and then pass that file saved on your disk to `git am` to apply it: | |
6883 | +- | |
6884 | +-[source,console] | |
6885 | +----- | |
6886 | +-$ git am 0001-limit-log-function.patch | |
6887 | +-Applying: Add limit to log function | |
6888 | +----- | |
6889 | ++However, if someone uploaded a patch file generated via `format-patch` to a ticketing system or something similar, you can save the file locally and then pass that file saved on your disk to `git am` to apply it: | |
6890 | + | |
6891 | + You can see that it applied cleanly and automatically created the new commit for you. | |
6892 | + The author information is taken from the email's `From` and `Date` headers, and the message of the commit is taken from the `Subject` and body (before the patch) of the email. | |
6893 | + For example, if this patch was applied from the mbox example above, the commit generated would look something like this: | |
6894 | + | |
6895 | +-[source,console] | |
6896 | +----- | |
6897 | +-$ git log --pretty=fuller -1 | |
6898 | +-commit 6c5e70b984a60b3cecd395edd5b48a7575bf58e0 | |
6899 | +-Author: Jessica Smith <jessica@example.com> | |
6900 | +-AuthorDate: Sun Apr 6 10:17:23 2008 -0700 | |
6901 | +-Commit: Scott Chacon <schacon@gmail.com> | |
6902 | +-CommitDate: Thu Apr 9 09:19:06 2009 -0700 | |
6903 | +- | |
6904 | +- Add limit to log function | |
6905 | +- | |
6906 | +- Limit log functionality to the first 20 | |
6907 | +----- | |
6908 | +- | |
6909 | + The `Commit` information indicates the person who applied the patch and the time it was applied. | |
6910 | + The `Author` information is the individual who originally created the patch and when it was originally created. | |
6911 | + | |
6912 | +@@ -124,84 +66,41 @@ | |
6913 | + Perhaps your main branch has diverged too far from the branch the patch was built from, or the patch depends on another patch you haven't applied yet. | |
6914 | + In that case, the `git am` process will fail and ask you what you want to do: | |
6915 | + | |
6916 | +-[source,console] | |
6917 | +----- | |
6918 | +-$ git am 0001-see-if-this-helps-the-gem.patch | |
6919 | +-Applying: See if this helps the gem | |
6920 | +-error: patch failed: ticgit.gemspec:1 | |
6921 | +-error: ticgit.gemspec: patch does not apply | |
6922 | +-Patch failed at 0001. | |
6923 | +-When you have resolved this problem run "git am --resolved". | |
6924 | +-If you would prefer to skip this patch, instead run "git am --skip". | |
6925 | +-To restore the original branch and stop patching run "git am --abort". | |
6926 | +----- | |
6927 | +- | |
6928 | + This command puts conflict markers in any files it has issues with, much like a conflicted merge or rebase operation. | |
6929 | +-You solve this issue much the same way -- edit the file to resolve the conflict, stage the new file, and then run `git am --resolved` to continue to the next patch: | |
6930 | ++You solve this issue much the same way – edit the file to resolve the conflict, stage the new file, and then run `git am --resolved` to continue to the next patch: | |
6931 | + | |
6932 | +-[source,console] | |
6933 | + ---- | |
6934 | + $ (fix the file) | |
6935 | + $ git add ticgit.gemspec | |
6936 | + $ git am --resolved | |
6937 | +-Applying: See if this helps the gem | |
6938 | ++Applying: seeing if this helps the gem | |
6939 | + ---- | |
6940 | + | |
6941 | + If you want Git to try a bit more intelligently to resolve the conflict, you can pass a `-3` option to it, which makes Git attempt a three-way merge. | |
6942 | + This option isn't on by default because it doesn't work if the commit the patch says it was based on isn't in your repository. | |
6943 | +-If you do have that commit -- if the patch was based on a public commit -- then the `-3` option is generally much smarter about applying a conflicting patch: | |
6944 | +- | |
6945 | +-[source,console] | |
6946 | +----- | |
6947 | +-$ git am -3 0001-see-if-this-helps-the-gem.patch | |
6948 | +-Applying: See if this helps the gem | |
6949 | +-error: patch failed: ticgit.gemspec:1 | |
6950 | +-error: ticgit.gemspec: patch does not apply | |
6951 | +-Using index info to reconstruct a base tree... | |
6952 | +-Falling back to patching base and 3-way merge... | |
6953 | +-No changes -- Patch already applied. | |
6954 | +----- | |
6955 | ++If you do have that commit – if the patch was based on a public commit – then the `-3` option is generally much smarter about applying a conflicting patch: | |
6956 | + | |
6957 | +-In this case, without the `-3` option the patch would have been considered as a conflict. | |
6958 | +-Since the `-3` option was used the patch applied cleanly. | |
6959 | ++In this case, this patch had already been applied. | |
6960 | ++Without the `-3` option, it looks like a conflict. | |
6961 | + | |
6962 | + If you're applying a number of patches from an mbox, you can also run the `am` command in interactive mode, which stops at each patch it finds and asks if you want to apply it: | |
6963 | + | |
6964 | +-[source,console] | |
6965 | +----- | |
6966 | +-$ git am -3 -i mbox | |
6967 | +-Commit Body is: | |
6968 | +--------------------------- | |
6969 | +-See if this helps the gem | |
6970 | +--------------------------- | |
6971 | +-Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all | |
6972 | +----- | |
6973 | +- | |
6974 | + This is nice if you have a number of patches saved, because you can view the patch first if you don't remember what it is, or not apply the patch if you've already done so. | |
6975 | + | |
6976 | + When all the patches for your topic are applied and committed into your branch, you can choose whether and how to integrate them into a longer-running branch. | |
6977 | + | |
6978 | +-[[_checking_out_remotes]] | |
6979 | + ==== Checking Out Remote Branches | |
6980 | + | |
6981 | +-(((branches, remote))) | |
6982 | + If your contribution came from a Git user who set up their own repository, pushed a number of changes into it, and then sent you the URL to the repository and the name of the remote branch the changes are in, you can add them as a remote and do merges locally. | |
6983 | + | |
6984 | + For instance, if Jessica sends you an email saying that she has a great new feature in the `ruby-client` branch of her repository, you can test it by adding the remote and checking out that branch locally: | |
6985 | + | |
6986 | +-[source,console] | |
6987 | +----- | |
6988 | +-$ git remote add jessica git://github.com/jessica/myproject.git | |
6989 | +-$ git fetch jessica | |
6990 | +-$ git checkout -b rubyclient jessica/ruby-client | |
6991 | +----- | |
6992 | +- | |
6993 | +-If she emails you again later with another branch containing another great feature, you could directly `fetch` and `checkout` because you already have the remote setup. | |
6994 | ++If she emails you again later with another branch containing another great feature, you can fetch and check out because you already have the remote setup. | |
6995 | + | |
6996 | + This is most useful if you're working with a person consistently. | |
6997 | + If someone only has a single patch to contribute once in a while, then accepting it over email may be less time consuming than requiring everyone to run their own server and having to continually add and remove remotes to get a few patches. | |
6998 | + You're also unlikely to want to have hundreds of remotes, each for someone who contributes only a patch or two. | |
6999 | +-However, scripts and hosted services may make this easier -- it depends largely on how you develop and how your contributors develop. | |
7000 | ++However, scripts and hosted services may make this easier – it depends largely on how you develop and how your contributors develop. | |
7001 | + | |
7002 | + The other advantage of this approach is that you get the history of the commits as well. | |
7003 | + Although you may have legitimate merge issues, you know where in your history their work is based; a proper three-way merge is the default rather than having to supply a `-3` and hope the patch was generated off a public commit to which you have access. | |
7004 | +@@ -209,53 +108,22 @@ | |
7005 | + If you aren't working with a person consistently but still want to pull from them in this way, you can provide the URL of the remote repository to the `git pull` command. | |
7006 | + This does a one-time pull and doesn't save the URL as a remote reference: | |
7007 | + | |
7008 | +-[source,console] | |
7009 | +----- | |
7010 | +-$ git pull https://github.com/onetimeguy/project | |
7011 | +-From https://github.com/onetimeguy/project | |
7012 | +- * branch HEAD -> FETCH_HEAD | |
7013 | +-Merge made by the 'recursive' strategy. | |
7014 | +----- | |
7015 | +- | |
7016 | +-[[_what_is_introduced]] | |
7017 | + ==== Determining What Is Introduced | |
7018 | + | |
7019 | +-(((branches, diffing))) | |
7020 | + Now you have a topic branch that contains contributed work. | |
7021 | + At this point, you can determine what you'd like to do with it. | |
7022 | + This section revisits a couple of commands so you can see how you can use them to review exactly what you'll be introducing if you merge this into your main branch. | |
7023 | + | |
7024 | +-It's often helpful to get a review of all the commits that are in this branch but that aren't in your `master` branch. | |
7025 | +-You can exclude commits in the `master` branch by adding the `--not` option before the branch name. | |
7026 | ++It's often helpful to get a review of all the commits that are in this branch but that aren't in your master branch. | |
7027 | ++You can exclude commits in the master branch by adding the `--not` option before the branch name. | |
7028 | + This does the same thing as the `master..contrib` format that we used earlier. | |
7029 | + For example, if your contributor sends you two patches and you create a branch called `contrib` and applied those patches there, you can run this: | |
7030 | + | |
7031 | +-[source,console] | |
7032 | +----- | |
7033 | +-$ git log contrib --not master | |
7034 | +-commit 5b6235bd297351589efc4d73316f0a68d484f118 | |
7035 | +-Author: Scott Chacon <schacon@gmail.com> | |
7036 | +-Date: Fri Oct 24 09:53:59 2008 -0700 | |
7037 | +- | |
7038 | +- See if this helps the gem | |
7039 | +- | |
7040 | +-commit 7482e0d16d04bea79d0dba8988cc78df655f16a0 | |
7041 | +-Author: Scott Chacon <schacon@gmail.com> | |
7042 | +-Date: Mon Oct 22 19:38:36 2008 -0700 | |
7043 | +- | |
7044 | +- Update gemspec to hopefully work better | |
7045 | +----- | |
7046 | +- | |
7047 | + To see what changes each commit introduces, remember that you can pass the `-p` option to `git log` and it will append the diff introduced to each commit. | |
7048 | + | |
7049 | + To see a full diff of what would happen if you were to merge this topic branch with another branch, you may have to use a weird trick to get the correct results. | |
7050 | + You may think to run this: | |
7051 | + | |
7052 | +-[source,console] | |
7053 | +----- | |
7054 | +-$ git diff master | |
7055 | +----- | |
7056 | +- | |
7057 | + This command gives you a diff, but it may be misleading. | |
7058 | + If your `master` branch has moved forward since you created the topic branch from it, then you'll get seemingly strange results. | |
7059 | + This happens because Git directly compares the snapshots of the last commit of the topic branch you're on and the snapshot of the last commit on the `master` branch. | |
7060 | +@@ -263,294 +131,165 @@ | |
7061 | + | |
7062 | + If `master` is a direct ancestor of your topic branch, this isn't a problem; but if the two histories have diverged, the diff will look like you're adding all the new stuff in your topic branch and removing everything unique to the `master` branch. | |
7063 | + | |
7064 | +-What you really want to see are the changes added to the topic branch -- the work you'll introduce if you merge this branch with `master`. | |
7065 | +-You do that by having Git compare the last commit on your topic branch with the first common ancestor it has with the `master` branch. | |
7066 | ++What you really want to see are the changes added to the topic branch – the work you'll introduce if you merge this branch with master. | |
7067 | ++You do that by having Git compare the last commit on your topic branch with the first common ancestor it has with the master branch. | |
7068 | + | |
7069 | + Technically, you can do that by explicitly figuring out the common ancestor and then running your diff on it: | |
7070 | + | |
7071 | +-[source,console] | |
7072 | +----- | |
7073 | +-$ git merge-base contrib master | |
7074 | +-36c7dba2c95e6bbb78dfa822519ecfec6e1ca649 | |
7075 | +-$ git diff 36c7db | |
7076 | +----- | |
7077 | ++However, that isn't convenient, so Git provides another shorthand for doing the same thing: the triple-dot syntax. | |
7078 | ++In the context of the `diff` command, you can put three periods after another branch to do a `diff` between the last commit of the branch you're on and its common ancestor with another branch: | |
7079 | + | |
7080 | +-or, more concisely: | |
7081 | +- | |
7082 | +-[source,console] | |
7083 | +----- | |
7084 | +-$ git diff $(git merge-base contrib master) | |
7085 | +----- | |
7086 | +- | |
7087 | +-However, neither of those is particularly convenient, so Git provides another shorthand for doing the same thing: the triple-dot syntax. | |
7088 | +-In the context of the `git diff` command, you can put three periods after another branch to do a `diff` between the last commit of the branch you're on and its common ancestor with another branch: | |
7089 | +- | |
7090 | +-[source,console] | |
7091 | +----- | |
7092 | +-$ git diff master...contrib | |
7093 | +----- | |
7094 | +- | |
7095 | +-This command shows you only the work your current topic branch has introduced since its common ancestor with `master`. | |
7096 | ++This command shows you only the work your current topic branch has introduced since its common ancestor with master. | |
7097 | + That is a very useful syntax to remember. | |
7098 | + | |
7099 | + ==== Integrating Contributed Work | |
7100 | + | |
7101 | +-(((integrating work))) | |
7102 | + When all the work in your topic branch is ready to be integrated into a more mainline branch, the question is how to do it. | |
7103 | + Furthermore, what overall workflow do you want to use to maintain your project? | |
7104 | + You have a number of choices, so we'll cover a few of them. | |
7105 | + | |
7106 | + ===== Merging Workflows | |
7107 | + | |
7108 | +-(((workflows, merging))) | |
7109 | +-One basic workflow is to simply merge all that work directly into your `master` branch. | |
7110 | ++One simple workflow merges your work into your `master` branch. | |
7111 | + In this scenario, you have a `master` branch that contains basically stable code. | |
7112 | +-When you have work in a topic branch that you think you've completed, or work that someone else has contributed and you've verified, you merge it into your master branch, delete that just-merged topic branch, and repeat. | |
7113 | +- | |
7114 | +-For instance, if we have a repository with work in two branches named `ruby_client` and `php_client` that looks like <<merwf_a>>, and we merge `ruby_client` followed by `php_client`, your history will end up looking like <<merwf_b>>. | |
7115 | ++When you have work in a topic branch that y |