logo telecom ipp

GIT Level 1: INF103

Overview

  • concepts: working copy, stage and local repository (.git)
  • git init: create a local repository
  • git status: get info on the local repository
  • git log: get info on the commits
  • git add: start tracking a new file, or stage new changes for next commit
  • git commit: store a new set of changes to tracked files

Demo (by the teacher in a terminal)

  • git checkout: change the version of the local copy
  • git remote add: connect a remote repository to the local repository
  • git push: push the state of the local repository to a remote repository
  • git pull: pull the state of a remote repository to the local repository
    • merge
  • git clone: clone a remote repository to a local folder
  • .gitignore: list of files to ignore

Concepts

  • Version control is about saving versions with documentation
  • It applies to software, but also to text documents such as lateX
  • There is often a distant server and a local copy of a version
  • Free systems: CVS, then SVN, now GIT
  • Elements in the GIT system are:
    • your files, the working copy
    • an index or stage, i.e. a place for changed files ready to be put in the repository
    • a repository

Usage

Basic usage loop is:

  1. Change your source code
  2. Add the changes to the index or stage
  3. Commit = store a copy of the changes in the index on the repository

Graphically, files and commits

Vocabulary

Repository

A repository is the set of data of the version control system, which includes at least all versions of all files (or a way to recreate them). It also includes author, timing, dependencies, branches information...

A GIT repository, repo for short, can be rather large.

  • local repository:
    • on your machine
    • to be able to work with no connection
    • in a folder named .git
    • contains ALL of the project (you can restore a project from a local repo)
    • do NOT touch or modify!
  • we will see later about distant repositories

Commit

A commit describes a new version as a collection of changes made to at least one file. A commit may contain many changes of many files, changes to the text, the file name, the file permissions, the file location, etc. It has a unique label to identify the version (SHA1).

A commit also has a descriptive message: it is really important to create a simple, clear message for each commit that is coherent with its content.

commit

Working copy

The working copy is a copy of the content of the current version of your repository. The current version is the one marked as HEAD.

Either your working copy is modified, i.e. different from the state of the current version in git, then you can do:

  • prepare (stage) some or all of your changes for the next commit, then commit
  • forget your changes and return to the current version in git
  • stash your changes (save a set of changes in the stash queue) and return to the current version in git

Or your working copy is unmodified. Then you can do:

  • switch to another version
  • pop a previous stash (saved set of changes)

You cannot switch to another version if your working copy is modified

The state of your working copy is given by git status

Basic GIT configuration

To make your life easier, you should execute these commands:

git config --global core.pager ''
git config --global core.editor nano
git config --global user.name "your name"
git config --global user.email "your@email"

The first line is about git log. git log uses by default a simplified editor called "less" which requires typing "q" to exit from the list of commits.

The second line is about git commit. If you do not use the -m option, then git commit triggers the very old vi text editor by default. nano is a text editor that is a bit easier to use.

The last two lines are simplifying the commit messages.

git init

This command creates a repository in the current folder.

There is no message if all goes well.

git status

This command shows information about the status of the git repository in the current folder.

The message includes:

  • the state of the repository
  • the list of untracked files in red
  • the list of files added to the index or stage in green

If the message contains an error, maybe you are simply not in the right folder. Use cd to go to the folder where you did git init.

git add

This command adds a given file, or all the files in the given folder to the index or stage.

git add src/tp01/Main.java adds the src/tp01/Main.java file

git add src/tp01 adds all the files in src/tp01

If all goes well, there is no message. If there is a message, it means something went wrong, so you have to read the message to understand.

git commit

This command commits all files in the stage to the repository.

git commit opens the text editor to force you to create a commit message

git commit -m "message" commits with the given message.

If all goes well, there is no message. If there is a message, it means something went wrong, so you have to read the message to understand.

git log

This command lists information on previous commits.

git log lists all commits

git log --stat lists all commits including which file was modified in each commit.

Usage scenarios

Create new repo:

  • create folder
  • git init
  • copy files to folder
  • git add
  • git commit ...

Basic questions: how do I...

  • create a local repository ? git init
  • know if changes were made to the working copy ? git status
  • add a new file for tracking ? git add file
  • add a changed file to the next commit ? git add file
  • get info on commits ? git log
  • commit changes ? git commit -m "message"
  • know if changes have been staged ? git status and the files are in green

=> demo how all these commands work in practice in a terminal

Distant repositories

For exchange with others, you need to be able to access their repositories and they need to be able to access your repository (or a copy of it).

Git allows you to transfer all or part of a repository to another repository.

Repositories can be put on GIT servers: they have a URL for access.

Scenario 2

Get existing software and adapt it:

  • git clone URL to create a local repo from the distant repo
  • git checkout BRANCH to checkout the branch you want to use, if not master
  • change files, test changes, git add, git commit
  • git push to send the changes to the distant repo

More Usage

Wider usage loop is:

  1. Pull changes from the distant repo (from your colleagues)
  2. Make changes, test, stage and commit, repeat (this is the earlier loop)
  3. Push your changes to the distant repo

Graphically, repositories and actions

GIT

Scenario 3

  • share work between lab rooms at school and your home desktop:
    • first time
      • same "create new repo"
      • git remote add distant URL to add URL as distant repo
      • git push --set-upstream distant master to connect local master to distant master and send data
      • at home: git clone URL
    • after
      • on one side, change and commit and git push
      • on the other side, git pull merges local and distant changes
      • resolve conflicts
      • git commit

