Collaborative development with Git | Workshop
-
Upload
anuchit-chalothorn -
Category
Technology
-
view
618 -
download
0
description
Transcript of Collaborative development with Git | Workshop
Anuchit [email protected]
Collaborative development with Git
Git Workshop● Overview● Git Basics● Undo Git Changes● Git Branches● Rewriting Git History● Git Remote Repository● Git Workflow
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
Local
Centralized
Distributed
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.
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)
Git BasicGit basic tutorial provide the important commands to work with Git
Get Git Download git at http://git-scm.com/downloads choose your os version :)
Workshop 1 Download Git and install to your machine, after install try to use git command.
The git config command lets you configure your Git installation (or an individual repository) from the command line.
Git config
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
Define the author email to be used for all commits by the current user.
git config --global user.email <email>
Git config user.email
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
Config the line ending for your operating system
git config --global core.autocrlf true
git config --global core.autocrlf input
Git config core.autocrlf
Config the line color for git message
git config --global color.ui auto
Git config color.ui
Workshop 2 Setup Git config to your profile; name, email, line break, ui auto color and choose your favorite text editor.
Git initThe git init command creates a new Git repository.
git init
git init <directory>
Git init --bareShared repositories should always be created with the --bare flag
Bare
NonBare
NonBare
NonBare
Examplecd /repo
git init --bare my-project.git
my-project.git
Workshop 3 Create git repositories call myrepo as working repository and master-proj as shared repository, see what is difference of two repository.
Git cloneThe git clone command copies an existing Git repository.
git clone <repo>
git clone <repo> <directory>
Origin Working Copy
Collaboration
GitRepo
GitRepo
GitRepo
Repo to Repo Collaboration
SVNRepo
Central Repo to Working Copy Collaboration
Examplegit clone ssh://[email protected]/repo/my-project.git
cd my-project
local repoexample.com/repo/my-project.git
Workshop 4 create local git repo call master-proj and clone master-proj to myproj and create a blank file called README
The git add command adds a change in the working directory to the staging area.
git add <file>
git add <directory>
Git add
Examplegit add .git commit
git add hello.phpgit commit
Working Directory
StagedSnapshot
CommittedHistory
Add Commit
Workshop 5 Add README to your repository
The git commit command commits the staged snapshot to the project history.
git commit -m "<message>"
Git commit
git add hello.php
git commit
Example
hello.phphello.php hello.php
Workshop 6 Commit all change to your repository, create snapshot to project history.
The git status command displays the state of the working directory and the staging area.
git status
Git status
Workshop 7 Use git status after add, edit file or commit file.
The git log command displays committed snapshots. It lets you list the project history, filter it, and search for specific changes.
Git log
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
Workshop 8Use git log to see the committed history of your repository.
Undo Git ChangesWorking with previous revisions of your software project.
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
git log --oneline
git checkout a1e8fb5
Example
Mastera1e8fb5
Examplegit checkout master
git checkout a1e8fb5 hello.py
Working Directory
StagedSnapshot
CommittedHistory
Add Commit
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)
The git revert command undoes a committed snapshot. it the ‘safe’ way to undo.
git revert <commit>
Git revert
git commit -am “update readme”
git revert 15df9b6
Example
15df9b6
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
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
git commit -am “update readme”
git reset --hard 15df9b6
Example
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
The git clean command removes untracked files from your working directory.
git clean
git clean -f
git clean -df
git clean -xf
Git clean
touch somefile.txt
git reset --hard
git clean -df
Example
Workshop 12Create new file call index.php and try to use git clean to delete untracked file.
First, we'll take a look at creating branches, which is like requesting a new project history.
Git Branches
The git branch command lets you create, list, rename, and delete branches.
git branch
git branch <branch>
Git branch
git branch experiment
Example
Master
Experiment
Workshop 12Create new branch call issue123 for your current revision. use git branch to show branches.
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
git branch experiment
git checkout experiment
Example
Experiment
Master
Workshop 13Change to branch issue123 add some file and commit to this branch then change to master to compare.
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
git merge experiment
git branch -d experiment
Example
Experiment
Master
Experiment
Master
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.
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.
Git rebaseRebasing is the process of moving a branch to a new base commit.
git rebase <base>
Example
master
feature
master
featurefeature
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"
Example# Merge back into master
git checkout feature
git rebase master
# Merge back into master
git checkout master
git merge feature
Git rebase -iRunning git rebase with the -i flag begins an interactive rebasing session.
git rebase -i <base>
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"
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
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
Git reflogGit keeps track of updates to the tip of branches using a mechanism called reflog.
git reflog
git reflog --relative-date
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
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
Workshop 16Use git reflog to see changes and reset to any commit revision.
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.
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
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
Workshop 17● Setup git server add following users (or your team)
○ John○ Jessie○ Jessica
● Create share project called webapps
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
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
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
Workshop 18● Lets John first commit following files to repository
○ README○ LICENSE○ CONTRIBUTOR
● Another users clone repository to their machine
Git fetchThe git fetch command imports commits from a remote repository into your local repo.
git fetch <remote>
git fetch <remote> <branch>
Examplecd myprojectgit initgit remote add origin git@gitserver:/git/project.gitgit fetch origin
git checkout master
git merge origin/master
Workshop 19● Lets Jessica create feature branch and commit to
repository● Another users fetch feature branch from repository to
their machine
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>
Examplecd myprojectgit initgit remote add origin git@gitserver:/git/project.git
git checkout mastergit pull --rebase origin
Workshop 20Create working repository in machine and add remote repository and use git pull to merge remote repository to working repository.
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>
Exampleecho “this is a readme” > README
git commit -am “update readme”
git push origin master
CentralRepo
John’sRepo
Jess’sRepo
origin
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
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.
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.
Example# Initialized central repository on servergit init --bare /part/to/repo.git
Example# developer clone from repositorygit clone ssh://user@host/path/to/repo.git
Example# John done his featuregit status
git add
git commit
git push origin master
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.
Example# Jessie rebase on top of John’s commitgit pull --rebase origin master
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
Example# Jessie successfully push her featuregit push origin master
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
Feature Branch Workflowthe Centralized Workflow, adding feature branches to your development process is an easy way to encourage collaboration and streamline communication between developers.
Master
Issue #123
Feature
Example# clone from repositorygit clone ssh://user@host/path/to/repo.git
Example# Jessie branch new featuregit checkout -b jessie-feature master
git status
git add <some-file>
git commit
Example# Jessie push her featuregit push -u origin jessie-feature
Example# Jessie push her feature to remote repositorygit push
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
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
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.
Master
Developer
v0.1 v0.2 v0.3 v0.4
Feature
Master
Developer
v0.1 v0.2 v0.3 v1.0
Feature
Release
Example# Jessie create development branch from mastergit checkout -b develop master
git push -u origin developer
Example# John add new featuregit checkout -b feature developer
git status
git add <file>
git commit
Example# John ready to merge feature to developmentgit pull origin developer
git checkout developer
git merge feature
git push
git branch -d feature
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
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
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
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.
ExampleProject maintainer initialized official repositorygit init --bare /part/to/repo.git
Official
ExampleAnother developers clone (fork) official repository.git clone user@gitserver/user/repo.git
OfficialJohn Jessie
ExampleDeveloper clone their own repositorygit remote add upstream user@gitserver/user/repo.git
ExampleDeveloper work their featuresgit checkout -b feature
# Edit some code
git commit -am “add feature”
ExampleDeveloper publish their featuresgit push origin feature
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
ExampleDevelopers synchronized with official repository, the main codebase has moved forward, other developers should synchronize with the official repository
git pull upstream master
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
Workshop 26Discussion in your developer team, choose workflow or mix and match your developer team. Present your idea, workflow to another groups
Future study
● Git GUI Client ● Git & GitHub Foundations Training ● Git Documentation● Git Tutorial● Pro Git Book