Hands-on 6b

  • Initializing the working environment
  • Modifying files
  • Using the history
  • List of changes
  • View differences between versions
  • Going back to an earlier version

Initializing the working environment

You will now initialize a workspace (that is, a directory) containing a working copy of the repository associated to your project You should first set your user name and e-mail address for git.

REMEMBER: the lines starting with a $ prompt are commands that you need to type ; if there is no prompt, the line is a result displayed by the previous command. If the result is not as expected, try to understand it, then call a lecturer. //: <> (, as well as your editor. )

$ git config --global user.name "First_Name LAST_NAME"
$ git config --global user.email "First_Name.LAST_NAME@u-psud.fr"

Hereafter, I will exemplify the following with “First_Name.LAST_NAME”=“vincent.lafage” ⇒ YOU WILL HAVE TO REPLACE THIS WITH YOUR OWN IDENTIFIER.

In terminal, change your current directory to the Project directory (the directory containing the work of the last session).

$ cd ~/Project

Then clone the central repository matching your identifier and your freshly registered password as your local repository and move to the newly created directory. Each computer has its own project, named NEnn_project

⇒ YOU WILL HAVE TO REPLACE THIS nn WITH THE nn VALUE OF YOUR OWN NE-nn IDENTIFIER.

$ git clone https://vincent.lafage@gitlab.in2p3.fr/MasterNuclearEnergy/NEnn_project.git
Cloning into 'NEnn_project'...
Password for 'https://vincent.lafage@gitlab.in2p3.fr': 
warning: You appear to have cloned an empty repository.
$ cd NEnn_project

Fill the new repository

$ touch README.md
$ git add README.md
$ git commit -m "add README"
$ git push -u origin master

Check the status of the workspace:

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

nothing to commit, working tree clean
$ git log
commit b74162bb998208998b6569c9f9da9a7f9b9bb565 (HEAD -> master, origin/master)
Author: Vincent LAFAGE <vincent.lafage@u-psud.fr>
Date:   Sun Jan 7 21:53:55 2018 +0100

add README

Add files from session1: http://ipnwww.in2p3.fr/cours-ne-data-processing/web/program/session-1/hands-on-1b/

