Quick tutorial for git¶
Introduction¶
This page is a quick introduction to git , and its regular use via Bitbucket.
This article contains very quick explanations about how to start using and mastering the mighty git software, its installation, and its use thanks to the awesome Bitbucket.
Links are provided to other tutorials and other explanations.
I also use that page to join and publish a few configuration files to improve and customize your use of git, some advices, and some aliases to ease the use of git on the command line (thanks to your ~/.bash_aliases
file for the command-line GNU/Bash environment).
Note
Licence ?
This page, and all related files are freely distributed under the terms of the GPLv3 licence ! (like everything I publish or distribute).
About Git¶
The official website of git is git-scm.com.
What is Git ?¶
« Git is a decentralized version control software. This is free software created by Linus Torvalds, Linux kernel copyright and distributed under the terms of the GNU General Public License Version 2. »
« Git is designed to be efficient in speed and ensure the integrity of data stored, sent or received. Completely free and open-source since 2005, it quickly became the most popular version control system for software development. »
What’s the point?¶
« A version control software is a software designed to store a set of files by holding the chronology of all the changes that have been made on it. »
« git allows notably to find all the previous versions of a lot of related files. It also allows an effective and secure control of your files (and even of entire projects) and also provide a complete track changes made to each file. »
Needless to say, I am using git constantly, mainly when I work on Linux ((X)Ubuntu™ 15.10) on a daily basis, and from time to time on Windows™.
Is it really so popular?¶
Yes:
The biggest web companies use git: Facebook, Google, Twitter, Netflix or LinkedIn for instance.
The git project itself has been developed by a small team, and they have been using git themselves: GitHub.com/git for quite some time!
In November 2014, GitHub seemed to have more that 7 millions of users, Bitbucket had almost 2 millions, and gitorious more than 400 000!
The wonderful software toolbox GitLab claims to be used by more than 100,000 companies and organizations from the world! See for instance the Crans gitlab (and my not-so-used account on it).
An important distinction¶
Bitbucket.org, GitHub.com, Gitorious.org (which hosts the source code for the amazing « Frédo » mathematics books), or the Crans GitLab are just hosting services (awesome services, but just this).
git is the version management software (free, open-source and free), used by all these web services.
Documentation about git¶
A little loose, but still in the order from the best reference to the blurrier:
This first tutorial by the team running Bitbucket (Atlassian),
www.Atlassian.com/git/ covers lots of things. I like the graphic and English styles of these explanations,
the help.GitHub.com website is really well designed, and it can sure help you,
this project on GitHub is a git style guide, to help you learn how to correctly name your branches, commits etc, see this article by @agis,
this last tutorial on readwrite.com is quite well written (second part).
Two documents in PDF to download in order to never panic when you will get lost during your first days with git:
The first one is given by training.GitHub.com,
The second one is by Atlassian.
Other resources (less useful, but can help):
that other unofficial tutorial about the use of both git and Bitbucket,
this third tutorial is kinda ugly but might be useful.
My (amateur) use of git¶
My setup¶
- State
Actively maintained. (November 2014)
For these two configuration files (on Linux), a global .gitconfig
and .gitignore
can be in your HOME
(~/
, or /home/pseudo
), and a particular file for every repository can also be created.
.gitconfig to configure the complete behavior of git (inspired by the one of the Crans developers, thanks to them).
.gitignore lets say which files should be ignored (ie. not considered) by git when it indicates the unsupervised files.
Voir aussi
- About
.gitconfig
git-config
is a git command, and it interacts with the file(s).gitconfig
. The page git-scm.com/docs/git-config gives in-depth explanations on how to use a.gitconfig
. More details are also given here on the book on git-scm.com. These explanations by Atlassian or this small article by Nick Berardi can also help. Two other examples: GitHub.com/matagus/gitconfig, GitHub.com/alikins/gitconfig.- About
.gitignore
These explications on help.GitHub.com why and how to use
.gitignore
. GitHub even offer a collection of .gitignore files suitable for different kinds of projects. For instance, that one is perfectly designed to use with a Python project.This other reference page git-scm.com/docs/gitignore or that last article on kernel.org can also help.
- Punk Rock Git
This article by Ian Miell explains very well some git commands and their core concept.
From the Command Line¶
You can consult my bash_aliases (also here on Bitbucket.org/lbesson/bin). I defined the following Bash aliases, which allow you to see the changed, deleted or added files:
alias GitChanged='clear ; git status | grep --color=always 'modified' | less -r'
for the modified files,alias GitDeleted='clear ; git status | grep --color=always 'deleted' | less -r'
for the deleted files,alias GitAdded='clear ; git status | grep --color=always 'added' | less -r'
for the added files,and
alias GitSize='clear; git count-objects -v -H | grep "size-pack" | sed s/"size-pack: "/"The size of the repository is "/'
displays the storage size of the current repository (e.g. 10.43 MB currently for this project web-sphinx).
$ git count-objects -v -H | grep "size-pack" | sed s/"size-pack: "/"The size of the repository is "/
The size of the repository is 17.33 Mio
These ones are good shortcuts, which by default also launches local compression of the just filed after each operation (on average it saves time):
alias Push='clear; git push && git gc'
to upload the latest changes on the mother repository,alias Pull='clear; git gc && git pull && git gc'
to download the latest changes from the mother repository,alias Commit='clear; git commit -m'
to quickly validate the last changes (ie. doing a commit),alias Add='git add'
to easily add files,alias Aggressive='git gc --aggressive'
launches an « aggressive » compression,alias s='clear ; git status | less -r'
is a handy shortcut for the most used git command:git status
.
And finally, I have defined an practical alias to synchronize the current project with the deposit mother and its sub-folder on my website.
alias Sync='clear; echo -e "Synchronizing (git push, gc, send_zamok, send_dpt)…"; git push; git gc --aggressive; make send_zamok; make send_dpt; alert'
For instance, that repository is here locally on my website (publis/ansi-colors). That other one is here locally on my website (publis/Bomberman). And that last one is also here locally on my website (python-demos).
Note
Details about aliases Bash?
More information is available in this book about Bash programming, on the Linux documentation project, that other doc, or that last Wikipédia about aliases on generic scripting programming language.
Avertissement
Git does * not * store compressed diffs, but entire files, at every release (in compressed form, okay)!
Contrary to what I believed for a long time (until 2020!), There is a difference between what git displays when we do a git diff file
and what git stores when we add this file in a new commit!
I thought for a long time that git stores the diff
, but actually no!
This is the biggest « urban legend » or misunderstanding on git, and I discovered my mistake thanks to this blog post on GitHub.blog (and this one too).
With Sublime Text 3¶
Note
A graphical interface for git?
The use of any good GUI for git can save a lot of time. To write webpages and code, I like Sublime Text 3 (ST3). Regardless of your text editor or IDE, it should be possible to bring this kind of functionality to easily use git without always going through console commands.
For example, Atlassian offers SourceTree, for Windows™ and Mac™ OS X. And GitHub have developed their own application for Mac™ OS X or Windows™.
The following free plugins allow a smooth and easy use of git on Sublime Text 3:
The small GitGutter will help to visualize the difference of each line of each file regarding the last commited version for the git repository in which it is located (visually great to see the changes since the last commit).
The wonderful SublimeGit (installable from sublime.wbond.net/packages/SublimeGit). Allows a full integration of git via the control panel. Free but annoys you regularly to ask you buy an (useless) 10$ commercial licence… But it is really awesome, so you will support this.
GitSavvy seems to be a good free and open-source alternative.
Voir aussi
- sublimetext.en.html
Please read the recent page sublimetext.en.html for more details on the use I make of Sublime Text 3.
With Bitbucket.org¶
Like the other hosting services, Bitbucket is free, flexible, powerful, 24h/24 and 7/7 functionnal, and allow to easily publish your code on Internet.
Note
GitHub offers pretty much the same (awesome) features, for free too.
Bitbucket offers in addition to the public repositories and unlimited numbers of private repos. Every repos is limited to 2GB (you really have to want to break the system go beyond!). Signing up is quick, just go to this page https://Bitbucket.org/ ! (Note that Bitbucket can also be used with a Google account, Facebook, GitHub or even with OpenID.)
Once your profile is created, it will look like mine, but more empty: Bitbucket.org/lbesson (see this one for another example Bitbucket.org/jilljenn).
Public deposits are visible to all: Bitbucket.org/lbesson/profile/repositories.
For example, here are some of my repositories:
Bitbucket.org/lbesson/web-sphinx stores the sources of this website, as for instance the reStructuredText (
.rst
) source code for that (→ and with a direct link to THAT line),Bitbucket.org/lbesson/bin hosts dozens of scripts, especially some in Bash and Python
Bitbucket.org/lbesson/web-sphinx-scripts stores local versions of all Javascript scripts used by this site,
Bitbucket.org/lbesson/cv stores the \(\LaTeX{}\) sources for my résumés in English and in French: cv.en.tex or cv.fr.tex,
Bitbucket.org/lbesson/munstrap a small repository hosting my French translation of the theme Munstap, adaptive and modern theme for Munin.
Further reading is available here en.WikiPedia.org/wiki/Bitbucket.
Publish web pages with Bitbucket?¶
If your username is Bitbucket STUFF, it is possible to create a repository called « STUFF.bitbucket.org », and then all documents will be stored there you will be automatically available on the website http://STUFF.bitbucket.org !
More details are given on cette page de doc par Atlassian, and here is their example (tutorials.bitbucket.org) is online at https://tutorials.bitbucket.org)
(This service is unfortunately a little less advanced than GitHub (GitHub.io, see the doc here), but works well anyway.)
Note
See for example this little page lbesson.Bitbucket.org/README.html whose source is hosted here Bitbucket.org/lbesson/lbesson.bitbucket.org/src/master/README.html.
I mainly use lbesson.Bitbucket.org for:
squirt a bookmarklet for Firefox, Chrome or Opéra, allowing to read a webpage super fast (hey, by the way, it is available on that website also, go on, try by hitting the « q » on your keyboard! Cool right?),
StrapDown.js is a pretty project to quickly and easily write and publish responsive webpages, in Markdown. Such StrapDown-powered document are directly ready to be published like-this on any website or server, without any boring compilation step required by you ! (This project currently is only downloaded about 20 times a month, thanks to the zip archive StrapDown.js.zip),
My own copies of the (legendary) 2048 game: lbo.k.vu/2048 (the original one), lbo.k.vu/2048-agreg (a version for mathematicians) or the interesting lbo.k.vu/2048-AI (available with an Artificial Intelligence).
and this homepage that acts as a nice wrapper for my website.
And even if the certificate is not valid, it is perfectly possible to use this feature with HTTPS activated: https://lbesson.Bitbucket.org/README.html (if your browser rattle and displays an error is perfectly normal!). If you are using a relatively new browser, it may grumble and explain why (you will see that the only reason is that the SSL certificat for https://bitbucket.org has been signed for only a few sub-domains of the website bitbucket.org, not all of them). But in fact the traffic will be well encrypted and secured by the SSL certificate, no worries :)
A French web interface for Bitbucket?¶
For a little over a year, it is possible to change the interface of the site in French (and many other languages) in your personal settings.
Note
French translation?
In March and April 2013, I initiated and for some time I managed the translation of the site and the service of the English into French. I translated almost 90% presque 90% du contenu initial, of the original content, left some small typos, but we did a good job. With few other motivated and myself, French was the first language translated to 100%! (Mid March 2013) I have neither the time nor the desire to take care of it, but the project continues to be maintained by others.
One last tip?¶
As with any technical and powerful software, git takes time to adapt. Be persistent, it is worth it.
« I wish you a very good programming experience, and good luck for your geeky projects! »
Additional credit¶
Thanks to Vincent Cohen-Addad for motivating me to start using git, in Octobre 2012 for the network programming project for my Computer Science (CS) Master of Science (MPRI, course #1-21) on which we worked together.