Collaborative development with Git | Workshop

131
Anuchit Chalothorn [email protected] Collaborative development with Git

description

Collaborative development with Git Workshop

Transcript of Collaborative development with Git | Workshop

Page 1: Collaborative development with Git | Workshop

Anuchit [email protected]

Collaborative development with Git

Page 2: Collaborative development with Git | Workshop

Git Workshop● Overview● Git Basics● Undo Git Changes● Git Branches● Rewriting Git History● Git Remote Repository● Git Workflow

Page 3: Collaborative development with Git | Workshop

OverviewVersion control is a system that records changes to a file or set of files over time so that you can recall specific versions later. A VCS allows you to: ● revert files back to a previous state● revert the entire project back to a previous state● review changes made over time● see who last modified something

Page 4: Collaborative development with Git | Workshop

Local

Page 5: Collaborative development with Git | Workshop

Centralized

Page 6: Collaborative development with Git | Workshop

Distributed

Page 7: Collaborative development with Git | Workshop

History of GitLinux Kernel was use DVCS call BitKeeper, proprietary software but in 2005 the company behind BitKeeper broke-down and tool’s free-of-charge was revoked. The Linux development community develop their own tool.

Page 8: Collaborative development with Git | Workshop

Goal of Git● Speed● Simple design● Strong support for non-linear development (1000+ for

parallel branches)● Fully distributed● Able handle a large project (data and size)

Page 9: Collaborative development with Git | Workshop

Git BasicGit basic tutorial provide the important commands to work with Git

Page 10: Collaborative development with Git | Workshop

Get Git Download git at http://git-scm.com/downloads choose your os version :)

Page 11: Collaborative development with Git | Workshop

Workshop 1 Download Git and install to your machine, after install try to use git command.

Page 12: Collaborative development with Git | Workshop

The git config command lets you configure your Git installation (or an individual repository) from the command line.

Git config

Page 13: Collaborative development with Git | Workshop

Define the author name to be used for all commits in the current repository. Typically, you’ll want to use the --global flag to set configuration options for the current user.

git config --global user.name <name>

Git config user.name

Page 14: Collaborative development with Git | Workshop

Define the author email to be used for all commits by the current user.

git config --global user.email <email>

Git config user.email

Page 15: Collaborative development with Git | Workshop

Define the text editor used by commands like git commit for all users on the current machine. eg; vi

git config --system core.editor <editor>

Git config core.editor

Page 16: Collaborative development with Git | Workshop

Config the line ending for your operating system

git config --global core.autocrlf true

git config --global core.autocrlf input

Git config core.autocrlf

Page 17: Collaborative development with Git | Workshop

Config the line color for git message

git config --global color.ui auto

Git config color.ui

Page 18: Collaborative development with Git | Workshop

Workshop 2 Setup Git config to your profile; name, email, line break, ui auto color and choose your favorite text editor.

Page 19: Collaborative development with Git | Workshop

Git initThe git init command creates a new Git repository.

git init

git init <directory>

Page 20: Collaborative development with Git | Workshop

Git init --bareShared repositories should always be created with the --bare flag

Bare

NonBare

NonBare

NonBare

Page 21: Collaborative development with Git | Workshop

Examplecd /repo

git init --bare my-project.git

my-project.git

Page 22: Collaborative development with Git | Workshop

Workshop 3 Create git repositories call myrepo as working repository and master-proj as shared repository, see what is difference of two repository.

Page 23: Collaborative development with Git | Workshop

Git cloneThe git clone command copies an existing Git repository.

git clone <repo>

git clone <repo> <directory>

Origin Working Copy

Page 24: Collaborative development with Git | Workshop

Collaboration

GitRepo

GitRepo

GitRepo

Repo to Repo Collaboration

SVNRepo

Central Repo to Working Copy Collaboration

Page 25: Collaborative development with Git | Workshop

Examplegit clone ssh://[email protected]/repo/my-project.git

cd my-project

local repoexample.com/repo/my-project.git

Page 26: Collaborative development with Git | Workshop

Workshop 4 create local git repo call master-proj and clone master-proj to myproj and create a blank file called README

Page 27: Collaborative development with Git | Workshop

The git add command adds a change in the working directory to the staging area.

git add <file>

git add <directory>

Git add

Page 28: Collaborative development with Git | Workshop

Examplegit add .git commit

git add hello.phpgit commit

Working Directory

StagedSnapshot

CommittedHistory

Add Commit

