Best Practices. Joaquim Rocha IT-DSS-TD

Similar documents
Common Git Commands. Git Crash Course. Teon Banek April 7, Teon Banek (TakeLab) Common Git Commands TakeLab 1 / 18

git commit --amend git rebase <base> git reflog git checkout -b Create and check out a new branch named <branch>. Drop the -b

Creating a Patch. Created by Carl Heymann on 2010 Sep 14 1

Git. A Distributed Version Control System. Carlos García Campos

About SJTUG. SJTU *nix User Group SJTU Joyful Techie User Group

Git. Ľubomír Prda. IT4Innovations.

Version Control System - Git. zswu

Git. all meaningful operations can be expressed in terms of the rebase command. -Linus Torvalds, 2015

Distributed Version Control (with Git)

CESSDA Expert Seminar 13 & 14 September 2016 Prague, Czech Republic

Version Control: Gitting Started

Git Workflows. Sylvain Bouveret, Grégory Mounié, Matthieu Moy

Git Tutorial. André Sailer. ILD Technical Meeting April 24, 2017 CERN-EP-LCD. ILD Technical Meeting, Apr 24, 2017 A. Sailer: Git Tutorial 1/36

git Version: 2.0b Merge combines trees, and checks out the result Pull does a fetch, then a merge If you only can remember one command:

GUIDE TO MAKE A REAL CONTRIBUTION TO AN OPEN SOURCE PROJECT 1. 1

What is version control? (discuss) Who has used version control? Favorite VCS? Uses of version control (read)

Why? Clean Model Branches Local reflog Flows Doc Ex. Advanced use of Git. Matthieu Moy

Introduction, Instructions and Conventions

Index. Alias syntax, 31 Author and commit attributes, 334

Being More Productive with Git

Why? Clean Model Branches Local reflog Flows Doc Ex. Advanced use of Git. Matthieu Moy

a handful of Git workflows for the agilist steven harman twitter: stevenharman

Advanced Git. Luc Sarzyniec. Xilopix, February / 88

What is git? Distributed Version Control System (VCS); Created by Linus Torvalds, to help with Linux development;

AIS Grid School 2015

Gitting things done. Hands-on introduction to git niceties. Jan Urbański Ducksboard

Fundamentals of Git 1

VCS VERSION CONTROL SYSTEMS

Git Like You Mean it. Alan Ott SCaLE 16x March 8-11, 2018

GIT Princípy tvorby softvéru, FMFI UK Jana Kostičová,

Versioning with git. Moritz August Git/Bash/Python-Course for MPE. Moritz August Versioning with Git

Outline. Introduction to Version Control Systems Origins of Git Git terminology & concepts Basic Git commands Branches in Git Git over the network

GIT CHEAT SHEET. CONFIG --global => write to ~/.gitconfig, otherwise to.git/config. git config [--global] -l git config [--global] -e

Git for (not-so) dummies

Topics covered. Introduction to Git Git workflows Git key concepts Hands on session Branching models. Git 2

Algorithm Engineering

Git. (Why not CVS?... because Git.) Karel Zak Florian Festi Bart Trojanowski December 20, 2007

Beyond git add/commit/push

CS 520: VCS and Git. Intermediate Topics Ben Kushigian

Tutorial: Getting Started with Git. Introduction to version control Benefits of using Git Basic commands Workflow

E, F. deleteall command, 352 directory structure, 350 export_data method, 353 inline_data method, 353 print_export method, 351 target directory, 351

Outline The three W s Overview of gits structure Using git Final stuff. Git. A fast distributed revision control system

Git. Presenter: Haotao (Eric) Lai Contact:

I m an egotistical bastard, and I name all my projects after myself. First Linux, now git. Linus Torvalds, creator of Linux and Git

Working in Teams CS 520 Theory and Practice of Software Engineering Fall 2018

Git. A fast distributed revision control system. Nils Moschüring PhD Student (LMU)

Git - A brief overview

Introduction to Supercomputing

August 22, New Views on your History with git replace. Christian Couder

Basics of Git GitHub

Git for Newbies. ComMouse Dongyue Studio

Git, the magical version control

git-flow Documentation

CS 390 Software Engineering Lecture 5 More Git

