Overview
Demo (by the teacher in a terminal)
working copy
Basic usage loop is:
Commit
= store a copy of the changes in the index on the repositoryA 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.
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.
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:
Or your working copy is unmodified. Then you can do:
You cannot switch to another version if your working copy is modified
The state of your working copy is given by git status
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.
This command creates a repository in the current folder.
There is no message if all goes well.
This command shows information about the status of the git repository in the current folder.
The message includes:
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
.
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.
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.
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.
Create new repo:
|
![]() |
git init
git status
git add file
git add file
git log
git commit -m "message"
git status
and the files are in green=> demo how all these commands work in practice in a terminal
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.
Get existing software and adapt it:
git clone URL
to create a local repo from the distant repogit checkout BRANCH
to checkout the branch you want to use, if not mastergit add
, git commit
git push
to send the changes to the distant repoWider usage loop is:
Pull
changes from the distant repo (from your colleagues)commit
, repeat (this is the earlier loop)Push
your changes to the distant repogit remote add distant URL
to add URL as distant repogit push --set-upstream distant master
to connect local master to distant master and send datagit clone URL
git push
git pull
merges local and distant changesgit commit
git init
, a local repo has no remote
git remote add distant URL
git push --set-upstream distant master
git clone
, a local repo has one remote called "origin"git push
sends the state of the current branch to the first remotegit push remoteName
sends the state of the current branch to the remote remoteNamegit fetch
is the opposite of git push, it copies information from the remote branch to the local branch without changing the working copygit pull
is the combination of git fetch
and git 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) {}
}
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:
git add
and git commit
git clone URL
*.class
= do not track any file with extension .classbin/
= do not track the content of the bin folderfile.txt
= do not track file with name file.txt, even if it is textThis resource is a reference, exhaustive and somewhat hard to read: git-scm.org
.ssh
, create it if neededssh-keygen -t ed25519
and answer questions.ssh folder
, edit or create the file config
and add the lines below.Addition to config:
Host gitlab.enst.fr
PreferredAuthentications publickey
IdentityFile ~/.ssh/<le nom de votre clef privée>
IdentitiesOnly yes
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 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.
Demo
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
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)