Page 29: Collaborative development with Git | Workshop

Workshop 5 Add README to your repository

Page 30: Collaborative development with Git | Workshop

The git commit command commits the staged snapshot to the project history.

git commit -m "<message>"

Git commit

Page 31: Collaborative development with Git | Workshop

git add hello.php

git commit

Example

hello.phphello.php hello.php

Page 32: Collaborative development with Git | Workshop

Workshop 6 Commit all change to your repository, create snapshot to project history.

Page 33: Collaborative development with Git | Workshop

The git status command displays the state of the working directory and the staging area.

git status

Git status

Page 34: Collaborative development with Git | Workshop

Workshop 7 Use git status after add, edit file or commit file.

Page 35: Collaborative development with Git | Workshop

The git log command displays committed snapshots. It lets you list the project history, filter it, and search for specific changes.

Git log

Page 36: Collaborative development with Git | Workshop

git log

git log -n <limit>

git log --oneline

git log --stat

git log --author="<pattern>"

git log --grep="<pattern>"

git log <since>..<until>

Example

Working Directory

StagedSnapshot

CommittedHistory

Git Status Git Log

Page 37: Collaborative development with Git | Workshop

Workshop 8Use git log to see the committed history of your repository.

Page 38: Collaborative development with Git | Workshop

Undo Git ChangesWorking with previous revisions of your software project.

Page 39: Collaborative development with Git | Workshop

The git checkout command serves three distinct functions: checking out files, checking out commits, and checking out branches.

git checkout master

git checkout <commit> <file>

git checkout <commit>

Git checkout

Page 40: Collaborative development with Git | Workshop

git log --oneline

git checkout a1e8fb5

Example

Mastera1e8fb5

Page 41: Collaborative development with Git | Workshop

Examplegit checkout master

git checkout a1e8fb5 hello.py

Working Directory

StagedSnapshot

CommittedHistory

Add Commit

Page 42: Collaborative development with Git | Workshop

Workshop 9Add 2 lines in in readme.txt and commit change to your repository, check revision form git log and try to change to the first state and try to change state back to the latest revision (master)

Page 43: Collaborative development with Git | Workshop

The git revert command undoes a committed snapshot. it the ‘safe’ way to undo.

git revert <commit>

Git revert

Page 44: Collaborative development with Git | Workshop

git commit -am “update readme”

git revert 15df9b6

Example

15df9b6

Page 45: Collaborative development with Git | Workshop

Workshop 10Add 1 lines in in readme.txt and commit change to your repository, check revision form git log and try to revert to previous version by using git revert

Page 46: Collaborative development with Git | Workshop

The git reset command undo changes in dangerous method — it is a permanent undo.

git reset <file>

git reset <commit>

git reset --hard

git reset --hard <commit>

Git reset

Page 47: Collaborative development with Git | Workshop

git commit -am “update readme”

git reset --hard 15df9b6

Example

Page 48: Collaborative development with Git | Workshop

Workshop 11Add 1 lines in in readme.txt and commit change to your repository, check revision form git log and try to revert to previous version by using git reset

Page 49: Collaborative development with Git | Workshop

The git clean command removes untracked files from your working directory.

git clean

git clean -f

git clean -df

git clean -xf

Git clean

Page 50: Collaborative development with Git | Workshop

touch somefile.txt

git reset --hard

git clean -df

Example

Page 51: Collaborative development with Git | Workshop

Workshop 12Create new file call index.php and try to use git clean to delete untracked file.

Page 52: Collaborative development with Git | Workshop

First, we'll take a look at creating branches, which is like requesting a new project history.

Git Branches

Page 53: Collaborative development with Git | Workshop

The git branch command lets you create, list, rename, and delete branches.

git branch

git branch <branch>

Git branch

Page 54: Collaborative development with Git | Workshop

git branch experiment

Example

Master

Experiment

Page 55: Collaborative development with Git | Workshop

Workshop 12Create new branch call issue123 for your current revision. use git branch to show branches.

Page 56: Collaborative development with Git | Workshop

The git checkout command lets you navigate between the branches created by git branch.

git checkout <existing-branch>

git checkout -b <new-branch>

Git checkout

Page 57: Collaborative development with Git | Workshop

git branch experiment

git checkout experiment

Example

Experiment

Master

Page 58: Collaborative development with Git | Workshop

Workshop 13Change to branch issue123 add some file and commit to this branch then change to master to compare.

Page 59: Collaborative development with Git | Workshop

