1Everyday GIT With 20 Commands Or So 2=================================== 3 4<<Basic Repository>> commands are needed by people who have a 5repository --- that is everybody, because every working tree of 6git is a repository. 7 8In addition, <<Individual Developer (Standalone)>> commands are 9essential for anybody who makes a commit, even for somebody who 10works alone. 11 12If you work with other people, you will need commands listed in 13the <<Individual Developer (Participant)>> section as well. 14 15People who play the <<Integrator>> role need to learn some more 16commands in addition to the above. 17 18<<Repository Administration>> commands are for system 19administrators who are responsible for the care and feeding 20of git repositories. 21 22 23Basic Repository[[Basic Repository]] 24------------------------------------ 25 26Everybody uses these commands to maintain git repositories. 27 28 * gitlink:git-init[1] or gitlink:git-clone[1] to create a 29 new repository. 30 31 * gitlink:git-fsck[1] to check the repository for errors. 32 33 * gitlink:git-gc[1] to do common housekeeping tasks such as 34 repack and prune. 35 36Examples 37~~~~~~~~ 38 39Check health and remove cruft.:: 40+ 41------------ 42$ git fsck <1> 43$ git count-objects <2> 44$ git gc <3> 45------------ 46+ 47<1> running without `\--full` is usually cheap and assures the 48repository health reasonably well. 49<2> check how many loose objects there are and how much 50disk space is wasted by not repacking. 51<3> repacks the local repository and performs other housekeeping tasks. Running 52without `--prune` is a safe operation even while other ones are in progress. 53 54Repack a small project into single pack.:: 55+ 56------------ 57$ git gc <1> 58$ git gc --prune 59------------ 60+ 61<1> pack all the objects reachable from the refs into one pack, 62then remove the other packs. 63 64 65Individual Developer (Standalone)[[Individual Developer (Standalone)]] 66---------------------------------------------------------------------- 67 68A standalone individual developer does not exchange patches with 69other people, and works alone in a single repository, using the 70following commands. 71 72 * gitlink:git-show-branch[1] to see where you are. 73 74 * gitlink:git-log[1] to see what happened. 75 76 * gitlink:git-checkout[1] and gitlink:git-branch[1] to switch 77 branches. 78 79 * gitlink:git-add[1] to manage the index file. 80 81 * gitlink:git-diff[1] and gitlink:git-status[1] to see what 82 you are in the middle of doing. 83 84 * gitlink:git-commit[1] to advance the current branch. 85 86 * gitlink:git-reset[1] and gitlink:git-checkout[1] (with 87 pathname parameters) to undo changes. 88 89 * gitlink:git-merge[1] to merge between local branches. 90 91 * gitlink:git-rebase[1] to maintain topic branches. 92 93 * gitlink:git-tag[1] to mark known point. 94 95Examples 96~~~~~~~~ 97 98Use a tarball as a starting point for a new repository.:: 99+ 100------------ 101$ tar zxf frotz.tar.gz 102$ cd frotz 103$ git-init 104$ git add . <1> 105$ git commit -m "import of frotz source tree." 106$ git tag v2.43 <2> 107------------ 108+ 109<1> add everything under the current directory. 110<2> make a lightweight, unannotated tag. 111 112Create a topic branch and develop.:: 113+ 114------------ 115$ git checkout -b alsa-audio <1> 116$ edit/compile/test 117$ git checkout -- curses/ux_audio_oss.c <2> 118$ git add curses/ux_audio_alsa.c <3> 119$ edit/compile/test 120$ git diff HEAD <4> 121$ git commit -a -s <5> 122$ edit/compile/test 123$ git reset --soft HEAD^ <6> 124$ edit/compile/test 125$ git diff ORIG_HEAD <7> 126$ git commit -a -c ORIG_HEAD <8> 127$ git checkout master <9> 128$ git merge alsa-audio <10> 129$ git log --since='3 days ago' <11> 130$ git log v2.43.. curses/ <12> 131------------ 132+ 133<1> create a new topic branch. 134<2> revert your botched changes in `curses/ux_audio_oss.c`. 135<3> you need to tell git if you added a new file; removal and 136modification will be caught if you do `git commit -a` later. 137<4> to see what changes you are committing. 138<5> commit everything as you have tested, with your sign-off. 139<6> take the last commit back, keeping what is in the working tree. 140<7> look at the changes since the premature commit we took back. 141<8> redo the commit undone in the previous step, using the message 142you originally wrote. 143<9> switch to the master branch. 144<10> merge a topic branch into your master branch. 145<11> review commit logs; other forms to limit output can be 146combined and include `\--max-count=10` (show 10 commits), 147`\--until=2005-12-10`, etc. 148<12> view only the changes that touch what's in `curses/` 149directory, since `v2.43` tag. 150 151 152Individual Developer (Participant)[[Individual Developer (Participant)]] 153------------------------------------------------------------------------ 154 155A developer working as a participant in a group project needs to 156learn how to communicate with others, and uses these commands in 157addition to the ones needed by a standalone developer. 158 159 * gitlink:git-clone[1] from the upstream to prime your local 160 repository. 161 162 * gitlink:git-pull[1] and gitlink:git-fetch[1] from "origin" 163 to keep up-to-date with the upstream. 164 165 * gitlink:git-push[1] to shared repository, if you adopt CVS 166 style shared repository workflow. 167 168 * gitlink:git-format-patch[1] to prepare e-mail submission, if 169 you adopt Linux kernel-style public forum workflow. 170 171Examples 172~~~~~~~~ 173 174Clone the upstream and work on it. Feed changes to upstream.:: 175+ 176------------ 177$ git clone git://git.kernel.org/pub/scm/.../torvalds/linux-2.6 my2.6 178$ cd my2.6 179$ edit/compile/test; git commit -a -s <1> 180$ git format-patch origin <2> 181$ git pull <3> 182$ git log -p ORIG_HEAD.. arch/i386 include/asm-i386 <4> 183$ git pull git://git.kernel.org/pub/.../jgarzik/libata-dev.git ALL <5> 184$ git reset --hard ORIG_HEAD <6> 185$ git gc --prune <7> 186$ git fetch --tags <8> 187------------ 188+ 189<1> repeat as needed. 190<2> extract patches from your branch for e-mail submission. 191<3> `git pull` fetches from `origin` by default and merges into the 192current branch. 193<4> immediately after pulling, look at the changes done upstream 194since last time we checked, only in the 195area we are interested in. 196<5> fetch from a specific branch from a specific repository and merge. 197<6> revert the pull. 198<7> garbage collect leftover objects from reverted pull. 199<8> from time to time, obtain official tags from the `origin` 200and store them under `.git/refs/tags/`. 201 202 203Push into another repository.:: 204+ 205------------ 206satellite$ git clone mothership:frotz frotz <1> 207satellite$ cd frotz 208satellite$ git config --get-regexp '^(remote|branch)\.' <2> 209remote.origin.url mothership:frotz 210remote.origin.fetch refs/heads/*:refs/remotes/origin/* 211branch.master.remote origin 212branch.master.merge refs/heads/master 213satellite$ git config remote.origin.push \ 214 master:refs/remotes/satellite/master <3> 215satellite$ edit/compile/test/commit 216satellite$ git push origin <4> 217 218mothership$ cd frotz 219mothership$ git checkout master 220mothership$ git merge satellite/master <5> 221------------ 222+ 223<1> mothership machine has a frotz repository under your home 224directory; clone from it to start a repository on the satellite 225machine. 226<2> clone sets these configuration variables by default. 227It arranges `git pull` to fetch and store the branches of mothership 228machine to local `remotes/origin/*` tracking branches. 229<3> arrange `git push` to push local `master` branch to 230`remotes/satellite/master` branch of the mothership machine. 231<4> push will stash our work away on `remotes/satellite/master` 232tracking branch on the mothership machine. You could use this as 233a back-up method. 234<5> on mothership machine, merge the work done on the satellite 235machine into the master branch. 236 237Branch off of a specific tag.:: 238+ 239------------ 240$ git checkout -b private2.6.14 v2.6.14 <1> 241$ edit/compile/test; git commit -a 242$ git checkout master 243$ git format-patch -k -m --stdout v2.6.14..private2.6.14 | 244 git am -3 -k <2> 245------------ 246+ 247<1> create a private branch based on a well known (but somewhat behind) 248tag. 249<2> forward port all changes in `private2.6.14` branch to `master` branch 250without a formal "merging". 251 252 253Integrator[[Integrator]] 254------------------------ 255 256A fairly central person acting as the integrator in a group 257project receives changes made by others, reviews and integrates 258them and publishes the result for others to use, using these 259commands in addition to the ones needed by participants. 260 261 * gitlink:git-am[1] to apply patches e-mailed in from your 262 contributors. 263 264 * gitlink:git-pull[1] to merge from your trusted lieutenants. 265 266 * gitlink:git-format-patch[1] to prepare and send suggested 267 alternative to contributors. 268 269 * gitlink:git-revert[1] to undo botched commits. 270 271 * gitlink:git-push[1] to publish the bleeding edge. 272 273 274Examples 275~~~~~~~~ 276 277My typical GIT day.:: 278+ 279------------ 280$ git status <1> 281$ git show-branch <2> 282$ mailx <3> 283& s 2 3 4 5 ./+to-apply 284& s 7 8 ./+hold-linus 285& q 286$ git checkout -b topic/one master 287$ git am -3 -i -s -u ./+to-apply <4> 288$ compile/test 289$ git checkout -b hold/linus && git am -3 -i -s -u ./+hold-linus <5> 290$ git checkout topic/one && git rebase master <6> 291$ git checkout pu && git reset --hard next <7> 292$ git merge topic/one topic/two && git merge hold/linus <8> 293$ git checkout maint 294$ git cherry-pick master~4 <9> 295$ compile/test 296$ git tag -s -m "GIT 0.99.9x" v0.99.9x <10> 297$ git fetch ko && git show-branch master maint 'tags/ko-*' <11> 298$ git push ko <12> 299$ git push ko v0.99.9x <13> 300------------ 301+ 302<1> see what I was in the middle of doing, if any. 303<2> see what topic branches I have and think about how ready 304they are. 305<3> read mails, save ones that are applicable, and save others 306that are not quite ready. 307<4> apply them, interactively, with my sign-offs. 308<5> create topic branch as needed and apply, again with my 309sign-offs. 310<6> rebase internal topic branch that has not been merged to the 311master, nor exposed as a part of a stable branch. 312<7> restart `pu` every time from the next. 313<8> and bundle topic branches still cooking. 314<9> backport a critical fix. 315<10> create a signed tag. 316<11> make sure I did not accidentally rewind master beyond what I 317already pushed out. `ko` shorthand points at the repository I have 318at kernel.org, and looks like this: 319+ 320------------ 321$ cat .git/remotes/ko 322URL: kernel.org:/pub/scm/git/git.git 323Pull: master:refs/tags/ko-master 324Pull: next:refs/tags/ko-next 325Pull: maint:refs/tags/ko-maint 326Push: master 327Push: next 328Push: +pu 329Push: maint 330------------ 331+ 332In the output from `git show-branch`, `master` should have 333everything `ko-master` has, and `next` should have 334everything `ko-next` has. 335 336<12> push out the bleeding edge. 337<13> push the tag out, too. 338 339 340Repository Administration[[Repository Administration]] 341------------------------------------------------------ 342 343A repository administrator uses the following tools to set up 344and maintain access to the repository by developers. 345 346 * gitlink:git-daemon[1] to allow anonymous download from 347 repository. 348 349 * gitlink:git-shell[1] can be used as a 'restricted login shell' 350 for shared central repository users. 351 352link:howto/update-hook-example.txt[update hook howto] has a good 353example of managing a shared central repository. 354 355 356Examples 357~~~~~~~~ 358We assume the following in /etc/services:: 359+ 360------------ 361$ grep 9418 /etc/services 362git 9418/tcp # Git Version Control System 363------------ 364 365Run git-daemon to serve /pub/scm from inetd.:: 366+ 367------------ 368$ grep git /etc/inetd.conf 369git stream tcp nowait nobody \ 370 /usr/bin/git-daemon git-daemon --inetd --export-all /pub/scm 371------------ 372+ 373The actual configuration line should be on one line. 374 375Run git-daemon to serve /pub/scm from xinetd.:: 376+ 377------------ 378$ cat /etc/xinetd.d/git-daemon 379# default: off 380# description: The git server offers access to git repositories 381service git 382{ 383 disable = no 384 type = UNLISTED 385 port = 9418 386 socket_type = stream 387 wait = no 388 user = nobody 389 server = /usr/bin/git-daemon 390 server_args = --inetd --export-all --base-path=/pub/scm 391 log_on_failure += USERID 392} 393------------ 394+ 395Check your xinetd(8) documentation and setup, this is from a Fedora system. 396Others might be different. 397 398Give push/pull only access to developers.:: 399+ 400------------ 401$ grep git /etc/passwd <1> 402alice:x:1000:1000::/home/alice:/usr/bin/git-shell 403bob:x:1001:1001::/home/bob:/usr/bin/git-shell 404cindy:x:1002:1002::/home/cindy:/usr/bin/git-shell 405david:x:1003:1003::/home/david:/usr/bin/git-shell 406$ grep git /etc/shells <2> 407/usr/bin/git-shell 408------------ 409+ 410<1> log-in shell is set to /usr/bin/git-shell, which does not 411allow anything but `git push` and `git pull`. The users should 412get an ssh access to the machine. 413<2> in many distributions /etc/shells needs to list what is used 414as the login shell. 415 416CVS-style shared repository.:: 417+ 418------------ 419$ grep git /etc/group <1> 420git:x:9418:alice,bob,cindy,david 421$ cd /home/devo.git 422$ ls -l <2> 423 lrwxrwxrwx 1 david git 17 Dec 4 22:40 HEAD -> refs/heads/master 424 drwxrwsr-x 2 david git 4096 Dec 4 22:40 branches 425 -rw-rw-r-- 1 david git 84 Dec 4 22:40 config 426 -rw-rw-r-- 1 david git 58 Dec 4 22:40 description 427 drwxrwsr-x 2 david git 4096 Dec 4 22:40 hooks 428 -rw-rw-r-- 1 david git 37504 Dec 4 22:40 index 429 drwxrwsr-x 2 david git 4096 Dec 4 22:40 info 430 drwxrwsr-x 4 david git 4096 Dec 4 22:40 objects 431 drwxrwsr-x 4 david git 4096 Nov 7 14:58 refs 432 drwxrwsr-x 2 david git 4096 Dec 4 22:40 remotes 433$ ls -l hooks/update <3> 434 -r-xr-xr-x 1 david git 3536 Dec 4 22:40 update 435$ cat info/allowed-users <4> 436refs/heads/master alice\|cindy 437refs/heads/doc-update bob 438refs/tags/v[0-9]* david 439------------ 440+ 441<1> place the developers into the same git group. 442<2> and make the shared repository writable by the group. 443<3> use update-hook example by Carl from Documentation/howto/ 444for branch policy control. 445<4> alice and cindy can push into master, only bob can push into doc-update. 446david is the release manager and is the only person who can 447create and push version tags. 448 449HTTP server to support dumb protocol transfer.:: 450+ 451------------ 452dev$ git update-server-info <1> 453dev$ ftp user@isp.example.com <2> 454ftp> cp -r .git /home/user/myproject.git 455------------ 456+ 457<1> make sure your info/refs and objects/info/packs are up-to-date 458<2> upload to public HTTP server hosted by your ISP.