Outline. Version Control System (VCS) basics Git terminology & concepts Basic Git commands Branches in Git Git over the network (time permitting)

Version Control for Fun and Profit

Version Control with Git

Git: (Distributed) Version Control

Introduction to distributed version control with git

Revision control Advanced git

Revision Control. An Introduction Using Git 1/15

Submitting your Work using GIT

How to be a git. Dominic Mitchell

Git version control with Eclipse (EGit) Tutorial

Revision control. INF5750/ Lecture 2 (Part I)

Outline. Introduction to Version Control Systems Origins of Git Git terminology & concepts Basic Git commands Branches in Git Git over the network

RSARTE Git Integration

git the SCM system Jan-Simon Möller training.linuxfoundation.org

February 2 nd Jean Parpaillon

Git: Distributed Version Control

A Practical Introduction to Version Control Systems

Laboratorio di Programmazione. Prof. Marco Bertini

Git Introduction CS 400. February 11, 2018

GitHub for Developers Training Manual. -v1.0

Working with GIT. Florido Paganelli Lund University MNXB Florido Paganelli MNXB Working with git 1/47

Introduction to Git. Database Systems DataLab, CS, NTHU Spring, 2018

Introduction to GIT. Jordi Blasco 14 Oct 2011

2 Initialize a git repository on your machine, add a README file, commit and push

Source Code Management wih git

USING GIT FOR AUTOMATION AND COLLABORATION JUSTIN ELLIOTT - MATT HANSEN PENN STATE UNIVERSITY

GETTING TO KNOW GIT: PART II JUSTIN ELLIOTT PENN STATE UNIVERSITY

CSE 391 Lecture 9. Version control with Git

GIT. CS 490MT/5555, Spring 2017, Yongjie Zheng

An introduction to git

Software Project (Lecture 4): Git & Github

CPSC 491. Lecture 19 & 20: Source Code Version Control. VCS = Version Control Software SCM = Source Code Management

A quick (and maybe practical) guide to Git and version control. By Jay Johnson

Git: Distributed Version Control

Pragmatic Guide to Git

Version Control. Version Control

Software Revision Control for MASS. Git Installation / Configuration / Use

Maintaining an Out-of-Tree Driver and an Upstream Driver Simultaneously (with minimal pain)

GIT for companies Mark Struberg, INSO TU Vienna

Git. It is easy to shoot your foot off with git, but also easy to revert to a previous foot and merge it with your current leg.

License. Introduction to Version Control with Git. Local Version Control Systems. Why Use Version Control?

Version Control. Version Control

תוכנית יומית לכנס התכנסות וארוחת בוקר

Version Control with Git

Contribute To Linux Mainline

Agenda. Several projects are using GIT Developer(s) Junio Hamano, Linus Torvalds. Qt Stable release (January 31, 2011)

Transcription:

Best Practices joaquim.rocha@cern.ch IT-DSS-TD January 24, 2014

About yours truly Contributor to/author of many Open Source projects Member of the GNOME Foundation Former member of Igalia and Red Hat Currently at the Data Storage Services group at CERN http://www.joaquimrocha.com

I m an egotistical bastard, so I name all my projects after myself. First Linux, now git" Linus Torvalds

What s git Distributed Version Control System (DVCS) Initially written in C by Linus Torvalds Replacement for BitKeeper in Linux kernel development Widely used nowadays, both for new and old projects

What s git

It s the sandbox

Holds the new changes to be committed

Points to the current commit

Working Tree with Changes

Index with Changes

Typical beginners workflow with git: 1 git clone URL or git init. 2 Do stuff... 3 git add --all or git add FILE 4 git commit -m Update" [or another bad description] 5 git push

Best Practices

Adding Files

Adding Files Do NOT: git add * git add. git add --all git add -u.... and unless you re adding a new (unknown to index) file: git add FILE_PATH

Adding Files Please Do: git add --patch

Adding Files git add --patch or git add -p asks you which chunks of code should be added and how! means you review your code before adding it! helps you create atomic commits!

Adding Files: Example [...] if (delegateauthlibpath) loaddelegateauthlib(delegateauthlibpath); + + fprintf(stderr, "WHY THE HELL DOESN T THIS WORK!!"); } AuthChangeFsUid::~AuthChangeFsUid() Stage this hunk \[y,n,q,a,d,/,j,j,g,e,?\]?

