working with git - conygreneueda.conygre.com/citi/content/infrastructure/git.pdf · $ git push to...
Post on 25-Sep-2020
12 Views
Preview:
TRANSCRIPT
Copyright 2014 TPS Services Ltd
A user experience
Working With GIT
Copyright 2014 TPS Services Ltd
Content● What is source control?
● Why GIT?
● GIT architecture
● Connecting to GIT
● Creating a repository
● Adding and removing files
● Commitment
● Conflicts
● Being available (push/pull/fetch)
● Versions (branching)
● Tagging
● Getting information
Copyright 2014 TPS Services Ltd
What is source control?● Keep track of code changes
● Revert back to working code quickly
● Resolve coding disputes
● Version control
● Central repository of everything code based
● Prevent accidental overwrites
● Enable developers to work on same project
● And/or code simultaneously
Copyright 2014 TPS Services Ltd
Why GIT● Many tools available
– Subversion, Perforce, CVS, RCS
● These require network connectivity
● GIT is distributed
– Each user has own copy of repository
– Can commit changes locally
– No immediate network required
● Merge back to main repository
– Network required
Copyright 2014 TPS Services Ltd
GIT Help● On line help available
http://git-scm.com/docs/
Copyright 2014 TPS Services Ltd
GIT Architecture● Server/client
● Roll your own solutions require
– Ssh and or web server
– File storage
– Authentication mechanism
● Fully distributed
– Any failure of server allows client to copy back
● Data stored like snapshots
– Reference to all your files at that point in time
– .git is the repository
Copyright 2014 TPS Services Ltd
GIT Architecture
REPO
Developers
GIT Server
REPO
REPOREPO
Copyright 2014 TPS Services Ltd
GIT Local
http://git-scm.com/book/en/v2/Getting-Started-About-Version-Control
Copyright 2014 TPS Services Ltd
GIT servers● Github
– Cloud solution
● Windows
– Bonobo Git Server● Requires IIS and MVC4 runtime
● Linux
– Configure sshd
– Configure a web server● Apache
Copyright 2014 TPS Services Ltd
GIT Clients● Store local copies of repository
● Enables developer to work offline
– Local commits stored in history
– Git deals with histories when pushed
– Git deals with conflicts on push
● Windows
– GITBash
– Tortoise GIT requires GITBash
● Linux
– Native git command line client
Copyright 2014 TPS Services Ltd
Command line GIT● Simple and easy to use
● Syntax
git <action> [<args>]
Copyright 2014 TPS Services Ltd
GIT attributes● Enables GIT to remember settings
● Global applies to all your repositories
– Stored in $HOME/.gitconfig
● Local specific to a repository
– Set this type within the desired repository
– GIT_DIR/config
git config [--global] <attribute> <value>
http://git-scm.com/docs/git-config
Copyright 2014 TPS Services Ltd
Setting GIT attributes● Git needs to identify you
– User name
– Email address
● Cache password
git config --global user.name “Steve”
git config --global user.email “steve@example.local”
git config --global credential.helper 'cache --timeout=3600'
Default is 15 minutes if not specified
git config --global diff.external /usr/bin/diff
Copyright 2014 TPS Services Ltd
GIT Config file example
[user]email = steve@example.comname = Steve
[credential]helper = cache –timeout=3600
[diff]External = /usr/bin/diff
● File content
● List attributes
git config --list
Copyright 2014 TPS Services Ltd
Exercise● Add the following global information
– Email = gituser1@localhost
– Username = Example User
● Set the credentials cache to 4 hours
● Which file is this information stored in?
● List the configuration with a git command
Copyright 2014 TPS Services Ltd
Solution● Add the following global information
– Email = gituser1@localhost● git config --global user.email 'gituser1@localhost'
– Username = Example User● git config --global user.name 'Example User'
● Set the credentials cache to 4 hours
– git config --global credential.helper 'cache --timeout=14400'
● Which file is this information stored in?
– $HOME/.gitconfig
● List the configuration with a git command
– git config --list
Copyright 2014 TPS Services Ltd
Connecting to GIT● Requires user account to connect
● Requires hostname or up address
● Requires protocol (ssh or http[s])
● Commands that connect
– init
– push
– clone
– pull
– fetch
Copyright 2014 TPS Services Ltd
GIT URLs● Git allows many connection types
– ssh
– git
– http[s]
– ftp[s]
– rsync
[ssh://][user@]host.xz[:port]/path/to/repo.git/
git://host.xz[:port]/path/to/repo.git/
http[s]://host.xz[:port]/path/to/repo.git/
ftp[s]://host.xz[:port]/path/to/repo.git/
rsync://host.xz/path/to/repo.git/
Copyright 2014 TPS Services Ltd
Creating a repository● Must be done on the server
– Either through ssh or web interface
● With ssh
– Log on to server
– Change to the git repository directory
– Make a new directory● mkdir /git/projects/testone.git
● cd /git/projects/testone.git
– Initialise the repository● git init --bare
● On your workstation
– git clone <repository details> [optional local directory]● e.g. git clone git@myhost.local:projects/testone.git
● e.g git clone http://myhost.local/projects/testone.git
Copyright 2014 TPS Services Ltd
Adding A Repo In GitHub● Log in to your github account
● Select the + to add a repository
Copyright 2014 TPS Services Ltd
GitHub New Repo Form
Copyright 2014 TPS Services Ltd
Cloning● How you connect to a GIT repository
● Makes a local copy of repository
– Allows you to work off line from real repository
– All commits done locally
● Syntax
git clone <repositoryURL> [<local directory>]
Enables multiple copies of repository locally
Use the relevant URL for connection to your repository
Copyright 2014 TPS Services Ltd
Cloning Example● Get first version
● Another copy
git clone git@mygit.local:/project/testone.git
Creates testone directory
git clone git@mygit.local:/project/testone.git anothertest
Creates anothertest directory
Directories created with latest versions at time of clone
http://git-scm.com/docs/git-clone
Copyright 2014 TPS Services Ltd
Exercise● Clone the following repositories
– All repositories are in the directory git
– puppet using web url with host on port 8080
– crm using git
– cadcam using ssh and user gituser
Copyright 2014 TPS Services Ltd
Solution● Clone the following repositories
– All repositories are in the directory git
– puppet using web url with host on port 8080● git clone http://localhost:8080/git/puppet.git
– crm using git● git clone git://localhost/git/crm.git
– cadcam using ssh and user gituser● git clone gituser@localhost/git/cadcam.git
Copyright 2014 TPS Services Ltd
Managing files● GIT is for team work
– You can edit the files as though they were your own
– All members can add, remove and change files
● Operations
– add● Puts files in staging area
– rm● Tell Git you wish to remove a file from repository
– mv● Tell Git you wish to rename a file
git add file1 [file2 [filen]] git add * git add .
git rm file1 [file2 [filen]] git rm \*
git mv source newName
Copyright 2014 TPS Services Ltd
What have I changed?● Use the status command
● A new file added
● Modified file
git status
$ git statusOn branch masterChanges to be committed: (use "git reset HEAD <file>..." to unstage)
new file: README
$ git statusOn branch masterChanges to be committed: (use "git reset HEAD <file>..." to unstage)
modified: README
Copyright 2014 TPS Services Ltd
Nothing changed
$ git statusOn branch masterYour branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
Copyright 2014 TPS Services Ltd
Be committed● You need to be committed
– Changes saved when you commit to the repository
– Allows you to roll back to older versions
$ git commit -m 'J183: Some useful comment'[master 463dc4f] J183: Some useful comment 1 files changed, 2 insertions(+) create mode 100644 README
Short-hand comment with -m
If no -m you will be placed in default text editor to create a comment
Copyright 2014 TPS Services Ltd
By-pass staging● Add and commit in one go
– For existing files only
● Specify file names with commit
● Syntax
$ git commit -m 'updated' README [master c2b1bbf] updated 1 file changed, 1 insertion(+)
git commit -m 'comment' file1 [file2 [filen]]
Copyright 2014 TPS Services Ltd
Conflicts● Same file changed on local repository
– More than 1 person working on same file
– Both commit locally
– First push wins● Subsequent pushes require resolution of conflicts
$ git pushTo git@myhost.local:project.git ! [rejected] master -> master (fetch first)error: failed to push some refs to 'git@myhost.local:project.git'hint: Updates were rejected because the remote contains work that you dohint: not have locally. This is usually caused by another repository pushinghint: to the same ref. You may want to first integrate the remote changeshint: (e.g., 'git pull ...') before pushing again.hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Copyright 2014 TPS Services Ltd
Conflicts (2)● So we pull as we are told
$ git pullremote: Counting objects: 5, done.remote: Total 3 (delta 0), reused 0 (delta 0)Unpacking objects: 100% (3/3), done.From myhost.local:project d1b896f..c2b1bbf master -> origin/masterAuto-merging READMECONFLICT (content): Merge conflict in READMEAutomatic merge failed; fix conflicts and then commit the result.
Copyright 2014 TPS Services Ltd
What are the changes?● git diff
$ git diff READMEdiff --cc READMEindex 4275b69,71b5210..0000000--- a/README+++ b/README@@@ -1,2 -1,2 +1,6 @@@ ladjask++<<<<<<< HEAD +alksdsdjlk++=======+ sakjdaldkakdj++>>>>>>> c2b1bbfc909fac8e0b0eea7a3ff1f156a187d8ba
In central repositoryIn your file
Copyright 2014 TPS Services Ltd
Status of changes
$ git statusOn branch masterYour branch and 'origin/master' have diverged,and have 1 and 1 different commit each, respectively. (use "git pull" to merge the remote branch into yours)
You have unmerged paths. (fix conflicts and run "git commit")
Unmerged paths: (use "git add <file>..." to mark resolution)
both modified: README
no changes added to commit (use "git add" and/or "git commit -a")
Copyright 2014 TPS Services Ltd
Example Merge
$ vi README$ git commit -m 'changed' README$ git push
USER 1
$ vi README$ git commit -m 'changed' README$ git pushTo git@myhost.local:project.git ! [rejected] master -> master (fetch first)….$ git pull$ git mergetool --tool=meld
USER 2
$ git commit -m 'new comment'$ git push
Copyright 2014 TPS Services Ltd
Merge tools for GIT● Linux
– meld
● Windows
– winmerge
● Mac
– Kaleidoscope
– DeltaWalker
– Araxis Merge
– FileMerge
● All platforms
– kdiff3
Copyright 2014 TPS Services Ltd
Cheating merge● A manual change process
– Copy your files that are conflicting to new location
– Remove your local directory
– Clone the repository
– Add your changes to the files
– Commit then push
Copyright 2014 TPS Services Ltd
Availability● Making your changes available to your team
– Push regularly
● Getting your teams changes
– Pull frequently
● Provides an element of high availability
● Commands
– push = place your local changes to central repository
– pull = get updates from central repository into local
– fetch = like pull but no automatic merge
Copyright 2014 TPS Services Ltd
Updating● Fetch
– Pull but no merge
– File remains unchanged
$ git fetchremote: Counting objects: 5, done.remote: Total 3 (delta 0), reused 0 (delta 0)Unpacking objects: 100% (3/3), done.From myhost.local:project dcf1d25..523cd06 master -> origin/master
$ git merge origin/masterUpdating dcf1d25..523cd06Fast-forward README | 1 + 1 file changed, 1 insertion(+)
Copyright 2014 TPS Services Ltd
Checking before merging● Once fetched check difference
● git diff
$ git diff master origin/masterdiff --git a/README b/READMEindex 0995e88..90e0a63 100644--- a/README+++ b/README@@ -2,3 +2,4 @@ ladjask 434444 alksdjask 1111+laksdjlaskdj
Differenced line
From central repo
From local
Copyright 2014 TPS Services Ltd
Git diff new file● GIT fetch has new files
– Use git diff to see
$ git diff master origin/masterdiff --git a/newone b/newonenew file mode 100644index 0000000..fbba1e9--- /dev/null+++ b/newone@@ -0,0 +1 @@+laksdjasjd
New file and datain central repo
No file in local
Copyright 2014 TPS Services Ltd
Multiple file change
$ git diff master origin/masterdiff --git a/README b/READMEindex 90e0a63..9bffd37 100644--- a/README+++ b/README@@ -3,3 +3,4 @@ ladjask alksdjask 1111 laksdjlaskdj+55555diff --git a/newone b/newoneindex fbba1e9..8bd840f 100644--- a/newone+++ b/newone@@ -1 +1,3 @@ laksdjasjd+111112+22222
Copyright 2014 TPS Services Ltd
Magic fetch● Pull does all the magic
– Fetches and merges in one go
– Pull before you start making changes
– Always ensure you are working on the lated
– Pull regularly
– Cannot resolve merge conflicts
$ git pullremote: Counting objects: 7, done.remote: Compressing objects: 100% (3/3), done.remote: Total 4 (delta 0), reused 0 (delta 0)Unpacking objects: 100% (4/4), done.From myhost.local:project 8172e81..3781012 master -> origin/masterUpdating 8172e81..3781012Fast-forward README | 1 + newone | 1 + 2 files changed, 2 insertions(+)
Copyright 2014 TPS Services Ltd
Giving back● Push to let everyone else see your changes
– Must have a commit list first
$ git pushCounting objects: 7, done.Delta compression using up to 8 threads.Compressing objects: 100% (3/3), done.Writing objects: 100% (4/4), 362 bytes | 0 bytes/s, done.Total 4 (delta 0), reused 0 (delta 0)To git@myhost.local:project.git 8172e81..3781012 master -> master
Copyright 2014 TPS Services Ltd
Branching● What is branching?
– Diverge from the main line of work
– A way of keeping different versions● Project wide
● Local user – trying things out
● What can we do with branching?
– Have master as the stable release
– Use branches for● Release versions
● Test, before merging to another branch
● Research or trying something out from other branches
● Keep parallel versions running
– e.g. Support Android 2 on one branch and 4 on another, but sharing original code before branch
http://git-scm.com/book/en/v1/Git-Branching-What-a-Branch-Is
Copyright 2014 TPS Services Ltd
Branching quick view
git checkout testing
Commit on testing branch
New change on master
Copyright 2014 TPS Services Ltd
How To Branch● Basic example
– Git web site has simple example● http://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging
● Creating a branch and make it active
● Creating a new branch
git checkout -b newBranchName
git branch newBranchName
Copyright 2014 TPS Services Ltd
Developing on a branch● Multiple users can develop on the same branch
– Branch must be pushed to the remote repository
– Other users checkout the branch
– Work on branch as normal
● Example
User1 session:$ git checkout -b v2Switched to a new branch 'v2'$ git branch master* v2# Make updates to file called codefile$ git add codefile$ git commit -m 'new version for codefile' codefile# Make your branch available to public$ git push origin v2
User2 session:$ git branch* master$ git checkout v2$ git branch master* v2
Copyright 2014 TPS Services Ltd
File on branches● Example
$ git branch* master v2$ cat codefile# This is version 1 of the file# Created in the master branch of the project$ git checkout v2Switched to branch 'v2'$ cat codefile # This is version 1 of the file# Created in the master branch of the project# This is added on branch v2$ git checkout masterYour branch is up-to-date with 'origin/master'.$ cat codefile# This is version 1 of the file# Created in the master branch of the project
Copyright 2014 TPS Services Ltd
Merging branches● Bring changes into continual development
– Local branch to public version branch
– Version branch to master
# Changes made on codefile in v2 branchgit commit -m 'Merging to master'[v2 fff84dd] Merging to master 1 file changed, 1 insertion(+)$ git push origin v2Counting objects: 7, done.Delta compression using up to 4 threads.Compressing objects: 100% (3/3), done.Writing objects: 100% (3/3), 401 bytes | 0 bytes/s, done.Total 3 (delta 0), reused 0 (delta 0)To git@localhost:project.git e65f543..fff84dd v2 -> v2
$ git checkout masterSwitched to branch 'master'$ git merge v2Updating c6d2ab2..fff84ddFast-forward codefile | 2 ++ 1 file changed, 2 insertions(+)
Copyright 2014 TPS Services Ltd
Merge one file change● Merge only changes in one file from a branch
# Changes made on the v2 branch and pushed$ git checkout masterSwitched to branch 'master'Your branch is up-to-date with 'origin/master'.$ git checkout --patch v2 codefilediff --git b/codefile a/codefileindex cdd53a4..936993f 100644--- b/codefile+++ a/codefile@@ -2,3 +2,4 @@ # Created in the master branch of the project # This is added on branch v2 # A change on v2 which will merge back to master along with the rest+we won't merge this change thoughApply this hunk to index and worktree [y,n,q,a,d,/,e,?]? y
Copyright 2014 TPS Services Ltd
Branching with Puppet● Puppet is a good example of using branches
– Environments● Development
● Test
● QA
● Production
● Each environment a branch
● Changes merged from
– Dev to Test end of sprint
– Test to QA if all unit tests pass
– QA to Prod if integration tests pass
● Great for use with tools like Hudson/Jenkins
● Could also consider tags too
Copyright 2014 TPS Services Ltd
Tagging● 2 types
– Lightweight● Pointer to specific commit
● Tracks tag names only for simple ref
● Great for checking out previous locations by name
– Annotated● Full object store
● Good for marking release points
– Can use tag to pull code into another environment● Continue to develop without affecting tag
http://git-scm.com/book/en/v2/Git-Basics-Tagging
Copyright 2014 TPS Services Ltd
Tag commands● git tag
– List all tags
● git tag tagName
– Create a lightweight tag
● git tag -a tagName -m comment [commitChecksum]
– Create annotated tag
● git show tagName
– Show tag detail
● git push origin tagName
– Make tags public
Copyright 2014 TPS Services Ltd
Example# Code development# commits, merges, etc all made# Code passes all tests# Current sprint signed off and code good$ git tag -a v1.1 -m 'Release Jira1234'$ git push origin v1.1Total 0 (delta 0), reused 0 (delta 0)To git@localhost:project.git * [new tag] v1.1 -> v1.1
$ git show v1.1commit fff84dd4f9de21749f7899e97989f6044d5687f2Author: Steve Shilling <steve@therapypages.com>Date: Sun Nov 9 20:02:17 2014 +0000 Merging to masterdiff --git a/codefile b/codefileindex 636da7f..cdd53a4 100644--- a/codefile+++ b/codefile@@ -1,3 +1,4 @@ # This is version 0.1 of the file # Created in the master branch of the project # This is added on branch v1+# A change on v1 which will merge back to master along with the rest
Copyright 2014 TPS Services Ltd
Cloning Tag Code● Need to clone release only
– e.g. releasing to production
● Don't want any other part of the branch
$ git clone --branch tagname url [localdir]
$ git clone --branch release3 git@localhost:project.git rel3
Copyright 2014 TPS Services Ltd
Tagging Flow
dev1 dev2
Master branch
local local
commit commit
Dev branchpush
Code freeze
git tag -a v0.1 -m 'Release 0.1 ref jira010'
Merge if tests pass and signed off
Prodrepo
git clone --branch v0.1 git@ourgit.com:project.git proj-v0.1
git clone --branch v0.2 git@ourgit.com:project.git proj-v0.2
Tag v0.2
Copyright 2014 TPS Services Ltd
Information Commands● git log
– Shows commit logs
– http://git-scm.com/docs/git-log
● git rev-list
– Lists commit objects in reverse date order
– http://git-scm.com/docs/git-rev-list
● git diff
– Show differences between commits, commits and working tree and others
– http://git-scm.com/docs/git-diff
● git blame
– Show revisions and author line modifications
– http://git-scm.com/docs/git-blame
● git show
– Show different object information
– http://git-scm.com/docs/git-log
Copyright 2014 TPS Services Ltd
References● http://git-scm.com/docs
● http://gnuradio.org/redmine/projects/gnuradio/wiki/DevelopingWithGit
● http://git-scm.com/book/en/v1/Git-Branching-What-a-Branch-Is
●
top related