├── .gitignore ├── LICENSE.md ├── NOTES ├── README.md ├── _config.yml ├── _layouts ├── reference.html └── zh_reference.html ├── about.html ├── basic └── index.html ├── branching └── index.html ├── cookbook.html ├── creating └── index.html ├── css ├── 960.css ├── grid.css ├── ie.css ├── ie6.css ├── layout.css ├── nav.css ├── reset.css └── text.css ├── favicon.ico ├── favicon.png ├── images └── snapshots.png ├── index.html ├── inspect └── index.html ├── js ├── action.js ├── jquery-1.4.1.min.js └── jquery-ui.min.js ├── remotes └── index.html └── zh ├── .gitignore ├── about.html ├── basic └── index.html ├── branching └── index.html ├── cookbook.html ├── creating └── index.html ├── images └── snapshots.png ├── index.html ├── inspect └── index.html └── remotes └── index.html /.gitignore: -------------------------------------------------------------------------------- 1 | _site 2 | *~ 3 | .*.swp 4 | .DS_Store 5 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # License 2 | The prose, course text, slide layouts, class outlines, diagrams, HTML, CSS, and Markdown code in the set of educational materials located in this repository are licensed as _CC BY 3.0_. The Octocat, GitHub logo and other already-copyrighted and already-reserved trademarks and images are not covered by this license. 3 | 4 | ------------------------ 5 | 6 | # Attribution 3.0 Unported (CC BY 3.0) 7 | This is a [human-readable summary](http://creativecommons.org/licenses/by/3.0/deed.en_US) of the [Legal Code (the full license)](http://creativecommons.org/licenses/by/3.0/legalcode). 8 | 9 | 10 | ## You are free: 11 | 12 | * to Share — to copy, distribute and transmit the work 13 | * to Remix — to adapt the work 14 | to make commercial use of the work 15 | 16 | 17 | ## Under the following conditions: 18 | 19 | * Attribution — You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). 20 | 21 | ## With the understanding that: 22 | 23 | * Waiver — Any of the above conditions can be waived if you get permission from the copyright holder. 24 | * Public Domain — Where the work or any of its elements is in the public domain under applicable law, that status is in no way affected by the license. 25 | * Other Rights — In no way are any of the following rights affected by the license: 26 | * Your fair dealing or fair use rights, or other applicable copyright exceptions and limitations; 27 | * The author's moral rights; 28 | * Rights other persons may have either in the work itself or in how the work is used, such as publicity or privacy rights. 29 | 30 | Notice — For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page. 31 | 32 | -------------------------------- 33 | 34 | # Attribution 3.0 Unported (CC BY 3.0) 35 | 36 | http://creativecommons.org/licenses/by/3.0/legalcode 37 | 38 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE LEGAL SERVICES. DISTRIBUTION OF THIS LICENSE DOES NOT CREATE AN ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES REGARDING THE INFORMATION PROVIDED, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM ITS USE. 39 | 40 | License 41 | 42 | THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED. 43 | 44 | BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS. 45 | 46 | 1. Definitions 47 | 48 | "Adaptation" means a work based upon the Work, or upon the Work and other pre-existing works, such as a translation, adaptation, derivative work, arrangement of music or other alterations of a literary or artistic work, or phonogram or performance and includes cinematographic adaptations or any other form in which the Work may be recast, transformed, or adapted including in any form recognizably derived from the original, except that a work that constitutes a Collection will not be considered an Adaptation for the purpose of this License. For the avoidance of doubt, where the Work is a musical work, performance or phonogram, the synchronization of the Work in timed-relation with a moving image ("synching") will be considered an Adaptation for the purpose of this License. 49 | "Collection" means a collection of literary or artistic works, such as encyclopedias and anthologies, or performances, phonograms or broadcasts, or other works or subject matter other than works listed in Section 1(f) below, which, by reason of the selection and arrangement of their contents, constitute intellectual creations, in which the Work is included in its entirety in unmodified form along with one or more other contributions, each constituting separate and independent works in themselves, which together are assembled into a collective whole. A work that constitutes a Collection will not be considered an Adaptation (as defined above) for the purposes of this License. 50 | "Distribute" means to make available to the public the original and copies of the Work or Adaptation, as appropriate, through sale or other transfer of ownership. 51 | "Licensor" means the individual, individuals, entity or entities that offer(s) the Work under the terms of this License. 52 | "Original Author" means, in the case of a literary or artistic work, the individual, individuals, entity or entities who created the Work or if no individual or entity can be identified, the publisher; and in addition (i) in the case of a performance the actors, singers, musicians, dancers, and other persons who act, sing, deliver, declaim, play in, interpret or otherwise perform literary or artistic works or expressions of folklore; (ii) in the case of a phonogram the producer being the person or legal entity who first fixes the sounds of a performance or other sounds; and, (iii) in the case of broadcasts, the organization that transmits the broadcast. 53 | "Work" means the literary and/or artistic work offered under the terms of this License including without limitation any production in the literary, scientific and artistic domain, whatever may be the mode or form of its expression including digital form, such as a book, pamphlet and other writing; a lecture, address, sermon or other work of the same nature; a dramatic or dramatico-musical work; a choreographic work or entertainment in dumb show; a musical composition with or without words; a cinematographic work to which are assimilated works expressed by a process analogous to cinematography; a work of drawing, painting, architecture, sculpture, engraving or lithography; a photographic work to which are assimilated works expressed by a process analogous to photography; a work of applied art; an illustration, map, plan, sketch or three-dimensional work relative to geography, topography, architecture or science; a performance; a broadcast; a phonogram; a compilation of data to the extent it is protected as a copyrightable work; or a work performed by a variety or circus performer to the extent it is not otherwise considered a literary or artistic work. 54 | "You" means an individual or entity exercising rights under this License who has not previously violated the terms of this License with respect to the Work, or who has received express permission from the Licensor to exercise rights under this License despite a previous violation. 55 | "Publicly Perform" means to perform public recitations of the Work and to communicate to the public those public recitations, by any means or process, including by wire or wireless means or public digital performances; to make available to the public Works in such a way that members of the public may access these Works from a place and at a place individually chosen by them; to perform the Work to the public by any means or process and the communication to the public of the performances of the Work, including by public digital performance; to broadcast and rebroadcast the Work by any means including signs, sounds or images. 56 | "Reproduce" means to make copies of the Work by any means including without limitation by sound or visual recordings and the right of fixation and reproducing fixations of the Work, including storage of a protected performance or phonogram in digital form or other electronic medium. 57 | 2. Fair Dealing Rights. Nothing in this License is intended to reduce, limit, or restrict any uses free from copyright or rights arising from limitations or exceptions that are provided for in connection with the copyright protection under copyright law or other applicable laws. 58 | 59 | 3. Subject to the terms and conditions of this License, Licensor hereby grants You a worldwide, royalty-free, non-exclusive, perpetual (for the duration of the applicable copyright) license to exercise the rights in the Work as stated below: 60 | 61 | to Reproduce the Work, to incorporate the Work into one or more Collections, and to Reproduce the Work as incorporated in the Collections; 62 | to create and Reproduce Adaptations provided that any such Adaptation, including any translation in any medium, takes reasonable steps to clearly label, demarcate or otherwise identify that changes were made to the original Work. For example, a translation could be marked "The original work was translated from English to Spanish," or a modification could indicate "The original work has been modified."; 63 | to Distribute and Publicly Perform the Work including as incorporated in Collections; and, 64 | to Distribute and Publicly Perform Adaptations. 65 | 66 | For the avoidance of doubt: 67 | Non-waivable Compulsory License Schemes. In those jurisdictions in which the right to collect royalties through any statutory or compulsory licensing scheme cannot be waived, the Licensor reserves the exclusive right to collect such royalties for any exercise by You of the rights granted under this License; 68 | Waivable Compulsory License Schemes. In those jurisdictions in which the right to collect royalties through any statutory or compulsory licensing scheme can be waived, the Licensor waives the exclusive right to collect such royalties for any exercise by You of the rights granted under this License; and, 69 | Voluntary License Schemes. The Licensor waives the right to collect royalties, whether individually or, in the event that the Licensor is a member of a collecting society that administers voluntary licensing schemes, via that society, from any exercise by You of the rights granted under this License. 70 | 71 | The above rights may be exercised in all media and formats whether now known or hereafter devised. The above rights include the right to make such modifications as are technically necessary to exercise the rights in other media and formats. Subject to Section 8(f), all rights not expressly granted by Licensor are hereby reserved. 72 | 73 | 4. Restrictions. The license granted in Section 3 above is expressly made subject to and limited by the following restrictions: 74 | 75 | You may Distribute or Publicly Perform the Work only under the terms of this License. You must include a copy of, or the Uniform Resource Identifier (URI) for, this License with every copy of the Work You Distribute or Publicly Perform. You may not offer or impose any terms on the Work that restrict the terms of this License or the ability of the recipient of the Work to exercise the rights granted to that recipient under the terms of the License. You may not sublicense the Work. You must keep intact all notices that refer to this License and to the disclaimer of warranties with every copy of the Work You Distribute or Publicly Perform. When You Distribute or Publicly Perform the Work, You may not impose any effective technological measures on the Work that restrict the ability of a recipient of the Work from You to exercise the rights granted to that recipient under the terms of the License. This Section 4(a) applies to the Work as incorporated in a Collection, but this does not require the Collection apart from the Work itself to be made subject to the terms of this License. If You create a Collection, upon notice from any Licensor You must, to the extent practicable, remove from the Collection any credit as required by Section 4(b), as requested. If You create an Adaptation, upon notice from any Licensor You must, to the extent practicable, remove from the Adaptation any credit as required by Section 4(b), as requested. 76 | If You Distribute, or Publicly Perform the Work or any Adaptations or Collections, You must, unless a request has been made pursuant to Section 4(a), keep intact all copyright notices for the Work and provide, reasonable to the medium or means You are utilizing: (i) the name of the Original Author (or pseudonym, if applicable) if supplied, and/or if the Original Author and/or Licensor designate another party or parties (e.g., a sponsor institute, publishing entity, journal) for attribution ("Attribution Parties") in Licensor's copyright notice, terms of service or by other reasonable means, the name of such party or parties; (ii) the title of the Work if supplied; (iii) to the extent reasonably practicable, the URI, if any, that Licensor specifies to be associated with the Work, unless such URI does not refer to the copyright notice or licensing information for the Work; and (iv) , consistent with Section 3(b), in the case of an Adaptation, a credit identifying the use of the Work in the Adaptation (e.g., "French translation of the Work by Original Author," or "Screenplay based on original Work by Original Author"). The credit required by this Section 4 (b) may be implemented in any reasonable manner; provided, however, that in the case of a Adaptation or Collection, at a minimum such credit will appear, if a credit for all contributing authors of the Adaptation or Collection appears, then as part of these credits and in a manner at least as prominent as the credits for the other contributing authors. For the avoidance of doubt, You may only use the credit required by this Section for the purpose of attribution in the manner set out above and, by exercising Your rights under this License, You may not implicitly or explicitly assert or imply any connection with, sponsorship or endorsement by the Original Author, Licensor and/or Attribution Parties, as appropriate, of You or Your use of the Work, without the separate, express prior written permission of the Original Author, Licensor and/or Attribution Parties. 77 | Except as otherwise agreed in writing by the Licensor or as may be otherwise permitted by applicable law, if You Reproduce, Distribute or Publicly Perform the Work either by itself or as part of any Adaptations or Collections, You must not distort, mutilate, modify or take other derogatory action in relation to the Work which would be prejudicial to the Original Author's honor or reputation. Licensor agrees that in those jurisdictions (e.g. Japan), in which any exercise of the right granted in Section 3(b) of this License (the right to make Adaptations) would be deemed to be a distortion, mutilation, modification or other derogatory action prejudicial to the Original Author's honor and reputation, the Licensor will waive or not assert, as appropriate, this Section, to the fullest extent permitted by the applicable national law, to enable You to reasonably exercise Your right under Section 3(b) of this License (right to make Adaptations) but not otherwise. 78 | 79 | 5. Representations, Warranties and Disclaimer 80 | 81 | UNLESS OTHERWISE MUTUALLY AGREED TO BY THE PARTIES IN WRITING, LICENSOR OFFERS THE WORK AS-IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE WORK, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF TITLE, MERCHANTIBILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, ACCURACY, OR THE PRESENCE OF ABSENCE OF ERRORS, WHETHER OR NOT DISCOVERABLE. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED WARRANTIES, SO SUCH EXCLUSION MAY NOT APPLY TO YOU. 82 | 83 | 6. Limitation on Liability. EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE LAW, IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY SPECIAL, INCIDENTAL, CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES ARISING OUT OF THIS LICENSE OR THE USE OF THE WORK, EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 84 | 85 | 7. Termination 86 | 87 | This License and the rights granted hereunder will terminate automatically upon any breach by You of the terms of this License. Individuals or entities who have received Adaptations or Collections from You under this License, however, will not have their licenses terminated provided such individuals or entities remain in full compliance with those licenses. Sections 1, 2, 5, 6, 7, and 8 will survive any termination of this License. 88 | Subject to the above terms and conditions, the license granted here is perpetual (for the duration of the applicable copyright in the Work). Notwithstanding the above, Licensor reserves the right to release the Work under different license terms or to stop distributing the Work at any time; provided, however that any such election will not serve to withdraw this License (or any other license that has been, or is required to be, granted under the terms of this License), and this License will continue in full force and effect unless terminated as stated above. 89 | 8. Miscellaneous 90 | 91 | Each time You Distribute or Publicly Perform the Work or a Collection, the Licensor offers to the recipient a license to the Work on the same terms and conditions as the license granted to You under this License. 92 | Each time You Distribute or Publicly Perform an Adaptation, Licensor offers to the recipient a license to the original Work on the same terms and conditions as the license granted to You under this License. 93 | If any provision of this License is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this License, and without further action by the parties to this agreement, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable. 94 | No term or provision of this License shall be deemed waived and no breach consented to unless such waiver or consent shall be in writing and signed by the party to be charged with such waiver or consent. 95 | This License constitutes the entire agreement between the parties with respect to the Work licensed here. There are no understandings, agreements or representations with respect to the Work not specified here. Licensor shall not be bound by any additional provisions that may appear in any communication from You. This License may not be modified without the mutual written agreement of the Licensor and You. 96 | The rights granted under, and the subject matter referenced, in this License were drafted utilizing the terminology of the Berne Convention for the Protection of Literary and Artistic Works (as amended on September 28, 1979), the Rome Convention of 1961, the WIPO Copyright Treaty of 1996, the WIPO Performances and Phonograms Treaty of 1996 and the Universal Copyright Convention (as revised on July 24, 1971). These rights and subject matter take effect in the relevant jurisdiction in which the License terms are sought to be enforced according to the corresponding provisions of the implementation of those treaty provisions in the applicable national law. If the standard suite of rights granted under applicable copyright law includes additional rights not granted under this License, such additional rights are deemed to be included in the License; this License is not intended to restrict the license of any rights under applicable law. 97 | 98 | Creative Commons Notice 99 | 100 | Creative Commons is not a party to this License, and makes no warranty whatsoever in connection with the Work. Creative Commons will not be liable to You or any party on any legal theory for any damages whatsoever, including without limitation any general, special, incidental or consequential damages arising in connection to this license. Notwithstanding the foregoing two (2) sentences, if Creative Commons has expressly identified itself as the Licensor hereunder, it shall have all rights and obligations of Licensor. 101 | Except for the limited purpose of indicating to the public that the Work is licensed under the CCPL, Creative Commons does not authorize the use by either party of the trademark "Creative Commons" or any related trademark or logo of Creative Commons without the prior written consent of Creative Commons. Any permitted use will be in compliance with Creative Commons' then-current trademark usage guidelines, as may be published on its website or otherwise made available upon request from time to time. For the avoidance of doubt, this trademark restriction does not form part of this License. 102 | 103 | Creative Commons may be contacted at http://creativecommons.org/. 104 | -------------------------------------------------------------------------------- /NOTES: -------------------------------------------------------------------------------- 1 | Introduction 2 | (how git is different) 3 | 4 | Getting & Creating Projects 5 | * init 6 | * clone 7 | 8 | Basic Snapshotting 9 | * add 10 | * status 11 | * diff 12 | * commit 13 | * rm, mv 14 | * reset 15 | 16 | Branching and Merging Contexts 17 | * branch 18 | * merge 19 | * cherry-pick 20 | * log 21 | 22 | Sharing and Updating Projects 23 | * fetch, pull 24 | * push 25 | 26 | Inspection, Comparison and History 27 | * log 28 | * diff 29 | 30 | Fixing History 31 | * rebase 32 | * revert 33 | 34 | 35 |
The Git Reference site was put together by the GitHub 8 | team.
9 |9 | In order to do anything in Git, you need to have a Git repository. This 10 | is where Git stores the data for the snapshots you are saving. 11 |
12 | 13 |14 | There are two main ways to get a Git repository. One way is to simply 15 | initialize a new one from an existing directory, such as a new project or 16 | a project new to source control. The second way is to clone one from a 17 | public Git repository, as you would do if you wanted a copy or wanted to 18 | work with someone on a project. We will cover both of these here. 19 |
20 | 21 |To create a repository from an existing directory of files, you can
36 | simply run git init
in that directory. For example,
37 | let's say we have a directory with a few files in it, like this:
38 |
41 | $ cd konnichiwa 42 | $ ls 43 | README hello.rb 44 |45 | 46 |
This is a project where we are writing examples of the "Hello World"
47 | program in every language. So far, we just have Ruby, but hey, it's
48 | a start. To start version controlling this with Git, we can simply
49 | run git init
.
50 |
53 | $ git init 54 | Initialized empty Git repository in /opt/konnichiwa/.git/ 55 |56 | 57 |
Now you can see that there is a .git
subdirectory in your
58 | project. This is your Git repository where all the data of your
59 | project snapshots are stored.
60 |
63 | $ ls -a 64 | . .. .git README hello.rb 65 |66 | 67 |
Congratulations, you now have a skeleton Git repository and can start 68 | snapshotting your project. 69 |
70 | 71 |
72 | In a nutshell, you use git init
to make an
73 | existing directory of content into a new Git repository. You can do this
74 | in any directory at any time, completely locally.
90 | If you need to collaborate with someone on a project, or if you want to
91 | get a copy of a project so you can look at or use the code, you will
92 | clone it. You simply run the git clone [url]
command with
93 | the URL of the project you want to copy.
94 |
97 | $ git clone git://github.com/schacon/simplegit.git
98 | Initialized empty Git repository in /private/tmp/simplegit/.git/
99 | remote: Counting objects: 100, done.
100 | remote: Compressing objects: 100% (86/86), done.
101 | remote: Total 100 (delta 35), reused 0 (delta 0)
102 | Receiving objects: 100% (100/100), 9.51 KiB, done.
103 | Resolving deltas: 100% (35/35), done.
104 | $ cd simplegit/
105 | $ ls
106 | README Rakefile lib
107 |
108 |
109 |
110 | This will copy the entire history of that project so you have it locally
111 | and it will give you a working directory of the main branch of that project
112 | so you can look at the code or start editing it. If you change into the
113 | new directory, you can see the .git
subdirectory - that is
114 | where all the project data is.
115 |
118 | $ ls -a 119 | . .. .git README Rakefile lib 120 | $ cd .git 121 | $ ls 122 | HEAD description info packed-refs 123 | branches hooks logs refs 124 | config index objects 125 |126 | 127 |
128 | By default, Git will create a directory that is the same name as the 129 | project in the URL you give it - basically whatever is after the last slash 130 | of the URL. If you want something different, you can just put it at the 131 | end of the command, after the URL. 132 |
133 | 134 |
135 | In a nutshell, you use git clone
to get a
136 | local copy of a Git repository so you can look at it or start modifying
137 | it.
8 | This is the Git reference site. It is meant to be a quick 9 | reference for learning and remembering the most important and 10 | commonly used Git commands. The commands are organized into 11 | sections of the type of operation you may be trying to do, and 12 | will present the common options and commands needed to accomplish 13 | these common tasks. 14 |
15 |16 | Each section will link to the next section, so it can be used 17 | as a tutorial. Every page will also link to more in-depth 18 | Git documentation such as the official manual pages and relevant 19 | sections in the Pro Git book, 20 | so you can learn more about any of 21 | the commands. First, we'll start with thinking about source code 22 | management like Git does. 23 |
24 |31 | The first important thing to understand about Git is 32 | that it thinks about version control very differently than 33 | Subversion or Perforce or whatever SCM you may be used to. It 34 | is often easier to learn Git by trying to forget your assumptions 35 | about how version control works and try to think about it in the 36 | Git way. 37 |
38 | 39 |40 | Let's start from scratch. Assume you are designing a new source 41 | code management system. How did you do basic version control before 42 | you used a tool for it? Chances are that you simply copied your 43 | project directory to save what it looked like at that point. 44 |
45 | 46 |$ cp -R project project.bak47 | 48 |
49 | That way, you can easily revert files that get messed up later, or 50 | see what you have changed by comparing what the project looks like 51 | now to what it looked like when you copied it. 52 |
53 | 54 |55 | If you are really paranoid, you may do this often, maybe putting the 56 | date in the name of the backup: 57 |
58 | 59 |$ cp -R project project.2010-06-01.bak60 | 61 |
62 | In that case, you may have a bunch of snapshots of your project that 63 | you can compare and inspect from. You can even use this model to 64 | fairly effectively share changes with someone. If you zip up your 65 | project at a known state and put it on your website, other developers 66 | can download that, change it and send you a patch pretty easily. 67 |
68 | 69 |70 | $ wget http://example.com/project.2010-06-01.zip 71 | $ unzip project.2010-06-01.zip 72 | $ cp -R project.2010-06-01 project-my-copy 73 | $ cd project-my-copy 74 | $ (change something) 75 | $ diff project-my-copy project.2010-06-01 > change.patch 76 | $ (email change.patch)77 | 78 |
79 | Now the original developer can apply that patch to their copy of the 80 | project and they have your changes. This is how many open source 81 | projects have been collaborated on for several years. 82 |
83 | 84 |85 | This actually works fairly well, so let's say we want to write a tool 86 | to make this basic process faster and easier. Instead of writing a tool 87 | that versions each file individually, like Subversion, we would probably 88 | write one that makes it easier to store snapshots of our project without 89 | having to copy the whole directory each time. 90 |
91 | 92 |
93 | This is essentially what Git is. You tell Git you want to save a snapshot
94 | of your project with the git commit
command and it basically
95 | records a manifest of what all of the files in your project look like at
96 | that point. Then most of the commands work with those manifests to see
97 | how they differ or pull content out of them, etc.
98 |
103 | If you think about Git 104 | as a tool for storing and comparing and merging snapshots of your project, 105 | it may be easier to understand what is going on and how to do things 106 | properly. 107 |
108 | 109 |On to Getting and Creating Projects »
113 | -------------------------------------------------------------------------------- /inspect/index.html: -------------------------------------------------------------------------------- 1 | --- 2 | layout: reference 3 | --- 4 | 5 |14 | So now you have a bunch of branches that you are using for short lived 15 | topics, long lived features and what not. How do you keep track of them? 16 | Git has a couple of tools to help you figure out where work was done, what 17 | the difference between two branches are and more. 18 |
19 | 20 |
21 | In a nutshell you can use git log
to find specific
22 | commits in your project history - by author, date, content or
23 | history. You can use git diff
to compare two different points
24 | in your history - generally to see how two branches differ or what has
25 | changed from one version of your software to another.
26 |
We've already seen how to use git log
to compare branches,
42 | by looking at the commits on one branch that are not reachable from another.
43 | (If you don't remember, it looks like this: git log branchA ^branchB
).
44 | However, you can also use git log
to look for specific commits.
45 | Here we'll be looking at some of the more commonly used git log
46 | options, but there are many. Take a look at the official docs for the whole
47 | list.
48 |
56 | To filter your commit history to only the ones done by a specific author,
57 | you can use the --author
option. For example, let's say we're
58 | looking for the commits in the Git source code done by Linus. We would
59 | type something like git log --author=Linus
. The search is
60 | case sensitive and will also search the email address. The following
61 | example will use the -[number]
option, which will limit the
62 | results to the last [number] commits.
63 |
66 | $ git log --author=Linus --oneline -5 67 | 81b50f3 Move 'builtin-*' into a 'builtin/' subdirectory 68 | 3bb7256 make "index-pack" a built-in 69 | 377d027 make "git pack-redundant" a built-in 70 | b532581 make "git unpack-file" a built-in 71 | 112dd51 make "mktag" a built-in 72 |73 | 74 |
80 | If you want to specify a date range that you're interested in filtering your
81 | commits down to, you can use a number of options such as --since
82 | and --before
, or you can also use --until
and
83 | --after
. For example, to see all the commits in
84 | the Git project before three weeks ago but after April 18th, you could run this
85 | (We're also going to use --no-merges
to remove merge commits):
86 |
89 | $ git log --oneline --before={3.weeks.ago} --after={2010-04-18} --no-merges 90 | 5469e2d Git 1.7.1-rc2 91 | d43427d Documentation/remote-helpers: Fix typos and improve language 92 | 272a36b Fixup: Second argument may be any arbitrary string 93 | b6c8d2d Documentation/remote-helpers: Add invocation section 94 | 5ce4f4e Documentation/urls: Rewrite to accomodate transport::address 95 | 00b84e9 Documentation/remote-helpers: Rewrite description 96 | 03aa87e Documentation: Describe other situations where -z affects git diff 97 | 77bc694 rebase-interactive: silence warning when no commits rewritten 98 | 636db2c t3301: add tests to use --format="%N" 99 |100 | 101 |
107 | You may also want to look for commits with a certain phrase in the commit
108 | message. Use --grep
for that. Let's say there
109 | was a commit that dealt with using the P4EDITOR environment variable and
110 | you wanted to remember what that change looked like - you could find the commit
111 | with --grep
.
112 |
115 | $ git log --grep=P4EDITOR --no-merges
116 | commit 82cea9ffb1c4677155e3e2996d76542502611370
117 | Author: Shawn Bohrer
118 | Date: Wed Mar 12 19:03:24 2008 -0500
119 |
120 | git-p4: Use P4EDITOR environment variable when set
121 |
122 | Perforce allows you to set the P4EDITOR environment variable to your
123 | preferred editor for use in perforce. Since we are displaying a
124 | perforce changelog to the user we should use it when it is defined.
125 |
126 | Signed-off-by: Shawn Bohrer <shawn.bohrer@gmail.com>
127 | Signed-off-by: Simon Hausmann <simon@lst.de>
128 |
129 |
130 |
131 | Git will logically OR all --grep
and --author
132 | arguments. If you want to use --grep
and --author
133 | to see commits that were authored by someone AND have a specific message
134 | content, you have to add the --all-match
option. In these
135 | examples we're going to use the --format
option, so we can see
136 | who the author of each commit was.
137 |
If we look for the commit messages with 'p4 depo' in them, we get these 140 | three commits:
141 | 142 |143 | $ git log --grep="p4 depo" --format="%h %an %s" 144 | ee4fd1a Junio C Hamano Merge branch 'master' of git://repo.or.cz/git/fastimport 145 | da4a660 Benjamin Sergeant git-p4 fails when cloning a p4 depo. 146 | 1cd5738 Simon Hausmann Make incremental imports easier to use by storing the p4 d 147 |148 | 149 |
If we add a --author=Hausmann
argument, instead of further
150 | filtering it down to the one commit by Simon, it instead will show all
151 | commits by Simon OR commits with "p4 depo" in the message:
154 | $ git log --grep="p4 depo" --format="%h %an %s" --author="Hausmann" 155 | cdc7e38 Simon Hausmann Make it possible to abort the submission of a change to Pe 156 | f5f7e4a Simon Hausmann Clean up the git-p4 documentation 157 | 30b5940 Simon Hausmann git-p4: Fix import of changesets with file deletions 158 | 4c750c0 Simon Hausmann git-p4: git-p4 submit cleanups. 159 | 0e36f2d Simon Hausmann git-p4: Removed git-p4 submit --direct. 160 | edae1e2 Simon Hausmann git-p4: Clean up git-p4 submit's log message handling. 161 | 4b61b5c Simon Hausmann git-p4: Remove --log-substitutions feature. 162 | 36ee4ee Simon Hausmann git-p4: Ensure the working directory and the index are cle 163 | e96e400 Simon Hausmann git-p4: Fix submit user-interface. 164 | 38f9f5e Simon Hausmann git-p4: Fix direct import from perforce after fetching cha 165 | 2094714 Simon Hausmann git-p4: When skipping a patch as part of "git-p4 submit" m 166 | 1ca3d71 Simon Hausmann git-p4: Added support for automatically importing newly ap 167 | ... 168 |169 | 170 |
However, adding --all-match
will get the results you're
171 | looking for:
174 | $ git log --grep="p4 depo" --format="%h %an %s" --author="Hausmann" --all-match 175 | 1cd5738 Simon Hausmann Make incremental imports easier to use by storing the p4 d 176 |177 | 178 |
184 | What if you write really horrible commit messages? Or, what if you are 185 | looking for when a function was introduced, or where variables started 186 | to be used? You can also tell Git to look through the diff of each 187 | commit for a string. For example, if we wanted to find which commits 188 | modified anything that looked like the function name 189 | 'userformat_find_requirements', we would run this: (note there is no '=' 190 | between the '-S' and what you are searching for) 191 |
192 | 193 |
194 | $ git log -Suserformat_find_requirements
195 | commit 5b16360330822527eac1fa84131d185ff784c9fb
196 | Author: Johannes Gilger
197 | Date: Tue Apr 13 22:31:12 2010 +0200
198 |
199 | pretty: Initialize notes if %N is used
200 |
201 | When using git log --pretty='%N' without an explicit --show-notes, git
202 | would segfault. This patches fixes this behaviour by loading the needed
203 | notes datastructures if --pretty is used and the format contains %N.
204 | When --pretty='%N' is used together with --no-notes, %N won't be
205 | expanded.
206 |
207 | This is an extension to a proposed patch by Jeff King.
208 |
209 | Signed-off-by: Johannes Gilger
210 | Signed-off-by: Junio C Hamano
211 |
212 |
213 |
219 | Each commit is a snapshot of the project, but since each commit records the
220 | snapshot it was based off of, Git can always calculate the difference and
221 | show it to you as a patch. That means for any commit you can get the patch
222 | that commit introduced to the project. You can either do this by running
223 | git show [SHA]
with a specific commit SHA, or you can run
224 | git log -p
, which tells Git to put the patch after each commit.
225 | It is a great way to summarize what has happened on a branch or between
226 | commits.
227 |
230 | $ git log -p --no-merges -2 231 | commit 594f90bdee4faf063ad07a4a6f503fdead3ef606 232 | Author: Scott Chacon <schacon@gmail.com> 233 | Date: Fri Jun 4 15:46:55 2010 +0200 234 | 235 | reverted to old class name 236 | 237 | diff --git a/ruby.rb b/ruby.rb 238 | index bb86f00..192151c 100644 239 | --- a/ruby.rb 240 | +++ b/ruby.rb 241 | @@ -1,7 +1,7 @@ 242 | -class HiWorld 243 | +class HelloWorld 244 | def self.hello 245 | puts "Hello World from Ruby" 246 | end 247 | end 248 | 249 | -HiWorld.hello 250 | +HelloWorld.hello 251 | 252 | commit 3cbb6aae5c0cbd711c098e113ae436801371c95e 253 | Author: Scott Chacon <schacon@gmail.com> 254 | Date: Fri Jun 4 12:58:53 2010 +0200 255 | 256 | fixed readme title differently 257 | 258 | diff --git a/README b/README 259 | index d053cc8..9103e27 100644 260 | --- a/README 261 | +++ b/README 262 | @@ -1,4 +1,4 @@ 263 | -Hello World Examples 264 | +Many Hello World Examples 265 | ====================== 266 | 267 | This project has examples of hello world in 268 |269 | 270 |
This is a really nice way of summarizing changes or reviewing a series 271 | of commits before merging them or releasing something.
272 | 273 |If the -p
option is too verbose for you, you can summarize
279 | the changes with --stat
instead. Here is the same log output
280 | with --stat
instead of -p
283 | $ git log --stat --no-merges -2 284 | commit 594f90bdee4faf063ad07a4a6f503fdead3ef606 285 | Author: Scott Chacon <schacon@gmail.com> 286 | Date: Fri Jun 4 15:46:55 2010 +0200 287 | 288 | reverted to old class name 289 | 290 | ruby.rb | 4 ++-- 291 | 1 files changed, 2 insertions(+), 2 deletions(-) 292 | 293 | commit 3cbb6aae5c0cbd711c098e113ae436801371c95e 294 | Author: Scott Chacon <schacon@gmail.com> 295 | Date: Fri Jun 4 12:58:53 2010 +0200 296 | 297 | fixed readme title differently 298 | 299 | README | 2 +- 300 | 1 files changed, 1 insertions(+), 1 deletions(-) 301 |302 | 303 |
Same basic information, but a little more compact - it still lets you 304 | see relative changes and which files were modified.
305 | 306 |Finally, to see the absolute changes between any two commit snapshots,
322 | you can use the git diff
command. This is largely used in two
323 | main situations - seeing how two branches differ from one another and
324 | seeing what has changed since a release or some other older point in
325 | history. Let's look at both of these situations.
To see what has changed since the last release, you can simply run
328 | git diff [version]
(or whatever you tagged the release).
329 | For example, if we want to see what has changed in our project since
330 | the v0.9 release, we can run git diff v0.9
.
331 |
334 | $ git diff v0.9 335 | diff --git a/README b/README 336 | index d053cc8..d4173d5 100644 337 | --- a/README 338 | +++ b/README 339 | @@ -1,4 +1,4 @@ 340 | -Hello World Examples 341 | +Many Hello World Lang Examples 342 | ====================== 343 | 344 | This project has examples of hello world in 345 | diff --git a/ruby.rb b/ruby.rb 346 | index bb86f00..192151c 100644 347 | --- a/ruby.rb 348 | +++ b/ruby.rb 349 | @@ -1,7 +1,7 @@ 350 | -class HiWorld 351 | +class HelloWorld 352 | def self.hello 353 | puts "Hello World from Ruby" 354 | end 355 | end 356 | 357 | -HiWorld.hello 358 | +HelloWorld.hello 359 |360 | 361 |
Just like git log
, you can use the --stat
362 | option with it.
365 | $ git diff v0.9 --stat 366 | README | 2 +- 367 | ruby.rb | 4 ++-- 368 | 2 files changed, 3 insertions(+), 3 deletions(-) 369 |370 | 371 |
To compare two divergent branches, however, you can run something like
372 | git diff branchA branchB
but the problem is that it will do
373 | exactly what you are asking - it will basically give you a patch file that
374 | would turn the snapshot at the tip of branchA into the snapshot at the tip
375 | of branchB. This means if the two branches have diverged - gone in different
376 | directions - it will remove all the work that was introduced into branchA
377 | and then add everything that was introduced into branchB. This is probably
378 | not what you want - you want the changes added to branchB that are not in
379 | branchA, so you really want the difference between where the two branches
380 | diverged and the tip of branchB. So, if our history looks like this:
383 | $ git log --graph --oneline --decorate --all 384 | * 594f90b (HEAD, tag: v1.0, master) reverted to old class name 385 | | * 1834130 (erlang) added haskell 386 | | * ab5ab4c added erlang 387 | |/ 388 | * 8d585ea Merge branch 'fix_readme' 389 | ... 390 |391 | 392 |
And we want to see what is on the "erlang" branch compared to the "master"
393 | branch, running git diff master erlang
will give us the wrong
394 | thing.
397 | $ git diff --stat master erlang 398 | erlang_hw.erl | 5 +++++ 399 | haskell.hs | 4 ++++ 400 | ruby.rb | 4 ++-- 401 | 3 files changed, 11 insertions(+), 2 deletions(-) 402 |403 | 404 |
You see that it adds the erlang and haskell files, which is what we did 405 | in that branch, but then the output also reverts the changes to the ruby file 406 | that we did in the master branch. What we really want to see is just the 407 | changes that happened in the "erlang" branch (adding the two files). We can 408 | get the desired result by doing the diff from the common commit they diverged 409 | from:
410 | 411 |412 | $ git diff --stat 8d585ea erlang 413 | erlang_hw.erl | 5 +++++ 414 | haskell.hs | 4 ++++ 415 | 2 files changed, 9 insertions(+), 0 deletions(-) 416 |417 | 418 |
That's what we're looking for, but we don't want to have to figure out
419 | what commit the two branches diverged from every time. Luckily, Git has a
420 | shortcut for this. If you run git diff master...erlang
(with
421 | three dots in between the branch names), Git will automatically figure out
422 | what the common commit (otherwise known as the "merge base") of the two
423 | commit is and do the diff off of that.
426 | $ git diff --stat master erlang 427 | erlang_hw.erl | 5 +++++ 428 | haskell.hs | 4 ++++ 429 | ruby.rb | 4 ++-- 430 | 3 files changed, 11 insertions(+), 2 deletions(-) 431 | $ git diff --stat master...erlang 432 | erlang_hw.erl | 5 +++++ 433 | haskell.hs | 4 ++++ 434 | 2 files changed, 9 insertions(+), 0 deletions(-) 435 |436 | 437 |
Nearly every time you want to compare two branches, you'll want to use 438 | the triple-dot syntax, because it will almost always give you what you want. 439 |
440 | 441 |As a bit of an aside, you can also have Git manually calculate what the
442 | merge-base (first common ancestor commit) of any two commits would be with
443 | the git merge-base
command:
446 | $ git merge-base master erlang 447 | 8d585ea6faf99facd39b55d6f6a3b3f481ad0d3d 448 |449 | 450 |
You can do the equivalent of git diff master...erlang
451 | by running this:
454 | $ git diff --stat $(git merge-base master erlang) erlang 455 | erlang_hw.erl | 5 +++++ 456 | haskell.hs | 4 ++++ 457 | 2 files changed, 9 insertions(+), 0 deletions(-) 458 |459 | 460 |
You may prefer using the easier syntax though.
461 | 462 | 463 |
464 | In a nutshell you can use git diff
to see how a project
465 | has changed since a known point in the past or to see what unique work is
466 | in one branch since it diverged from another. Always use
467 | git diff branchA...branchB
to inspect branchB relative to
468 | branchA to make things easier.
469 |
And that's it! For more information, try reading the 475 | Pro Git book.
476 | -------------------------------------------------------------------------------- /js/action.js: -------------------------------------------------------------------------------- 1 | jQuery.githubUser = function(username, callback) { 2 | jQuery.getJSON("http://github.com/api/v1/json/" + username + "?callback=?", callback); 3 | } 4 | 5 | var nicknames = ["Dragon", "Gizzard", "The Enforcer", 6 | "Mizungu", "The Hammer"] 7 | var phrases = ["also feels that specialization is for insects", 8 | "my dogs named tater tooooo", 9 | "every movie should have a cast dance scene at the end", 10 | "honey, you're a tool of the masses", 11 | "my life is kinda like the Matrix, really", 12 | "too fat to fit into Japanese pants", 13 | "it's like the finger of God!"] 14 | function changeBlurb() { 15 | var phrase = phrases[Math.floor(Math.random()*phrases.length)] 16 | $('#page-heading').text(phrase) 17 | t=setTimeout("changeBlurb()",30000); 18 | } 19 | 20 | function changeNickname() { 21 | var nickname = nicknames[Math.floor(Math.random()*nicknames.length)] 22 | $('#nickname').text(nickname) 23 | t=setTimeout("changeNickname()",30000); 24 | } 25 | 26 | function showHome() { 27 | $('#break').show() 28 | $('.non_home_box').hide('scale') 29 | $('.home_box').show('scale') 30 | } 31 | 32 | function showTalks() { 33 | $('#break').hide() 34 | $('#info_box').show() 35 | $("#info_box").load("talks.html", null, function() { 36 | window.location.hash = 'talks' 37 | }); 38 | } 39 | 40 | function showScribbles() { 41 | $('#break').hide() 42 | $('#info_box').show() 43 | $("#info_box").load("scribbles.html", null, function() { 44 | window.location.hash = 'scribbles' 45 | }); 46 | } 47 | 48 | function showAbout() { 49 | $('#break').hide() 50 | $('#info_box').show() 51 | $("#info_box").load("about.html", null, function() { 52 | window.location.hash = 'about' 53 | $("a#menu_talks").click(function(event){ 54 | hideAll() 55 | showTalks() 56 | }) 57 | }); 58 | } 59 | 60 | function showCode() { 61 | $('#break').hide() 62 | $('#info_box').show() 63 | $("#info_box").load("code.html", null, function() { 64 | $.githubUser('schacon', function(data) { 65 | $('#github-projects').text('') 66 | var repos = data.user.repositories; 67 | repos.sort(function(a,b) { 68 | return b.watchers - a.watchers; 69 | }); 70 | 71 | $(repos).each(function() { 72 | if((!this.fork) && (!this.private)) { 73 | $('#github-projects').append("\ 74 |14 | Git doesn't have a central server like Subversion. All of the commands 15 | so far have been done locally, just updating a local database. 16 | To collaborate with other developers in Git, you have to put all that 17 | data on a server that the other developers have access to. The way Git 18 | does this is to synchronize your data with another repository. There 19 | is no real difference between a server and a client - a Git repository 20 | is a Git repository and you can synchronize between any two easily. 21 |
22 | 23 |Once you have a Git repository, either one that you set up on your 24 | own server, or one hosted someplace like GitHub, you can tell Git to 25 | either push any data that you have that is not in the remote repository 26 | up, or you can ask Git to fetch differences down from the other repo. 27 |
28 | 29 |You can do this any time you are online, it does not have to correspond
30 | with a commit
or anything else. Generally you will do a
31 | number of commits locally, then fetch data from the online shared repository
32 | you cloned the project from to get up to date, merge any new work into the
33 | stuff you did, then push your changes back up.
36 | In a nutshell you can update your project with git fetch
37 | and share your changes with git push
. You can manage your
38 | remote repositories with git remote
.
39 |
Unlike centralized version control systems that have a client that is 56 | very different from a server, Git repositories are all basically equal and 57 | you simply synchronize between them. This makes it easy to have more than 58 | one remote repository - you can have some that you have read-only access to 59 | and others that you can write to as well.
60 | 61 |So that you don't have to use the full URL of a remote repository every
62 | time you want to synchronize with it, Git stores an alias or nickname for
63 | each remote repository URL you are interested in. You use the
64 | git remote
command to manage this list of remote repos that
65 | you care about.
Without any arguments, Git will simply show you the remote repository
73 | aliases that it has stored. By default, if you cloned the project (as
74 | opposed to creating a new one locally), Git will automatically add the
75 | URL of the repository that you cloned from under the name 'origin'. If
76 | you run the command with the -v
option, you can see the
77 | actual URL for each alias.
80 | $ git remote 81 | origin 82 | $ git remote -v 83 | origin git@github.com:github/git-reference.git (fetch) 84 | origin git@github.com:github/git-reference.git (push) 85 |86 | 87 |
You see the URL there twice because Git allows you to have different 88 | push and fetch URLs for each remote in case you want to use different 89 | protocols for reads and writes.
90 | 91 |If you want to share a locally created repository, or you want to take
97 | contributions from someone else's repository - if you want to interact in
98 | any way with a new repository, it's generally easiest to add it as a remote.
99 | You do that by running git remote add [alias] [url]
. That
100 | adds [url]
under a local remote named [alias]
.
For example, if we want to share our Hello World program with the world, 103 | we can create a new repository on a server (Using GitHub as an example), 104 | which should give you a URL, in this case "git@github.com:schacon/hw.git". 105 | To add that to our project so we can push to it and fetch updates from it 106 | we would do this:
107 | 108 |109 | $ git remote 110 | $ git remote add github git@github.com:schacon/hw.git 111 | $ git remote -v 112 | github git@github.com:schacon/hw.git (fetch) 113 | github git@github.com:schacon/hw.git (push) 114 |115 | 116 |
Like the branch naming, remote alias names are arbitrary - just as 'master'
117 | has no special meaning but is widely used because git init
118 | sets it up by default, 'origin' is often used as a remote name because
119 | git clone
sets it up by default as the cloned-from URL. In
120 | this case we'll name the remote 'github', but you could name it just
121 | about anything.
122 |
Git addeth and Git taketh away. If you need to remove a remote - you are
130 | not using it anymore, the project is gone, etc - you can remove it with
131 | git remote rm [alias]
.
134 | $ git remote -v 135 | github git@github.com:schacon/hw.git (fetch) 136 | github git@github.com:schacon/hw.git (push) 137 | $ git remote add origin git://github.com/pjhyett/hw.git 138 | $ git remote -v 139 | github git@github.com:schacon/hw.git (fetch) 140 | github git@github.com:schacon/hw.git (push) 141 | origin git://github.com/pjhyett/hw.git (fetch) 142 | origin git://github.com/pjhyett/hw.git (push) 143 | $ git remote rm origin 144 | $ git remote -v 145 | github git@github.com:schacon/hw.git (fetch) 146 | github git@github.com:schacon/hw.git (push) 147 |148 | 149 |
If you want to rename remote aliases without having to delete them and add them again
155 | you can do that by running git remote rename [old-alias] [new-alias]
. This will
156 | allow you to modify the current name of the remote.
159 | $ git remote add github git@github.com:schacon/hw.git 160 | $ git remote -v 161 | github git@github.com:schacon/hw.git (fetch) 162 | github git@github.com:schacon/hw.git (push) 163 | $ git remote rename github origin 164 | $ git remote -v 165 | origin git@github.com:schacon/hw.git (fetch) 166 | origin git@github.com:schacon/hw.git (push) 167 |168 | 169 |
170 | In a nutshell with git remote
you can list our
171 | remote repositories and whatever URL
172 | that repository is using. You can use git remote add
to
173 | add new remotes, git remote rm
to delete existing ones or git remote rename [old-alias] [new-alias]
to rename them.
174 |
Should you ever need to update a remote's URL, you can do so with
182 | the git remote set-url
command.
183 |
186 | $ git remote -v 187 | github git@github.com:schacon/hw.git (fetch) 188 | github git@github.com:schacon/hw.git (push) 189 | origin git://github.com/pjhyett/hw.git (fetch) 190 | origin git://github.com/pjhyett/hw.git (push) 191 | $ git remote set-url origin git://github.com/github/git-reference.git 192 | $ git remote -v 193 | github git@github.com:schacon/hw.git (fetch) 194 | github git@github.com:schacon/hw.git (push) 195 | origin git://github.com/github/git-reference.git (fetch) 196 | origin git://github.com/github/git-reference.git (push) 197 |198 | 199 |
In addition to this, you can set a different push URL when you
200 | include the --push
flag. This allows you to fetch from
201 | one repo while pushing to another and yet both use the same remote alias.
202 |
205 | $ git remote -v 206 | github git@github.com:schacon/hw.git (fetch) 207 | github git@github.com:schacon/hw.git (push) 208 | origin git://github.com/github/git-reference.git (fetch) 209 | origin git://github.com/github/git-reference.git (push) 210 | $ git remote set-url --push origin git://github.com/pjhyett/hw.git 211 | $ git remote -v 212 | github git@github.com:schacon/hw.git (fetch) 213 | github git@github.com:schacon/hw.git (push) 214 | origin git://github.com/github/git-reference.git (fetch) 215 | origin git://github.com/pjhyett/hw.git (push) 216 |217 | 218 |
Internally, the git remote set-url
command calls
219 | git config remote
, but has the added benefit of reporting
220 | back any errors. git config remote
on the other hand, will
221 | silently fail if you mistype an argument or option and not actually set
222 | anything.
223 |
For example, we'll update the github
remote but
226 | instead reference it as guhflub
in both invocations.
227 |
230 | $ git remote -v 231 | github git@github.com:schacon/hw.git (fetch) 232 | github git@github.com:schacon/hw.git (push) 233 | origin git://github.com/github/git-reference.git (fetch) 234 | origin git://github.com/github/git-reference.git (push) 235 | $ git config remote.guhflub git://github.com/mojombo/hw.git 236 | $ git remote -v 237 | github git@github.com:schacon/hw.git (fetch) 238 | github git@github.com:schacon/hw.git (push) 239 | origin git://github.com/github/git-reference.git (fetch) 240 | origin git://github.com/github/git-reference.git (push) 241 | $ git remote set-url guhflub git://github.com/mojombo/hw.git 242 | fatal: No such remote 'guhflub' 243 |244 | 245 |
246 | In a nutshell, you can update the locations of your remotes
247 | with git remote set-url
. You can also set different push
248 | and fetch URLs under the same remote alias.
249 |
Git has two commands to update itself from a remote repository.
278 | git fetch
will synchronize you with another repo, pulling down any data
279 | that you do not have locally and giving you bookmarks to where each branch on
280 | that remote was when you synchronized. These are called "remote branches" and are
281 | identical to local branches except that Git will not allow you to check them out -
282 | however, you can merge from them, diff them to other branches, run history logs on
283 | them, etc. You do all of that stuff locally after you synchronize.
284 |
The second command that will fetch down new data from a remote server is
287 | git pull
. This command will basically run a git fetch
288 | immediately followed by a git merge
of the branch on that remote
289 | that is tracked by whatever branch you are currently in. Running the
290 | fetch
and merge
commands separately involves less magic
291 | and less problems, but if you like the idea of pull
, you can
292 | read about it in more detail in the
293 | official docs.
294 |
Assuming you have a remote all set up and you want to pull in updates, you
297 | would first run git fetch [alias]
to tell Git to fetch down all the
298 | data it has that you do not, then you would run git merge [alias]/[branch]
299 | to merge into your current branch anything new you see on the server
300 | (like if someone else has pushed in the meantime). So, if you were working on a
301 | Hello World project with several other people and wanted to bring in any changes
302 | that had been pushed since we last connected, we would do something like this:
305 | $ git fetch github 306 | remote: Counting objects: 4006, done. 307 | remote: Compressing objects: 100% (1322/1322), done. 308 | remote: Total 2783 (delta 1526), reused 2587 (delta 1387) 309 | Receiving objects: 100% (2783/2783), 1.23 MiB | 10 KiB/s, done. 310 | Resolving deltas: 100% (1526/1526), completed with 387 local objects. 311 | From github.com:schacon/hw 312 | 8e29b09..c7c5a10 master -> github/master 313 | 0709fdc..d4ccf73 c-langs -> github/c-langs 314 | 6684f82..ae06d2b java -> github/java 315 | * [new branch] ada -> github/ada 316 | * [new branch] lisp -> github/lisp 317 |318 | 319 |
Here we can see that since we last synchronized with this remote, five branches 320 | have been added or updated. The 'ada' and 'lisp' branches are new, where the 321 | 'master', 'c-langs' and 'java' branches have been updated. In our example case, 322 | other developers are pushing proposed updates to remote branches for review before 323 | they're merged into 'master'. 324 |
325 | 326 |You can see the mapping that Git makes. The 'master' branch on the remote
327 | repository becomes a branch named 'github/master' locally. That way you can
328 | merge the 'master' branch on that remote into the local 'master' branch by running
329 | git merge github/master
. Or, you can see what new commits are on that
330 | branch by running git log github/master ^master
. If your remote
331 | is named 'origin' it would be origin/master
instead. Almost any
332 | command you would run using local branches you can use remote branches with too.
333 |
If you have more than one remote repository, you can either fetch from specific
336 | ones by running git fetch [alias]
or you can tell Git to synchronize
337 | with all of your remotes by running git fetch --all
.
338 |
341 | In a nutshell you run git fetch [alias]
to synchronize your
342 | repository with a remote repository, fetching all the data it has that you do
343 | not into branch references locally for merging and whatnot.
344 |
To share the cool commits you've done with others, you need to push your
362 | changes to the remote repository. To do this, you run
363 | git push [alias] [branch]
which will attempt to make your [branch]
364 | the new [branch] on the [alias] remote. Let's try it by initially pushing
365 | our 'master' branch to the new 'github' remote we created earlier.
368 | $ git push github master 369 | Counting objects: 25, done. 370 | Delta compression using up to 2 threads. 371 | Compressing objects: 100% (25/25), done. 372 | Writing objects: 100% (25/25), 2.43 KiB, done. 373 | Total 25 (delta 4), reused 0 (delta 0) 374 | To git@github.com:schacon/hw.git 375 | * [new branch] master -> master 376 |377 | 378 |
Pretty easy. Now if someone clones that repository they will get exactly 379 | what we have committed and all of its history.
380 | 381 |What if you have a topic branch like the 'erlang' branch created earlier 382 | and want to share just that? You can just push that branch instead.
383 | 384 |385 | $ git push github erlang 386 | Counting objects: 7, done. 387 | Delta compression using up to 2 threads. 388 | Compressing objects: 100% (6/6), done. 389 | Writing objects: 100% (6/6), 652 bytes, done. 390 | Total 6 (delta 1), reused 0 (delta 0) 391 | To git@github.com:schacon/hw.git 392 | * [new branch] erlang -> erlang 393 |394 | 395 |
Now when people clone or fetch from that repository, they'll get an 'erlang' 396 | branch they can look at and merge from. You can push any branch to any 397 | remote repository that you have write access to in this way. If your branch 398 | is already on the server, it will try to update it, if it is not, Git will 399 | add it.
400 | 401 |The last major issue you run into with pushing to remote branches is the
402 | case of someone pushing in the meantime. If you and another developer clone
403 | at the same time, you both do commits, then she pushes and then you try to
404 | push, Git will by default not allow you to overwrite her changes. Instead,
405 | it basically runs git log
on the branch you're trying to push and
406 | makes sure it can see the current tip of the server's branch in your push's
407 | history. If it can't see what is on the server in your history, it concludes
408 | that you are out of date and will reject your push. You will rightly have to
409 | fetch, merge then push again - which makes sure you take her changes into
410 | account.
This is what happens when you try to push a branch to a remote branch 413 | that has been updated in the meantime:
414 | 415 |416 | $ git push github master 417 | To git@github.com:schacon/hw.git 418 | ! [rejected] master -> master (non-fast-forward) 419 | error: failed to push some refs to 'git@github.com:schacon/hw.git' 420 | To prevent you from losing history, non-fast-forward updates were rejected 421 | Merge the remote changes before pushing again. See the 'Note about 422 | fast-forwards' section of 'git push --help' for details. 423 |424 | 425 |
You can fix this by running git fetch github; git merge github/master
426 | and then pushing again.
427 |
430 | In a nutshell you run git push [alias] [branch]
to update a
431 | remote repository with the changes you've made locally. It will take what your
432 | [branch] looks like and push it to be [branch] on the remote, if possible. If
433 | someone else has pushed since you last fetched and merged, the Git server will
434 | deny your push until you are up to date.
435 |
On to Inspection and Comparison »
441 | -------------------------------------------------------------------------------- /zh/.gitignore: -------------------------------------------------------------------------------- 1 | _site 2 | *~ 3 | -------------------------------------------------------------------------------- /zh/about.html: -------------------------------------------------------------------------------- 1 | --- 2 | layout: zh_reference 3 | --- 4 | 10 | -------------------------------------------------------------------------------- /zh/basic/index.html: -------------------------------------------------------------------------------- 1 | --- 2 | layout: zh_reference 3 | --- 4 | 5 |14 | Git 的工作就是创建和保存你的项目的快照及与之后的快照进行对比。本章将对有关创建与提交你的项目的快照的命令作介绍。 15 |
16 | 17 |18 | 这里有个重要的概念,Git 有一个叫做“索引”的东东,有点像是你的快照的缓存区。这就使你能够从更改的文件中创建出一系列组织良好的快照,而不是一次提交所有的更改。 19 |
20 | 21 |
22 | 简而言之,使用 git add
添加需要追踪的新文件和待提交的更改,
23 | 然后使用 git status
和 git diff
查看有何改动,
24 | 最后用 git commit
将你的快照记录。这就是你要用的基本流程,绝大部分时候都是这样的。
25 |
42 | 在 Git 中,在提交你修改的文件之前,你需要把它们添加到缓存。如果该文件是新创建的,你可以执行
43 | git add
将该文件添加到缓存,但是,即使该文件已经被追踪了 —— 也就是说,曾经提交过了 ——
44 | 你仍然需要执行
回到我们的 Hello World 示例,初始化该项目之后,我们就要用 git add
将我们的文件添加进去了。
48 | 我们可以用 git status
看看我们的项目的当前状态。
49 |
52 | $ git status -s 53 | ?? README 54 | ?? hello.rb 55 |56 | 57 |
我们有俩尚未被追踪的文件,得添加一下。
58 | 59 |60 | $ git add README hello.rb 61 |62 | 63 |
现在我们再执行 git status
,就可以看到这俩文件已经加上去了。
66 | $ git status -s 67 | A README 68 | A hello.rb 69 |70 | 71 |
72 | 新项目中,添加所有文件很普遍,可以在当前工作目录执行命令:git add .
。
73 | 因为 Git 会递归地将你执行命令时所在的目录中的所有文件添加上去,所以如果你将当前的工作目录作为参数,
74 | 它就会追踪那儿的所有文件了。如此,git add .
就和 git add README hello.rb
75 | 有一样的效果。
76 | 此外,效果一致的还有 git add *
,不过那只是因为我们这还木有子目录,不需要递归地添加新文件。
77 |
好了,现在我们改个文件,再跑一下 git status
,有点古怪。
81 | $ vim README 82 | $ git status -s 83 | AM README 84 | A hello.rb 85 |86 | 87 |
“AM” 状态的意思是,这个文件在我们将它添加到缓存之后又有改动。这意味着如果我们现在提交快照,
88 | 我们记录的将是上次跑 git add
的时候的文件版本,而不是现在在磁盘中的这个。
89 | Git 并不认为磁盘中的文件与你想快照的文件必须是一致的 —— (如果你需要它们一致,)得用 git add
命令告诉它。
90 |
93 | 一言以蔽之,
94 | 当你要将你的修改包含在即将提交的快照里的时候,执行 git add
。
95 | 任何你没有添加的改动都不会被包含在内 —— 这意味着你可以比绝大多数其他源代码版本控制系统更精确地归置你的快照。
96 |
请查看《Pro Git》中 git add
的 “-p” 参数,以了解更多关于提交文件的灵活性的例子。
99 |
正如你在 git add
小节中所看到的,你可以执行 git status
117 | 命令查看你的代码在缓存与当前工作目录的状态。我演示该命令的时候加了 -s
参数,以获得简短的结果输出。
118 | 若没有这个标记,命令 git status
将告诉你更多的提示与上下文欣喜。
119 | 以下便是同样状态下,有跟没有 -s
参数的输出对比。简短的输出如下:
120 |
123 | $ git status -s 124 | AM README 125 | A hello.rb 126 |127 | 128 |
而同样的状态,详细的输出看起来是这样的:
129 | 130 |131 | $ git status 132 | # On branch master 133 | # 134 | # Initial commit 135 | # 136 | # Changes to be committed: 137 | # (use "git rm --cached <file>..." to unstage) 138 | # 139 | # new file: README 140 | # new file: hello.rb 141 | # 142 | # Changed but not updated: 143 | # (use "git add <file>..." to update what will be committed) 144 | # (use "git checkout -- <file>..." to discard changes in working directory) 145 | # 146 | # modified: README 147 | # 148 |149 | 150 |
你很容易发现简短的输出看起来很紧凑。而详细输出则很有帮助,提示你可以用何种命令完成你接下来可能要做的事情。 151 |
152 | 153 |Git 还会告诉你在你上次提交之后,有哪些文件被删除、修改或者存入缓存了。
154 | 155 |156 | $ git status -s 157 | M README 158 | D hello.rb 159 |160 | 161 |
你可以看到,在简短输出中,有两栏。第一栏是缓存的,第二栏则是工作目录的。
162 | 所以假设你临时提交了 README 文件,然后又改了些,并且没有执行 git add
,你会看到这个:
165 | $ git status -s 166 | MM README 167 | D hello.rb 168 |169 | 170 |
171 | 一言以蔽之,执行 git status
以查看在你上次提交之后有啥被修改或者临时提交了,
172 | 从而决定自己是否需要提交一次快照,同时也能知道有什么改变被记录进去了。
173 |
git diff
有两个主要的应用场景。我们将在此介绍其一,
190 | 在 检阅与对照 一章中,我们将介绍其二。
191 | 我们这里介绍的方式是用此命令描述已临时提交的或者已修改但尚未提交的改动。
192 |
如果没有其他参数,git diff
会以规范化的 diff 格式(一个补丁)显示自从你上次提交快照之后尚未缓存的所有更改。
200 |
203 | $ vim hello.rb 204 | $ git status -s 205 | M hello.rb 206 | $ git diff 207 | diff --git a/hello.rb b/hello.rb 208 | index d62ac43..8d15d50 100644 209 | --- a/hello.rb 210 | +++ b/hello.rb 211 | @@ -1,7 +1,7 @@ 212 | class HelloWorld 213 | 214 | def self.hello 215 | - puts "hello world" 216 | + puts "hola mundo" 217 | end 218 | 219 | end 220 |221 | 222 |
所以,git status
显示你上次提交更新至后所更改或者写入缓存的改动,
223 | 而 git diff
一行一行地显示这些改动具体是啥。
224 | 通常执行完 git status
之后接着跑一下 git diff
是个好习惯。
225 |
git diff --cached
命令会告诉你有哪些内容已经写入缓存了。
233 | 也就是说,此命令显示的是接下来要写入快照的内容。所以,如果你将上述示例中的 hello.rb
234 | 写入缓存,因为 git diff
显示的是尚未缓存的改动,所以在此执行它不会显示任何信息。
235 |
238 | $ git status -s 239 | M hello.rb 240 | $ git add hello.rb 241 | $ git status -s 242 | M hello.rb 243 | $ git diff 244 | $ 245 |246 | 247 |
如果你想看看已缓存的改动,你需要执行的是
248 | git diff --cached
。
251 | $ git status -s 252 | M hello.rb 253 | $ git diff 254 | $ 255 | $ git diff --cached 256 | diff --git a/hello.rb b/hello.rb 257 | index d62ac43..8d15d50 100644 258 | --- a/hello.rb 259 | +++ b/hello.rb 260 | @@ -1,7 +1,7 @@ 261 | class HelloWorld 262 | 263 | def self.hello 264 | - puts "hello world" 265 | + puts "hola mundo" 266 | end 267 | 268 | end 269 |270 | 271 |
如果你想一并查看已缓存的与未缓存的改动,可以执行 git diff HEAD
——
277 | 也就是说你要看到的是工作目录与上一次提交的更新的区别,无视缓存。
278 | 假设我们又改了些 ruby.rb
的内容,那缓存的与未缓存的改动我们就都有了。
279 | 以上三个 diff
命令的结果如下:
280 |
282 | $ vim hello.rb 283 | $ git diff 284 | diff --git a/hello.rb b/hello.rb 285 | index 4f40006..2ae9ba4 100644 286 | --- a/hello.rb 287 | +++ b/hello.rb 288 | @@ -1,7 +1,7 @@ 289 | class HelloWorld 290 | 291 | + # says hello 292 | def self.hello 293 | puts "hola mundo" 294 | end 295 | 296 | end 297 | $ git diff --cached 298 | diff --git a/hello.rb b/hello.rb 299 | index 2aabb6e..4f40006 100644 300 | --- a/hello.rb 301 | +++ b/hello.rb 302 | @@ -1,7 +1,7 @@ 303 | class HelloWorld 304 | 305 | def self.hello 306 | - puts "hello world" 307 | + puts "hola mundo" 308 | end 309 | 310 | end 311 | $ git diff HEAD 312 | diff --git a/hello.rb b/hello.rb 313 | index 2aabb6e..2ae9ba4 100644 314 | --- a/hello.rb 315 | +++ b/hello.rb 316 | @@ -1,7 +1,8 @@ 317 | class HelloWorld 318 | 319 | + # says hello 320 | def self.hello 321 | - puts "hello world" 322 | + puts "hola mundo" 323 | end 324 | 325 | end 326 |327 | 328 |
如果我们不想要看整个 diff 输出,但是又想比 git status
详细点,
334 | 就可以用 --stat
选项。该选项使它显示摘要而非全文。上文示例在使用 --stat
选项时,输出如下:
335 |
338 | $ git status -s 339 | MM hello.rb 340 | $ git diff --stat 341 | hello.rb | 1 + 342 | 1 files changed, 1 insertions(+), 0 deletions(-) 343 | $ git diff --cached --stat 344 | hello.rb | 2 +- 345 | 1 files changed, 1 insertions(+), 1 deletions(-) 346 | $ git diff HEAD --stat 347 | hello.rb | 3 ++- 348 | 1 files changed, 2 insertions(+), 1 deletions(-) 349 |350 | 351 |
你还可以在上述命令后面制定一个目录,从而只查看特定文件或子目录的 diff
输出。
352 |
356 | 简而言之,
357 | 执行 git diff
来查看执行 git status
的结果的详细信息 ——
358 | 一行一行地显示这些文件是如何被修改或写入缓存的。
359 |
现在你使用 git add
命令将想要快照的内容写入了缓存,
377 | 执行 git commit
就将它实际存储快照了。
378 | Git 为你的每一个提交都记录你的名字与电子邮箱地址,所以第一步是告诉 Git 这些都是啥。
379 |
382 | $ git config --global user.name 'Your Name' 383 | $ git config --global user.email you@somedomain.com 384 |385 | 386 |
让我们写入缓存,并提交对 hello.rb
的所有改动。在首个例子中,我们使用 -m
选项以在命令行中提供提交注释。
387 |
390 | $ git add hello.rb
391 | $ git status -s
392 | M hello.rb
393 | $ git commit -m 'my hola mundo changes'
394 | [master 68aa034] my hola mundo changes
395 | 1 files changed, 2 insertions(+), 1 deletions(-)
396 |
397 |
398 | 现在我们已经记录了快照。如果我们再执行 git status
,会看到我们有一个“干净的工作目录”。
399 | 这意味着我们在最近一次提交之后,没有做任何改动 —— 在我们的项目中没有未快照的工作。
400 |
403 | $ git status 404 | # On branch master 405 | nothing to commit (working directory clean) 406 |407 | 408 |
如果你漏掉了 -m
选项,Git 会尝试为你打开一个编辑器以填写提交信息。
409 | 如果 Git 在你对它的配置中找不到相关信息,默认会打开 vim
。屏幕会像这样:
410 |
413 | 414 | # Please enter the commit message for your changes. Lines starting 415 | # with '#' will be ignored, and an empty message aborts the commit. 416 | # On branch master 417 | # Changes to be committed: 418 | # (use "git reset HEAD <file>..." to unstage) 419 | # 420 | # modified: hello.rb 421 | # 422 | ~ 423 | ~ 424 | ".git/COMMIT_EDITMSG" 9L, 257C 425 |426 | 427 |
在此,你在文件头部添加实际的提交信息。以“#”开头的行都会被无视 ——Git 将 git status
的输出结果放在那儿以提示你都改了、缓存了啥。
428 |
通常,撰写良好的提交信息是很重要的。以开放源代码项目为例,多多少少以以下格式写你的提示消息是个不成文的规定: 431 |
432 | 433 |434 | 简短的关于改动的总结(25个字或者更少) 435 | 436 | 如果有必要,更详细的解释文字。约 36 字时换行。在某些情况下, 437 | 第一行会被作为电子邮件的开头,而剩余的则会作为邮件内容。 438 | 将小结从内容隔开的空行是至关重要的(除非你没有内容); 439 | 如果这两个待在一起,有些 git 工具会犯迷糊。 440 | 441 | 空行之后是更多的段落。 442 | 443 | - 列表也可以 444 | 445 | - 通常使用连字符(-)或者星号(*)来标记列表,前面有个空格, 446 | 在列表项之间有空行,不过这些约定也会有些变化。 447 | 448 | # Please enter the commit message for your changes. Lines starting 449 | # with '#' will be ignored, and an empty message aborts the commit. 450 | # On branch master 451 | # Changes to be committed: 452 | # (use "git reset HEAD <file>..." to unstage) 453 | # 454 | # modified: hello.rb 455 | # 456 | ~ 457 | ~ 458 | ~ 459 | ".git/COMMIT_EDITMSG" 25L, 884C written 460 |461 | 462 |
463 | 提交注解是很重要的。因为 Git 很大一部分能耐就是它在组织本地提交和与他人分享的弹性, 464 | 它很给力地能够让你为逻辑独立的改变写三到四条提交注解,以便你的工作被同仁审阅。因为提交与推送改动是有区别的, 465 | 请务必花时间将各个逻辑独立的改动放到另外一个提交,并附上一份良好的提交注解, 466 | 以使与你合作的人能够方便地了解你所做的,以及你为何要这么做。 467 |
468 | 469 |如果你觉得 git add
提交缓存的流程太过繁琐,Git 也允许你用 -a
选项跳过这一步。
475 | 基本上这句话的意思就是,为任何已有记录的文件执行 git add
——
476 | 也就是说,任何在你最近的提交中已经存在,并且之后被修改的文件。
477 | 这让你能够用更 Subversion 方式的流程,修改些文件,然后想要快照所有所做的改动的时候执行 git commit -a
。
478 | 不过你仍然需要执行 git add
来添加新文件,就像 Subversion 一样。
479 |
482 | $ vim hello.rb 483 | $ git status -s 484 | M hello.rb 485 | $ git commit -m 'changes to hello file' 486 | # On branch master 487 | # Changed but not updated: 488 | # (use "git add <file>..." to update what will be committed) 489 | # (use "git checkout -- <file>..." to discard changes in working directory) 490 | # 491 | # modified: hello.rb 492 | # 493 | no changes added to commit (use "git add" and/or "git commit -a") 494 | $ git commit -am 'changes to hello file' 495 | [master 78b2670] changes to hello file 496 | 1 files changed, 2 insertions(+), 1 deletions(-) 497 |498 | 499 |
注意,如果你不缓存改动,直接执行 git commit
,Git 会直接给出 git status
500 | 命令的输出,提醒你啥也没缓存。我已将该消息中的重要部分高亮,它说没有添加需要提交的缓存。
501 | 如果你使用 -a
,它会缓存并提交每个改动(不含新文件)。
502 |
505 | 现在你就完成了整个快照的流程 ——改些文件,然后用 git add
将要提交的改动提交到缓存,
506 | 用 git status
和 git diff
看看你都改了啥,最后 git commit
永久地保存快照。
507 |
510 | 简而言之,执行 git commit
记录缓存区的快照。如果需要的话,这个快照可以用来做比较、共享以及恢复。
511 |
git reset
可能是人类写的最费解的命令了。
528 | 我用 Git 有些年头了,甚至还写了本书,但有的时候还是会搞不清它会做什么。
529 | 所以,我只说三个明确的,通常有用的调用。请你跟我一样尽管用它 —— 因为它可以很有用。
530 |
在此例中,我们可以用它来将不小心缓存的东东取消缓存。假设你修改了两个文件,想要将它们记录到两个不同的提交中去。
533 | 你应该缓存并提交一个,再缓存并提交另外一个。如果你不小心两个都缓存了,那要如何才能取消缓存呢?
534 | 你可以用 git reset HEAD -- file
。
535 | 技术上说,在这里你不需要使用 --
—— 它用来告诉 Git 这时你已经不再列选项,剩下的是文件路径了。
536 | 不过养成使用它分隔选项与路径的习惯很重要,即使在你可能并不需要的时候。
537 |
好,让我们看看取消缓存是什么样子的。这里我们有两个最近提交之后又有所改动的文件。我们将两个都缓存,并取消缓存其中一个。
540 | 541 |542 | $ git status -s 543 | M README 544 | M hello.rb 545 | $ git add . 546 | $ git status -s 547 | M README 548 | M hello.rb 549 | $ git reset HEAD -- hello.rb 550 | Unstaged changes after reset: 551 | M hello.rb 552 | $ git status -s 553 | M README 554 | M hello.rb 555 |556 | 557 |
558 | 现在你执行 git commit
将只记录 README
文件的改动,并不含现在并不在缓存中的 hello.rb
。
559 |
562 | 如果你好奇,它实际的操作是将该文件在“索引”中的校验和重置为最近一次提交中的值。
563 | git add
会计算一个文件的校验和,将它添加到“索引”中,
564 | 而 git reset HEAD
将它改写回原先的,从而取消缓存操作。
565 |
568 | 如果你想直接执行 git unstage
,你可以在 Git 中配置个别名。
569 | 执行 git config --global alias.unstage "reset HEAD"
即可。
570 | 一旦执行完它,你就可以直接用 git unstage [file]
作为代替了。
571 |
如果你忘了取消缓存的命令,Git 的常规 git status
输出的提示会很有帮助。
574 | 例如,在你有已缓存的文件时,如果你不带 -s
执行 git status
,它将告诉你怎样取消缓存:
575 |
578 | $ git status 579 | # On branch master 580 | # Changes to be committed: 581 | # (use "git reset HEAD <file>..." to unstage) 582 | # 583 | # modified: README 584 | # modified: hello.rb 585 | # 586 |587 | 588 |
589 | 简而言之,执行 git reset HEAD
以取消之前 git add
添加,但不希望包含在下一提交快照中的缓存。
590 |
608 | git rm
会将条目从缓存区中移除。这与 git reset HEAD
将条目取消缓存是有区别的。
609 | “取消缓存”的意思就是将缓存区恢复为我们做出修改之前的样子。
610 | 在另一方面,git rm
则将该文件彻底从缓存区踢出,因此它不再下一个提交快照之内,进而有效地删除它。
611 |
614 | 默认情况下,git rm file
会将文件从缓存区和你的硬盘中(工作目录)删除。
615 | 如果要在工作目录中留着该文件,可以使用 git rm --cached
616 |
624 | 不像绝大多数其他版本控制系统,Git 并不记录记录文件重命名。它反而只记录快照,并对比快照以找到有啥文件可能被重命名了。
625 | 如果一个文件从更新中删除了,而在下次快照中新添加的另一个文件的内容与它很相似,Git 就知道这极有可能是个重命名。
626 | 因此,虽然有 git mv
命令,但它有点多余 —— 它做得所有事情就是 git rm --cached
,
627 | 重命名磁盘上的文件,然后再执行 git add
把新文件添加到缓存区。
628 | 你并不需要用它,不过如果觉得这样容易些,尽管用吧。
629 |
632 | 我自己并不使用此命令的普通形式 —— 删除文件。通常直接从硬盘删除文件,然后执行 git commit -a
会简单些。
633 | 它会自动将删除的文件从索引中移除。
634 |
637 | 简而言之,
638 | 执行 git rm
来删除 Git 追踪的文件。它还会删除你的工作目录中的相应文件。
639 |
分支是我最喜欢的 Git 特性之一。如果你用过其他版本控制系统,把你所知的分支给忘记,倒可能更有帮助些 —— 14 | 事实上,以我们使用分支的方式,把 Git 的分支看作 上下文 反而更合适。 15 | 当你检出分支时,你可以在两三个不同的分支之间来回切换。 16 |
17 | 18 |
19 | 简而言之,你可以执行 git branch (branchname)
来创建分支,
20 | 使用 git checkout (branchname)
命令切换到该分支,在该分支的上下文环境中,
21 | 提交快照等,之后可以很容易地来回切换。当你切换分支的时候,Git 会用该分支的最后提交的快照替换你的工作目录的内容,
22 | 所以多个分支不需要多个目录。使用 git merge
来合并分支。你可以多次合并到统一分支,
23 | 也可以选择在合并之后直接删除被并入的分支。
24 |
git branch
命令是 Git 中的通用分支管理工具,可以通过它完成多项任务。
52 | 我们先说你会用到的最多的命令 —— 列出分支、创建分支和删除分支。
53 | 我们还会介绍用来切换分支的 git checkout
命令。
54 |
没有参数时,git branch
会列出你在本地的分支。你所在的分支的行首会有个星号作标记。
62 | 如果你开启了彩色模式,当前分支会用绿色显示。
63 |
66 | $ git branch
67 | * master
68 |
69 |
70 | 此例的意思就是,我们有一个叫做“master”的分支,并且该分支是当前分支。
71 | 当你执行 git init
的时候,缺省情况下 Git 就会为你创建“master”分支。
72 | 但是这名字一点特殊意味都没有 —— 事实上你并不非得要一个叫做“master”的分支。
73 | 不过由于它是缺省分支名的缘故,绝大部分项目都有这个分支。
74 |
我们动手创建一个分支,并切换过去。执行 git branch (branchname)
即可。
82 |
85 | $ git branch testing
86 | $ git branch
87 | * master
88 | testing
89 |
90 |
91 | 现在我们可以看到,有了一个新分支。当你以此方式在上次提交更新之后创建了新分支,如果后来又有更新提交,
92 | 然后又切换到了“testing”分支,Git 将还原你的工作目录到你创建分支时候的样子 ——
93 | 你可以把它看作一个记录你当前进度的书签。让我们实际运用看看 ——
94 | 我们用 git checkout (branch)
切换到我们要修改的分支。
95 |
98 | $ ls 99 | README hello.rb 100 | $ echo 'test content' > test.txt 101 | $ echo 'more content' > more.txt 102 | $ git add *.txt 103 | $ git commit -m 'added two files' 104 | [master 8bd6d8b] added two files 105 | 2 files changed, 2 insertions(+), 0 deletions(-) 106 | create mode 100644 more.txt 107 | create mode 100644 test.txt 108 | $ ls 109 | README hello.rb more.txt test.txt 110 | $ git checkout testing 111 | Switched to branch 'testing' 112 | $ ls 113 | README hello.rb 114 |115 | 116 |
当我们切换到“测试”分支的时候,我们添加的新文件被移除了。切换回“master”分支的时候,它们有重新出现了。 117 |
118 | 119 |120 | $ ls 121 | README hello.rb 122 | $ git checkout master 123 | Switched to branch 'master' 124 | $ ls 125 | README hello.rb more.txt test.txt 126 |127 | 128 |
134 | 通常情况下,你会更希望立即切换到新分支,从而在该分支中操作,然后当此分支的开发日趋稳定时,
135 | 将它合并到稳定版本的分支(例如“master”)中去。
136 | 执行 git branch newbranch; git checkout newbranch
也很简单,
137 | 不过 Git 还为你提供了快捷方式:git checkout -b newbranch
。
138 |
141 | $ git branch 142 | * master 143 | $ ls 144 | README hello.rb more.txt test.txt 145 | $ git checkout -b removals 146 | Switched to a new branch 'removals' 147 | $ git rm more.txt 148 | rm 'more.txt' 149 | $ git rm test.txt 150 | rm 'test.txt' 151 | $ ls 152 | README hello.rb 153 | $ git commit -am 'removed useless files' 154 | [removals 8f7c949] removed useless files 155 | 2 files changed, 0 insertions(+), 2 deletions(-) 156 | delete mode 100644 more.txt 157 | delete mode 100644 test.txt 158 | $ git checkout master 159 | Switched to branch 'master' 160 | $ ls 161 | README hello.rb more.txt test.txt 162 |163 | 164 |
如你所见,我们创建了一个分支,在该分支的上下文中移除了一些文件,然后切换回我们的主分支,那些文件又回来了。 165 | 使用分支将工作切分开来,从而让我们能够在不同上下文中做事,并来回切换。 166 |
167 | 168 |169 | 创建新分支,在其中完成一部分工作,完成之后将它合并到主分支并删除。你会觉得这很方便,因为这么做很快很容易。 170 | 如此,当你觉得这部分工作并不靠谱,舍弃它很容易。并且,如果你必须回到稳定分支做些事情, 171 | 也可以很方便地这个独立分支的工作先丢在一边,完成要事之后再切换回来。 172 |
173 | 174 |假设我们要删除一个分支(比如上例中的“testing”分支,该分支没啥特殊的内容了),
180 | 可以执行 git branch -d (branch)
把它删掉。
181 |
184 | $ git branch 185 | * master 186 | testing 187 | $ git branch -d testing 188 | Deleted branch testing (was 78b2670). 189 | $ git branch 190 | * master 191 |192 | 193 |
194 | 简而言之 使用 git branch
列出现有的分支、创建新分支以及删除不必要或者已合并的分支。
195 |
一旦某分支有了独立内容,你终究会希望将它合并回到你的主分支。
212 | 你可以使用 git merge
命令将任何分支合并到当前分支中去。
213 | 我们那上例中的“removals”分支为例。假设我们创建了一个分支,移除了一些文件,并将它提交到该分支,
214 | 其实该分支是与我们的主分支(也就是“master”)独立开来的。
215 | 要想将这些移除操作包含在主分支中,你可以将“removals”分支合并回去。
216 |
219 | $ git branch 220 | * master 221 | removals 222 | $ ls 223 | README hello.rb more.txt test.txt 224 | $ git merge removals 225 | Updating 8bd6d8b..8f7c949 226 | Fast-forward 227 | more.txt | 1 - 228 | test.txt | 1 - 229 | 2 files changed, 0 insertions(+), 2 deletions(-) 230 | delete mode 100644 more.txt 231 | delete mode 100644 test.txt 232 | $ ls 233 | README hello.rb 234 |235 | 236 |
当然,合并并不仅仅是简单的文件添加、移除的操作,Git 也会合并修改 —— 事实上,它很会合并修改。 241 | 举例,我们看看在某分支中编辑某个文件,然后在另一个分支中把它的名字改掉再做些修改, 242 | 最后将这俩分支合并起来。你觉得会变成一坨 shi?我们试试看。 243 |
244 | 245 |246 | $ git branch 247 | * master 248 | $ cat hello.rb 249 | class HelloWorld 250 | def self.hello 251 | puts "Hello World" 252 | end 253 | end 254 | 255 | HelloWorld.hello 256 |257 | 258 |
首先,我们创建一个叫做“change_class”的分支,切换过去,从而将重命名类等操作独立出来。我们将类名从 “HelloWorld” 改为 “HiWorld”。 259 |
260 | 261 |262 | $ git checkout -b change_class 263 | M hello.rb 264 | Switched to a new branch 'change_class' 265 | $ vim hello.rb 266 | $ head -1 hello.rb 267 | class HiWorld 268 | $ git commit -am 'changed the class name' 269 | [change_class 3467b0a] changed the class name 270 | 1 files changed, 2 insertions(+), 4 deletions(-) 271 |272 | 273 |
然后,将重命名类操作提交到 “change_class” 分支中。
274 | 现在,假如切换回 “master” 分支我们可以看到类名恢复到了我们切换到 “change_class” 分支之前的样子。
275 | 现在,再做些修改(即代码中的输出),同时将文件名从 hello.rb
改为 ruby.rb
。
276 |
279 | $ git checkout master 280 | Switched to branch 'master' 281 | $ git mv hello.rb ruby.rb 282 | $ vim ruby.rb 283 | $ git diff 284 | diff --git a/ruby.rb b/ruby.rb 285 | index 2aabb6e..bf64b17 100644 286 | --- a/ruby.rb 287 | +++ b/ruby.rb 288 | @@ -1,7 +1,7 @@ 289 | class HelloWorld 290 | 291 | def self.hello 292 | - puts "Hello World" 293 | + puts "Hello World from Ruby" 294 | end 295 | 296 | end 297 | $ git commit -am 'added from ruby' 298 | [master b7ae93b] added from ruby 299 | 1 files changed, 1 insertions(+), 1 deletions(-) 300 | rename hello.rb => ruby.rb (65%) 301 |302 | 303 |
现在这些改变已经记录到我的 “master” 分支了。请注意,这里类名还是 “HelloWorld”,而不是 “HiWorld”。 304 | 然后我想将类名的改变合并过来,我把 “change_class” 分支合并过来就行了。 305 | 但是,我已经将文件名都改掉了,Git 知道该怎么办么? 306 |
307 | 308 |309 | $ git branch 310 | change_class 311 | * master 312 | $ git merge change_class 313 | Renaming hello.rb => ruby.rb 314 | Auto-merging ruby.rb 315 | Merge made by recursive. 316 | ruby.rb | 6 ++---- 317 | 1 files changed, 2 insertions(+), 4 deletions(-) 318 | $ cat ruby.rb 319 | class HiWorld 320 | def self.hello 321 | puts "Hello World from Ruby" 322 | end 323 | end 324 | 325 | HiWorld.hello 326 |327 | 328 |
不错,它就是发现了。请注意,在这部操作,我没有遇到合并冲突,并且文件已经重命名、类名也换掉了。挺酷。 329 |
330 | 331 |那么,Git 合并很有魔力,我们再也不用处理合并冲突了,对吗?不太确切。 336 | 不同分支中修改了相同区块的代码,电脑自己猜不透神马的情况下,冲突就摆在我们面前了。 337 | 我们看看两个分支中改了同一行代码的例子。 338 |
339 | 340 |341 | $ git branch 342 | * master 343 | $ git checkout -b fix_readme 344 | Switched to a new branch 'fix_readme' 345 | $ vim README 346 | $ git commit -am 'fixed readme title' 347 | [fix_readme 3ac015d] fixed readme title 348 | 1 files changed, 1 insertions(+), 1 deletions(-) 349 |350 | 351 |
我们在某分支中修改了 README 文件中的一行,并提交了。我们再在 “master” 分支中对同个文件的同一行内容作不同的修改。 352 |
353 | 354 |355 | $ git checkout master 356 | Switched to branch 'master' 357 | $ vim README 358 | $ git commit -am 'fixed readme title differently' 359 | [master 3cbb6aa] fixed readme title differently 360 | 1 files changed, 1 insertions(+), 1 deletions(-) 361 |362 | 363 |
有意思的来了 —— 我们将前一个分支合并到 “master” 分支,一个合并冲突就出现了。 364 |
365 | 366 |367 | $ git merge fix_readme 368 | Auto-merging README 369 | CONFLICT (content): Merge conflict in README 370 | Automatic merge failed; fix conflicts and then commit the result. 371 | $ cat README 372 | <<<<<<< HEAD 373 | Many Hello World Examples 374 | ======= 375 | Hello World Lang Examples 376 | >>>>>>> fix_readme 377 | 378 | This project has examples of hello world in 379 | nearly every programming language. 380 |381 | 382 |
你可以看到,Git 在产生合并冲突的地方插入了标准的与 Subversion 很像的合并冲突标记。 383 | 轮到我们去解决这些冲突了。在这里我们就手动把它解决。如果你要 Git 打开一个图形化的合并工具, 384 | 可以看看 git 合并工具 385 | (比如 kdiff3、emerge、p4merge 等)。 386 |
387 | 388 |389 | $ vim README here I'm fixing the conflict 390 | $ git diff 391 | diff --cc README 392 | index 9103e27,69cad1a..0000000 393 | --- a/README 394 | +++ b/README 395 | @@@ -1,4 -1,4 +1,4 @@@ 396 | - Many Hello World Examples 397 | -Hello World Lang Examples 398 | ++Many Hello World Lang Examples 399 | 400 | This project has examples of hello world in 401 |402 | 403 |
在 Git 中,处理合并冲突的时候有个很酷的提示。
404 | 如果你执行 git diff
,就像我演示的这样,它会告诉你冲突的两方,和你是如何解决的。
405 | 现在是时候把它标记为已解决了。在 Git 中,我们可以用 git add
—— 要告诉 Git 文件冲突已经解决,你必须把它写入缓存区。
406 |
409 | $ git status -s 410 | UU README 411 | $ git add README 412 | $ git status -s 413 | M README 414 | $ git commit 415 | [master 8d585ea] Merge branch 'fix_readme' 416 |417 | 418 |
现在我们成功解决了合并中的冲突,并提交了结果 419 |
420 | 421 |
422 | 简而言之 使用 git merge
将另一个分支并入当前的分支中去。
423 | Git 会自动以最佳方式将两个不同快照中独特的工作合并到一个新快照中去。
424 |
到目前为止,我们已经提交快照到项目中,在不同的各自分离的上下文中切换,
442 | 但假如我们忘了自己是如何到目前这一步的那该怎么办?或者假如我们想知道此分支与彼分支到底有啥区别?
443 | Git 提供了一个告诉你使你达成当前快照的所有提交消息的工具,叫做 git log
。
444 |
要理解日志(log)命令,你需要了解当执行 git commit
以存储一个快照的时候,都有啥信息被保存了。
447 | 除了文件详单、提交消息和提交者的信息,Git 还保存了你的此次提交所基于的快照。
448 | 也就是,假如你克隆了一个项目,你是在什么快照的基础上做的修改而得到新保存的快照的?
449 | 这有益于为项目进程提供上下文,使 Git 能够弄明白谁做了什么改动。
450 | 如果 Git 有你的快照所基于的快照的话,它就能自动判断你都改变了什么。而新提交所基于的提交,被称作新提交的“父亲”。
451 |
某分支的按时间排序的“父亲”列表,当你在该分支时,可以执行 git log
以查看。
454 | 例如,如果我们在本章中操作的 Hello World 项目中执行 git log
,我们可以看到已提交的消息。
455 |
458 | $ git log 459 | commit 8d585ea6faf99facd39b55d6f6a3b3f481ad0d3d 460 | Merge: 3cbb6aa 3ac015d 461 | Author: Scott Chacon <schacon@gmail.com> 462 | Date: Fri Jun 4 12:59:47 2010 +0200 463 | 464 | Merge branch 'fix_readme' 465 | 466 | Conflicts: 467 | README 468 | 469 | commit 3cbb6aae5c0cbd711c098e113ae436801371c95e 470 | Author: Scott Chacon <schacon@gmail.com> 471 | Date: Fri Jun 4 12:58:53 2010 +0200 472 | 473 | fixed readme title differently 474 | 475 | commit 3ac015da8ade34d4c7ebeffa2053fcac33fb495b 476 | Author: Scott Chacon <schacon@gmail.com> 477 | Date: Fri Jun 4 12:58:36 2010 +0200 478 | 479 | fixed readme title 480 | 481 | commit 558151a95567ba4181bab5746bc8f34bd87143d6 482 | Merge: b7ae93b 3467b0a 483 | Author: Scott Chacon <schacon@gmail.com> 484 | Date: Fri Jun 4 12:37:05 2010 +0200 485 | 486 | Merge branch 'change_class' 487 | ... 488 |489 | 490 |
我们可以用 --oneline
选项来查看历史记录的紧凑简洁的版本。
491 |
494 | $ git log --oneline 495 | 8d585ea Merge branch 'fix_readme' 496 | 3cbb6aa fixed readme title differently 497 | 3ac015d fixed readme title 498 | 558151a Merge branch 'change_class' 499 | b7ae93b added from ruby 500 | 3467b0a changed the class name 501 | 17f4acf first commit 502 |503 | 504 |
505 | 这告诉我们的是,此项目的开发历史。如果提交消息描述性很好,这就能为我们提供关于有啥改动被应用、或者影响了当前快照的状态、以及这快照里头都有啥。 506 |
507 | 508 |我们还可以用它的十分有帮助的 --graph
选项,查看历史中什么时候出现了分支、合并。以下为相同的命令,开启了拓扑图选项:
509 |
512 | $ git log --oneline --graph 513 | * 8d585ea Merge branch 'fix_readme' 514 | |\ 515 | | * 3ac015d fixed readme title 516 | * | 3cbb6aa fixed readme title differently 517 | |/ 518 | * 558151a Merge branch 'change_class' 519 | |\ 520 | | * 3467b0a changed the class name 521 | * | b7ae93b added from ruby 522 | |/ 523 | * 17f4acf first commit 524 |525 | 526 |
现在我们可以更清楚明了地看到何时工作分叉、又何时归并。
527 | 这对查看发生了什么、应用了什么改变很有帮助,并且极大地帮助你管理你的分支。
528 | 让我们创建一个分支,在里头做些事情,然后切回到主分支,也做点事情,然后看看 log
529 | 命令是如何帮助我们理清这俩分支上都发生了啥的。
530 |
首先我们创建一个分支,来添加 Erlang 编程语言的 Hello World 示例 —— 533 | 我们想要在一个分支里头做这个,以避免让可能还不能工作的代码弄乱我们的稳定分支。 534 | 这样就可以切来切去,片叶不沾身。 535 |
536 | 537 |538 | $ git checkout -b erlang 539 | Switched to a new branch 'erlang' 540 | $ vim erlang_hw.erl 541 | $ git add erlang_hw.erl 542 | $ git commit -m 'added erlang' 543 | [erlang ab5ab4c] added erlang 544 | 1 files changed, 5 insertions(+), 0 deletions(-) 545 | create mode 100644 erlang_hw.erl 546 |547 | 548 |
由于我们玩函数式编程很开心,以至于沉迷其中,又在“erlang”分支中添加了一个 Haskell 的示例程序。 549 |
550 | 551 |552 | $ vim haskell.hs 553 | $ git add haskell.hs 554 | $ git commit -m 'added haskell' 555 | [erlang 1834130] added haskell 556 | 1 files changed, 4 insertions(+), 0 deletions(-) 557 | create mode 100644 haskell.hs 558 |559 | 560 |
最后,我们决定还是把 Ruby 程序的类名改回原先的样子。与其创建另一个分支,我们可以返回主分支,改变它,然后直接提交。 561 |
562 | 563 |564 | $ git checkout master 565 | Switched to branch 'master' 566 | $ ls 567 | README ruby.rb 568 | $ vim ruby.rb 569 | $ git commit -am 'reverted to old class name' 570 | [master 594f90b] reverted to old class name 571 | 1 files changed, 2 insertions(+), 2 deletions(-) 572 |573 | 574 |
现在假设我们有段时间不做这个项目了,我们做别的去了。
575 | 当我们回来的时候,我们想知道“erlang”分支都是啥,而主分支的进度又是怎样。
576 | 仅仅看分支的名字,我们是无从知道自己还在里面有 Haskell 的改动的,但是用 git log
我们就可以。
577 | 如果你在命令行中提供一个分支名字,它就会显示该分支历史中“可及”的提交,即从该分支创立起可追溯的影响了最终的快照的提交。
578 |
581 | $ git log --oneline erlang
582 | 1834130 added haskell
583 | ab5ab4c added erlang
584 | 8d585ea Merge branch 'fix_readme'
585 | 3cbb6aa fixed readme title differently
586 | 3ac015d fixed readme title
587 | 558151a Merge branch 'change_class'
588 | b7ae93b added from ruby
589 | 3467b0a changed the class name
590 | 17f4acf first commit
591 |
592 |
593 | 如此,我们很容易就看到分支里头还包括了 Haskell 代码(高亮显示了)。 594 | 更酷的是,我们很容易地告诉 Git,我们只对某个分支中可及的提交感兴趣。换句话说,某分支中与其他分支相比唯一的提交。 595 |
596 | 597 |在此例中,如果我们想要合并“erlang”分支,我们需要看当合并的时候,都有啥提交会作用到我们的快照上去。
598 | 我们告诉 Git 的方式是,在不想要看到的分支前放一个 ^
。
599 | 例如,如果我们想要看“erlang”分支中但不在主分支中的提交,我们可以用 erlang ^master
,或者反之。
600 |
603 | $ git log --oneline erlang ^master 604 | 1834130 added haskell 605 | ab5ab4c added erlang 606 | $ git log --oneline master ^erlang 607 | 594f90b reverted to old class name 608 |609 | 610 |
这为我们提供了一个良好的、简易的分支管理工具。它使我们能够非常容易地查看对某个分支唯一的提交,从而知道我们缺少什么,以及当我们要合并时,会有什么被合并进去。 611 |
612 | 613 |
614 | 简而言之 使用 git log
列出促成当前分支目前的快照的提交历史记录。这使你能够看到项目是如何到达现在的状况的。
615 |
633 | 如果你达到一个重要的阶段,并希望永远记住那个特别的提交快照,你可以使用 git tag
给它打上标签。
634 | 该 tag
命令基本上会给该特殊提交打上永久的书签,从而使你在将来能够用它与其他提交比较。
635 | 通常,你会在切取一个发布版本或者交付一些东西的时候打个标签。
636 |
比如说,我们想为我们的 Hello World 项目发布一个“1.0”版本。
639 | 我们可以用 git tag -a v1.0
命令给最新一次提交打上(HEAD
)“v1.0”的标签。
640 | -a
选项意为“创建一个带注解的标签”,从而使你为标签添加注解。绝大部分时候都会这么做的。
641 | 不用 -a
选项也可以执行的,但它不会记录这标签是啥时候打的,谁打的,也不会让你添加个标签的注解。
642 | 我推荐一直创建带注解的标签。
643 |
646 | $ git tag -a v1.0 647 |648 | 649 |
当你执行 git tag -a
命令时,Git 会打开你的编辑器,让你写一句标签注解,就像你给提交写注解一样。
650 |
现在,注意当我们执行 git log --decorate
时,我们可以看到我们的标签了:
653 |
656 | $ git log --oneline --decorate --graph
657 | * 594f90b (HEAD, tag: v1.0, master) reverted to old class name
658 | * 8d585ea Merge branch 'fix_readme'
659 | |\
660 | | * 3ac015d (fix_readme) fixed readme title
661 | * | 3cbb6aa fixed readme title differently
662 | |/
663 | * 558151a Merge branch 'change_class'
664 | |\
665 | | * 3467b0a changed the class name
666 | * | b7ae93b added from ruby
667 | |/
668 | * 17f4acf first commit
669 |
670 |
671 | 如果我们有新提交,该标签依然会待在该提交的边上,所以我们已经给那个特定快照永久打上标签,并且能够将它与未来的快照做比较。 672 |
673 | 674 |不过我们并不需要给当前提交打标签。如果我们忘了给某个提交打标签,又将它发布了,我们可以给它追加标签。
675 | 在相同的命令末尾加上提交的 SHA,执行,就可以了。
676 | 例如,假设我们发布了提交 558151a
(几个提交之前的事情了),但是那时候忘了给它打标签。
677 | 我们现在也可以:
680 | $ git tag -a v0.9 558151a
681 | $ git log --oneline --decorate --graph
682 | * 594f90b (HEAD, tag: v1.0, master) reverted to old class name
683 | * 8d585ea Merge branch 'fix_readme'
684 | |\
685 | | * 3ac015d (fix_readme) fixed readme title
686 | * | 3cbb6aa fixed readme title differently
687 | |/
688 | * 558151a (tag: v0.9) Merge branch 'change_class'
689 | |\
690 | | * 3467b0a changed the class name
691 | * | b7ae93b added from ruby
692 | |/
693 | * 17f4acf first commit
694 |
695 |
696 | 9 | 你得先有一个 Git 仓库,才能用它进行操作。仓库是 Git 存放你要保存的快照的数据的地方。 10 |
11 | 12 |拥有一个 Git 仓库的途径有两种。在已有的目录中,初始化一个新的,其一。 13 | 比如一个新的项目,或者一个已存在的项目,但该项目尚未有版本控制。如果你想要复制一份别人的项目, 14 | 或者与别人合作某个项目,也可以从一个公开的 Git 仓库克隆,其二。本章将对两者都做介绍。 15 |
16 |在目录中执行 git init
,就可以创建一个 Git 仓库了。比如,我们恰好有个目录,里头有些许文件,如下:
31 |
33 | $ cd konnichiwa 34 | $ ls 35 | README hello.rb 36 |37 | 38 |
39 | 在这个项目里头,我们会用各种编程语言写 "Hello World" 实例。
40 | 到目前为止,我们只有 Ruby 的,不过,这才刚上路嘛。为了开始用 Git 对这个项目作版本控制,我们执行一下 git init
。
41 |
44 | $ git init 45 | Initialized empty Git repository in /opt/konnichiwa/.git/ 46 | # 在 /opt/konnichiwa/.git 目录初始化空 Git 仓库完毕。 47 |48 | 49 |
现在你可以看到在你的项目目录中有个 .git
的子目录。
50 | 这就是你的 Git 仓库了,所有有关你的此项目的快照数据都存放在这里。
51 |
54 | $ ls -a 55 | . .. .git README hello.rb 56 |57 | 58 |
恭喜,现在你就有了一个 Git 仓库的架子,可以开始快照你的项目了。 59 |
60 | 61 |
62 | 简而言之,用 git init
来在目录中创建新的 Git 仓库。
63 | 你可以在任何时候、任何目录中这么做,完全是本地化的。
64 |
如果你需要与他人合作一个项目,或者想要复制一个项目,看看代码,你就可以克隆那个项目。
80 | 执行 git clone [url]
,[url] 为你想要复制的项目,就可以了。
81 |
84 | $ git clone git://github.com/schacon/simplegit.git
85 | Initialized empty Git repository in /private/tmp/simplegit/.git/
86 | remote: Counting objects: 100, done.
87 | remote: Compressing objects: 100% (86/86), done.
88 | remote: Total 100 (delta 35), reused 0 (delta 0)
89 | Receiving objects: 100% (100/100), 9.51 KiB, done.
90 | Resolving deltas: 100% (35/35), done.
91 | $ cd simplegit/
92 | $ ls
93 | README Rakefile lib
94 |
95 |
96 | 上述操作将复制该项目的全部记录,让你本地拥有这些。并且该操作将拷贝该项目的主分支,
97 | 使你能够查看代码,或编辑、修改。进到该目录中,你会看到 .git
子目录。
98 | 所有的项目数据都存在那里。
99 |
102 | $ ls -a 103 | . .. .git README Rakefile lib 104 | $ cd .git 105 | $ ls 106 | HEAD description info packed-refs 107 | branches hooks logs refs 108 | config index objects 109 |110 | 111 |
默认情况下,Git 会按照你提供的 URL 所指示的项目的名称创建你的本地项目目录。
112 | 通常就是该 URL 最后一个 /
之后的任何东西。如果你想要一个不一样的名字,
113 | 你可以在该命令后加上它,就在那个 URL 后面。
114 |
117 | 简而言之,使用 git clone
拷贝一个 Git 仓库到本地,让自己能够查看该项目,或者进行修改。
118 |
8 | 本站为 Git 参考手册。目的是为学习与记忆 Git 使用中最重要、最普遍的命令提供快速翻阅。 9 | 这些命令以你可能需要的操作类型划分,并且将提供日常使用中需要的一些常用的命令以及参数。 10 |
11 |12 | 每个章节都有到下一个章节的链接,所以本手册也可以当作一个入门指导。 13 | 每个页面还有一个深度 Git 文档阅读的链接,比如官方的使用手册页面或者 《Pro Git》 14 | 书中的相关章节,以便于你学习了解更多的 Git 命令。首先,我们要从如何以 Git 的思维方式管理源代码开始。 15 |
16 |23 | 懂得 Git,第一件重要的事情就是要知道它与 Subversion、Perforce 或者任何你用过的版本控制工具都有着很大的差别。 24 | 通常,忘掉你预想的版本控制方式,改以 Git 的方式思考,能够帮助你更好地学习 Git。 25 |
26 | 27 |28 | 让我们从头开始。假设你正在设计一个新的源代码管理系统。在你使用某个工具之前,是如何完成基本的源码版本控制工作的呢? 29 | 十有八九,你只是在项目到达某些阶段的时候,对项目做一份拷贝。 30 |
31 | 32 |$ cp -R project project.bak33 | 34 |
35 | 这样,你就可以在事情变得一团糟的时候很方便的返回到之前的状态,或者通过对比当前的项目与之前的拷贝,看看自己在之后的工作中,都做了哪些修改。 36 |
37 | 38 |39 | 如果你有点偏执,你可能会经常作上面说的事情,或许还会给项目拷贝加个日期: 40 |
41 | 42 |$ cp -R project project.2010-06-01.bak43 | 44 |
45 | 如此,你就有了一堆项目在各个阶段的快照,来作比较、查看。使用这种模式,你还可以有效地与人分享项目变更。 46 | 如果你会在项目到达一定阶段的时候给它打个包,丢到自己的网站上,那其他的开发者们,就能很方便地下载它,做点改动,并给你补丁回馈。 47 |
48 | 49 |50 | $ wget http://example.com/project.2010-06-01.zip 51 | $ unzip project.2010-06-01.zip 52 | $ cp -R project.2010-06-01 project-my-copy 53 | $ cd project-my-copy 54 | $ (做了某些修改) 55 | $ diff project-my-copy project.2010-06-01 > change.patch 56 | $ (通过E-mail发送修改补丁)57 | 58 |
59 | 以此方式,原先的开发者就能将其他人的改动应用到他的项目中去,其他开发者也能了解你做的变更。其实这便是许多开源项目采用过多年的协作方式。 60 |
61 | 62 |63 | 这办法其实很好使,所以假设我们现在想要写个工具,让这个办法更快、更简单。 64 | 我们与其实现一个工具以记录每个文件的版本,可能不如去实现个工具以使创建、储存项目的快照更加方便,不用每次都去人肉作整个项目的拷贝。 65 |
66 | 67 |
68 | 这就是 Git 的精要所在。你通过 git commit
告诉 Git 你想保存一份项目快照,
69 | Git 就会为你的项目中的各个文件的当前状态存一份记录。之后,绝大部分的 Git 命令都围绕这些记录展开。
70 | 比如查看它们的区别(diff),提取它们的内容,等等。
71 |
76 | 如果你将 Git 看作一个排序、对比以及合并项目更新的工具,那就容易理解状况和正确做事了。 77 |
78 | 79 |14 | 现在你有了一堆分支,短期的主题、长期的特性或者其它。怎样追踪他们呢? 15 | Git 有一组工具,可以帮助你弄明白工作是在哪儿完成的,两个分支间的区别是啥,等等。 16 |
17 | 18 |
19 | 简而言之 执行 git log
找到你的项目历史中的特定提交 ——
20 | 按作者、日期、内容或者历史记录。执行 git diff
比较历史记录中的两个不同的点 ——
21 | 通常是为了看看两个分支有啥区别,或者从某个版本到另一个版本,你的软件都有啥变化。
22 |
38 | 通过查看分支中另一分支看不到的提交记录,我们已经看到如何用 git log
来比较分支。
39 | (如果你不记得了,它看起来是这样的:git log branchA ^branchB
)。
40 | 而且,你也可以用 git log
去寻找特定的提交。
41 | 在此,我们会看到一些更广为使用的 git log
选项,不过哪有很多。
42 | 完整的清单可以看看官方文档。
43 |
51 | 要过滤你的提交历史,只寻找某个特定作者的提交,你可以使用 --author
选项。
52 | 例如,比方说我们要找 Git 源码中 Linus 提交的部分。
53 | 我们可以执行类似 git log --author=Linus
的命令。
54 | 这个查找是大小写敏感的,并且也会检索电子邮箱地址。
55 | 我在此例中使用 -[number]
选项,以限制结果为最近 [number] 次的提交。
56 |
59 | $ git log --author=Linus --oneline -5 60 | 81b50f3 Move 'builtin-*' into a 'builtin/' subdirectory 61 | 3bb7256 make "index-pack" a built-in 62 | 377d027 make "git pack-redundant" a built-in 63 | b532581 make "git unpack-file" a built-in 64 | 112dd51 make "mktag" a built-in 65 |66 | 67 |
73 | 如果你要指定一个你感兴趣的日期范围以过滤你的提交,可以执行几个选项 ——
74 | 我用 --since
和 --before
,但是你也可以用 --until
和 --after
。
75 | 例如,如果我要看 Git 项目中三周前且在四月十八日之后的所有提交,我可以执行这个(我还用了 --no-merges
选项以隐藏合并提交):
76 |
79 | $ git log --oneline --before={3.weeks.ago} --after={2010-04-18} --no-merges 80 | 5469e2d Git 1.7.1-rc2 81 | d43427d Documentation/remote-helpers: Fix typos and improve language 82 | 272a36b Fixup: Second argument may be any arbitrary string 83 | b6c8d2d Documentation/remote-helpers: Add invocation section 84 | 5ce4f4e Documentation/urls: Rewrite to accomodate transport::address 85 | 00b84e9 Documentation/remote-helpers: Rewrite description 86 | 03aa87e Documentation: Describe other situations where -z affects git diff 87 | 77bc694 rebase-interactive: silence warning when no commits rewritten 88 | 636db2c t3301: add tests to use --format="%N" 89 |90 | 91 |
97 | 你或许还想根据提交注释中的某个特定短语查找提交记录。可以用 --grep
选项。
98 | 比如说我知道有个提交是有关使用 P4EDITOR 环境变量,又想回忆起那个改动是啥样子的 —— 我可以用 --grep
选项找到该提交。
99 |
102 | $ git log --grep=P4EDITOR --no-merges
103 | commit 82cea9ffb1c4677155e3e2996d76542502611370
104 | Author: Shawn Bohrer
105 | Date: Wed Mar 12 19:03:24 2008 -0500
106 |
107 | git-p4: Use P4EDITOR environment variable when set
108 |
109 | Perforce allows you to set the P4EDITOR environment variable to your
110 | preferred editor for use in perforce. Since we are displaying a
111 | perforce changelog to the user we should use it when it is defined.
112 |
113 | Signed-off-by: Shawn Bohrer <shawn.bohrer@gmail.com>
114 | Signed-off-by: Simon Hausmann <simon@lst.de>
115 |
116 |
117 |
118 | Git 会对所有的 --grep
和 --author
参数作逻辑或。
119 | 如果你用 --grep
和 --author
时,想看的是某人写作的并且有某个特殊的注释内容的提交记录,
120 | 你需要加上 --all-match
选项。
121 | 在这些例子中,我会用上 --format
选项,这样我们就可以看到每个提交的作者是谁了。
122 |
125 | 如果我查找注释内容含有 “p4 depo”的提交,我得到了三个提交: 126 |
127 | 128 |129 | $ git log --grep="p4 depo" --format="%h %an %s" 130 | ee4fd1a Junio C Hamano Merge branch 'master' of git://repo.or.cz/git/fastimport 131 | da4a660 Benjamin Sergeant git-p4 fails when cloning a p4 depo. 132 | 1cd5738 Simon Hausmann Make incremental imports easier to use by storing the p4 d 133 |134 | 135 |
136 | 如果我加上 --author=Hausmann
参数,与进一步过滤上述结果到 Simon 的唯一提交相反,
137 | 它会告诉我所有 Simon 的提交,或者注释中有“p4 demo”的提交:
138 |
141 | $ git log --grep="p4 depo" --format="%h %an %s" --author="Hausmann" 142 | cdc7e38 Simon Hausmann Make it possible to abort the submission of a change to Pe 143 | f5f7e4a Simon Hausmann Clean up the git-p4 documentation 144 | 30b5940 Simon Hausmann git-p4: Fix import of changesets with file deletions 145 | 4c750c0 Simon Hausmann git-p4: git-p4 submit cleanups. 146 | 0e36f2d Simon Hausmann git-p4: Removed git-p4 submit --direct. 147 | edae1e2 Simon Hausmann git-p4: Clean up git-p4 submit's log message handling. 148 | 4b61b5c Simon Hausmann git-p4: Remove --log-substitutions feature. 149 | 36ee4ee Simon Hausmann git-p4: Ensure the working directory and the index are cle 150 | e96e400 Simon Hausmann git-p4: Fix submit user-interface. 151 | 38f9f5e Simon Hausmann git-p4: Fix direct import from perforce after fetching cha 152 | 2094714 Simon Hausmann git-p4: When skipping a patch as part of "git-p4 submit" m 153 | 1ca3d71 Simon Hausmann git-p4: Added support for automatically importing newly ap 154 | ... 155 |156 | 157 |
158 | 不过,如果加上 --all-match
,结果就是我想要的了:
159 |
162 | $ git log --grep="p4 depo" --format="%h %an %s" --author="Hausmann" --all-match 163 | 1cd5738 Simon Hausmann Make incremental imports easier to use by storing the p4 d 164 |165 | 166 |
172 | 如果你写的提交注释都极度糟糕怎么办?或者,如果你要找某个函数是何时引入的,某些变量是在哪里开始被使用的? 173 | 你可以告诉 Git 在每个提交之间的差值中查找特定字符串。 174 | 例如,如果我们想要找出哪个提交修改出了类似函数名“userformat_find_requirements”, 175 | 我们可以执行(注意在“-S”与你要找的东东之间没有“=”): 176 |
177 | 178 |
179 | $ git log -Suserformat_find_requirements
180 | commit 5b16360330822527eac1fa84131d185ff784c9fb
181 | Author: Johannes Gilger
182 | Date: Tue Apr 13 22:31:12 2010 +0200
183 |
184 | pretty: Initialize notes if %N is used
185 |
186 | When using git log --pretty='%N' without an explicit --show-notes, git
187 | would segfault. This patches fixes this behaviour by loading the needed
188 | notes datastructures if --pretty is used and the format contains %N.
189 | When --pretty='%N' is used together with --no-notes, %N won't be
190 | expanded.
191 |
192 | This is an extension to a proposed patch by Jeff King.
193 |
194 | Signed-off-by: Johannes Gilger
195 | Signed-off-by: Junio C Hamano
196 |
197 |
198 |
204 | 每个提交都是项目的一个快照。由于每个提交都记录它所基于的快照,Git 能够经常对它们求差值,并以补丁形式向你展示。
205 | 这意味着,对任意提交,你都可以获取该提交给项目引入补丁。
206 | 你可以用 git show [SHA]
加上某个特定的提交 SHA 获取,或者执行 git log -p
,
207 | 它会告诉 Git 输出每个提交之后的补丁。这是个总结某一分支或者两个提交之间都发生了神马的好途径。
208 |
211 | $ git log -p --no-merges -2 212 | commit 594f90bdee4faf063ad07a4a6f503fdead3ef606 213 | Author: Scott Chacon <schacon@gmail.com> 214 | Date: Fri Jun 4 15:46:55 2010 +0200 215 | 216 | reverted to old class name 217 | 218 | diff --git a/ruby.rb b/ruby.rb 219 | index bb86f00..192151c 100644 220 | --- a/ruby.rb 221 | +++ b/ruby.rb 222 | @@ -1,7 +1,7 @@ 223 | -class HiWorld 224 | +class HelloWorld 225 | def self.hello 226 | puts "Hello World from Ruby" 227 | end 228 | end 229 | 230 | -HiWorld.hello 231 | +HelloWorld.hello 232 | 233 | commit 3cbb6aae5c0cbd711c098e113ae436801371c95e 234 | Author: Scott Chacon <schacon@gmail.com> 235 | Date: Fri Jun 4 12:58:53 2010 +0200 236 | 237 | fixed readme title differently 238 | 239 | diff --git a/README b/README 240 | index d053cc8..9103e27 100644 241 | --- a/README 242 | +++ b/README 243 | @@ -1,4 +1,4 @@ 244 | -Hello World Examples 245 | +Many Hello World Examples 246 | ====================== 247 | 248 | This project has examples of hello world in 249 |250 | 251 |
252 | 这是个总结改动,以及合并或发布之前重审一系列提交的好方式。 253 |
254 | 255 |
261 | 如果 -p
选项对你来说太详细了,你可以用 --stat
总结这些改动。
262 | 这是不用 -p
,而用 --stat
选项时,同一份日志的输出。
263 |
266 | $ git log --stat --no-merges -2 267 | commit 594f90bdee4faf063ad07a4a6f503fdead3ef606 268 | Author: Scott Chacon <schacon@gmail.com> 269 | Date: Fri Jun 4 15:46:55 2010 +0200 270 | 271 | reverted to old class name 272 | 273 | ruby.rb | 4 ++-- 274 | 1 files changed, 2 insertions(+), 2 deletions(-) 275 | 276 | commit 3cbb6aae5c0cbd711c098e113ae436801371c95e 277 | Author: Scott Chacon <schacon@gmail.com> 278 | Date: Fri Jun 4 12:58:53 2010 +0200 279 | 280 | fixed readme title differently 281 | 282 | README | 2 +- 283 | 1 files changed, 1 insertions(+), 1 deletions(-) 284 |285 | 286 |
287 | 同样的基本信息,但更紧凑 —— 它仍然让你看到相对改动,和改动了哪些文件。 288 |
289 | 290 |
306 | 最后,要查看两个提交快照的绝对改动,你可以用 git diff
命令。
307 | 这在两个主要情况中广为使用 —— 查看两个分支彼此之间的差值,和查看自发布或者某个旧历史点之后都有啥变了。让我们看看这俩情况。
308 |
311 | 你仅需执行 git diff [version]
(或者你给该发布打的任何标签)就可以查看自最近发布之后的改动。
312 | 例如,如果我们想要看看自 v0.9 发布之后我们的项目改变了啥,我们可以执行 git diff v0.9
313 |
316 | $ git diff v0.9 317 | diff --git a/README b/README 318 | index d053cc8..d4173d5 100644 319 | --- a/README 320 | +++ b/README 321 | @@ -1,4 +1,4 @@ 322 | -Hello World Examples 323 | +Many Hello World Lang Examples 324 | ====================== 325 | 326 | This project has examples of hello world in 327 | diff --git a/ruby.rb b/ruby.rb 328 | index bb86f00..192151c 100644 329 | --- a/ruby.rb 330 | +++ b/ruby.rb 331 | @@ -1,7 +1,7 @@ 332 | -class HiWorld 333 | +class HelloWorld 334 | def self.hello 335 | puts "Hello World from Ruby" 336 | end 337 | end 338 | 339 | -HiWorld.hello 340 | +HelloWorld.hello 341 |342 | 343 |
344 | 正如 git log
,你可以给它加上 --stat
参数。
345 |
348 | $ git diff v0.9 --stat 349 | README | 2 +- 350 | ruby.rb | 4 ++-- 351 | 2 files changed, 3 insertions(+), 3 deletions(-) 352 |353 | 354 |
355 | 要比较两个不同的分支,你可以执行类似 git diff branchA branchB
的命令。
356 | 不过它的问题在于它会完完全全按你说的作 —— 它会直接给你个补丁文件,该补丁能够将甲分支的最新快照变成乙分支的最新快照的样子。
357 | 这意味着如果两个分支已经产生分歧 —— 奔往两个不同方向了 —— 它会移除甲分支中引入的所有工作,然后累加乙分支中的所有工作。
358 | 这大概不是你要的吧 —— 你想要不在甲分支中的乙分支的改动。所以你真的需要的是两个分支叉开去时,和最新的乙分支的差别。
359 | 所以,如果我们的历史记录看起来像这样:
360 |
363 | $ git log --graph --oneline --decorate --all 364 | * 594f90b (HEAD, tag: v1.0, master) reverted to old class name 365 | | * 1834130 (erlang) added haskell 366 | | * ab5ab4c added erlang 367 | |/ 368 | * 8d585ea Merge branch 'fix_readme' 369 | ... 370 |371 | 372 |
373 | 并且,我们想要看“erlang”分支与主分支相比的查别。执行 git diff master erlang
会给我们错误的结果。
374 |
377 | $ git diff --stat master erlang 378 | erlang_hw.erl | 5 +++++ 379 | haskell.hs | 4 ++++ 380 | ruby.rb | 4 ++-- 381 | 3 files changed, 11 insertions(+), 2 deletions(-) 382 |383 | 384 |
385 | 你可以看到,它加上了 erlang 和 haskell 文件,这确实是我们在该分支中做的, 386 | 但是它同时恢复了我们在主分支中改动的 ruby 文件。我们真心想要的只是“erlang”分支中的改动(添加两个文件)。 387 | 我们可以通过求两个分支分歧时的共同提交与该分支的差值得到想要的结果: 388 |
389 | 390 |391 | $ git diff --stat 8d585ea erlang 392 | erlang_hw.erl | 5 +++++ 393 | haskell.hs | 4 ++++ 394 | 2 files changed, 9 insertions(+), 0 deletions(-) 395 |396 | 397 |
398 | 这才是我们在找的,但是我们可不想要每次都要找出两个分支分歧时的那次提交。
399 | 幸运的是,Git 为此提供了一个快捷方式。
400 | 如果你执行 git diff master...erlang
(在分支名之间有三个半角的点),
401 | Git 就会自动找出两个分支的共同提交(也被成为合并基础),并求差值。
402 |
405 | $ git diff --stat master erlang 406 | erlang_hw.erl | 5 +++++ 407 | haskell.hs | 4 ++++ 408 | ruby.rb | 4 ++-- 409 | 3 files changed, 11 insertions(+), 2 deletions(-) 410 | $ git diff --stat master...erlang 411 | erlang_hw.erl | 5 +++++ 412 | haskell.hs | 4 ++++ 413 | 2 files changed, 9 insertions(+), 0 deletions(-) 414 |415 | 416 |
417 | 几乎每一次你要对比两个分支的时候,你都会想用三个点的语法,因为它通常会给你你想要的。 418 |
419 | 420 |
421 | 顺带提一句,你还可以让 Git 手工计算两次提交的合并基础(第一个共同的祖提交),即 git merge-base
命令:
422 |
425 | $ git merge-base master erlang 426 | 8d585ea6faf99facd39b55d6f6a3b3f481ad0d3d 427 |428 | 429 |
430 | 所以你执行下面这个也跟 git diff master...erlang
一样:
431 |
434 | $ git diff --stat $(git merge-base master erlang) erlang 435 | erlang_hw.erl | 5 +++++ 436 | haskell.hs | 4 ++++ 437 | 2 files changed, 9 insertions(+), 0 deletions(-) 438 |439 | 440 |
当然,我会推荐简单点的那个。
441 | 442 | 443 |
444 | 简而言之 使用 git diff
查看某一分支自它偏离出来起与过去某一点之间项目的改动。
445 | 总是使用 git diff branchA...branchB
来查看 branchB 与 branchA 的相对差值,这会让事情简单点。
446 |
就是这样了! 请阅读 452 | 《Pro Git》 以获得更多信息。
453 | -------------------------------------------------------------------------------- /zh/remotes/index.html: -------------------------------------------------------------------------------- 1 | --- 2 | layout: zh_reference 3 | --- 4 | 5 |14 | Git 并不像 Subversion 那样有个中心服务器。 15 | 目前为止所有的命令都是本地执行的,更新的只是本地的数据库。 16 | 要通过 Git 与其他开发者合作,你需要将数据放到一台其他开发者能够连接的服务器上。 17 | Git 实现此流程的方式是将你的数据与另一个仓库同步。在服务器与客户端之间并没有实质的区别 —— 18 | Git 仓库就是 Git 仓库,你可以很容易地在两者之间同步。 19 |
20 | 21 |22 | 一旦你有了个 Git 仓库,不管它是在你自己的服务器上,或者是由 GitHub 之类的地方提供, 23 | 你都可以告诉 Git 推送你拥有的远端仓库还没有的数据,或者叫 Git 从别的仓库把差别取过来。 24 |
25 | 26 |
27 | 联网的时候你可以随时做这个,它并不需要对应一个 commit
或者别的什么。
28 | 一般你会本地提交几次,然后从你的项目克隆自的线上的共享仓库提取数据以保持最新,将新完成的合并到你完成的工作中去,然后推送你的改动会服务器。
29 |
32 | 简而言之 使用 git fetch
更新你的项目,使用 git push
分享你的改动。
33 | 你可以用 git remote
管理你的远程仓库。
34 |
不像中心化的版本控制系统(客户端与服务端很不一样),Git 仓库基本上都是一致的,并且并可以同步他们。 51 | 这使得拥有多个远端仓库变得容易 —— 你可以拥有一些只读的仓库,另外的一些也可写的仓库。 52 |
53 | 54 |
55 | 当你需要与远端仓库同步的时候,不需要使用它详细的链接。Git 储存了你感兴趣的远端仓库的链接的别名或者昵称。
56 | 你可以使用 git remote
命令管理这个远端仓库列表。
57 |
65 | 如果没有任何参数,Git 会列出它存储的远端仓库别名了事。默认情况下,如果你的项目是克隆的(与本地创建一个新的相反),
66 | Git 会自动将你的项目克隆自的仓库添加到列表中,并取名“origin”。
67 | 如果你执行时加上 -v
参数,你还可以看到每个别名的实际链接地址。
68 |
71 | $ git remote 72 | origin 73 | $ git remote -v 74 | origin git@github.com:github/git-reference.git (fetch) 75 | origin git@github.com:github/git-reference.git (push) 76 |77 | 78 |
在此你看到了该链接两次,是因为 Git 允许你为每个远端仓库添加不同的推送与获取的链接,以备你读写时希望使用不同的协议。 79 |
80 | 81 |
87 | 如果你希望分享一个本地创建的仓库,或者你想要获取别人的仓库中的贡献 ——
88 | 如果你想要以任何方式与一个新仓库沟通,最简单的方式通常就是把它添加为一个远端仓库。
89 | 执行 git remote add [alias] [url]
就可以。
90 | 此命令将 [url]
以 [alias]
的别名添加为本地的远端仓库。
91 |
94 | 例如,假设我们想要与整个世界分享我们的 Hello World 程序。 95 | 我们可以在一台服务器上创建一个新仓库(我以 GitHub 为例子)。 96 | 它应该会给你一个链接,在这里就是“git@github.com:schacon/hw.git”。 97 | 要把它添加到我们的项目以便我们推送以及获取更新,我们可以这样: 98 |
99 | 100 |101 | $ git remote 102 | $ git remote add github git@github.com:schacon/hw.git 103 | $ git remote -v 104 | github git@github.com:schacon/hw.git (fetch) 105 | github git@github.com:schacon/hw.git (push) 106 |107 | 108 |
109 | 像分支的命名一样,远端仓库的别名是强制的 —— 就像“master”,没有特别意义,但它广为使用,
110 | 因为 git init
默认用它;“origin”经常被用作远端仓库别名,就因为 git clone
111 | 默认用它作为克隆自的链接的别名。此例中,我决定给我的远端仓库取名“github”,但我叫它随便什么都可以。
112 |
120 | Git addeth and Git taketh away. 如果你需要删除一个远端 —— 不再需要它了、项目已经没了,等等 —— 你可以使用 git remote rm [alias]
把它删掉。
121 |
124 | $ git remote -v 125 | github git@github.com:schacon/hw.git (fetch) 126 | github git@github.com:schacon/hw.git (push) 127 | $ git remote add origin git://github.com/pjhyett/hw.git 128 | $ git remote -v 129 | github git@github.com:schacon/hw.git (fetch) 130 | github git@github.com:schacon/hw.git (push) 131 | origin git://github.com/pjhyett/hw.git (fetch) 132 | origin git://github.com/pjhyett/hw.git (push) 133 | $ git remote rm origin 134 | $ git remote -v 135 | github git@github.com:schacon/hw.git (fetch) 136 | github git@github.com:schacon/hw.git (push) 137 |138 | 139 |
140 | 简而言之 你可以用 git remote
列出你的远端仓库和那些仓库的链接。
141 | 你可以使用 git remote add
添加新的远端仓库,用 git remote rm
删掉已存在的那些。
142 |
171 | Git 有两个命令用来从某一远端仓库更新。
172 | git fetch
会使你与另一仓库同步,提取你本地所没有的数据,为你在同步时的该远端的每一分支提供书签。
173 | 这些分支被叫做“远端分支”,除了 Git 不允许你检出(切换到该分支)之外,跟本地分支没区别 ——
174 | 你可以将它们合并到当前分支,与其他分支作比较差异,查看那些分支的历史日志,等等。同步之后你就可以在本地操作这些。
175 |
178 | 第二个会从远端服务器提取新数据的命令是 git pull
。
179 | 基本上,该命令就是在 git fetch
之后紧接着 git merge
远端分支到你所在的任意分支。
180 | 我个人不太喜欢这命令 —— 我更喜欢 fetch
和 merge
分开来做。少点魔法,少点问题。
181 | 不过,如果你喜欢这主意,你可以看一下 git pull
的
182 | 官方文档。
183 |
186 | 假设你配置好了一个远端,并且你想要提取更新,你可以首先执行 git fetch [alias]
187 | 告诉 Git 去获取它有你没有的数据,然后你可以执行 git merge [alias]/[branch]
188 | 以将服务器上的任何更新(假设有人这时候推送到服务器了)合并到你的当前分支。
189 | 那么,如果我是与两三个其他人合作 Hello World 项目,并且想要将我最近连接之后的所有改动拿过来,我可以这么做:
190 |
193 | $ git fetch github 194 | remote: Counting objects: 4006, done. 195 | remote: Compressing objects: 100% (1322/1322), done. 196 | remote: Total 2783 (delta 1526), reused 2587 (delta 1387) 197 | Receiving objects: 100% (2783/2783), 1.23 MiB | 10 KiB/s, done. 198 | Resolving deltas: 100% (1526/1526), completed with 387 local objects. 199 | From github.com:schacon/hw 200 | 8e29b09..c7c5a10 master -> github/master 201 | 0709fdc..d4ccf73 c-langs -> github/c-langs 202 | 6684f82..ae06d2b java -> github/java 203 | * [new branch] ada -> github/ada 204 | * [new branch] lisp -> github/lisp 205 |206 | 207 |
208 | 可以看到自从上一次与远端仓库同步以后,又新赠或更新了五个分支。 209 | “ada”与“lisp”分支是新的,而“master”、“clang”与“java”分支则被更新了。 210 | 在此例中,我的团队在合并入主分支之前,将提议的更新推送到远端分支以审核。 211 |
212 | 213 |
214 | 你可以看到 Git 做的映射。远端仓库的主分支成为了本地的一个叫做“github/master”的分支。
215 | 这样我就可以执行 git merge github/master
将远端的主分支和并入我的本地主分支。
216 | 或者,我可以 git log github/master ^master
看看该分支上的新提交。
217 | 如果你的远端仓库叫做“origin”,那远端主分支就会叫做 origin/master
。几乎所有能在本地分支上执行的命令都可以在远端分支上用。
218 |
221 | 如果你有多个远端仓库,你可以执行 git fetch [alias]
提取特定的远端仓库,
222 | 或者执行 git fetch --all
告诉 Git 同步所有的远端仓库。
223 |
226 | 简而言之 执行 git fetch [alias]
来将你的仓库与远端仓库同步,提取所有它独有的数据到本地分支以合并或者怎样。
227 |
245 | 想要与他人分享你牛鼻的提交,你需要将改动推送到远端仓库。
246 | 执行 git push [alias] [branch]
,就会将你的 [branch] 分支推送成为 [alias] 远端上的 [branch] 分支。
247 | 让我们试试推送我们的主分支到先前添加的“github”远端仓库上去。
248 |
251 | $ git push github master 252 | Counting objects: 25, done. 253 | Delta compression using up to 2 threads. 254 | Compressing objects: 100% (25/25), done. 255 | Writing objects: 100% (25/25), 2.43 KiB, done. 256 | Total 25 (delta 4), reused 0 (delta 0) 257 | To git@github.com:schacon/hw.git 258 | * [new branch] master -> master 259 |260 | 261 |
262 | 挺简单。现在如果有人从该仓库克隆,他会得到我提交的完完全全的一份历史记录了。 263 |
264 | 265 |266 | 如果有个像之前创建的“erlang”分支那样的主题分支,想只分享这个,该怎么办呢?你可以相应的只推送该分支。 267 |
268 | 269 |270 | $ git push github erlang 271 | Counting objects: 7, done. 272 | Delta compression using up to 2 threads. 273 | Compressing objects: 100% (6/6), done. 274 | Writing objects: 100% (6/6), 652 bytes, done. 275 | Total 6 (delta 1), reused 0 (delta 0) 276 | To git@github.com:schacon/hw.git 277 | * [new branch] erlang -> erlang 278 |279 | 280 |
281 | 现在当人们从该仓库克隆时,他们就会得到一个“erlang”分支以查阅、合并。 282 | 用这种方式,你可以推送任何分支到任何你有写权限的仓库。 283 | 如果你的分支已经在该仓库中了,它会试着去更新,如果它不再,Git 会把它加上。 284 |
285 | 286 |
287 | 最后一个当你推送到远端分支时会碰到的主要问题是,其他人在此期间也推送了的情况。
288 | 如果你和另一个开发者同时克隆了,又都有提交,那么当她推送后你也想推送时,默认情况下 Git 不会让你覆盖她的改动。
289 | 相反的,它会在你试图推送的分支上执行 git log
,确定它能够在你的推送分支的历史记录中看到服务器分支的当前进度。
290 | 如果它在在你的历史记录中看不到,它就会下结论说你过时了,并打回你的推送。
291 | 你需要正式提取、合并,然后再次推送 —— 以确定你把她的改动也考虑在内了。
292 |
295 | 当你试图推送到某个以被更新的远端分支时,会出现下面这种情况: 296 |
297 | 298 |299 | $ git push github master 300 | To git@github.com:schacon/hw.git 301 | ! [rejected] master -> master (non-fast-forward) 302 | error: failed to push some refs to 'git@github.com:schacon/hw.git' 303 | To prevent you from losing history, non-fast-forward updates were rejected 304 | Merge the remote changes before pushing again. See the 'Note about 305 | fast-forwards' section of 'git push --help' for details. 306 |307 | 308 |
309 | 你可以修正这个问题。执行 git fetch github; git merge github/master
,然后再推送
310 |
313 | 简而言之 执行 git push [alias] [branch]
将你的本地改动推送到远端仓库。
314 | 如果可以的话,它会依据你的 [branch] 的样子,推送到远端的 [branch] 去。
315 | 如果在你上次提取、合并之后,另有人推送了,Git 服务器会拒绝你的推送,知道你是最新的为止。
316 |