Changes between Version 1 and Version 2 of GitCrashCourse


Ignore:
Timestamp:
04/23/10 10:56:34 (14 years ago)
Author:
Samuli Seppänen
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • GitCrashCourse

    v1 v2  
    33What this means is that you actually download the complete code repository to your own hard drive.  This is different from centralised VCS/SCMs, which only have a copy of the files you checked out.  With git you get a complete copy of the complete history, locally.  This is one of the reasons why git can be very quick.
    44
     5 {{{
    56 git clone {repository} [{local name}]
     7 }}}
    68
    79The {repository} string can point at a git:// URL, http[s]:// URL or a local file URL.  When the cloning is completed, you will automatically have checked out the 'master' branch.
     
    1113Git should know who you are, as that will be important for the git commit logs.  So please enter a directory created by ''git clone'' and do:
    1214
     15 {{{
    1316 git config user.name 'Your name'
    1417 git config user.email 'my@email.com'
     18 }}}
    1519
    1620This will only set these variables locally in this repository.  I would probably recommend you to make these changes globally as well, as this can be easy to forget if your do another git clone.
    1721
     22 {{{
    1823 git config --global user.name 'Your name'
    1924 git config --global user.email 'my@email.com'
     25 }}}
    2026
    2127Some might only need to do the latter one, but some might want to use different e-mail addresses for their commits for different projects.
     
    2733This is highly recommended, as it is easier to read commit logs and patches via the git commands this way.
    2834
     35 {{{
    2936 git config --global color.branch auto
    3037 git config --global color.diff auto
     
    3340 git config --global color.status auto
    3441 git config --global color.pager true
     42 }}}
    3543
    3644Notice that we here force colours when using the pager.  This might mean you need to add one more change as well:
    3745
     46 {{{
    3847 git config --global core.pager 'less -X -R -F'
     48 }}}
    3949
    4050 === Preparing for sending patches via mail ===
     
    4252If you choose to install the git-send-email package, you can very easily submit patches via e-mail.  We will go into these details later on, but some configuration should be done.
    4353
     54 {{{
    4455 git config --global sendemail.chainreplyto false
    4556 git config --global sendemail.smtpserver {your SMTP server}
     57 }}}
    4658
    4759If you're using SSL or TLS against your SMTP server, add this:
     
    4961
    5062If you're authenticating against the SMTP server, you must add:
     63 
     64 {{{
    5165 git config --global sendemail.smtpuser {your-username}
     66 }}}
    5267
    5368You can also set your password in the config, by setting sendemail.smtppass - '''but the password will be saved in clear text!'''
     
    5772Now do this:
    5873
     74 {{{
    5975 cat ~/.gitconfig
    6076 cat .git/config
     77 }}}
    6178
    6279As you can see, the configuration files are not that hard to read.  These files may be edited manually if you want to as well.
     
    6481 == Checking the status of the project ==
    6582
     83 {{{
    6684 git status
     85 }}}
    6786
    6887This command gives you an overview over all files which have been modified and all new and unregistered files.  The output of ''git status'' should be pretty obvious.
     
    7089 == Checking your changes ==
    7190
     91 {{{
    7292 git diff [{filename}]
     93 }}}
    7394
    7495This will list all local changes which has not been committed.
     
    7899All files new files will need to be added to an ''index'' to be prepared for a commit.  Consider this as a "pre stage" which needs to be done.  This is also different from other VCS/SCMs.  But it has it's advantages as well.  So if you add or modifies files, you need to do this to add files to the index when you want to commit your changes:
    79100
     101 {{{
    80102 git add {filename/directory}
     103 }}}
    81104
    82105If you have a long list of files under the ''git status'' section called ''Changed but not updated:'', you can do:
    83106
     107 {{{
    84108 git add -u
     109 }}}
    85110
    86111This adds all these files into the index.  If you now try to do a ''git diff', you'll see that nothing turns up.  And if you modifies one of the files and do ''git diff' again, you'll find that only your last changes is shown.  This is normal behaviour.  git compares to what's in saved the index and what the local file looks like.
     
    88113If you want to see the changes from what's in your index and the previously committed version, you'll need to do:
    89114
     115 {{{
    90116 git diff --cached
     117 }}}
    91118
    92119Please also try:
    93120
     121 {{{
    94122 git status
     123 }}}
    95124
    96125Notice the change of the message for the files which you have added.  Now, let's commit!
    97126
     127 {{{
    98128 git commit -s
     129 }}}
    99130
    100131Now an editor should turn up, you can enter your commit statement and exit.  Notice the ''-s'' argument.  That means ''sign-off'.  That adds an 'Signed-off-by: ' signature at the end of the commit log.  Please do it a habit to always sign-off your commits.  That simply means you approve the commit.  And this is especially important if you commit work for others.
     
    102133To change the author of some code in the commit log, you can do that via the commit as well:
    103134
     135 {{{
    104136 git commit -s --author "Authors name <authors@email.com>"
     137 }}}
    105138
    106139This is important to remember if you apply patches from others.  This way it is possible to track from whom the different code parts came from.
     
    108141 == Look at the commit log ==
    109142
     143 {{{
    110144 git log
     145 }}}
    111146
    112147This is the most basic usage.  Other nice usages is to follow a files commit log, and not the complete project's commit history:
     148
     149 {{{
    113150 git log --follow {filename}
     151 }}}
    114152
    115153If you also add -p, you can also see the diff.
    116154
    117155There's also a feature called ''git show''.  This shows the contents of a commit or other git objects.  If you just do
     156
     157 {{{
    118158 git show
    119 
     159 }}}
    120160it will show the last git commit.  But you can give it a commit ID which you can find in the ''git log''.  If you give it a branch name or a tag name, you will see the git commit of the branch point or at the commit where the tag points at.
    121161
     162 {{{
    122163 git show 684790552dde0475745015a76762ecc5a11eedab     (commit ID)
    123164 git show 684790552dd                                  (partial commit ID)
    124165 git show origin/allmerged                             (remote branch)
    125166 git show v2.1.1                                       (tag name)
     167 }}}
    126168
    127169 == Creating patch files ==
    128170
    129171When you have done some commits, you probably want to share them.  Say you want to pick out the last commit.
     172
     173 {{{
    130174 git format-patch HEAD^1
     175 }}}
    131176
    132177This notation might look odd.  HEAD is a keyword, which means head of the commit log.  It's where the last commit happened.  It's because ''git format-patch'' takes ranges.  This notation means, from HEAD - 1 commit and to the last commit.  If you do just HEAD, the range "distance" will be 0, and no patch file will be created.  It's like saying ''from A to A''.  But you really want to say, from ''A to B''.  To explain that, you can do:
     178
     179 {{{
    133180 git format-patch HEAD^1..HEAD
     181 }}}
    134182
    135183So, what happens if you change HEAD^1 to HEAD^3 ... well, you get the 3 top-most commits as patch files.  ''git format-patch'' will report the file names it generates on-the-fly.
     
    137185''git format-patch'' can also take commit ID's which you find in the commit log, in addition to branch names and tag names.  So if you want to have all commits from OpenVPN 2.1_rc15 to OpenVPN 2.1_rc22 ... you can do:
    138186
     187 {{{
    139188 git format-patch v2.1_rc15..v2.1_rc22
     189 }}}
    140190
    141191You can also do this with ''git log''.
     
    145195If you have installed the git-send-email package and done the configuration steps mentioned above, sending patches to the mailing list is just as easy as creating the patch files.  Please note that the ''git send-email'' command expects to get patch file(s).  Just do:
    146196
     197 {{{
    147198 git send-email --to {email address}  {patch files}
     199 }}}
    148200
    149201and follow the instructions you're given.  That's all.
     
    152204
    153205If you wonder where 'v2.1_rc15' came from in the example above, that's a tag name.  And tags can be found by doing:
     206
     207 {{{
    154208 git tag -l
     209 }}}
    155210
    156211A tag is just a name of a particular commit ID.  Commit ID's can be tricky to remember, and then you can insert tag names which are a bit more descriptive.
     
    161216
    162217First, let's look at some branches:
     218
     219{{{
    163220git branch
     221}}}
    164222
    165223This will list all your local branches.  But we also have some remote branches.
     224
     225{{{
    166226git branch -r
     227}}}
    167228
    168229Notice the difference between them, where remote branches are prefixed with ''origin/''.  This is because git tracks remote branches separately from your local branches.  So you can have a master branch which is different from origin/master.  Another thing, to be able to do commits on remote branches, you must check them out as a local branch before you can do commit.
     
    173234
    174235In OpenVPN, there's a branch called ''allmerged''.  So to checkout that branch locally, we do:
     236
     237 {{{
    175238 git checkout -b allmerged origin/allmerged
     239 }}}
    176240
    177241That's it :)  Now you can do commits to your own copy of the ''allmerged'' branch.
     
    179243If you find out that you want to do some experimental stuff, to see how it works out, just do:
    180244
     245 {{{
    181246 git checkout -b my_test_branch
     247 }}}
    182248
    183249And it will create a new branch, enter it, from the latest commit where you where.  You can now even commit into this branch.
     
    187253If you find out that one branch has some stuff you want to test out with your own stuff in your own branch, it's quite easy.
    188254
     255 {{{
    189256 git merge {branch to merge into your branch}
     257 }}}
    190258
    191259If there are some conflicts, just do a ''git diff'' to see what they are.  You need to solve these issues before you then can continue.  The conflict blocks are pretty visible and you will hopefully understand what to leave in the file and what to remove.  When that's done, it's the same procedure as before - ''git add'' and ''git commit''.