CESSDA Expert Seminar 13 & 14 September 2016 Prague, Czech Republic - basics Matthäus Zloch GESIS
Outline for this session Git introduction and some theory Git command basics (plus some little advanced) Goals Understand basic concepts of git What is best practise using git
Not part of this presentation Motivation for version control Using git with other UI-Tools**
Git introduction Git (/ɡɪt/) is a version control system that is used for software development and other version control tasks. As a distributed revision control system it is aimed at speed, data integrity, and support for distributed, non-linear workflows. Git was created by Linus Torvalds in 2005 for development of the Linux kernel, with other kernel developers contributing to its initial development. https://en.wikipedia.org/wiki/git_(software)
Git introduction Git (/ɡɪt/) is a version control system that is used for software development and other version control tasks. As a distributed revision control system it is aimed at speed, data integrity, and support for distributed, non-linear workflows. Git was created by Linus Torvalds in 2005 for development of the Linux kernel, with other kernel developers contributing to its initial development. https://en.wikipedia.org/wiki/git_(software)
Strengths of Git Don t need a server (in the beginning) Operations are local, just sync between remote server You ve got the whole history Every commit, every tag, every branch All repository information is stored in one directory:.git/
Strengths of Git Don t need a server (in the beginning) Operations are local, just sync between remote server You ve got the whole history Every commit, every tag, every branch All repository information is stored in one directory You need a server, to collaborate Allows for following code development workflows
Git Introduction Git is primarily a command line tool UI Tools available execute Git commands in background e.g. Sourcetree, Atlassian https://www.atlassian.com/software/sourcetree IDE s provide a UI
Git Introduction Git is primarily a command line tool UI Tools available execute Git commands in background e.g. Sourcetree, Atlassian https://www.atlassian.com/software/sourcetree IDE s provide a UI Need to understand git "under the hood to profit from UI Tools!
Git hosted https://git.kernel.org/cgit/git/git.git/ Git bash https://git-scm.com/downloads
Git Internal Workflow
Git Internal Workflow 3 levels* Work Working Directory initialize Staging / Index Git Repo / HEAD Stage stage a change Commit commit changes * aka trees
Git Internal Workflow with Remote Working Directory Staging / Index Git Repo / HEAD Git Repo / HEAD 3 + 1 levels Work initialize Stage stage a change Commit commit changes Add remote sync with remote Sync with remote
Git Internal Workflow with Remote Working Directory Staging / Index Git Repo / HEAD Git Repo / HEAD 3 + 1 levels Work checkout project (clone) Stage stage a change Commit commit changes Sync with remote sync with remote
Git Internal Workflow Put local directory under version control $ cd my-project/ $ git init Checkout a remote repository $ cd my-project/ $ git clone username@host:/path/to/repository
Git Internal Workflow Stage local changes $ git add file1.php file2.php $ git add *.php $ git add.
Git Internal Workflow Check status $ git status
Git Internal Workflow Commit some changes (comment in editor) $ git commit With inline comment $ git commit --m "first commit"
Git Internal Workflow Each commit is unique and identified via an SHA1 hash value $ git commit --m "first commit" Hashes can be shortened by the first 7 places
Git Remote Repositories Work in collaboration with others Provide a home for your project Remote Server
Git Remote Repositories Think of remote repositories as adding some frontend services and features for your code Remote Server
Git Remote Repositories They provide features like code analysis automatic building, continuous integration issue tracking, documentation, wiki etc. More important a social platform discuss code pull-/merge-requests
Git Remote Repositories Connect new project with remote repository $ git remote add origin <address> Remote Server https://bitbucket.org https://github.com/ https://any-other.com/
Git Remote Repositories Connect new project with remote repository $ git remote add origin <address> Clone an existing project $ git clone https://mazlo@bitbucket.org/cessda/cessda.guidelines.api.git $ git clone git@bitbucket.org:cessda/cessda.guidelines.api.git
Git Remote Repositories Connect new project with remote repository $ git remote add origin <address> Clone an existing project $ git clone https://mazlo@bitbucket.org/cessda/cessda.guidelines.api.git $ git clone git@bitbucket.org:cessda/cessda.guidelines.api.git Sync local with remote $ git push origin master $ git push github master
Git Remote Repositories Sync remote with local $ git fetch origin $ git merge origin/master In short $ git pull
Git Remote Repositories Can clone/push via HTTPS or SSH (preferred) Provides a secure connection Authenticate against a remote repository
Git Log
Git Log With clone you ve got the whole project history Each commit and 1 st sentence of message per line $ git log --online
Git Log All commits: Matthäus, last 2 weeks, keyword: fix $ git log --author="matthäus" --before="2 weeks ago" --grep="fix"
Git Log Advanced view with all branches $ git log --graph --all --decorate
Git Branching Branches are used to write code which does not affect other code Features Releases Bug fixes Documentation Think of branches as labels or pointers to a specific commit
Git Branching master You ve initialized a repo and done some commits t
Git Branching master Now you create a branch and switch to it (checkout) i-got-it t
Git Branching master On the current branch, you do two more commit i-got-it Only this pointer is updated, master is left behind t
Git Branching master i-got-it You create one more branch, but do not switch i-got-it t
Git Branching master i-got-it On the current branch, you do two more commits you-name-it t
Git Branching Create a branch $ git branch you-name-it List all branches $ git branch
Git Branching Change to another branch $ git checkout you-name-it Create a branch and switch to it $ git checkout b i-got-it $ git status
Git Branching Some Fact Hash: sometimes called a Revision master i-got-it HEAD: think of it as the current branch; May point to a specific Revision you-name-it First comment has no parent A commit may have more than one parent
Git Branching Some Fact master i-got-it you-name-it A commit may have more than one parent?
Git Merging
Git Merging Merging means Join the code, local or remote Update of pointers
Git Merging Merging means Join the code, local or remote Update of pointers Most simple merge is called a "fast-forward"
Git Merging
Git Merging master i-got-it i-got-it you-name-it $ git checkout i-got-it $ git merge you-name-it
Git Merging master i-got-it i-got-it General rule: first checkout the branch (base), then pull-in/merge-in the feature you-name-it $ git checkout i-got-it $ git merge you-name-it
Git Merging i-got-it master you-name-it $ git checkout i-got-it $ git merge you-name-it
Git Merging master i-got-it This one is called a merge-commit you-name-it
Git Merging See merges in the log $ git log --graph --all --decorate
Git Merging master i-got-it you-name-it
Git Merging - Conflicts Can happen, will happen Most often when state of a file is unclear Happens when you merge code Code on local branches git merge Code on local with remote branches git pull
Git Merging Ugly i-got-it master Changes made in the same file, same line you-name-it $ git checkout i-got-it $ git merge you-name-it
Git Merging Ugly Things to do 1. Do not panic 2. Check status to see which files need a resolution $ git status
Git Merging Ugly Things to do 3. Do not panic 4. See the log to see what changes are coming in $ git log..you-name-it
Git Merging Ugly Things to do 3. Resolve conflicts in IDE $ vim file1.php 4. Commit changes $ git commit am "resolve merge conflicts..."
How to prevent merge conflicts Isolate tasks for developers Watch out for line-endings Provide a global settings file for your IDE Provide global git configuration Use clear commit messages Is the change necessary? Fix? Workaround? Check-in as own git project ;) Merge often/asap Do not have long living feature-branches Update your current branch frequently
Git Reset Going back in time
Git Reset Going back in time 3 levels 3 types of reset Working Directory Staging / Index Git Repo / HEAD
Git Reset Going back in time 3 levels 3 types of reset With git reset you can bring a file back to a previous version Working Directory Staging / Index Git Repo / HEAD
Git Reset Going back in time 3 levels 3 types of reset With git reset you can bring a file back to a previous version bring a file back into staging area Working Directory Staging / Index Git Repo / HEAD
Git Reset Going back in time 3 levels 3 types of reset With git reset you can bring a file back to a previous version bring a file back into staging area bring a file back into the working state Working Directory Staging / Index Git Repo / HEAD
Git Reset --hard master i-got-it you-name-it
Git Reset --hard master i-got-it you-name-it $ git checkout you-name-it
Git Reset --hard master i-got-it you-name-it $ git checkout you-name-it $ git reset --hard i-got-it $ git reset --hard abe761f you-name-it
Git Reset --hard master i-got-it you-name-it $ git checkout you-name-it $ git reset --hard i-got-it $ git reset --hard abe761f you-name-it $ git checkout i-got-it
Git Reset --hard master you-name-it i-got-it $ git checkout you-name-it $ git reset --hard i-got-it $ git reset --hard abe761f you-name-it $ git checkout i-got-it $ git reset --hard 87e22a3 $ git reset --hard 87e22a3 you-name-it
Git Reset --soft Go back: Staged state $ git reset --soft dfaa708 Working Directory Staging / Index Git Repo / HEAD $ git status
Git Reset --soft Go back: Staged state $ git reset --soft dfaa708 Working Directory Staging / Index Git Repo / HEAD $ git status Useful when you ve done a commit, but forgot something to add
Git Reset --mixed (default) Go back: Working state $ git add file2.php Working Directory Staging / Index Git Repo / HEAD $ git reset --mixed dfaa708 $ git status
Git Reset Going back in time Warning Please watch out with git reset, you can actually lose you code! git reset is the only way you can really lose code in git
Other useful commands Temporarily ignore changes (no need to commit) $ git stash $ git stash pop Create a patch and send someone via email $ git format-patch master --stdout > fix-news-section.patch $ git appy --check fix-news-section.patch
Other useful commands Check what changes have been made $ git diff $ git diff --cached
Best practises using git
Best Practises Using Git Commit often to see clear history of changes
Best Practises Using Git Commit often to see clear history of changes Commit isolated, logical units of changes One file, but two independent changes $ git add -p $ git commit -m "news -> add buttongroup" $ git add -p $ git commit -m "news, fix -> wrap spans"
Best Practises Using Git A commit message is short and in present tense Suggestion "<context>, <action> -> <short description>", e.g. "news section, fix -> sort by id" A more advanced commit should contain a short title in present tense and a more detailed message below
Best Practises Using Git Create branches (several times a day) Do not hesitate to throw away branches (do not CTRL+Z to revert)
Best Practises Using Git Create branches (several times a day) Do not hesitate to throw away branches (do not CTRL+Z to revert) Do not hesitate to socialise on coding platforms Comment Create issues Discuss code, organise code
Best Practises Using Git Use only one tool to interact with your repository e.g. Sourcetree, Atlassian, https://www.atlassian.com/software/sourcetree Use command completion helpers https://github.com/git/git/blob/master/contrib/completion/gitcompletion.bash
Best Practises Using Git Keep practising!
$ cat $HOME/.gitconfig Thank you
Attributes and Links Git logo by Jason Long http://git-scm.com/downloads/logos, CC BY 3.0, https://commons.wikimedia.org/w/index.php?curid=19329352 Pro Git (free book, also for proginners ) https://www.git-tower.com/learn/git/ebook/en/commandline/advanced-topics/git-flow Git Flow https://www.git-tower.com/learn/git/ebook/en/commandline/advanced-topics/git-flow
in your circle of trust. website: www.cessda.net / twitter: @CESSDA_Data