The git merge command lets you take the independent lines of development created by git branch and integrate them into a single branch.

Git merge

Page 60: Collaborative development with Git | Workshop

git merge experiment

git branch -d experiment

Example

Experiment

Master

Experiment

Master

Page 61: Collaborative development with Git | Workshop

Workshop 14Change to branch issue123 edit some file and commit then change to master branch and merge issue123 to master branch then delete issue123 branch.

Page 62: Collaborative development with Git | Workshop

Rewriting Git HistoryGit also designed to give you total control over your development workflow, letting you define exactly what your project history looks like; however, it also creates the potential to lose commits.

Page 63: Collaborative development with Git | Workshop

Git rebaseRebasing is the process of moving a branch to a new base commit.

git rebase <base>

Page 64: Collaborative development with Git | Workshop

Example

master

feature

master

featurefeature

Page 65: Collaborative development with Git | Workshop

Example# Start a new feature

git checkout -b feature master

# Edit files

git commit -a -m "Start developing a feature"

git checkout master

# Edit files

git commit -a -m "Fix security hole"

Page 66: Collaborative development with Git | Workshop

Example# Merge back into master

git checkout feature

git rebase master

# Merge back into master

git checkout master

git merge feature

Page 67: Collaborative development with Git | Workshop

Git rebase -iRunning git rebase with the -i flag begins an interactive rebasing session.

git rebase -i <base>

Page 68: Collaborative development with Git | Workshop

Example# Start a new feature

git checkout -b feature master

# Edit files

git commit -a -m "Start developing a feature"

git checkout master

# Edit files

git commit -a -m "Fix security hole"

Page 69: Collaborative development with Git | Workshop

Example# Merge back into master

git checkout feature

git rebase -i master

# Config merge method in change log

# Merge back into master

git checkout master

git merge feature

Page 70: Collaborative development with Git | Workshop

Workshop 15● Initial git repo

○ add blank readme.txt and index.php● Branch new feature from master call feature then

○ update index.php with some function ● Change branch to master

○ update readme.txt● Change branch to feature

○ merge changes from base● Change branch to master

○ merge feature to master

Page 71: Collaborative development with Git | Workshop

Git reflogGit keeps track of updates to the tip of branches using a mechanism called reflog.

git reflog

git reflog --relative-date

Page 72: Collaborative development with Git | Workshop

Examplegit reflog

12aeb71 HEAD@{0}: merge feature: Fast-forward

df6c71f HEAD@{1}: reset: moving to df6c71f

12aeb71 HEAD@{2}: merge feature: Fast-forward

df6c71f HEAD@{3}: checkout: moving from feature to master

Page 73: Collaborative development with Git | Workshop

Examplegit reflog --relative-date

12aeb71 HEAD@{9 minutes ago}: merge feature: Fast-forward

df6c71f HEAD@{11 minutes ago}: reset: moving to df6c71f

12aeb71 HEAD@{13 minutes ago}: merge feature: Fast-forward

df6c71f HEAD@{14 minutes ago}: checkout: moving from feature to master

Page 74: Collaborative development with Git | Workshop

Workshop 16Use git reflog to see changes and reset to any commit revision.

Page 75: Collaborative development with Git | Workshop

Remote RepositoriesGit’s collaboration model, which gives every developer their own copy of the repository, complete with its own local history and branch structure. Instead of committing a changeset from a working copy to the central repository, Git lets you share entire branches between repositories.

Page 76: Collaborative development with Git | Workshop

Quick Git Server (1)# install ssh server and gitsudo apt-get install openssh-server

sudo apt-get install git

# add git user for all users accesssudo adduser --group sudo git

# then import user’s public key to .ssh/authorized_keyssudo su git

cd

mkdir .ssh

cat id_rsa.john.pub >> .ssh/authorized_keys

Page 77: Collaborative development with Git | Workshop

Quick Git Server (2)# Make git repo dir at /git for easy accesssudo mkdir /git

sudo chown git:git /git

# Make share repo called project.gitsudo su git

cd /git

git init --bare project.git

# Disable git user to ssh to serversudo usermod -s /usr/bin/git-shell git

Page 78: Collaborative development with Git | Workshop

Workshop 17● Setup git server add following users (or your team)

○ John○ Jessie○ Jessica

● Create share project called webapps

Page 79: Collaborative development with Git | Workshop

Git remoteThe git remote command lets you create, view, and delete connections to other repositories.

git remote

git remote add <name> <url>