Committing

Committing Do atomic commits! Write useful, descriptive commit messages! Don t be afraid of committing (you ll see why later)!

Atomic Commits Sherlock was investigating why the new version of his team s software didn t work...

Atomic Commits He quickly looked at his favorite git UI (which uses git log --oneline log) for clues... 1d60cd2 Update for 1.5.0 817ee03 Innocent stuff... 8341828 Innocent stuff #1... afee627 Innocent stuff #2... d4c29b7 Innocent stuff #3... c3bdb6d Innocent stuff #4... a3e8c35 Innocent stuff #5... fd9f14b Innocent stuff #6... da8a2cf Innocent stuff #7... c2debc0 Innocent stuff #8... 283293c Innocent stuff #9......

Atomic Commits Not finding anything suspicious in the log, Sherlock starts a bisect in order to find the problem! With all the compiling and testing, he spends hours in the bisect before finding the culprit: 1d60cd2 Update for 1.5.0

Atomic Commits Sherlock finds out the commit has more to it than meets the eye... commit 1d60cd23c6597f1d11288644691b582bd531e704 Author: Moriarty <moriarty.indeed@evil-r-us.co.uk> Date: Thu Jun 6 18:06:06 2013 +0100 Update for 1.5.0 Evil stuff because I can! More evil stuff because I can!...

Always write atomic commits!

Write useful commit messages

Write useful commit messages A good commit message: Imperative tense summary, <= 50 chars When necessary, more details can come here, until 72 chars each line. A reference to a bug tracker issue can be added as well: http://bugtracker.earth/issue/1985

Write useful commit messages Bad fixes a crash Awful fix Good Fix crash when performing update The issue was being caused when the Updater was called and a network connection that had been used before is no longer available. https://sherlocksbugtracker.co.uk/issue/1985

Know How to Use References

Know How to Use References Specific References: The current commit: HEAD A hash from some commit, e.g.: d3a7c852d2c789f791b11091894cc71387e562e9 Also works with just a prefix: d3a7c85 A branch, e.g.: master, newfeature A tag, e.g.: release0.9

Know How to Use References Relative References: Referring to previous commits: Commit 1 position before: ref~1 or ref^ Commit 5 position before: ref~5 or ref^^^^^ E.g. a branch s parent commit: newfeature^ Referring to different parents: 1st parent of a commit: ref^1 2nd parent of a commit: ref^2 Nth parent of a commit: ref^n

Use git bisect to track issues

Use git bisect to track issues git bisect helps finding a commit that introduced a bug by making a binary search $ git bisect start $ git bisect bad $ git bisect good HEAD~10... [check until finding the culprit] $ git bisect reset... [fix it]

git reset

git reset Want to undo adding a file (after git add FILE_PATH)? git reset FILE_PATH The file is now in the Working Tree (only) again Index moved back

git reset Want to undo the previous commit? git reset HEADˆ The changes are now only in the Working Tree (you have to add them again before committing) HEAD and Index moved back one step

git reset Want to undo the previous commit but keep it ready to commit? git reset -soft HEADˆ The changes are now in the Index (ready to be committed) HEAD moved back one step

git reset Want to delete the previous commit? git reset -hard HEADˆ It s as if the previous commit didn t happen HEAD, Index and Working Tree move back one step

git reflog

git reflog Sometimes things don t go as expected! E.g.:... $ git commit -m "Very important commit"... $ git reset --hard HEAD^ $ git log --oneline e897f7f Other things... 61be62b Other things #1...... $ NOOOOOOOOOOOOO bash: NOOOOOOOOOOOOO: command not found...

git reflog Luckily, git reflog comes to rescue! $ git reflog e897f7f HEAD@{0}: reset: moving to HEAD^ ca33ef2 HEAD@{1}: commit: Very important commit e897f7f HEAD@{2}: checkout: moving from master to importantfeature... $ git rebase ca33ef2 $ git log --oneline ca33ef2 Very important commit e897f7f Other things... 61be62b Other things #1......

Branches

Branches A branch is simply a pointer to a commit! (unlike other VCS which copied directories...)

