Git: “It’s just a tree”

Pierre-Antoine Comby

Created: 2021-07-03 Sat 14:35

What is Git?

Version Controlling Software

  • Until (probably) now: manual backup
    git-intro_Final-1.1.pdf
    git-intro_review_by_expert (send by email, modified three times)
  • VCS
    • Have a backup of each iteration of a project
    • Develop multiples projects together.
    • Handling conflict without human fight (non-guarateed feature)
    • Fix his mess easily

Local

local.png

  • no code sharing or build-in backup

Centralized

centralized.png

  • synchronous updates required

Distributed

distributed.png

  • Every computer have a full copy of the project.
  • asynchronous updates possibles.

Git: a distributed VCS

  • Other VCS Software on the market (Bitkeeper, SVN, CVS)
  • But Git is better
  • Created by Linus Torvald for developping the Linux Kernel
  • Other VCS use delta comparisation storage, git takes snapshot of your project.
  • If a file is not modified, Git use a pointer to this file (reduce data storage).

deltas.png

snapshot.png

Why use Git :

  • Work on text-based files (but not only)
  • Because (nearly) everybody does (12Mi people worldwide)
  • Manage small projects (lecture notes, small scripts, your blog)
  • Manage big projects (Linux Kernel, Django, GCC, almost every Open source project…)
  • Distributed System
  • Multiplateform

Install Git

In the following we will use the Command Line interface. Windows user can use the git bash software.

Configure Git

Who are you ?

 git config --global user.name "USCTrobot"
 git config --global user.email USCTrobot@kit.edu

choose your editor (linux user)

 git config --global core.editor nano
  • all config parameters is save in ~/.gitconfig

Let’s go !

Start a Git repository

  • From scratch :

      $ cd my_awesome_project
      $ git init
    

    this a created a .git folder in your project, which will contains the history of your modifications.

  • Start from someone else work

      $ git clone https://gitlab.crans.org/plop/toto
    

Save your modifications

The 3 states of Git

lifecycle.png

Add files

git add

$ git add CONTRIBUTINGS.md
$

I did multiples modifications to a file

$ git add -p

Ignore files

  • sensibles files (passwords, user data )
  • compiled files (.pyc, .pdf, .exe …)

Use a commited file: .gitignore.

# useless to follow:
*.[ao]
*~
*.pyc
*.pdf
# full folder:
auto/
  • You can set global ignore rules in the ~/.gitignore_global (and use git config --global core.excludes_files ~/.gitignore_global)

Check state of our Repositories

git status

git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   CONTRIBUTINGS.md

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
  README.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)
  LICENCE.md

Condense display : git status -s

Check your modification

git diff

  • git status give only the global state of your repository. Git can also show internal modifications:
$ git diff

I did git add and then git diff and nothing shows up

  • git diff only work on unstaged files by default (by comparing them with staged and commited files). For change in the staged area (compare with commited area)
$ git diff --staged

Save your modification

git commit

  • Save your work, (nearly) forever in the history of your project.
$ git commit # open a text editor for commit message.

Commit are build with:

  • all staged modifications
  • dates, author, … (automatic)
  • a commit message
    • “what is this modification ? why am I doing it ?”
  • a hash (ex 5b05ec755a65ecda62e32d09ddc513a549b941f6 )
  • a reference to the last commit (“parent”)

All together your commit form a chained list.

faster : git commit -m "message direct"

Delete files

git rm

git rm files_to_delete
  • remove your file from the indexed (followed) files, and delete it.
  • The history about this file is still in the database.

Move/Rename files

git mv

$ mv old_name.txt new_name.txt
$ git rm old_name.txt
$ git add new_name.txt

make it easy:

git mv README.txt README

Logs of the tree

$ git log
commit 5b05ec755a65ecda62e32d09ddc513a549b941f6 (HEAD -> master, origin/master)
Author: Pierre-antoine Comby <comby@crans.org>
Date:   Thu Mar 7 22:39:47 2019 +0100

    update avant partiel

commit fbd4103b7dc1ca88b2ae084cc0d693db39f45dd8
Author: Pierre-antoine Comby <comby@crans.org>
Date:   Thu Mar 7 14:44:34 2019 +0100

    sous optimal mais fonctionnel