git remote rm <name>

CentralRepo

John’sRepo

Jess’sRepo

origin

Page 80: Collaborative development with Git | Workshop

Example# on John’s computercd myprojectgit initgit add .git commit -m 'initial commit'git remote add origin git@gitserver:/git/project.gitgit push origin master

Page 81: Collaborative development with Git | Workshop

Example# on John’s computergit clone git@gitserver:/git/project.git myproject

cd myproject

touch README

git add README

git commit -am “add readme”git push origin master

Page 82: Collaborative development with Git | Workshop

Workshop 18● Lets John first commit following files to repository

○ README○ LICENSE○ CONTRIBUTOR

● Another users clone repository to their machine

Page 83: Collaborative development with Git | Workshop

Git fetchThe git fetch command imports commits from a remote repository into your local repo.

git fetch <remote>

git fetch <remote> <branch>

Page 84: Collaborative development with Git | Workshop

Examplecd myprojectgit initgit remote add origin git@gitserver:/git/project.gitgit fetch origin

git checkout master

git merge origin/master

Page 85: Collaborative development with Git | Workshop

Workshop 19● Lets Jessica create feature branch and commit to

repository● Another users fetch feature branch from repository to

their machine

Page 86: Collaborative development with Git | Workshop

Git pullThe git pull command merge upstream changes into your local repository, it’s combined git fetch and git merge in single command.

git pull <remote>

git pull --rebase <remote>

Page 87: Collaborative development with Git | Workshop

Examplecd myprojectgit initgit remote add origin git@gitserver:/git/project.git

git checkout mastergit pull --rebase origin

Page 88: Collaborative development with Git | Workshop

Workshop 20Create working repository in machine and add remote repository and use git pull to merge remote repository to working repository.

Page 89: Collaborative development with Git | Workshop

Git pushThe git pull command merge upstream changes into your local repository, it’s combined git fetch and git merge in single command.

git push

git push <remote>

Page 90: Collaborative development with Git | Workshop

Exampleecho “this is a readme” > README

git commit -am “update readme”

git push origin master

CentralRepo

John’sRepo

Jess’sRepo

origin

Page 91: Collaborative development with Git | Workshop

Workshop 21Create working repository in machine and add remote repository and use git pull to merge remote repository to working repository. Edit some file and push to remote repository

Page 92: Collaborative development with Git | Workshop

Git WorkflowsThe possible workflows can make it hard to know where to begin when implementing Git in the workplace. These workflows are designed to be guidelines rather than concrete rules.

Page 93: Collaborative development with Git | Workshop

Centralized WorkflowThe Centralized Workflow uses a central repository to serve as the single point-of-entry for all changes to the project. Instead of trunk, the default development branch is called master and all changes are committed into this branch.

Page 94: Collaborative development with Git | Workshop

Example# Initialized central repository on servergit init --bare /part/to/repo.git

Page 95: Collaborative development with Git | Workshop

Example# developer clone from repositorygit clone ssh://user@host/path/to/repo.git

Page 96: Collaborative development with Git | Workshop

Example# John done his featuregit status

git add

git commit

git push origin master

Page 97: Collaborative development with Git | Workshop

Example# Jessie try to push her featuregit push origin master

# Git will refuse the request with a rather verbose error message. She needs to pull John’s updates into her repository, integrate them with her local changes, and then try again.

Page 98: Collaborative development with Git | Workshop

Example# Jessie rebase on top of John’s commitgit pull --rebase origin master

Page 99: Collaborative development with Git | Workshop

Example# Jessie and John working on relate feature rebasing process will generate conflictsgit status

git add <somefile>

git rebase --continue

# If you get to this point and you have no idea what’s going on, don’t panic. Just execute following command and you’ll be right back.git rebase --abort

Page 100: Collaborative development with Git | Workshop

Example# Jessie successfully push her featuregit push origin master

Page 101: Collaborative development with Git | Workshop

Workshop 22● Group 2-3 people● Setup your team git repository server.● Use centralized workflow to fix code in sample application. ● Sample application can pull from https://github.

com/anoochit/git-workshop.git

Page 102: Collaborative development with Git | Workshop

Feature Branch Workflowthe Centralized Workflow, adding feature branches to your development process is an easy way to encourage collaboration and streamline communication between developers.

Page 103: Collaborative development with Git | Workshop

Master

Issue #123

Feature

Page 104: Collaborative development with Git | Workshop

Example# clone from repositorygit clone ssh://user@host/path/to/repo.git

