Christian Couder | b27a23e | 2008-05-24 20:56:44 +0200 | [diff] [blame] | 1 | gittutorial(7) |
| 2 | ============== |
| 3 | |
| 4 | NAME |
| 5 | ---- |
Thomas Ackermann | 022cf2b | 2014-11-11 20:13:36 +0100 | [diff] [blame] | 6 | gittutorial - A tutorial introduction to Git |
Christian Couder | b27a23e | 2008-05-24 20:56:44 +0200 | [diff] [blame] | 7 | |
| 8 | SYNOPSIS |
| 9 | -------- |
Martin von Zweigbergk | 7791a1d | 2011-07-01 22:38:26 -0400 | [diff] [blame] | 10 | [verse] |
Christian Couder | b27a23e | 2008-05-24 20:56:44 +0200 | [diff] [blame] | 11 | git * |
| 12 | |
| 13 | DESCRIPTION |
| 14 | ----------- |
Linus Torvalds | 8c7fa24 | 2005-05-31 19:50:34 -0700 | [diff] [blame] | 15 | |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 16 | This tutorial explains how to import a new project into Git, make |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 17 | changes to it, and share changes with other developers. |
Linus Torvalds | 8c7fa24 | 2005-05-31 19:50:34 -0700 | [diff] [blame] | 18 | |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 19 | If you are instead primarily interested in using Git to fetch a project, |
J. Bruce Fields | cd50aba | 2007-05-17 23:56:08 -0400 | [diff] [blame] | 20 | for example, to test the latest version, you may prefer to start with |
| 21 | the first two chapters of link:user-manual.html[The Git User's Manual]. |
| 22 | |
Jonathan Nieder | 46e56e8 | 2008-06-30 17:17:07 -0500 | [diff] [blame] | 23 | First, note that you can get documentation for a command such as |
Jonathan Nieder | 483bc4f | 2008-06-30 13:56:34 -0500 | [diff] [blame] | 24 | `git log --graph` with: |
Linus Torvalds | 8c7fa24 | 2005-05-31 19:50:34 -0700 | [diff] [blame] | 25 | |
Junio C Hamano | 8db9307 | 2005-08-30 13:51:01 -0700 | [diff] [blame] | 26 | ------------------------------------------------ |
Jonathan Nieder | 3861cd5 | 2008-06-30 17:10:25 -0500 | [diff] [blame] | 27 | $ man git-log |
Junio C Hamano | 8db9307 | 2005-08-30 13:51:01 -0700 | [diff] [blame] | 28 | ------------------------------------------------ |
Linus Torvalds | 8c7fa24 | 2005-05-31 19:50:34 -0700 | [diff] [blame] | 29 | |
Christian Couder | 6e702c2 | 2008-11-17 16:43:04 +0100 | [diff] [blame] | 30 | or: |
| 31 | |
| 32 | ------------------------------------------------ |
| 33 | $ git help log |
| 34 | ------------------------------------------------ |
| 35 | |
| 36 | With the latter, you can use the manual viewer of your choice; see |
| 37 | linkgit:git-help[1] for more information. |
| 38 | |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 39 | It is a good idea to introduce yourself to Git with your name and |
Nicolas Pitre | c14261e | 2007-01-14 22:44:18 -0500 | [diff] [blame] | 40 | public email address before doing any operation. The easiest |
| 41 | way to do so is: |
Junio C Hamano | 6658923 | 2006-11-29 00:17:01 -0800 | [diff] [blame] | 42 | |
| 43 | ------------------------------------------------ |
Tom Prince | e0d10e1 | 2007-01-28 16:16:53 -0800 | [diff] [blame] | 44 | $ git config --global user.name "Your Name Comes Here" |
| 45 | $ git config --global user.email you@yourdomain.example.com |
Junio C Hamano | 6658923 | 2006-11-29 00:17:01 -0800 | [diff] [blame] | 46 | ------------------------------------------------ |
| 47 | |
| 48 | |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 49 | Importing a new project |
Junio C Hamano | 2a29da7 | 2005-08-23 15:28:34 -0700 | [diff] [blame] | 50 | ----------------------- |
Junio C Hamano | 3eb5128 | 2005-07-15 11:40:56 -0700 | [diff] [blame] | 51 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 52 | Assume you have a tarball `project.tar.gz` with your initial work. You |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 53 | can place it under Git revision control as follows. |
Junio C Hamano | 3eb5128 | 2005-07-15 11:40:56 -0700 | [diff] [blame] | 54 | |
Junio C Hamano | c951734 | 2005-08-24 16:46:11 -0700 | [diff] [blame] | 55 | ------------------------------------------------ |
Junio C Hamano | dcc6e28 | 2006-01-22 22:43:59 -0800 | [diff] [blame] | 56 | $ tar xzf project.tar.gz |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 57 | $ cd project |
Nicolas Pitre | 515377e | 2007-01-07 12:31:29 -0500 | [diff] [blame] | 58 | $ git init |
Junio C Hamano | c951734 | 2005-08-24 16:46:11 -0700 | [diff] [blame] | 59 | ------------------------------------------------ |
| 60 | |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 61 | Git will reply |
Junio C Hamano | c951734 | 2005-08-24 16:46:11 -0700 | [diff] [blame] | 62 | |
Junio C Hamano | c951734 | 2005-08-24 16:46:11 -0700 | [diff] [blame] | 63 | ------------------------------------------------ |
Shawn O. Pearce | ef0a89a | 2006-12-15 00:44:58 -0500 | [diff] [blame] | 64 | Initialized empty Git repository in .git/ |
Junio C Hamano | c951734 | 2005-08-24 16:46:11 -0700 | [diff] [blame] | 65 | ------------------------------------------------ |
Junio C Hamano | 2a29da7 | 2005-08-23 15:28:34 -0700 | [diff] [blame] | 66 | |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 67 | You've now initialized the working directory--you may notice a new |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 68 | directory created, named `.git`. |
J. Bruce Fields | 93f9cc6 | 2007-05-18 00:51:42 -0400 | [diff] [blame] | 69 | |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 70 | Next, tell Git to take a snapshot of the contents of all files under the |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 71 | current directory (note the `.`), with `git add`: |
Junio C Hamano | 2a29da7 | 2005-08-23 15:28:34 -0700 | [diff] [blame] | 72 | |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 73 | ------------------------------------------------ |
| 74 | $ git add . |
| 75 | ------------------------------------------------ |
| 76 | |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 77 | This snapshot is now stored in a temporary staging area which Git calls |
J. Bruce Fields | 93f9cc6 | 2007-05-18 00:51:42 -0400 | [diff] [blame] | 78 | the "index". You can permanently store the contents of the index in the |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 79 | repository with `git commit`: |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 80 | |
| 81 | ------------------------------------------------ |
Junio C Hamano | 6658923 | 2006-11-29 00:17:01 -0800 | [diff] [blame] | 82 | $ git commit |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 83 | ------------------------------------------------ |
| 84 | |
J. Bruce Fields | 93f9cc6 | 2007-05-18 00:51:42 -0400 | [diff] [blame] | 85 | This will prompt you for a commit message. You've now stored the first |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 86 | version of your project in Git. |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 87 | |
J. Bruce Fields | 84dee6b | 2007-01-06 22:38:38 -0500 | [diff] [blame] | 88 | Making changes |
| 89 | -------------- |
| 90 | |
J. Bruce Fields | 93f9cc6 | 2007-05-18 00:51:42 -0400 | [diff] [blame] | 91 | Modify some files, then add their updated contents to the index: |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 92 | |
| 93 | ------------------------------------------------ |
J. Bruce Fields | 84dee6b | 2007-01-06 22:38:38 -0500 | [diff] [blame] | 94 | $ git add file1 file2 file3 |
J. Bruce Fields | 93f9cc6 | 2007-05-18 00:51:42 -0400 | [diff] [blame] | 95 | ------------------------------------------------ |
| 96 | |
| 97 | You are now ready to commit. You can see what is about to be committed |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 98 | using `git diff` with the `--cached` option: |
J. Bruce Fields | 93f9cc6 | 2007-05-18 00:51:42 -0400 | [diff] [blame] | 99 | |
| 100 | ------------------------------------------------ |
| 101 | $ git diff --cached |
| 102 | ------------------------------------------------ |
| 103 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 104 | (Without `--cached`, `git diff` will show you any changes that |
J. Bruce Fields | 93f9cc6 | 2007-05-18 00:51:42 -0400 | [diff] [blame] | 105 | you've made but not yet added to the index.) You can also get a brief |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 106 | summary of the situation with `git status`: |
J. Bruce Fields | 93f9cc6 | 2007-05-18 00:51:42 -0400 | [diff] [blame] | 107 | |
| 108 | ------------------------------------------------ |
| 109 | $ git status |
Stefan Naewe | 8942821 | 2014-11-13 10:40:07 +0000 | [diff] [blame] | 110 | On branch master |
| 111 | Changes to be committed: |
Nguyễn Thái Ngọc Duy | 80f537f | 2019-04-25 16:45:58 +0700 | [diff] [blame] | 112 | (use "git restore --staged <file>..." to unstage) |
Stefan Naewe | 8942821 | 2014-11-13 10:40:07 +0000 | [diff] [blame] | 113 | |
| 114 | modified: file1 |
| 115 | modified: file2 |
| 116 | modified: file3 |
| 117 | |
J. Bruce Fields | 93f9cc6 | 2007-05-18 00:51:42 -0400 | [diff] [blame] | 118 | ------------------------------------------------ |
| 119 | |
| 120 | If you need to make any further adjustments, do so now, and then add any |
| 121 | newly modified content to the index. Finally, commit your changes with: |
| 122 | |
| 123 | ------------------------------------------------ |
Junio C Hamano | c1d179f | 2007-01-03 08:38:01 -0800 | [diff] [blame] | 124 | $ git commit |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 125 | ------------------------------------------------ |
| 126 | |
Fred Maranhão | 2feaf4e | 2008-06-11 19:09:48 -0400 | [diff] [blame] | 127 | This will again prompt you for a message describing the change, and then |
J. Bruce Fields | 93f9cc6 | 2007-05-18 00:51:42 -0400 | [diff] [blame] | 128 | record a new version of the project. |
J. Bruce Fields | 84dee6b | 2007-01-06 22:38:38 -0500 | [diff] [blame] | 129 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 130 | Alternatively, instead of running `git add` beforehand, you can use |
Junio C Hamano | 6658923 | 2006-11-29 00:17:01 -0800 | [diff] [blame] | 131 | |
| 132 | ------------------------------------------------ |
| 133 | $ git commit -a |
| 134 | ------------------------------------------------ |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 135 | |
J. Bruce Fields | 93f9cc6 | 2007-05-18 00:51:42 -0400 | [diff] [blame] | 136 | which will automatically notice any modified (but not new) files, add |
| 137 | them to the index, and commit, all in one step. |
J. Bruce Fields | 84dee6b | 2007-01-06 22:38:38 -0500 | [diff] [blame] | 138 | |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 139 | A note on commit messages: Though not required, it's a good idea to |
谢致邦 (XIE Zhibang) | 1627e6b | 2023-10-08 15:19:26 +0000 | [diff] [blame] | 140 | begin the commit message with a single short (no more than 50 |
| 141 | characters) line summarizing the change, followed by a blank line and |
| 142 | then a more thorough description. The text up to the first blank line in |
| 143 | a commit message is treated as the commit title, and that title is used |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 144 | throughout Git. For example, linkgit:git-format-patch[1] turns a |
Jeremy White | 52ffe99 | 2012-09-13 17:27:09 -0500 | [diff] [blame] | 145 | commit into email, and it uses the title on the Subject line and the |
| 146 | rest of the commit in the body. |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 147 | |
Nicolas Pitre | 366bfcb | 2006-12-04 11:13:39 -0500 | [diff] [blame] | 148 | Git tracks content not files |
| 149 | ---------------------------- |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 150 | |
Jonathan Nieder | 483bc4f | 2008-06-30 13:56:34 -0500 | [diff] [blame] | 151 | Many revision control systems provide an `add` command that tells the |
| 152 | system to start tracking changes to a new file. Git's `add` command |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 153 | does something simpler and more powerful: `git add` is used both for new |
J. Bruce Fields | 93f9cc6 | 2007-05-18 00:51:42 -0400 | [diff] [blame] | 154 | and newly modified files, and in both cases it takes a snapshot of the |
| 155 | given files and stages that content in the index, ready for inclusion in |
| 156 | the next commit. |
Nicolas Pitre | 366bfcb | 2006-12-04 11:13:39 -0500 | [diff] [blame] | 157 | |
J. Bruce Fields | 23c9ccb | 2007-06-10 16:20:34 -0400 | [diff] [blame] | 158 | Viewing project history |
| 159 | ----------------------- |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 160 | |
| 161 | At any point you can view the history of your changes using |
| 162 | |
| 163 | ------------------------------------------------ |
J. Bruce Fields | 67e6e5c | 2006-05-21 16:52:34 -0400 | [diff] [blame] | 164 | $ git log |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 165 | ------------------------------------------------ |
| 166 | |
| 167 | If you also want to see complete diffs at each step, use |
| 168 | |
| 169 | ------------------------------------------------ |
J. Bruce Fields | 67e6e5c | 2006-05-21 16:52:34 -0400 | [diff] [blame] | 170 | $ git log -p |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 171 | ------------------------------------------------ |
| 172 | |
Junio C Hamano | c1d179f | 2007-01-03 08:38:01 -0800 | [diff] [blame] | 173 | Often the overview of the change is useful to get a feel of |
| 174 | each step |
| 175 | |
| 176 | ------------------------------------------------ |
| 177 | $ git log --stat --summary |
| 178 | ------------------------------------------------ |
| 179 | |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 180 | Managing branches |
| 181 | ----------------- |
| 182 | |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 183 | A single Git repository can maintain multiple branches of |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 184 | development. To create a new branch named `experimental`, use |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 185 | |
| 186 | ------------------------------------------------ |
| 187 | $ git branch experimental |
| 188 | ------------------------------------------------ |
| 189 | |
| 190 | If you now run |
| 191 | |
| 192 | ------------------------------------------------ |
| 193 | $ git branch |
| 194 | ------------------------------------------------ |
| 195 | |
| 196 | you'll get a list of all existing branches: |
| 197 | |
| 198 | ------------------------------------------------ |
| 199 | experimental |
| 200 | * master |
| 201 | ------------------------------------------------ |
| 202 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 203 | The `experimental` branch is the one you just created, and the |
| 204 | `master` branch is a default branch that was created for you |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 205 | automatically. The asterisk marks the branch you are currently on; |
| 206 | type |
| 207 | |
| 208 | ------------------------------------------------ |
Nguyễn Thái Ngọc Duy | 328c6cb | 2019-03-29 17:39:19 +0700 | [diff] [blame] | 209 | $ git switch experimental |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 210 | ------------------------------------------------ |
| 211 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 212 | to switch to the `experimental` branch. Now edit a file, commit the |
| 213 | change, and switch back to the `master` branch: |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 214 | |
| 215 | ------------------------------------------------ |
| 216 | (edit file) |
| 217 | $ git commit -a |
Nguyễn Thái Ngọc Duy | 328c6cb | 2019-03-29 17:39:19 +0700 | [diff] [blame] | 218 | $ git switch master |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 219 | ------------------------------------------------ |
| 220 | |
| 221 | Check that the change you made is no longer visible, since it was |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 222 | made on the `experimental` branch and you're back on the `master` branch. |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 223 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 224 | You can make a different change on the `master` branch: |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 225 | |
| 226 | ------------------------------------------------ |
| 227 | (edit file) |
| 228 | $ git commit -a |
| 229 | ------------------------------------------------ |
| 230 | |
| 231 | at this point the two branches have diverged, with different changes |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 232 | made in each. To merge the changes made in `experimental` into `master`, run |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 233 | |
| 234 | ------------------------------------------------ |
Nicolas Pitre | c14261e | 2007-01-14 22:44:18 -0500 | [diff] [blame] | 235 | $ git merge experimental |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 236 | ------------------------------------------------ |
| 237 | |
| 238 | If the changes don't conflict, you're done. If there are conflicts, |
| 239 | markers will be left in the problematic files showing the conflict; |
| 240 | |
| 241 | ------------------------------------------------ |
| 242 | $ git diff |
| 243 | ------------------------------------------------ |
| 244 | |
| 245 | will show this. Once you've edited the files to resolve the |
| 246 | conflicts, |
| 247 | |
| 248 | ------------------------------------------------ |
| 249 | $ git commit -a |
| 250 | ------------------------------------------------ |
| 251 | |
| 252 | will commit the result of the merge. Finally, |
| 253 | |
| 254 | ------------------------------------------------ |
| 255 | $ gitk |
| 256 | ------------------------------------------------ |
| 257 | |
| 258 | will show a nice graphical representation of the resulting history. |
| 259 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 260 | At this point you could delete the `experimental` branch with |
Santi Béjar | 9c9410e | 2007-01-03 13:53:27 +0100 | [diff] [blame] | 261 | |
| 262 | ------------------------------------------------ |
| 263 | $ git branch -d experimental |
| 264 | ------------------------------------------------ |
| 265 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 266 | This command ensures that the changes in the `experimental` branch are |
Santi Béjar | 9c9410e | 2007-01-03 13:53:27 +0100 | [diff] [blame] | 267 | already in the current branch. |
| 268 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 269 | If you develop on a branch `crazy-idea`, then regret it, you can always |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 270 | delete the branch with |
| 271 | |
| 272 | ------------------------------------- |
| 273 | $ git branch -D crazy-idea |
Junio C Hamano | dc5f923 | 2005-12-05 00:57:48 -0800 | [diff] [blame] | 274 | ------------------------------------- |
| 275 | |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 276 | Branches are cheap and easy, so this is a good way to try something |
| 277 | out. |
Junio C Hamano | dc5f923 | 2005-12-05 00:57:48 -0800 | [diff] [blame] | 278 | |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 279 | Using Git for collaboration |
Junio C Hamano | 6f60300 | 2005-09-20 18:21:10 -0700 | [diff] [blame] | 280 | --------------------------- |
| 281 | |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 282 | Suppose that Alice has started a new project with a Git repository in |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 283 | `/home/alice/project`, and that Bob, who has a home directory on the |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 284 | same machine, wants to contribute. |
Junio C Hamano | 6f60300 | 2005-09-20 18:21:10 -0700 | [diff] [blame] | 285 | |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 286 | Bob begins with: |
Junio C Hamano | 6f60300 | 2005-09-20 18:21:10 -0700 | [diff] [blame] | 287 | |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 288 | ------------------------------------------------ |
Ian Katz | 5d5e88a | 2008-07-10 14:27:30 -0400 | [diff] [blame] | 289 | bob$ git clone /home/alice/project myrepo |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 290 | ------------------------------------------------ |
Junio C Hamano | 6f60300 | 2005-09-20 18:21:10 -0700 | [diff] [blame] | 291 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 292 | This creates a new directory `myrepo` containing a clone of Alice's |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 293 | repository. The clone is on an equal footing with the original |
Horst H. von Brand | abda1ef | 2006-06-03 16:27:26 -0400 | [diff] [blame] | 294 | project, possessing its own copy of the original project's history. |
Junio C Hamano | 6f60300 | 2005-09-20 18:21:10 -0700 | [diff] [blame] | 295 | |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 296 | Bob then makes some changes and commits them: |
Junio C Hamano | 6f60300 | 2005-09-20 18:21:10 -0700 | [diff] [blame] | 297 | |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 298 | ------------------------------------------------ |
| 299 | (edit files) |
Ian Katz | 5d5e88a | 2008-07-10 14:27:30 -0400 | [diff] [blame] | 300 | bob$ git commit -a |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 301 | (repeat as necessary) |
| 302 | ------------------------------------------------ |
Junio C Hamano | 6f60300 | 2005-09-20 18:21:10 -0700 | [diff] [blame] | 303 | |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 304 | When he's ready, he tells Alice to pull changes from the repository |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 305 | at `/home/bob/myrepo`. She does this with: |
Junio C Hamano | 6f60300 | 2005-09-20 18:21:10 -0700 | [diff] [blame] | 306 | |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 307 | ------------------------------------------------ |
Ian Katz | 5d5e88a | 2008-07-10 14:27:30 -0400 | [diff] [blame] | 308 | alice$ cd /home/alice/project |
| 309 | alice$ git pull /home/bob/myrepo master |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 310 | ------------------------------------------------ |
Junio C Hamano | 6f60300 | 2005-09-20 18:21:10 -0700 | [diff] [blame] | 311 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 312 | This merges the changes from Bob's `master` branch into Alice's |
J. Bruce Fields | 93ee782 | 2006-11-25 22:45:02 -0500 | [diff] [blame] | 313 | current branch. If Alice has made her own changes in the meantime, |
Miklos Vajna | c30e567 | 2009-01-23 19:02:29 +0100 | [diff] [blame] | 314 | then she may need to manually fix any conflicts. |
Junio C Hamano | 6f60300 | 2005-09-20 18:21:10 -0700 | [diff] [blame] | 315 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 316 | The `pull` command thus performs two operations: it fetches changes |
J. Bruce Fields | 93ee782 | 2006-11-25 22:45:02 -0500 | [diff] [blame] | 317 | from a remote branch, then merges them into the current branch. |
Junio C Hamano | 6f60300 | 2005-09-20 18:21:10 -0700 | [diff] [blame] | 318 | |
Junio C Hamano | dc29bc8 | 2008-07-10 14:01:57 -0700 | [diff] [blame] | 319 | Note that in general, Alice would want her local changes committed before |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 320 | initiating this `pull`. If Bob's work conflicts with what Alice did since |
Junio C Hamano | dc29bc8 | 2008-07-10 14:01:57 -0700 | [diff] [blame] | 321 | their histories forked, Alice will use her working tree and the index to |
| 322 | resolve conflicts, and existing local changes will interfere with the |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 323 | conflict resolution process (Git will still perform the fetch but will |
Andrei Rybak | f0b9224 | 2021-07-29 13:02:52 +0200 | [diff] [blame] | 324 | refuse to merge -- Alice will have to get rid of her local changes in |
Junio C Hamano | dc29bc8 | 2008-07-10 14:01:57 -0700 | [diff] [blame] | 325 | some way and pull again when this happens). |
| 326 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 327 | Alice can peek at what Bob did without merging first, using the `fetch` |
Junio C Hamano | dc29bc8 | 2008-07-10 14:01:57 -0700 | [diff] [blame] | 328 | command; this allows Alice to inspect what Bob did, using a special |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 329 | symbol `FETCH_HEAD`, in order to determine if he has anything worth |
Junio C Hamano | dc29bc8 | 2008-07-10 14:01:57 -0700 | [diff] [blame] | 330 | pulling, like this: |
| 331 | |
| 332 | ------------------------------------------------ |
| 333 | alice$ git fetch /home/bob/myrepo master |
Paolo Ciarrocchi | 53d1589 | 2008-08-28 14:23:52 +0200 | [diff] [blame] | 334 | alice$ git log -p HEAD..FETCH_HEAD |
Junio C Hamano | dc29bc8 | 2008-07-10 14:01:57 -0700 | [diff] [blame] | 335 | ------------------------------------------------ |
| 336 | |
| 337 | This operation is safe even if Alice has uncommitted local changes. |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 338 | The range notation `HEAD..FETCH_HEAD` means "show everything that is reachable |
| 339 | from the `FETCH_HEAD` but exclude anything that is reachable from `HEAD`". |
| 340 | Alice already knows everything that leads to her current state (`HEAD`), |
| 341 | and reviews what Bob has in his state (`FETCH_HEAD`) that she has not |
Thadeu Lima de Souza Cascardo | 21d777f | 2009-06-29 12:13:58 -0300 | [diff] [blame] | 342 | seen with this command. |
Paolo Ciarrocchi | 53d1589 | 2008-08-28 14:23:52 +0200 | [diff] [blame] | 343 | |
| 344 | If Alice wants to visualize what Bob did since their histories forked |
| 345 | she can issue the following command: |
| 346 | |
| 347 | ------------------------------------------------ |
| 348 | $ gitk HEAD..FETCH_HEAD |
| 349 | ------------------------------------------------ |
| 350 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 351 | This uses the same two-dot range notation we saw earlier with `git log`. |
Paolo Ciarrocchi | 53d1589 | 2008-08-28 14:23:52 +0200 | [diff] [blame] | 352 | |
| 353 | Alice may want to view what both of them did since they forked. |
| 354 | She can use three-dot form instead of the two-dot form: |
| 355 | |
| 356 | ------------------------------------------------ |
| 357 | $ gitk HEAD...FETCH_HEAD |
| 358 | ------------------------------------------------ |
| 359 | |
| 360 | This means "show everything that is reachable from either one, but |
| 361 | exclude anything that is reachable from both of them". |
| 362 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 363 | Please note that these range notation can be used with both `gitk` |
| 364 | and `git log`. |
Junio C Hamano | dc29bc8 | 2008-07-10 14:01:57 -0700 | [diff] [blame] | 365 | |
| 366 | After inspecting what Bob did, if there is nothing urgent, Alice may |
| 367 | decide to continue working without pulling from Bob. If Bob's history |
| 368 | does have something Alice would immediately need, Alice may choose to |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 369 | stash her work-in-progress first, do a `pull`, and then finally unstash |
Junio C Hamano | dc29bc8 | 2008-07-10 14:01:57 -0700 | [diff] [blame] | 370 | her work-in-progress on top of the resulting history. |
| 371 | |
Junio C Hamano | c1ff284 | 2007-01-17 01:10:14 +0100 | [diff] [blame] | 372 | When you are working in a small closely knit group, it is not |
| 373 | unusual to interact with the same repository over and over |
| 374 | again. By defining 'remote' repository shorthand, you can make |
| 375 | it easier: |
| 376 | |
| 377 | ------------------------------------------------ |
Ian Katz | 5d5e88a | 2008-07-10 14:27:30 -0400 | [diff] [blame] | 378 | alice$ git remote add bob /home/bob/myrepo |
Junio C Hamano | c1ff284 | 2007-01-17 01:10:14 +0100 | [diff] [blame] | 379 | ------------------------------------------------ |
| 380 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 381 | With this, Alice can perform the first part of the `pull` operation |
| 382 | alone using the `git fetch` command without merging them with her own |
Thadeu Lima de Souza Cascardo | 21d777f | 2009-06-29 12:13:58 -0300 | [diff] [blame] | 383 | branch, using: |
Junio C Hamano | 6f60300 | 2005-09-20 18:21:10 -0700 | [diff] [blame] | 384 | |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 385 | ------------------------------------- |
Ian Katz | 5d5e88a | 2008-07-10 14:27:30 -0400 | [diff] [blame] | 386 | alice$ git fetch bob |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 387 | ------------------------------------- |
Junio C Hamano | 6f60300 | 2005-09-20 18:21:10 -0700 | [diff] [blame] | 388 | |
Junio C Hamano | c1ff284 | 2007-01-17 01:10:14 +0100 | [diff] [blame] | 389 | Unlike the longhand form, when Alice fetches from Bob using a |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 390 | remote repository shorthand set up with `git remote`, what was |
Matthieu Moy | 0e615b2 | 2010-11-02 16:31:20 +0100 | [diff] [blame] | 391 | fetched is stored in a remote-tracking branch, in this case |
Junio C Hamano | c1ff284 | 2007-01-17 01:10:14 +0100 | [diff] [blame] | 392 | `bob/master`. So after this: |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 393 | |
| 394 | ------------------------------------- |
Ian Katz | 5d5e88a | 2008-07-10 14:27:30 -0400 | [diff] [blame] | 395 | alice$ git log -p master..bob/master |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 396 | ------------------------------------- |
| 397 | |
| 398 | shows a list of all the changes that Bob made since he branched from |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 399 | Alice's `master` branch. |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 400 | |
Junio C Hamano | c1ff284 | 2007-01-17 01:10:14 +0100 | [diff] [blame] | 401 | After examining those changes, Alice |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 402 | could merge the changes into her `master` branch: |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 403 | |
| 404 | ------------------------------------- |
Ian Katz | 5d5e88a | 2008-07-10 14:27:30 -0400 | [diff] [blame] | 405 | alice$ git merge bob/master |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 406 | ------------------------------------- |
| 407 | |
Matthieu Moy | 60109d0 | 2010-11-02 16:31:21 +0100 | [diff] [blame] | 408 | This `merge` can also be done by 'pulling from her own remote-tracking |
| 409 | branch', like this: |
J. Bruce Fields | 93ee782 | 2006-11-25 22:45:02 -0500 | [diff] [blame] | 410 | |
| 411 | ------------------------------------- |
Ian Katz | 5d5e88a | 2008-07-10 14:27:30 -0400 | [diff] [blame] | 412 | alice$ git pull . remotes/bob/master |
J. Bruce Fields | 93ee782 | 2006-11-25 22:45:02 -0500 | [diff] [blame] | 413 | ------------------------------------- |
| 414 | |
Junio C Hamano | c1ff284 | 2007-01-17 01:10:14 +0100 | [diff] [blame] | 415 | Note that git pull always merges into the current branch, |
Brian Hetro | 0278307 | 2007-08-23 20:44:13 -0400 | [diff] [blame] | 416 | regardless of what else is given on the command line. |
J. Bruce Fields | 93ee782 | 2006-11-25 22:45:02 -0500 | [diff] [blame] | 417 | |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 418 | Later, Bob can update his repo with Alice's latest changes using |
| 419 | |
| 420 | ------------------------------------- |
Ian Katz | 5d5e88a | 2008-07-10 14:27:30 -0400 | [diff] [blame] | 421 | bob$ git pull |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 422 | ------------------------------------- |
| 423 | |
| 424 | Note that he doesn't need to give the path to Alice's repository; |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 425 | when Bob cloned Alice's repository, Git stored the location of her |
J. Bruce Fields | d66409f | 2006-12-31 18:47:38 -0500 | [diff] [blame] | 426 | repository in the repository configuration, and that location is |
| 427 | used for pulls: |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 428 | |
| 429 | ------------------------------------- |
Ian Katz | 5d5e88a | 2008-07-10 14:27:30 -0400 | [diff] [blame] | 430 | bob$ git config --get remote.origin.url |
Alecs King | 8960b5a | 2007-07-06 00:21:16 +0800 | [diff] [blame] | 431 | /home/alice/project |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 432 | ------------------------------------- |
| 433 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 434 | (The complete configuration created by `git clone` is visible using |
Jonathan Nieder | 483bc4f | 2008-06-30 13:56:34 -0500 | [diff] [blame] | 435 | `git config -l`, and the linkgit:git-config[1] man page |
J. Bruce Fields | d66409f | 2006-12-31 18:47:38 -0500 | [diff] [blame] | 436 | explains the meaning of each option.) |
| 437 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 438 | Git also keeps a pristine copy of Alice's `master` branch under the |
| 439 | name `origin/master`: |
J. Bruce Fields | d66409f | 2006-12-31 18:47:38 -0500 | [diff] [blame] | 440 | |
| 441 | ------------------------------------- |
Ian Katz | 5d5e88a | 2008-07-10 14:27:30 -0400 | [diff] [blame] | 442 | bob$ git branch -r |
J. Bruce Fields | d66409f | 2006-12-31 18:47:38 -0500 | [diff] [blame] | 443 | origin/master |
| 444 | ------------------------------------- |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 445 | |
| 446 | If Bob later decides to work from a different host, he can still |
| 447 | perform clones and pulls using the ssh protocol: |
| 448 | |
| 449 | ------------------------------------- |
Ian Katz | 5d5e88a | 2008-07-10 14:27:30 -0400 | [diff] [blame] | 450 | bob$ git clone alice.org:/home/alice/project myrepo |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 451 | ------------------------------------- |
| 452 | |
Jeff King | 0d0bac6 | 2016-01-30 02:21:26 -0500 | [diff] [blame] | 453 | Alternatively, Git has a native protocol, or can use http; |
Dan McGee | 5162e69 | 2007-12-29 00:20:38 -0600 | [diff] [blame] | 454 | see linkgit:git-pull[1] for details. |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 455 | |
| 456 | Git can also be used in a CVS-like mode, with a central repository |
Dan McGee | 5162e69 | 2007-12-29 00:20:38 -0600 | [diff] [blame] | 457 | that various users push changes to; see linkgit:git-push[1] and |
Jonathan Nieder | 6998e4d | 2008-06-30 17:01:21 -0500 | [diff] [blame] | 458 | linkgit:gitcvs-migration[7]. |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 459 | |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 460 | Exploring history |
| 461 | ----------------- |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 462 | |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 463 | Git history is represented as a series of interrelated commits. We |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 464 | have already seen that the `git log` command can list those commits. |
| 465 | Note that first line of each `git log` entry also gives a name for the |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 466 | commit: |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 467 | |
| 468 | ------------------------------------- |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 469 | $ git log |
| 470 | commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7 |
| 471 | Author: Junio C Hamano <junkio@cox.net> |
| 472 | Date: Tue May 16 17:18:22 2006 -0700 |
| 473 | |
| 474 | merge-base: Clarify the comments on post processing. |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 475 | ------------------------------------- |
| 476 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 477 | We can give this name to `git show` to see the details about this |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 478 | commit. |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 479 | |
| 480 | ------------------------------------- |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 481 | $ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7 |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 482 | ------------------------------------- |
| 483 | |
Junio C Hamano | c1d179f | 2007-01-03 08:38:01 -0800 | [diff] [blame] | 484 | But there are other ways to refer to commits. You can use any initial |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 485 | part of the name that is long enough to uniquely identify the commit: |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 486 | |
| 487 | ------------------------------------- |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 488 | $ git show c82a22c39c # the first few characters of the name are |
| 489 | # usually enough |
| 490 | $ git show HEAD # the tip of the current branch |
| 491 | $ git show experimental # the tip of the "experimental" branch |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 492 | ------------------------------------- |
| 493 | |
Santi Béjar | 9c9410e | 2007-01-03 13:53:27 +0100 | [diff] [blame] | 494 | Every commit usually has one "parent" commit |
| 495 | which points to the previous state of the project: |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 496 | |
| 497 | ------------------------------------- |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 498 | $ git show HEAD^ # to see the parent of HEAD |
| 499 | $ git show HEAD^^ # to see the grandparent of HEAD |
| 500 | $ git show HEAD~4 # to see the great-great grandparent of HEAD |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 501 | ------------------------------------- |
| 502 | |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 503 | Note that merge commits may have more than one parent: |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 504 | |
| 505 | ------------------------------------- |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 506 | $ git show HEAD^1 # show the first parent of HEAD (same as HEAD^) |
| 507 | $ git show HEAD^2 # show the second parent of HEAD |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 508 | ------------------------------------- |
| 509 | |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 510 | You can also give commits names of your own; after running |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 511 | |
| 512 | ------------------------------------- |
Jonathan Nieder | b1889c3 | 2008-06-30 01:09:04 -0500 | [diff] [blame] | 513 | $ git tag v2.5 1b2e1d63ff |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 514 | ------------------------------------- |
| 515 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 516 | you can refer to `1b2e1d63ff` by the name `v2.5`. If you intend to |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 517 | share this name with other people (for example, to identify a release |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 518 | version), you should create a "tag" object, and perhaps sign it; see |
Dan McGee | 5162e69 | 2007-12-29 00:20:38 -0600 | [diff] [blame] | 519 | linkgit:git-tag[1] for details. |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 520 | |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 521 | Any Git command that needs to know a commit can take any of these |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 522 | names. For example: |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 523 | |
| 524 | ------------------------------------- |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 525 | $ git diff v2.5 HEAD # compare the current HEAD to v2.5 |
| 526 | $ git branch stable v2.5 # start a new branch named "stable" based |
| 527 | # at v2.5 |
| 528 | $ git reset --hard HEAD^ # reset your current branch and working |
Francis Daly | 3742506 | 2006-06-07 13:56:45 +0100 | [diff] [blame] | 529 | # directory to its state at HEAD^ |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 530 | ------------------------------------- |
| 531 | |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 532 | Be careful with that last command: in addition to losing any changes |
| 533 | in the working directory, it will also remove all later commits from |
| 534 | this branch. If this branch is the only branch containing those |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 535 | commits, they will be lost. Also, don't use `git reset` on a |
Junio C Hamano | a9d1836 | 2007-02-02 22:40:49 -0800 | [diff] [blame] | 536 | publicly-visible branch that other developers pull from, as it will |
| 537 | force needless merges on other developers to clean up the history. |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 538 | If you need to undo changes that you have pushed, use `git revert` |
Robin Rosenberg | 6e2e1cf | 2007-02-04 17:16:39 +0100 | [diff] [blame] | 539 | instead. |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 540 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 541 | The `git grep` command can search for strings in any version of your |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 542 | project, so |
| 543 | |
| 544 | ------------------------------------- |
| 545 | $ git grep "hello" v2.5 |
| 546 | ------------------------------------- |
| 547 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 548 | searches for all occurrences of "hello" in `v2.5`. |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 549 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 550 | If you leave out the commit name, `git grep` will search any of the |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 551 | files it manages in your current directory. So |
| 552 | |
| 553 | ------------------------------------- |
| 554 | $ git grep "hello" |
| 555 | ------------------------------------- |
| 556 | |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 557 | is a quick way to search just the files that are tracked by Git. |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 558 | |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 559 | Many Git commands also take sets of commits, which can be specified |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 560 | in a number of ways. Here are some examples with `git log`: |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 561 | |
| 562 | ------------------------------------- |
| 563 | $ git log v2.5..v2.6 # commits between v2.5 and v2.6 |
| 564 | $ git log v2.5.. # commits since v2.5 |
| 565 | $ git log --since="2 weeks ago" # commits from the last 2 weeks |
| 566 | $ git log v2.5.. Makefile # commits since v2.5 which modify |
| 567 | # Makefile |
| 568 | ------------------------------------- |
| 569 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 570 | You can also give `git log` a "range" of commits where the first is not |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 571 | necessarily an ancestor of the second; for example, if the tips of |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 572 | the branches `stable` and `master` diverged from a common |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 573 | commit some time ago, then |
| 574 | |
| 575 | ------------------------------------- |
Thadeu Lima de Souza Cascardo | 21d777f | 2009-06-29 12:13:58 -0300 | [diff] [blame] | 576 | $ git log stable..master |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 577 | ------------------------------------- |
| 578 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 579 | will list commits made in the `master` branch but not in the |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 580 | stable branch, while |
| 581 | |
| 582 | ------------------------------------- |
Thadeu Lima de Souza Cascardo | 21d777f | 2009-06-29 12:13:58 -0300 | [diff] [blame] | 583 | $ git log master..stable |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 584 | ------------------------------------- |
| 585 | |
| 586 | will show the list of commits made on the stable branch but not |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 587 | the `master` branch. |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 588 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 589 | The `git log` command has a weakness: it must present commits in a |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 590 | list. When the history has lines of development that diverged and |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 591 | then merged back together, the order in which `git log` presents |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 592 | those commits is meaningless. |
| 593 | |
Henrik Austad | c7719fb | 2009-01-05 16:25:36 +0100 | [diff] [blame] | 594 | Most projects with multiple contributors (such as the Linux kernel, |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 595 | or Git itself) have frequent merges, and `gitk` does a better job of |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 596 | visualizing their history. For example, |
| 597 | |
| 598 | ------------------------------------- |
| 599 | $ gitk --since="2 weeks ago" drivers/ |
| 600 | ------------------------------------- |
| 601 | |
| 602 | allows you to browse any commits from the last 2 weeks of commits |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 603 | that modified files under the `drivers` directory. (Note: you can |
J. Bruce Fields | 2be1bc4 | 2006-05-29 19:31:32 -0400 | [diff] [blame] | 604 | adjust gitk's fonts by holding down the control key while pressing |
| 605 | "-" or "+".) |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 606 | |
| 607 | Finally, most commands that take filenames will optionally allow you |
| 608 | to precede any filename by a commit, to specify a particular version |
J. Bruce Fields | 3857386 | 2006-05-29 19:31:33 -0400 | [diff] [blame] | 609 | of the file: |
J. Bruce Fields | f1fe384 | 2006-05-21 16:54:05 -0400 | [diff] [blame] | 610 | |
| 611 | ------------------------------------- |
| 612 | $ git diff v2.5:Makefile HEAD:Makefile.in |
| 613 | ------------------------------------- |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 614 | |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 615 | You can also use `git show` to see any such file: |
J. Bruce Fields | 3857386 | 2006-05-29 19:31:33 -0400 | [diff] [blame] | 616 | |
| 617 | ------------------------------------- |
Santi Béjar | 9c9410e | 2007-01-03 13:53:27 +0100 | [diff] [blame] | 618 | $ git show v2.5:Makefile |
J. Bruce Fields | 3857386 | 2006-05-29 19:31:33 -0400 | [diff] [blame] | 619 | ------------------------------------- |
| 620 | |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 621 | Next Steps |
| 622 | ---------- |
| 623 | |
J. Bruce Fields | e31952d | 2006-05-21 19:49:34 -0400 | [diff] [blame] | 624 | This tutorial should be enough to perform basic distributed revision |
| 625 | control for your projects. However, to fully understand the depth |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 626 | and power of Git you need to understand two simple ideas on which it |
J. Bruce Fields | e31952d | 2006-05-21 19:49:34 -0400 | [diff] [blame] | 627 | is based: |
| 628 | |
| 629 | * The object database is the rather elegant system used to |
| 630 | store the history of your project--files, directories, and |
| 631 | commits. |
| 632 | |
| 633 | * The index file is a cache of the state of a directory tree, |
| 634 | used to create commits, check out working directories, and |
| 635 | hold the various trees involved in a merge. |
| 636 | |
Jonathan Nieder | 6998e4d | 2008-06-30 17:01:21 -0500 | [diff] [blame] | 637 | Part two of this tutorial explains the object |
J. Bruce Fields | e31952d | 2006-05-21 19:49:34 -0400 | [diff] [blame] | 638 | database, the index file, and a few other odds and ends that you'll |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 639 | need to make the most of Git. You can find it at linkgit:gittutorial-2[7]. |
J. Bruce Fields | e31952d | 2006-05-21 19:49:34 -0400 | [diff] [blame] | 640 | |
J. Bruce Fields | cd50aba | 2007-05-17 23:56:08 -0400 | [diff] [blame] | 641 | If you don't want to continue with that right away, a few other |
J. Bruce Fields | e31952d | 2006-05-21 19:49:34 -0400 | [diff] [blame] | 642 | digressions that may be interesting at this point are: |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 643 | |
Dan McGee | 5162e69 | 2007-12-29 00:20:38 -0600 | [diff] [blame] | 644 | * linkgit:git-format-patch[1], linkgit:git-am[1]: These convert |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 645 | series of git commits into emailed patches, and vice versa, |
Henrik Austad | c7719fb | 2009-01-05 16:25:36 +0100 | [diff] [blame] | 646 | useful for projects such as the Linux kernel which rely heavily |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 647 | on emailed patches. |
| 648 | |
Dan McGee | 5162e69 | 2007-12-29 00:20:38 -0600 | [diff] [blame] | 649 | * linkgit:git-bisect[1]: When there is a regression in your |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 650 | project, one way to track down the bug is by searching through |
Martin Ågren | ad353d7 | 2023-04-15 19:29:11 +0200 | [diff] [blame] | 651 | the history to find the exact commit that's to blame. `git bisect` |
J. Bruce Fields | 927a503 | 2006-01-22 23:57:25 -0500 | [diff] [blame] | 652 | can help you perform a binary search for that commit. It is |
| 653 | smart enough to perform a close-to-optimal search even in the |
| 654 | case of complex non-linear history with lots of merged branches. |
| 655 | |
Thomas Rast | 801a011 | 2009-06-06 15:11:07 +0200 | [diff] [blame] | 656 | * linkgit:gitworkflows[7]: Gives an overview of recommended |
| 657 | workflows. |
| 658 | |
Philip Oakley | 673151a | 2014-10-10 22:25:37 +0100 | [diff] [blame] | 659 | * linkgit:giteveryday[7]: Everyday Git with 20 Commands Or So. |
J. Bruce Fields | e31952d | 2006-05-21 19:49:34 -0400 | [diff] [blame] | 660 | |
Jonathan Nieder | 6998e4d | 2008-06-30 17:01:21 -0500 | [diff] [blame] | 661 | * linkgit:gitcvs-migration[7]: Git for CVS users. |
Christian Couder | b27a23e | 2008-05-24 20:56:44 +0200 | [diff] [blame] | 662 | |
| 663 | SEE ALSO |
| 664 | -------- |
| 665 | linkgit:gittutorial-2[7], |
| 666 | linkgit:gitcvs-migration[7], |
Christian Couder | 497c833 | 2008-05-29 19:21:46 +0200 | [diff] [blame] | 667 | linkgit:gitcore-tutorial[7], |
| 668 | linkgit:gitglossary[7], |
Christian Couder | 6e702c2 | 2008-11-17 16:43:04 +0100 | [diff] [blame] | 669 | linkgit:git-help[1], |
Thomas Rast | 801a011 | 2009-06-06 15:11:07 +0200 | [diff] [blame] | 670 | linkgit:gitworkflows[7], |
Philip Oakley | 673151a | 2014-10-10 22:25:37 +0100 | [diff] [blame] | 671 | linkgit:giteveryday[7], |
Christian Couder | b27a23e | 2008-05-24 20:56:44 +0200 | [diff] [blame] | 672 | link:user-manual.html[The Git User's Manual] |
| 673 | |
| 674 | GIT |
| 675 | --- |
Stefan Beller | 941b9c5 | 2017-02-08 17:29:30 -0800 | [diff] [blame] | 676 | Part of the linkgit:git[1] suite |