Working with remote repositories

  • without a remote repository, you cannot exchange information with others
  • or even with yourself on another machine
  • local and distant repositories contain the same things
  • if created with git init, a local repo has no remote
    • to add a remote: git remote add distant URL
    • to connect a local branch to the distant branch:
      git push --set-upstream distant master
  • if created with git clone, a local repo has one remote called "origin"
  • git push sends the state of the current branch to the first remote
  • git push remoteName sends the state of the current branch to the remote remoteName
  • git fetch is the opposite of git push, it copies information from the remote branch to the local branch without changing the working copy
  • git pull is the combination of git fetch and git merge
    • fetch may have connection/network problems
    • merge may create merge conflicts that you have to resolve before going on

Your first Merge

Here is an example file:

public class Toto {
    public Toto() {}
}

You have this file in the working copy of your computer in the lab and in your computer at home.

You add a parameter to the constructor at home:

public class Toto {
    public Toto(int i) {}
}

You commit this change. You push.

A week later, you forgot you did this. You do it again in the lab, but like this:

public class Toto {
    public Toto(int var) {}
}

Your first Merge - 2

You commit this change and push. GIT says your local repo is behind the distant repo (because of the change you did lsat week and pushed). Then as instructed you git pull. Conflict!

Here is the content of your file:

public class Toto {
<<<<<<< HEAD
    public Toto(int var) {}
=======
    public Toto(int i) {}
>>>>>>> other
}

GIT recognized the same parts and show you both versions of the different parts.

To resolve the conflict, you have to:

  • edit the file
  • choose one of the two versions, or edit a mix
  • save the file
  • git add and git commit

On Merge

  • Merging binaries does not work.
  • If changes are on different parts of the text file, then git merges automatically.
  • There are good graphical interfaces for merging, use the one which works for you. I like smartGit, just for the merge view.
  • There is a more complex version of merge called rebase, avoid it for now.

Clone

git clone URL

  • creates a local repo
  • copies all the data from distant repo URL to the local repo
  • sets URL as remote 'origin'
  • checks out the branch 'master' in the working copy

.gitignore

  • by default, all files in the working copy folder can be tracked
  • tracking binaries and generated files makes no sense
  • .gitignore contains the name of files that should not be tracked, one per line
    • *.class = do not track any file with extension .class
    • bin/ = do not track the content of the bin folder
    • file.txt = do not track file with name file.txt, even if it is text
  • you can put a .gitignore in any folder in the working copy
  • .gitignore files should be tracked by GIT

Recommandations

  • create many, small, coherent commits
  • commit when you stop working, even if it does not work
  • do not push something that does not work to a shared repo
  • if stuck, look at this page for help on common git problems

This resource is a reference, exhaustive and somewhat hard to read: git-scm.org

Gitlab

  • Gitlab is a tool for organizations, like Telecom Paris, to provide distant repositories
  • Gitlab is a Git server wrapped into a web server: all of the interface is in your browser
  1. Setup of a safe connexion
  2. Clone a repository with a meaningful merge example
  3. Create a repository

Gitlab safe connection

  1. Create an SSH key pair in your root folder
    1. Open a terminal window in your root folder (PowerShell under Windows)
    2. Go to folder .ssh, create it if needed
    3. If you already have an SSH key pair, you may use the private key (content of .pub file)
    4. If not, execute ssh-keygen -t ed25519 and answer questions
    5. In this .ssh folder, edit or create the file config and add the lines below.
    6. Open the .pub file in a text editor and copy the content to the clipboard
  2. Put the public key in your Gitlab profile
    1. Connect to Gitlab
    2. Click the top-right icon to get the menu and choose "Preferences"
    3. Choose in the left sidebar: "SSH Keys"
    4. Click in the key content zone and paste the content of the .pub file
    5. Write a title then click on the "Add Key" button

Addition to config:

Host gitlab.enst.fr
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/<le nom de votre clef privée>
  IdentitiesOnly yes

Gitlab connexion sécurisée 2

Test the safe connection to Gitlab by trying git clone or push or pull on a non public repository, from the command line: git should not ask for your password anymore.

If you need to work on multiple computers, you may have to do this operation on all of them. The school computers all share a configuration where your root folder is accessible, so doing this on one of them is enough. You will have to do this on your laptop or your computer at home.

It is preferable to have a different key pair on each computer. If you want to use the same key, then copy the private key and the config file into the .ssh folder of the new computer.

Demo

Clone a repo for a merge

Clone the repository: git@gitlab.enst.fr:slr201-git-adv/learning-merge.git

There are two branches ready for a merge to master. None of the two allows automatic merging.

  • refac1 allows a merge with simple choices
    • on Complex.java, choose the entire refac1 side
    • on FFT.java, you have to choose the comments on one side and the refactorings on the other
  • refactor forces you to do manual editing of the code, because changes appear on the same source lines

Demo

Create a repository

Gitlab, in the Telecom Paris version, does not allow the students to create repositories anywhere. You can only create repos in groups we teachers have created. To create a repo elsewhere, create a ticket.

You can create a repo in the Gitlab groupe corresponding to your INF103 group. https://gitlab.telecom-paris.fr/2021INF103/gr5tp

Demo

Attention!

  • If you cannot connect, ask me, it must be solved before end of October.
  • Do not use the Gitlab web interface directly to create or change files and make commits in the browser.
  • There are many complex commands and interfaces for other tools, just ignore them.

Credits

I am using an image from Rémi Sharrock, one from the git-scm.org and a few from Mark Lodato

Other courses in this series are:

This course was designed by Jean-Claude Dufourd with James Eagan, and shared as Creative Commons 3, BY-NC-SA (attribution, non commercial, share alike)