Page 105: Collaborative development with Git | Workshop

Example# Jessie branch new featuregit checkout -b jessie-feature master

git status

git add <some-file>

git commit

Page 106: Collaborative development with Git | Workshop

Example# Jessie push her featuregit push -u origin jessie-feature

Page 107: Collaborative development with Git | Workshop

Example# Jessie push her feature to remote repositorygit push

Page 108: Collaborative development with Git | Workshop

Example# John is ready to accept the pull request, someone needs to merge the feature into the stable project (this can be done by either John or Jessie):git checkout master

git pull

git pull origin jessie-feature

git push

Page 109: Collaborative development with Git | Workshop

Workshop 23● Group 2-3 people● Setup your team git repository server.● Use feature branch workflow to fix code in sample application. ● Sample application can pull from https://github.

com/anoochit/git-workshop.git

Page 110: Collaborative development with Git | Workshop

Git Flow WorkflowThe Gitflow Workflow defines a strict branching model designed around the project release. While somewhat more complicated than the Feature Branch Workflow, this provides a robust framework for managing larger projects.

Page 111: Collaborative development with Git | Workshop

Master

Developer

v0.1 v0.2 v0.3 v0.4

Feature

Page 112: Collaborative development with Git | Workshop

Master

Developer

v0.1 v0.2 v0.3 v1.0

Feature

Release

Page 113: Collaborative development with Git | Workshop

Example# Jessie create development branch from mastergit checkout -b develop master

git push -u origin developer

Page 114: Collaborative development with Git | Workshop

Example# John add new featuregit checkout -b feature developer

git status

git add <file>

git commit

Page 115: Collaborative development with Git | Workshop

Example# John ready to merge feature to developmentgit pull origin developer

git checkout developer

git merge feature

git push

git branch -d feature

Page 116: Collaborative development with Git | Workshop

Example# Jessie prepare releasegit checkout -b release-1.0 develop

git checkout master

git merge release-1.0

git tag -a 0.1 -m "Initial public release" master

git push --tags

git branch -d release-1.0

Page 117: Collaborative development with Git | Workshop

Example# Discover some bugs git checkout -b issue-123 master

# Fix some bugsgit checkout master

git merge issue-123

git push

git branch -d issue-123

Page 118: Collaborative development with Git | Workshop

Workshop 24● Group 2-3 people● Setup your team git repository server.● Use git flow workflow to fix code in sample application. ● Sample application can pull from https://github.

com/anoochit/git-workshop.git

Page 119: Collaborative development with Git | Workshop

Forking WorkflowThe Forking Workflow is fundamentally different than the other workflows Instead of using a single server-side repository to act as the “central” codebase, it gives every developer a server-side repository. Each contributor has not one, but two Git repositories: a private local and a public server-side.

Page 120: Collaborative development with Git | Workshop
Page 121: Collaborative development with Git | Workshop

ExampleProject maintainer initialized official repositorygit init --bare /part/to/repo.git

Official

Page 122: Collaborative development with Git | Workshop

ExampleAnother developers clone (fork) official repository.git clone user@gitserver/user/repo.git

OfficialJohn Jessie

Page 123: Collaborative development with Git | Workshop

ExampleDeveloper clone their own repositorygit remote add upstream user@gitserver/user/repo.git

Page 124: Collaborative development with Git | Workshop

ExampleDeveloper work their featuresgit checkout -b feature

# Edit some code

git commit -am “add feature”

Page 125: Collaborative development with Git | Workshop

ExampleDeveloper publish their featuresgit push origin feature

Page 126: Collaborative development with Git | Workshop

ExampleProject maintainer integrate their feature1. Inspect the code2. Pull code in local repository and

manually merge

git fetch user@gitserver/user/repo.git feature

# Inspect code

git checkout master

git merge FETCH_HEAD

Page 127: Collaborative development with Git | Workshop

ExampleDevelopers synchronized with official repository, the main codebase has moved forward, other developers should synchronize with the official repository

git pull upstream master

Page 128: Collaborative development with Git | Workshop

Workshop 25● Group 2-3 people● Setup your own git repository servers.● Use forking workflow to fix code in sample application. ● Sample application can pull from https://github.

com/anoochit/git-workshop.git

Page 129: Collaborative development with Git | Workshop

Workshop 26Discussion in your developer team, choose workflow or mix and match your developer team. Present your idea, workflow to another groups

Page 131: Collaborative development with Git | Workshop