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™.

An important distinction

Documentation about git

A little loose, but still in the order from the best reference to the blurrier:

Two documents in PDF to download in order to never panic when you will get lost during your first days with git:

Other resources (less useful, but can help):

Note

And how does it work, really, underneath the nice web interface and command line tools? Is it (theoretically) complicated? Yes.

XKCD of Friday the 30th of October, about Git

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.

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).

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).

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:

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

lbesson.Bitbucket.org ?

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:

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.

Some « Buttons »?

As some GitHub fans offer ghbtns.com/ or buttons.GitHub.io/, a Bitbucket fan designed bb-btns.bitbucket.org/.

Here are some examples for my web-sphinx deposit:

The same, but hosted on lbesson.bitbucket.io/bbbtns/ (for my bin repository):

The same, but hosted on my website (./bbbtns/) (for the lbesson.Bitbucket.org repo):


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.