1Everyday GIT With 20 Commands Or So 2=================================== 3 4GIT suite has over 100 commands, and the manual page for each of 5them discusses what the command does and how it is used in 6detail, but until you know what command should be used in order 7to achieve what you want to do, you cannot tell which manual 8page to look at, and if you know that already you do not need 9the manual. 10 11Does that mean you need to know all of them before you can use 12git? Not at all. Depending on the role you play, the set of 13commands you need to know is slightly different, but in any case 14what you need to learn is far smaller than the full set of 15commands to carry out your day-to-day work. This document is to 16serve as a cheat-sheet and a set of pointers for people playing 17various roles. 18 19<<Basic Repository>> commands are needed by people who has a 20repository --- that is everybody, because every working tree of 21git is a repository. 22 23In addition, <<Individual Developer (Standalone)>> commands are 24essential for anybody who makes a commit, even for somebody who 25works alone. 26 27If you work with other people, you will need commands listed in 28<<Individual Developer (Participant)>> section as well. 29 30People who play <<Integrator>> role need to learn some more 31commands in addition to the above. 32 33<<Repository Administration>> commands are for system 34administrators who are responsible to care and feed git 35repositories to support developers. 36 37 38Basic Repository[[Basic Repository]] 39------------------------------------ 40 41Everybody uses these commands to feed and care git repositories. 42 43 * gitlink:git-init-db[1] or gitlink:git-clone[1] to create a 44 new repository. 45 46 * gitlink:git-fsck-objects[1] to validate the repository. 47 48 * gitlink:git-prune[1] to garbage collect crufts in the 49 repository. 50 51 * gitlink:git-repack[1] to pack loose objects for efficiency. 52 53Examples 54~~~~~~~~ 55 56Check health and remove cruft:: 57+ 58------------ 59$ git fsck-objects <1> 60$ git prune 61$ git count-objects <2> 62$ git repack <3> 63$ git prune <4> 64 65<1> running without "--full" is usually cheap and assures the 66repository health reasonably well. 67<2> check how many loose objects there are and how much 68diskspace is wasted by not repacking. 69<3> without "-a" repacks incrementally. repacking every 4-5MB 70of loose objects accumulation may be a good rule of thumb. 71<4> after repack, prune removes the duplicate loose objects. 72------------ 73 74Repack a small project into single pack:: 75+ 76------------ 77$ git repack -a -d <1> 78$ git prune 79------------ 80 81 82Individual Developer (Standalone)[[Individual Developer (Standalone)]] 83---------------------------------------------------------------------- 84 85A standalone individual developer does not exchange patches with 86other poeple, and works alone in a single repository, using the 87following commands. 88 89 * gitlink:git-show-branch[1] to see where you are. 90 91 * gitlink:git-log[1] to see what happened. 92 93 * gitlink:git-whatchanged[1] to find out where things have 94 come from. 95 96 * gitlink:git-checkout[1] and gitlink:git-branch[1] to switch 97 branches. 98 99 * gitlink:git-add[1] and gitlink:git-update-index[1] to manage 100 the index file. 101 102 * gitlink:git-diff[1] and gitlink:git-status[1] to see what 103 you are in the middle of doing. 104 105 * gitlink:git-commit[1] to advance the current branch. 106 107 * gitlink:git-reset[1] and gitlink:git-checkout[1] (with 108 pathname parameters) to undo changes. 109 110 * gitlink:git-pull[1] with "." as the remote to merge between 111 local branches. 112 113 * gitlink:git-rebase[1] to maintain topic branches. 114 115 * gitlink:git-tag[1] to mark known point. 116 117Examples 118~~~~~~~~ 119 120Extract a tarball and create a working tree and a new repository to keep track of it:: 121+ 122------------ 123$ tar zxf frotz.tar.gz 124$ cd frotz 125$ git-init-db 126$ git add . <1> 127$ git commit -m 'import of frotz source tree.' 128$ git tag v2.43 <2> 129 130<1> add everything under the current directory. 131<2> make a lightweight, unannotated tag. 132------------ 133 134Create a topic branch and develop:: 135+ 136------------ 137$ git checkout -b alsa-audio <1> 138$ edit/compile/test 139$ git checkout -- curses/ux_audio_oss.c <2> 140$ git add curses/ux_audio_alsa.c <3> 141$ edit/compile/test 142$ git diff <4> 143$ git commit -a -s <5> 144$ edit/compile/test 145$ git reset --soft HEAD^ <6> 146$ edit/compile/test 147$ git diff ORIG_HEAD <7> 148$ git commit -a -c ORIG_HEAD <8> 149$ git checkout master <9> 150$ git pull . alsa-audio <10> 151$ git log --since='3 days ago' <11> 152$ git log v2.43.. curses/ <12> 153 154<1> create a new topic branch. 155<2> revert your botched changes in "curses/ux_audio_oss.c". 156<3> you need to tell git if you added a new file; removal and 157modification will be caught if you do "commit -a" later. 158<4> to see what changes you are committing. 159<5> commit everything as you have tested, with your sign-off. 160<6> take the last commit back, keeping what is in the working tree. 161<7> look at the changes since the premature commit we took back. 162<8> redo the commit undone in the previous step, using the message 163you originally wrote. 164<9> switch to the master branch. 165<10> merge a topic branch into your master branch 166<11> or --since='aug 1', --max-count=10 167<12> view only the changes that touch what's in curses/ 168directory, since v2.43 tag. 169------------ 170 171 172Individual Developer (Participant)[[Individual Developer (Participant)]] 173------------------------------------------------------------------------ 174 175A developer working as a participant in a group project needs to 176learn how to communicate with others, and uses these commands in 177addition to the ones needed by a standalone developer. 178 179 * gitlink:git-pull[1] from "origin" to keep up-to-date with 180 the upstream. 181 182 * gitlink:git-push[1] to shared repository if you adopt CVS 183 style shared repository workflow. 184 185 * gitlink:git-format-patch[1] to prepare e-mail submission, if 186 you adopt Linux kernel-style public forum workflow. 187 188 189Examples 190~~~~~~~~ 191 192Clone the upstream and work on it. Feed changes to upstream:: 193+ 194------------ 195$ git clone git://git.kernel.org/pub/scm/.../torvalds/linux-2.6 my2.6 196$ cd my2.6 197$ edit/compile/test; git commit -a -s <1> 198$ git format-patch origin <2> 199$ git pull <3> 200$ git whatchanged -p ORIG_HEAD.. arch/i386 include/asm-i386 <4> 201$ git pull git://git.kernel.org/pub/.../jgarzik/libata-dev.git ALL <5> 202$ git reset --hard ORIG_HEAD <6> 203$ git prune <7> 204 205<1> repeat as needed. 206<2> extract patches from your branch for e-mail submission. 207<3> "pull" fetches from "origin" by default and merges. 208<4> look at the changes since last time we checked, only in the 209area we are interested in. 210<5> fetch from a specific branch from a specific repository and and merge. 211<6> revert the pull. 212<7> garbage collect leftover objects from reverted pull. 213------------ 214 215Branch off of a specific tag:: 216+ 217------------ 218$ git checkout -b private2.6.14 v2.6.14 <1> 219$ edit/compile/test; git commit -a 220$ git checkout master 221$ git format-patch -k -m --stdout v2.6.14..private2.6.14 | 222 git am -3 -k <2> 223 224<1> create a private branch based on a well known (but somewhat behind) 225tag. 226<2> forward port all changes in private2.6.14 branch to master branch 227without a formal "merging". 228------------ 229 230 231Integrator[[Integrator]] 232------------------------ 233 234A fairly central person acting as the integrator in a group 235project receives changes made by others, reviews and integrates 236them and publishes the result for others to use, using these 237commands in addition to the ones needed by participants. 238 239 * gitlink:git-am[1] to apply patches e-mailed in from your 240 contributors. 241 242 * gitlink:git-pull[1] to merge from your trusted lieutenants. 243 244 * gitlink:git-format-patch[1] to prepare and send suggested 245 alternative to contributors. 246 247 * gitlink:git-revert[1] to undo botched commits. 248 249 * gitlink:git-push[1] to publish the bleeding edge. 250 251 252Examples 253~~~~~~~~ 254 255My typical GIT day:: 256+ 257------------ 258$ git status <1> 259$ git show-branch <2> 260$ mailx <3> 261& s 2 3 4 5 ./+to-apply 262& s 7 8 ./+hold-linus 263& q 264$ git checkout master 265$ git am -3 -i -s -u ./+to-apply <4> 266$ compile/test 267$ git checkout -b hold/linus && git am -3 -i -s -u ./+hold-linus <5> 268$ git checkout topic/one && git rebase master <6> 269$ git checkout pu && git reset --hard master <7> 270$ git pull . topic/one topic/two && git pull . hold/linus <8> 271$ git fetch ko master:refs/tags/ko-master && 272 git show-branch master ko-master <9> 273$ git push ko <10> 274$ git checkout maint 275$ git cherry-pick master~4 <11> 276$ compile/test 277$ git tag -s -m 'GIT 0.99.9x' v0.99.9x <12> 278$ git push ko v0.99.9x <13> 279 280<1> see what I was in the middle of doing, if any. 281<2> see what topic branches I have and think about how ready 282they are. 283<3> read mails, save ones that are applicable, and save others 284that are not quite ready. 285<4> apply them, interactively, with my sign-offs. 286<5> create topic branch as needed and apply, again with my 287sign-offs. 288<6> rebase internal topic branch that has not been merged to the 289master, nor exposed as a part of a stable branch. 290<7> restart "pu" every time from the master. 291<8> and bundle topic branches still cooking. 292<9> make sure I did not accidentally rewound master beyond what I 293already pushed out. 294<10> push out the bleeding edge. 295<11> backport a critical fix. 296<12> create a signed tag. 297<13> push the tag out. 298------------ 299 300 301Repository Administration[[Repository Administration]] 302------------------------------------------------------ 303 304A repository administrator uses the following tools to set up 305and maintain access to the repository by developers. 306 307 * gitlink:git-daemon[1] to allow anonymous download from 308 repository. 309 310 * gitlink:git-shell[1] can be used as a 'restricted login shell' 311 for shared central repository users. 312 313 * link:howto/update-hook-example.txt[update hook howto] has a 314 good example of managing a shared central repository. 315 316 317Examples 318~~~~~~~~ 319 320Run git-daemon to serve /pub/scm from inetd:: 321+ 322------------ 323$ grep git /etc/inet.conf 324git stream tcp nowait nobody /usr/bin/git-daemon git-daemon --inetd --syslog --export-all /pub/scm 325------------ 326 327Give push/pull only access to developers:: 328+ 329------------ 330$ grep git /etc/shells 331/usr/bin/git-shell 332$ grep git /etc/passwd 333alice:x:1000:1000::/home/alice:/usr/bin/git-shell 334bob:x:1001:1001::/home/bob:/usr/bin/git-shell 335cindy:x:1002:1002::/home/cindy:/usr/bin/git-shell 336david:x:1003:1003::/home/david:/usr/bin/git-shell 337------------