Git: Difference between revisions

add note about push.default
 
(17 intermediate revisions by 2 users not shown)
Line 3: Line 3:
The bottom line is this: modern software development may take many "forms", but it usually boils down to [http://nvie.com/posts/a-successful-git-branching-model/ this].  Git enables such a branched workflow. That is why distributed version control in general, and git in particular, is the most widely adopted version control system for software development <ref>https://ianskerrett.wordpress.com/2014/06/23/eclipse-community-survey-2014-results/</ref>
The bottom line is this: modern software development may take many "forms", but it usually boils down to [http://nvie.com/posts/a-successful-git-branching-model/ this].  Git enables such a branched workflow. That is why distributed version control in general, and git in particular, is the most widely adopted version control system for software development <ref>https://ianskerrett.wordpress.com/2014/06/23/eclipse-community-survey-2014-results/</ref>


{{Messagebox |
See [[Git/hacks]] for example commands
|type = normal
 
{{Subpages|}}


|text = See [[Git/hacks]] for example commands
}}
== Intro to Git ==
== Intro to Git ==


Line 14: Line 13:
Why do we have git?  Because [http://whygitisbetterthanx.com Git is better than X]  Now that we have the "My DVCS is better than your DVCS" argument out of the way, you can actually get some valuable insights from that website if you are interested in comparing Git with Mercurial, Bazaar, Subversion or Perforce.  If I had to single out one primary advantage of Git, it would be that it actually features branching and merging.
Why do we have git?  Because [http://whygitisbetterthanx.com Git is better than X]  Now that we have the "My DVCS is better than your DVCS" argument out of the way, you can actually get some valuable insights from that website if you are interested in comparing Git with Mercurial, Bazaar, Subversion or Perforce.  If I had to single out one primary advantage of Git, it would be that it actually features branching and merging.


Repo visibility is completely customizable, as are individual permissions to write to repos.  I've installed a system called gitosis to handle the privileges through a special git repository. It uses Public Key cryptography rather than granting SSH accounts to everyone.  This makes it really easy to do your work securely without even needing a password.  For the curious, the actual mechanics of gitosis are detailed at [http://scie.nti.st/2007/11/14/hosting-git-repositories-the-easy-and-secure-way scie.nti.st]
Repo visibility is completely customizable, as are individual permissions to write to repos.  I've installed a system called gitosis to handle the privileges through a special git repository. It uses Public Key cryptography rather than granting SSH accounts to everyone.  This makes it really easy to do your work securely without even needing a password.  For the curious, the actual mechanics of gitosis are detailed at [http://scie.nti.st/2007/11/14/hosting-git-repositories-the-easy-and-secure-way scie.nti.st]. Although that link may still work, there is also https://git-scm.com/book/en/v1/Git-on-the-Server-Gitosis.


{{Messagebox |
{{Messagebox |
Line 27: Line 26:
== Initial Configuration ==
== Initial Configuration ==
In your [https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup first time setting up Git on a new computer], you want to configure your username and email among other settings.
In your [https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup first time setting up Git on a new computer], you want to configure your username and email among other settings.
<source lang="bash">
<syntaxhighlight lang="bash">
git config --global user.name "Greg Rundlett"
git config --global user.name "Greg Rundlett"
git config --global user.email greg@freephile.com
git config --global user.email greg@freephile.com
Line 33: Line 32:
git config --global diff.tool meld
git config --global diff.tool meld
git config --global --add color.ui true
git config --global --add color.ui true
# store password in memory for an hour
git config --global credential.helper 'cache --timeout=3600'
git config --global push.autoSetupRemote true
# recent packaged versions of git might be 1.8.3.1 (CentOS 7.4) whereas the current available is 2.15.1
# recent packaged versions of git might be 1.8.3.1 (CentOS 7.4) whereas the current available is 2.15.1
# push.default is unset; its implicit value is changing in
# push.default is unset; its implicit value is changing in
Line 38: Line 41:
# and adopt the new behavior now, use:
# and adopt the new behavior now, use:
git config --global push.default simple
git config --global push.default simple
</source>
</syntaxhighlight>
=== Example .gitconfig ===
=== Example .gitconfig ===
<code>git config --global -e</code> and paste in the following:
<code>git config --global -e</code> and paste in the following:
<source lang="ini">
<syntaxhighlight lang="ini">
[user]
[user]
         name = Greg Rundlett
         name = Greg Rundlett
Line 96: Line 99:
#      browser = google-chrome
#      browser = google-chrome
         browser = firefox
         browser = firefox
</source>
</syntaxhighlight>


{{Highlight
{{Highlight
|text = If you want to browse the git help in your browser (as above), then make sure you <code>sudo apt-get install git-doc</code> to get the HTML files}}
|text = If you want to browse the git help in your browser (as above), then make sure you <code>sudo apt-get install git-doc</code> to get the HTML files}}
== Decentralized Workflow and Branching Model ==
Now known as the "'''Git Flow'''" model. When using git for software development where you maintain long-running branches for various supported product versions and also work on the 'next' release in sprints or some development cadence, you need to use this model. '''Git Flow''' is a successful branching model for development, "main", release branches, hotfixes, etc. See https://nvie.com/posts/a-successful-git-branching-model/
In contrast, the 'GitHub Flow' model where you really only ever have one product that marches forward, the focus is on a simplified fork (branch) and Pull Request (PR) approach. This simplified model is more appropriate for simple products like documentation or a website.
== Submodules ==
Using multiple repositories to compose your project.  See https://git-scm.com/book/en/v2/Git-Tools-Submodules
<syntaxhighlight lang="bash">
git init myproject
cd myproject
git submodule add git@github.com:example.com/htdocs.git website
git submodule add https://gerrit.wikimedia.org/r/p/mediawiki/core.git mediawiki
git submodule add https://gitlab.com/Aranad/tools.git odtools
cd mediawiki/
git checkout REL1_31
cd ../
git commit -m 'initial commit'
git diff --submodule
git config --global diff.submodule log
git diff
git config -f .gitmodules submodule.mediawiki.branch REL1_31
git submodule update --remote
git status
git commit -am 'tracking REL1_31 in the mediawiki submodule'
git config status.submodulesummary 1
git log -p --submodule
cd mediawiki/
git submodule update --remote --merge
git config alias.sdiff '!'"git diff && git submodule foreach 'git diff'"
git config alias.spush 'push --recurse-submodules=on-demand'
git config alias.supdate 'submodule update --remote --merge'
</syntaxhighlight>


== Tools ==
== Tools ==
 
[[File:Meza.big.picture.pdf|thumb|Meza Big Picture|right]]
* http://git-scm.com/tools
* http://git-scm.com/tools
* http://github.com/guides/using-the-egit-eclipse-plugin-with-github
* http://github.com/guides/using-the-egit-eclipse-plugin-with-github
 
* [https://github.com/esc/git-big-picture Git Big Picture] is a nice visualization tool that produces images like the one on the right.


== Reporting ==
== Reporting ==
How many lines have I contributed since last year?
How many lines have I contributed since last year?
<source lang="bash">
<syntaxhighlight lang="bash">
git log --stat --author $(git config --get user.email) --since="last year" --until="last month" | awk -F',' '/files? changed/ {
git log --stat --author $(git config --get user.email) --since="last year" --until="last month" | awk -F',' '/files? changed/ {
     files += $1
     files += $1
Line 123: Line 159:


}'
}'
</source>
</syntaxhighlight>


== Visualizing ==
== Visualizing ==
Line 148: Line 184:
* http://flavio.castelli.name/2007/09/04/howto_use_git_with_svn/
* http://flavio.castelli.name/2007/09/04/howto_use_git_with_svn/


== Git Repo Hosting ==
It seems everyone uses GitHub these days (~8 million users, 20 million projects).  It's not a bad solution, but there are some drawbacks, and whether you're just looking to host your own project or setup enterprise-wide git hosting, there are other ways to host your projects in Git.  Let's take a look at some options.
<ol>
<li>'''GitHub''' can make it a bit easier for developers who are used to working with svn because they have 'built-in' [https://github.com/blog/1178-collaborating-on-github-with-subversion svn client] support.<ref>Anyone can use <code>git svn</code> and all the other built-in git subversion bridging commands, GitHub simplifies how to do it.</ref>  GitHub [https://enterprise.github.com/home Enterprise] can be installed on your own servers.  GitHub costs money for private repos.
<li>[https://about.gitlab.com/ '''GitLab'''] is the "popular" alternative to GitHub that comes in "Community", "Enterprise" and "Cloud" versions.  They say it's "[https://about.gitlab.com/better-than-github/ better than GitHub]"  One nice thing that I note is that they've integrated git-annex for large binary support.  This is a nice plus.  (I've personally used git-annex as a file backup system -- like DropBox. In my case, it was to make backups of UbuntuOne when that service was discontinued.)  You can install GitLab in minutes on your own server.  It's costs if you want the [https://about.gitlab.com/features/#compare Enterprise] version.<li>'''GitBlit''' is "a pure Java git server that just works".  It's a "true" open source project run by [https://www.linkedin.com/pub/james-moger/8b/3a3/65b James Moger] and scores of other contributors.  GitBlit is self-hosted, so you can get a feel for it quickly by looking at the projects own [https://dev.gitblit.com/ source code]. It does have some nice [http://gitblit.com/features.html features].
The current Wikimedia Ops team is using [http://gitblit.com/ Gitblit] to drive https://git.wikimedia.org/ Gitblit is an open-source, pure Java stack for managing, viewing, and serving Git repositories.
It's designed primarily as a tool for small workgroups who want to host centralized repositories.  It comes in two options: GO is an integrated single stack solution so you can really get up and running in no time flat; and there is a WAR version (assumes you already have a servlet container like Jetty or Tomcat.) Gitblit requires a Java 7 Runtime Environment (JRE)  Gitblit comes with a lot of [http://gitblit.com/features.html features]
<li>[http://gitolite.com/gitolite/index.html gitolite] is similar to the now defunct '''gitosis'''.
</ol>


== Background ==
== Background ==
Line 191: Line 216:
Only in /var/www/drupal/sites: www.example.org
Only in /var/www/drupal/sites: www.example.org
</pre>
</pre>
== Git Repo Hosting ==
See [[Git repo hosting]]


== Building a Git server ==
== Building a Git server ==
Line 198: Line 226:
Generally following the series of instructions at
Generally following the series of instructions at
http://vafer.org/blog/20080115011413  I installed Git-core, gitweb and gitosis on the  host
http://vafer.org/blog/20080115011413  I installed Git-core, gitweb and gitosis on the  host
<source lang="bash">
<syntaxhighlight lang="bash">
# install git, the git-svn tool and the viewvc equivalent
# install git, the git-svn tool and the viewvc equivalent
apt-get install git-core git-svn gitweb
apt-get install git-core git-svn gitweb
Line 278: Line 306:
# and install it
# and install it
apt-get install gitosis
apt-get install gitosis
</source>
</syntaxhighlight>