lots of usefull files:

  • -p show diff between commit.
  • -2 limit to the last two commits
  • --stat
  • --all
  • --graph
  • --oneline
  • --decorate

pro tip: git is –(A)ll –(G)raph –(O)neline –(D)ecorate

Cancel actions

I forgot something in my commit

$ git commit -m "validation initiale"
$ git add truc_oublie
$ git commit --amend

Unstage a staged modification

I git add something I did want to

git reset HEAD mon_fichier

-> proposed by git status

Delete a modification from an unstaged file

-> Revert the modification in your file.

$ git checkout -- mon_fichier

You lost FOREVER this modification.

Branches

A branch is a pointer

  • It is NOT a copy of the projet in a other state.
  • usefull to organize your work.
  • commit are pointing to a parent.
  • a branch (ex master) point to the last commit realized on this branch.

head-to-master.png

  • HEAD point to the active branch. C’est actual state of your repository (if you do not have any unstaged modifications).

Change branch

$ git branch testing # create a branch
$ git checkout testing

I’m lazy …

$ git checkout -b testing
$ git commit -m "testing something"

advance-testing.png

Diverge from your previous branch

$ git checkout master
$ git commit -m "mastering something"

advance-master.png

Branches et Merging

basic-branching-4.png

$ git checkout master
$ git merge hotfix
Updating f42c576..3a0874c
Fast-forward
 index.html | 2 ++
 1 file changed, 2 insertions(+)

basic-branching-5.png

Merge Commit

basic-merging-1.png

basic-merging-2.png

Merge Conflicts

$ git merge iss53
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

what happen ?

$ git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
  (use "git add <file>..." to mark resolution)

    both modified:      index.html

no changes added to commit (use "git add" and/or "git commit -a")

Git is not magic

$ nano index.html
<<<<<<< HEAD:index.html
<div id="footer"> On branch master</div>
======
<div id="footer">
 On branch iss53
</div>
>>>>>>> iss53:index.html
  • edit the conflicting file(s) (and delete what git added)
  • Configure git mergetool for help.
  • git add index.html et git commit pour conclure la fusion.

Cut off the branches

  • delete the label merged branch:
$ git branch -d iss53
  • If the branch is not merged git branch -D
    • Delete all the commit related to this branch!

remote repository

the more the merrier

show the remotes

$ git remote
origin
$ git remote -v
origin   git@git.scc.kit.edu:xyz/usct.git (fetch)
origin   git@git.scc.kit.edu:xyz/usct.git (push)

Add new remotes

$ git remote add pb https://github.com/paulboone/ticgit
$ git remote -v
origin https://github.com/schacon/ticgit (fetch)
origin https://github.com/schacon/ticgit (push)
pb https://github.com/paulboone/ticgit (fetch)
pb https://github.com/paulboone/ticgit (push)
  • More info ?
$ git remote show origin

Get the remote update work on my computer

$ git fetch [remote-name]
  • Get all the data you do not have.
  • Does not modify your work, only get them to their local remote branch.

For Get and merge

$ git pull

Push you work on the remote

I made some awesome modification, I want to share it to others.

$ git push origin master

Remotes branches

remote-branches-1.png remote-branches-2.png

*

remote-branches-3.png *

remote-branches-4.png *

remote-branches-5.png

GitLab

gitlab-logo.png

  • Gitlab : software factory
    • Web interface
    • Project Manager, Bug report, …
    • self-hosted !

GitLab at KIT

Add a ssh key

  • for easy and secure pushing
  • on your computer
$ cd ~/.ssh
$ ssh-keygen -b 4096 -t rsa
 => enter name for your key (ex GitlabKey)
$ cat gitlab.pub

copy paste it in your gitlab account settings.

  • your can now clone/pull/push via ssh.
  • encryption of commit are also possible via GPG.

Advanced Commands

git stash

  • Stash the changes in a dirty working directory away
    • Save your change , and revert to HEAD.
    • You have a clean repository
    • usefull for WIP
    • or for pulling with unstaged modifications.
  • Apply your change back with git stash apply
  • View the differents stashed modifications git stash list git stash show

git reset

git revert

git rebase

So you’ve a mess on your hand?

git-pretty.png

  • check the internet, and make hard backup of your .git folder if you are not sure of what you’re doing.