$ cp ../session1-start/* .

Indicate the files to add to the repository among the recently added files:

$ git add *

Revise the list of files that will be added to the repository and remove unnecessary files and that need not to be versioned (those generated by compiler for example): temporary copy, file backup, executable file, …

$ 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:   a.out
new file:   hello
new file:   hello.cxx
$ git reset a.out hello
$ git status
…

The files are not destroyed, they will be simply removed from the list of files scheduled for adding to the repository. Commit changes to the repository, with a message summarizing the changes.

$ git commit -m "Importing files from the first session"

Modifying files

Here, we propose to modify one or more files and store them in the repository. (Reminder: to modify a file, open it in your editor, Atom, and add some text, for example add a comment line in hello.cxx.) The operation can be repeated several times to fill out the history for the next step.

Modify an existing file hello.cxx, e.g. add a comment line, check the effect (with a terser version of git status), stage the modified file and commit the change in the repository:

$ git status -s
 M hello.cxx
?? a.out
?? hello
$ git add hello.cxx 
$ git status -s
M  hello.cxx
?? a.out
?? hello
$ git commit -m "First modification of hello"
[master 1ebd29e] First modification of hello
 1 file changed, 1 insertion(+)
$ git push -u origin master
Password for 'https://vincent.lafage@gitlab.in2p3.fr': 
Counting objects: 6, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 767 bytes | 767.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To https://gitlab.in2p3.fr/MasterNuclearEnergy_2017/NEnn_project.git
   b74162b..1ebd29e  master -> master
Branch master set up to track remote branch master from origin.

Copy an existing file (without using git cp), check the effect and save the change in the repository:

$ cp hello.cxx hi.cxx
$ git status -s
?? a.out
?? hello
?? hi.cxx
$ git add hi.cxx
$ git commit -m "Adding hi"
[master f52497c] Adding hi
 1 file changed, 9 insertions(+)
 create mode 100644 hi.cxx

… then push it to the central repository

$ git push -u origin master
Password for 'https://vincent.lafage@gitlab.in2p3.fr': 
Counting objects: 2, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 281 bytes | 281.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To https://gitlab.in2p3.fr/MasterNuclearEnergy_2017/NEnn_project.git
   1ebd29e..f52497c  master -> master
Branch master set up to track remote branch master from origin.

Delete the file added above (without using git rm), check the effect and save the change in the repository:

$ rm hi.cxx
$ git status -s
 D hi.cxx
?? a.out
?? hello
$ git rm hi.cxx
$ git status -s
D  hi.cxx
?? a.out
?? hello
$ git commit -m "Removing hi"
[master 340dfa4] Removing hi
 1 file changed, 9 deletions(-)
 delete mode 100644 hi.cxx

//: <> (Copy an existing file (using git cp), compare the effect and save the change in the repository:) //: <> ( $ git cp hello.cxx hi.cxx) //: <> ( $ git status -s) //: <> ( A + hi.cxx) //: <> ( $ git commit -m “Adding hi again”)

Using the history

In the following we will test two ways of using the history of versions stored in the repository: we will learn how to view differences between versions and how to return to an earlier version.

List of changes

To use the history, we usually begin by viewing the list of revisions released along with their description given with the check-in message.

View changes in normal mode:

$ git log hello.cxx
commit 1ebd29e34e835a5e236eecff13c84b7bcdf49ee9
Author: Vincent LAFAGE <vincent.lafage@u-psud.fr>
Date:   Sun Jan 7 22:58:06 2018 +0100
First modification of hello

commit 2826fd02c13ca26176b01684c450b6d5ae5fd28e
Author: Vincent LAFAGE <vincent.lafage@u-psud.fr>
Date:   Sun Jan 7 22:34:28 2018 +0100

Importing files from the first session

Note that each update of a file in the repository has an attributed identifier. In our example, there are two revisions. The attributed identifier is the so called hash string, for example ‘2826fd02c13ca26176b01684c450b6d5ae5fd28e’ for the commit done on Sun Jan 7 22:34:28 2018. It is not needed to copy all characters of the hash, usually we can use just first 8 characters, for example ‘2826fd02’.

View differences between versions

There are two ways to show the differences: between the workspace and the repository and between two versions in the repository.

In order to show the differences between the workspace and the latest version of the repository, modify an existing file hello.cxx first (e.g. add a comment line) and then do:

$ git diff hello.cxx

To show the differences between the workspace and an arbitrary revision in the repository first select the commit hash in the history (represented with the symbol COMMIT_ID in the instruction below) and then type:

$ git diff COMMIT_ID hello.cxx

Compare with the previous result. Show the differences between two revisions of the repository of the repository, selected from the history (the last revision is always called HEAD). They are represented with the symbols COMMIT_ID1 and COMMIT_ID2 in our instruction below. For the comparison to succeed, the file hello.cxx must exist in the two versions COMMIT_ID1 and COMMIT_ID2 (test to see what happens when this is not the case):

$ git diff COMMIT_ID1..COMMIT_ID2 hello.cxx

Reverting changes

It is sometimes useful to cancel changes in the work space and return to the version in the repository.

First modify the hello.cxx file and then check that the files was modified:

$ git status -s
 M hello.cxx
?? a.out
?? hello

Now revert the changes and check the file status again:

$ git checkout hello.cxx
$ git status -s
?? a.out
?? hello

//: <> ( called Etudiants. Initialize the directory which corresponds to your allocated group number, that is your identifier, (the zone is initially empty) and add the files produced during the previous session.)

//: <> (Delete the file added previously (using git rm), compare the effect and save the change in the repository:) //: <> ( $ git rm hi.cxx) //: <> ( $ git status -s) //: <> ( D hi.cxx) //: <> ( $ git commit -m “Removing hi again”)