Branches Create a new branch: git branch <new-branch> [<start-point>] Delete a branch: git branch -d -D <branch> Rename a branch: git branch -m <old-name> <new-name> List branches: git branch [-r -a] Move into (checkout) a branch: git checkout <branch> Create a branch and check it out: git checkout -b <branch>

Rebase a branch

Merge two branches

Branches Pro tip: Use one branch per feature/bug (contained development) Only merge with master after you re done Remember to rebase your feature branch before merging it to master Specify the origin and branch when pushing (might avoid mistakes) A good use of branches should prevent the need of git cherry-pick

Branches Proposed workflow:... [we re on master] $ git checkout -b newfeature... [make changes] $ git commit -m "New feature" $ git rebase master $ git checkout master $ git merge newfeature $ git push origin master If push fails: $ git pull --rebase

Working with Remote Branches List remote branches: $ git branch -r remotes/origin/head -> origin/master remotes/origin/master remotes/origin/newfeature remotes/origin/newfeature2 remotes/origin/newfeature3

Branches Push a branch to origin: $ git checkout newfeature $ git push origin newfeature

Branches Delete a remote branch in origin: $ git push origin :newfeature

Branches Replace a remote branch in origin: $ git push origin +otherfeature:newfeature It s not advisable to replace branches you share with other people (it breaks other people s branch)

git rebase --interactive

git rebase --interactive git rebase --interactive is a great tool that allows to: Meld commits together Remove commits Edit commits (it stops the rebase and allows to amend a commit) Reorder commits

git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for release 0.9.2 pick ca33ef2 Commit that should have been in the release # Rebase 8d9b5a1..ca33ef2 onto 8d9b5a1 # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit

git rebase --interactive pick d3a7c85 New changes squash b538761 Other changes pick 61be62b Some nice new changes pick ca33ef2 Commit that should have been in the release pick e897f7f Update for release 0.9.2 # Rebase 8d9b5a1..ca33ef2 onto 8d9b5a1 # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit

Name your stash

Name your stash git stash is great for storing changes away temporarily Use git stash save Description of changes" so it s easy to keep track of what s what!

Use repos for different modules

Use repos for different modules Sometimes modules shouldn t be in the same repo A base + plugins project should live in different repos e.g.: GStreamer

Do NOT add every single file

Do NOT add every single file Binary files, distro packages (rpms, debs) and other auto-generated files should not be included in the repo! Upload them somewhere else, a repo should be for source code.

Don t break others repos

Don t break others repos Make sure that you do not change history in main branches (shared with other people)! Be careful when resetting and rebasing Do not force a push to a shared branch

Pull and rebase

Pull and rebase Use git pull --rebase in order to avoid merges from upstream commits.

Other (Pro) Tips

Use git hooks to enforce standards Git hooks can be used to e.g.: make sure the source honors a certain coding style force a commit message to include a bug tracker reference etc.

git clean Be careful with git clean! git clean -xdf will really delete things, no reflog, no nothing! Always dry-run first! $ git clean -nxdf

Use command aliases and auto-completion Use git aliases and auto-completion for extra productivity: git config --global alias.ci commit git config --global alias.co checkout git config --global alias.st status Auto-completion: http://code-worrier.com/blog/autocomplete-git/

Show your HEAD in shell s prompt Change your shell s prompt to show your git branch: http://code-worrier.com/blog/git-branch-in-bash-prompt/ [17:42][~/presentations/git-best-practices(master)]$

Do NOT copy/paste diffs Create patches by using git format-patch Patches of the last 2 commits: $ git format-patch HEAD~2 0001-Some-nice-changes.patch 0002-Some-other-nice-changes.patch You can even send them by email from git using git send-email!

See what your tree looks like Having a global picture of your tree is important! Use git lola: git config --global alias.lola "log --graph --decorate --pretty=oneline --abbrev-commit --all"... or use tig for an interactive CLI viewer... or use or a graphical tool like gitg

Thank you! Git s logo, CC by Jason Long Linus Torvalds picture, CC by The Linux Foundation Kid picture, CC by Juhan Sonin Monkey picture, CC by Scott Monty Slides and diagrams based in Git: The Stupid Content Tracker by Mario Sánchez Prada