Git for physicists - Jagiellonian Universityusers.uj.edu.pl/~ufkapano/git/seminar.pdf · cloud...
Transcript of Git for physicists - Jagiellonian Universityusers.uj.edu.pl/~ufkapano/git/seminar.pdf · cloud...
Git for physi ists
Andrzej Kapanowski
Institute of Physi s, Jagiellonian University, Krakow, Poland
De ember 21, 2015
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 1 / 28
Outline
Outline
1
Motivation
2
Git basi s
3
Lo al repositories
4
Remote repositories
5
Git bran hing
6
Con�i ts
7
Remote bran hes
8
Summary
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 2 / 28
Motivation
Motivation
S ientists need many tools for their work: software for analysing
or visualizing data, sharing �les, ollaborating, writing up papers,
publishing, sear hing the literature. Programming languages are
needed to write unique programs or to glue di�erent systems.
Parallel omputing, loud omputing (PLGrid), big data,
bioinformati s (genomi s data), ...
Software Carpentry Foundation - tea hing resear hers basi
software skills (from 1998).
Publishing sour e ode of s ienti� programs - Nature 467
(2010).
There are many tools to learn - start from the best ones.
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 3 / 28
Git basi s
Getting started
Git is a free and open sour e distributed version ontrol system
designed to handle everything from small to very large proje ts
with speed and e� ien y.
Git home page: http://git-s m. om/.
Version ontrol is a system that re ords hanges to a set of �les
over time so that you an re all spe i� versions later.
Lo al VCS: keeping pat h sets.
Centralized VCS: a single server ontains all the versioned �les.
Distributed VCS: lients fully mirror the repository.
Git is available for all major platforms.
Proto ols: HTTP, FTP, rsyn , Git proto ole.
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 4 / 28
Git basi s
A short history of Git
Linus Torvalds (born in 1969) - the
reator of the Linux kernel.
1991-2002, Linux kernel with
pat hes and ar hived �les.
2002-2005, Linux kernel with
BitKeeper.
2005, Git was born.
Git = "unpleasant person" (British
English slang).
Git thinks about its data like a
stream of snapshots.
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 5 / 28
Git basi s
What to keep in a Git repo?
Short answer: text-based �les.
Sour e ode of programs written in Shell, Python, C/C++, ...
Websites (HTML, XHTML, CSS).
Arti les, reports, books (*.txt, *.tex, *. vs).
Sour e ode of programs for omputer algebra systems
(Maxima, Maple, Mathemati a).
Sour e ode of �gures in gnuplot, Pyxplot, ...
Use Dropbox (2.5 GB) or Google Drive (15 GB) for binaries.
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 6 / 28
Git basi s
First-time Git setup
The ommand line interfa e provides all Git ommands.
$ git --version # he k if Git is installed
$ git onfig --global user.name "Andrzej Kapanowski"
$ git onfig --global user.email
"andrzej.kapanowski�uj.edu.pl"
$ git onfig --list # he king settings
$ git help onfig # getting help
$ git onfig --help
$ man git- onfig
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 7 / 28
Git basi s
Getting a Git repository
Initializing a repo in an existing dire tory (git init).
$ d myproje t
$ git init # .git subdire tory is reated
$ git add -A # add all files to the staging area
$ git ommit -m "First ommit."
Cloning an existing repo (git lone).
$ git lone https://github. om/sympy/sympy.git
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 8 / 28
Lo al repositories
Che king the status of the repo
$ git status
# On bran h master
nothing to ommit (working dire tory lean)
$ vim README # a new file is reated
$ git status
# On bran h master
# Untra ked files:
# (use "git add <file>..." to in lude in what
# will be ommitted)
#
# README
nothing added to ommit but untra ked files present
(use "git add" to tra k)
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 9 / 28
Lo al repositories
Viewing the ommit history
$ git log # note SHA-1 hashes
ommit 48ae450a329 92558038de3b721a029d8eea8 14
Author: Andrzej Kapanowski <andrzej.kapanowski�uj.edu.pl>
Date: Wed De 9 09:54:56 2015 +0100
UnionFind hanged.
ommit 64e 8f8edb9 a891b18d94d906d885f91e67e220
Author: Andrzej Kapanowski <andrzej.kapanowski�uj.edu.pl>
Date: Tue De 8 14:12:12 2015 +0100
Sudoku 6x6 added.
...
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 10 / 28
Lo al repositories
Viewing the ommit history
$ git log --pretty=oneline -4
48ae UnionFind hanged.
64e Sudoku 6x6 added.
6355 Ri h omparisons.
60a6 lek ja10 hanged.
# Repo history:
# ...--60a6--6355--64e --48ae <-- master <-- HEAD
$ git log --grep=lek ja # "lek ja" in omments
$ git log --author=Andrzej
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 11 / 28
Lo al repositories
Viewing the ommit history
$ git bran h # lo al bran hes
* master
$ git he kout 60a6
Note: he king out '60a6'.
You are in 'deta hed HEAD' state.
...
HEAD is now at 60a68aa... lek ja10 hanged.
$ git bran h
* (no bran h)
master
$ git he kout master
...
Swit hed to bran h 'master'
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 12 / 28
Remote repositories
Remote repositories
github. om - over 30.2 million repositories, issue tra king, ode
reviews, syntax highlighted ode, markdown for formatting text,
paid plans. [GitHub Enterprise℄
bitbu ket.org - Atlassian (JIRA, Con�uen e), pull request,
bran h permissions, free private repos for up to 5 users.
[Bitbu ket Server℄
gitlab. om - private repos, ode reviews, issue tra king, wikis.
[GitLab Community Edition and Enterprise Edition℄
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 13 / 28
Remote repositories
Working with remotes
# git remote add [shortname℄ [url℄
$ git remote add origin
https://ufkapano�github. om/ufkapano/myproje t.git
$ git push -u origin master # only first time
$ git remote # show shortnames of remotes
origin
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 14 / 28
Remote repositories
Simple work�ow
$ git pull
# Changing files ...
$ git add -A
$ git ommit -m "Comments."
$ git push
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 15 / 28
Git bran hing
Bran hes in Git
A bran h is a lightweight movable pointer to a ommit.
Creating, deleting, and modifying bran hes is qui k and easy.
The default bran h name is master. This bran h points to the
last ommit we made. It moves forward automati ally after
every ommit.
Swit hing bran hes hanges �les in the working dire tory.
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 16 / 28
Git bran hing
Lo al bran hes
# git bran h [bran h_name℄ [sha1℄
$ git bran h testing
# A--B--C <-- master <-- HEAD
# ^
# |
# testing
$ git he kout testing
Swit hed to bran h 'testing'
# A--B--C <-- master
# ^
# |
# testing <-- HEAD
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 17 / 28
Git bran hing
Fast forward
# A--B--C <-- master
# \
# D--E <-- testing <-- HEAD
$ git he kout master # it hanges files!
Swit hed to bran h 'master'
$ git merge testing
Updating ..eeee
Fast-forward
...
# A--B--C--D--E <-- master <-- HEAD
# ^
# |
# testing
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 18 / 28
Git bran hing
Removing bran hes
# A--B--C <-- master
# \
# D--E <-- testing <-- HEAD
$ git he kout master # it hanges files!
Swit hed to bran h 'master'
$ git bran h -d testing # removing 'testing'
# A--B--C <-- master <-- HEAD
# \
# D--E (dangling ommits)
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 19 / 28
Git bran hing
Merging
# A--B--D <-- testing
# \
# C <-- master <-- HEAD
$ git merge testing # edit omments
Merge made by the 're ursive' strategy.
...
# A--B--D <-- testing
# \ \
# C--E <-- master <-- HEAD
$ git log --pretty=oneline -1
eeee Merge bran h 'testing'
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 20 / 28
Con�i ts
Merging with on�i ts
# A--B--D <-- testing
# \
# C <-- master <-- HEAD
$ git merge testing
Auto-merging a1.txt
CONFLICT ( ontent): Merge onfli t in a1.txt
Automati merge failed;
fix onfli ts and then ommit the result.
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 21 / 28
Con�i ts
Merging with on�i ts
$ at a1.txt
file a1
<<<<<<< HEAD
a1 hanged in master
=======
a1 hanged in testing
>>>>>>> testing
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 22 / 28
Con�i ts
Merging with on�i ts
$ git mergetool
merge tool andidates: meld opendiff kdiff3 tkdiff
xxdiff tortoisemerge gvimdiff diffuse e merge
p4merge araxis b 3 emerge vimdiff
Merging:
a1.txt
Normal merge onfli t for 'a1.txt':
{lo al}: modified file
{remote}: modified file
Hit return to start merge resolution tool (gvimdiff):
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 23 / 28
Con�i ts
Merging with on�i ts
$ git status
# On bran h master
# Changes to be ommitted:
#
# modified: a1.txt
#
# Untra ked files:
# (use "git add <file>..."
# to in lude in what will be ommitted)
#
# a1.txt.orig
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 24 / 28
Con�i ts
Merging with on�i ts
$ rm a1.txt.orig
$ git add -A
$ git ommit -m "Merge bran h testing."
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 25 / 28
Remote bran hes
Remote bran hes
$ git bran h --all # list all bran hes
* master # lo al bran h
remotes/origin/master # remote bran h
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 26 / 28
Remote bran hes
git pull = git fet h + git merge
$ git push
...
! [reje ted℄ master -> master (non-fast-forward)
...
# git fet h [remote_name℄ [bran h_name℄
$ git fet h origin master
$ git merge origin/master # onfli ts are possible ...
$ git add -A
$ git ommit -m "Merge bran h origin/master."
$ git push
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 27 / 28
Summary
Summary
Use Git to handle small and large proje ts like reating software,
writing s ienti� arti les, books, websites.
Use remote repositories to ollaborate with other persons, to
publish your software or data, for ba kups.
Thank you for your attention
A. Kapanowski (JU) Git for physi ists De ember 21, 2015 28 / 28