Nicolas Barrier
Witold Podlejski
Criscely Lujan-Paredes
Windows and Mac
Download and install Git from https://git-scm.com/downloads.
When done, open Git Bash
Linux
Open a Terminal
window and type:
sudo apt install git git-lfs git-flow
On Git Bash
or in the Terminal
:
git config --global user.name "Firstname Lastname"
git config --global user.email "myadresse@ird.fr"
Note
These two lines identify you in the history of a project.
git config --global --list
to see the global git configuration.Workspace
: your working directory \(\rightarrow\) your computerLocal
: the local repository \(\rightarrow\) contains the history of your projectIndex
: a buffer between Workspace
and Local
\(\rightarrow\) list of the files that will be sent from Workspace
to Local
git add
: the command to add the file(s) in the Index
git commit
: the command to validate the changes (moves the files from Index
to Local
)training-git
by typing mkdir training-git
cd training-git
ls -alrt
git init
ls -alrt
.Note
A .git
folder has appeared. It contains the full history of your project (Local
repository)
git status
and git log
Create a README.md
file. Type git status
\(\rightarrow\) README.md
is now in Workspace
but not in Index
nor in Local
Type git add README.md
and git status
git commit -m "First commit"
and type git log
Note
0f0e96a
is a short version of the identifier of the commit
README.md
file, add # Git training
and savegit status
git diff
git commit -m "Second commit"
git log
README.md
file and add ## Version v1.0.0
.git add README.md
git commit -m "Third commit"
git tag v1.0.0
and git log
git tag
to list all existing tagsIt is possible to tell Git to ignore some files by using a .gitignore file.
output.log
file and type git status
.gitignore
file and write *.log
. Type again git status
The output.log
file does not appear as an Untracked file
anymore
git add .gitignore
and git status
git commit -m "Fourth commit"
Tip
To list the ignored files, type git ls-files --others --ignored --exclude-from=.gitignore
git checkout v1.0.0
\(\rightarrow\) move to a taggit checkout 0f0e96a
\(\rightarrow\) move to a specific commitgit checkout main
\(\rightarrow\) move at the latest commit (replace main
by master
if the latter is the name of the main branch)Tip
HEAD
is a symbolic reference pointing to wherever you are in your commit history, as shown in git log
git diff 0f0e96a v1.0.0
\(\rightarrow\) compares a commit and a tag.Warning
Order matters when using git diff
. Differences are shown with the reference state considered to be the first argument.
Type git diff 0f0e96a c6dc2bc
\(\rightarrow\) compares two commits.
Type git diff 0f0e96a HEAD
\(\rightarrow\) compares where you are in the history (HEAD
) with a given commit.
In addition of saving the history, Git has other advantages. It allows to:
To do so, a \(4^{th}\) component in the Git architecture must be considered: the Remote
repository. It contains a remote version of the history of your project
There are several remote hosting possibilities:
Commercial hosts:
Institutional hosts:
In the following, we will use GitHub.
Tip
GitHub proposes extra-features for students, teachers and researchers. Visit https://education.github.com/benefits for more informations
Repositories
New
buttonCreate repository
To authentificate, you need to create an authentification token (see GitHub authentification of details for details).
To do so, click on your profile photo and then on Settings
:
Developer settings
.Personal access tokens
, click Tokens (classic)
.Generate new token
and Generate new token (classic)
.Click on the Generate token
box button.
Copy and save in a .txt
file or in a Password manager tool (KeePassXC) the token: this is your password! It should look like something like this:
ghp_***************************************
Terminal
or Git Bash
, type the following line:git remote add origin https://github.com/barriern/git-train.git
Warning
Replac barriern
by your GitHub login and git-train
by the name of your GitHub repository.
git remote -vv
Now that the local and remote repositories are linked, the same thing must be done with the branches.
git branch -M main
by replacing main
by the name of the remote branch on GitHub. It will rename the local branch with the same name.git push -u origin main
It connects the local and remote main branches (-u
option) and sends the commits to the remote branch
git branch -vv
Have a look at your repository on GitHub. Tags are missing!
Type git push --tags
and refresh the GitHub page.
Note
No need to specify the -u origin main
arguments since the two branches are already connected.
Navigate on the GitHub page to see what has been done.
In GitHub, click on the README.md
file and then on the edit button
Add a Update from Github
line and click on Commit changes
The Remote
change of README.md
is not yet visible in Workspace
!
Git Bash
or Terminal
, type git pull
README.md
file on your computer. You should see the update.x = 1
at the end of the README.md
file. Do not type pull!README.md
and add x = 2
.git add README.md
git commit -m "Fifth commit"
git push
. An error occurs because changes in Remote
have not been pulled in Local
.git pull
and git status
. An error occurs because there is a conflict in the README.md
file which cannot be solved by Git.README.md
file. You should see:<<<<<<< HEAD
x = 2
=======
x = 1
>>>>>>> 70a4c105e377db282c0769606960f0afcccdd071
Warning
These are conflicts markers. Git
does’t know whether to chose x = 1
or x = 2
. This is your job
x = 3
. Commit and push the changesTerminal
or Git Bash
, type cd ..
git clone https://github.com/umr-marbec/git-training
git log
to see the full history.git pull
Warning
Do not clone or initialize a Git repository in another Git repository!
To create a new repo more simply than done here:
README.md
file and eventually a LICENCE
file.remote
and local
repositories are synchonizedremote
and local
main branches are synchonizedgit pull
git commit
extensivelygit push
git status
extensively to know what you have doneGit Clients are softwares that facilitate the use of Git (see Git Guis for a list).
Beside, most code editors include Git functionalities
For those who want, extra slides are available on:
To version (reasonably) large files (images, data samples) \(\rightarrow\) Git with LFS extension.
Warning
Make sure that the remote host is compatible with LFS (GitHub is compatible)
git lfs install
to activate the extensiondata.csv
file and add Year,Size,Species
git lfs track "*.csv"
A .gitattributes
file has appeared, which list all the file extensions managed by Git LFS.
git add .gitattributes data.csv
git commit -m "Using LFS"
git push
data.csv
file.To create Git aliases (i.e. shortcuts):
git config --global alias.tree log --all --decorate --oneline --graph
git config --global alias.br branch -vv
git config --global alias.re remove -vv
You can now call the git tree
, git br
and git re
commands.
git checkout -b develop
git status
, git br
and git tree
README.md
file, add some text and save.git add README.md
git commit -m "3rd commit"
git br
and git tree
git checkout main
(or git checkout master
)git br
LICENCE
file and add some text in itgit add LICENCE
git commit -m "Third commit"
git tree
main
branch, type git merge develop -m "merge-develop"
git log
and git tree
The merge
command puts the commits from the argument branch (here develop
) and puts them into the current branch (here main
).
Note
During the merging process, another commit is created
git checkout -b feature develop
script.R
filegit add script.R
git commit -m "Fourth commit"
git checkout -b feat-com 1831e4e
replacing 1831e4e
by an actual commit ID.script.py
filegit add script.py
and git commit -m "Sixth commit"
git diff develop main
Warning
Order matters: it shows what has been added to main
branch compared to the develop
branch
git checkout main
git branch -d develop
git br
git branch -d feat-com
An error occurs! The suppression of feat-com
implies the loss of the d9d02608
commit. To force the suppression, use -D
instead of -d
.
git branch -D feat-com
Note
The suppression of develop
was ok because the content of commit 3rd
is included in the merge.
git revert c6dc2bc
(replace c6dc2bc
by your commit id)git init
: initialise a git project (create .git folder)git add [files]
: add files to list of tracked filesgit commit -m ”message”
: validate locally a version of the projectgit status
: see the unvalidated and untracked changesgit checkout [commit]
: load the project version corresponding to the indexgit pull
: import the changes from remote project to localgit push
: export the changes from local project to the remote servergit config --global user.name "Firstname Lastname"
git config --global user.email "myadresse@ird.fr"
git branch [branch_name]
: create a new branch (but you remain on the previous branch)git branch -b [branch_name]
: create a new branch and move to this newly created branchgit checkout [branch_name]
: move to the corresponding branchgit merge [branch_name1] [branch_name2]
: merge two different branch, you may need to resolve version conflict.git branch -d [branch_name1]
: delete a branch (safe mode)git branch -D [branch_name1]
: delete a branch (unsafe mode)git clone [URL]
: Import an existing project from remote server.git remote add origin [URL]
: link directly the local repository with a remote