Git

Transcription

GitDistributed Version Control cloud/http://githowto.com/09/23/16CS360Pacific Universityhttps://github.com/git/git1

Goal of Version Control Other options besides Git:–09/23/16CVS, Subversion, Bazaar, BitKeeper, Team Foundation Server,ClearCase, Mercurial (hg)CS360Pacific University3

History Allow multiple people to work on the same software easily Allow a single user to track all his/her changes Developed for use with the Linux Kernel– move away from proprietary BitKeeperModeled after Linux Kernel work flow–branches–distributed–data assuranceMix of local and remote repositoriesLet's first look at using the command line then we'll look at GitHub.09/23/16CS360Pacific Universityhttp://git-scm.com/about4

d-Git-Distributed-Workflows09/23/16CS360Pacific University5

d-Git-Distributed-Workflows09/23/16CS360Pacific University6

d-Git-Distributed-Workflows09/23/16CS360Pacific University7

Documentation http://git-scm.com/docs/–link to GitHub cheat sheet (PDF - 2 pages)–videos–free book (Pro Other-Environments-Gitin-Bash 09/23/16integrate git support into bashCS360Pacific University8

Setup Open a terminal– terminatorGo to Documentsscript -a --timing GitIntro.tm GitIntro.txtgit config --global core.editor "nano"# script will terminate when you type exit! Open two more terminals, don’t start script!09/23/16–double click punetid@linux, change names to One, Two, Three–make sure One is running script!CS360Pacific University9

Typical Workflow - Single User––git init builds the repository (.git directory) the repository is in your local working directorycreate .gitignore list types of files to not put into version control––Create files! Do work!–git add [filenames] ––add the files you just created to the index for staginggit commit -m “commit message” 09/23/16any file that is generated: *.obj, *.o, *.class, *.pycactually commit changes to the repositorygit logCS360Pacific Universityhttp://git-scm.com/docs10

Typical Workflow - Single User09/23/16One–mkdir MyCoolProject.git; cd MyCoolProject.git–git init–ls -al–geany .gitignore *.o test–create test.c from the next slide–gcc -c -o test.o test.c–gcc -o test test.o–git add test.c–git commit -m “initial add of test.c”–git logCS360Pacific Universityhttp://git-scm.com/docs11

test.c#include stdio.h int main(){printf(“HELLO”);return 0;}09/23/16CS360Pacific University12

Typical Workflow - Single User09/23/16–edit test.c to include printf(“-BYE\n”);–gcc -c -o test.o test.c–gcc -o test test.o–git add .–git status–git commit -m “updated test.c to say BYE”–git logCS360Pacific UniversityOnehttp://git-scm.com/docs13

I need to revert! gs git log --name-status git diff commit hash test.c git checkout commit hash test.c edit test.c git add . git commit git on-using-git/373848#373848* two dashes precede a command line option of more than on character09/23/16CS360Pacific University14

Branches Master–Main line of development–Often this is always kept buildableBranches–Initially a copy of Master (not always )–Used to build a feature–Used to fix a bug–Not necessarily always buildable–Not necessarily public –09/23/16Maybe local to a developerThis is where your organization’s culture comes into play.CS360Pacific University15

Typical Workflow Single user – bug fix! (or maybe feature add)git branchgit checkoutcbug1827c commitccYou might merge master intobug1827 if the changes inmaster are relevant to bug1827but you are not done with bug1827.Merges are non-destructive. Bothbranches continue to exist andcan be used/edited/committed/branched.git s/merging-vs-rebasing/09/23/16CS360Pacific Universityhttp://git-scm.com/docs/git-merge16

Typical Workflow Single user – bug fix! (or maybe feature add)–git branch bug1827 –create a branch to contain all the work for the bug fixgit checkout bug1827 start using that branch–Do work (add/commit)–git checkout masterto work on master again.git branchgit checkoutcbug1827–git merge --no-ff bug1827 ––c commitccreplay the commits on bug1827 into mastergit mergegit loggit branch -d bug1827cmaster09/23/16CS360Pacific Universityhttp://git-scm.com/docs/git-merge17

Typical Workflow OneSingle user – bug fix! (or maybe feature add)–git branch bug1827–git checkout bug1827–Add printf(“CS360\n”); to test.c–git add .–git commit -m “added CS360 line”–git log–git checkout master–cat test.c–git merge --no-ff bug1827–git log–git status–git branch–git branch -d bug182709/23/16git branchgit checkoutcbug1827c commitccgit mergecCS360Pacific 8

Typical Workflow OneSingle user – bug fix! (or maybe feature add)–git log --graph–git blame test.c–add printf(“Come back later”);–cat test.c–git stash–cat test.c–git stash list–git stash show–git stash apply–cat test.c–git add .–git commit -m “committed stashed line”–git log09/23/16git branchgit checkoutcbug1827c commitccgit mergecmasterCS360Pacific Universityhttp://git-scm.com/docs/git-merge19

Typical Workflow OneSingle user – bug fix! (or maybe feature add)–git branch MERGE CONFLICT–git checkout MERGE CONFLICT–add ! to “come back later”–git add .–git commit -m “added bang”–git checkout master–add to “come back later”–git add .–git commit -m “added angles”–git merge MERGE CONFLICT–edit test.c and choose which lines to keep–git add .–git commit -m “fixed merge conflict”09/23/16CS360Pacific Universitygit branchgit checkoutcbug1827c commitccgit mergecmasterhttp://git-scm.com/docs/git-merge20

Typical Workflow OneSingle user – bug fix! (or maybe feature add)–git log --graph–git blame test.c–git branch -d MERGE CONFLICT–git log --graphgit branchgit checkoutcbug1827c commitccgit mergecmaster09/23/16CS360Pacific Universityhttp://git-scm.com/docs/git-merge21

More commands http://git-scm.com/docs–git stash–git status––– what files have uncommitted changes? show the commits and log messagesgit loggit diff show the differences between local and committed files build a patch you can email to someone elsegit apply –git blame –who last changed each line of a file?git bisect 09/23/16apply a patch to your working directorytry to determine when a property of your project changedCS360Pacific University22

Workflow - Group odel/ 09/23/16CS360Pacific University23

TypicalWorkflowgroupGroup of developersrepository–someone else: git init --bare --shared–git clone address –pull down code and setup originmastergit remote -vgit branch bug1138 only a local branch is created–git checkout bug1138–do work–git add files / git commit–git checkout master–git merge --no-ff bug1138–git push origin master–git branch -d bug113809/23/16at addressoriginbug1138CS360Pacific Universityyour localrepositorymaster24

TwoTypicalWorkflowgroupGroup of developersrepositoryat address–setup the remote repos–ssh zeus.cs.pacificu.edu–mkdir gitTest.git; cd gitTest.git–git initmaster--bare --sharedoriginbug1138your localrepository09/23/16CS360Pacific Universitymaster25

Typical Workflow - group OneGroup of developers–cd /Documents–git st.git–cd gitTest–git remote -v–create test.c–git add .–git commit -m “initial commit”–git push origin master 09/23/16in terminal Two: git logCS360Pacific University26

Typical Workflow - group –git branch bug1138–git checkout bug1138–git branch in terminal Two: git branch–edit test.c / git add files / git commit–git log in terminal Two: git log–git checkout master–git merge --no-ff bug1138–git branch–git push origin master –09/23/16Onein terminal Two: get log --graph ; cat test.cgit branch -d bug1138CS360Pacific University27

Typical Workflow - groupgit fetchgit log .origin/mastergit checkout origin/mastergit checkout mastergit merge origin/masterGet changes fromorigin(remote repos)ORgit pull09/23/16–git pull performs lots of magic–hard to fix things when magic fails.CS360Pacific University28

Typical Workflow - group Three–mkdir /Documents/Fetch; cd /Documents/Fetch–git st.git–cd gitTest–git remote -v–cat test.c–git checkout -b FetchMe–edit test.c / git add files / git commit -m “fetch”–git log–git checkout master–git merge --no-ff FetchMe–git branch–git push origin master–git branch -d FetchMe09/23/16CS360Pacific University29

Typical Workflow - group –ls -al–cat test.c–git fetch–git log .origin/master–git checkout origin/master–git checkout master–git merge origin/master–edit test.c/add /commit -m “again”–git push origin master in terminal Three:–––09/23/16Onegit pullcat test.cgit logCS360Pacific University30

Conflict edit/add/commit/push edit/add/commit/push (ERROR)fetch/merge (ERROR)edit file (resolve conflict)add/commit/push HEADBUY BYE origin/masterfetch/merge09/23/16CS360Pacific University31

Tag git tag -a MarkTwo -m “this is a tag” git push --follow-tags git log --decorate fullOneA tag is just one type of ref.A ref is an alias for a particular commit.Other types of refs are branch names and remotes.Branches are easy in Git since they are just analias for a particular commit. fs-and-the-reflog/09/23/16CS360Pacific University32

Pull Request git checkout -b Request-Pull edit test.c git add . git commit -m "email" git checkout master git merge --no-ff Request-Pull git push origin master Onegit request-pull Test.git master09/23/16CS360Pacific University33

Cherry Pick git cherry-pick commit-hash pick a commit from one branch and reapply thatone commit to another branch–09/23/16rather than merge all the commits on the branch atthe current locationCS360Pacific University34

Rebase Move a set of commits from one starting point to another–“integrate changes from one branch to another”–“The golden rule of git rebase is to never use it on publicbranches.”–“potentially catastrophic” for collaborationAutomaticvs ccc372

Typical Workflow - GitHub Individual–Create repository at GitHub –git clone git@github.com:USER/REPOS.git 09/23/16setup .gitignore and license.pull down code and setup origin–git checkout -b bug1138–do work–git add files–git commit –git checkout master–git merge --no-ff bug1138–git push origin masterCS360Pacific University36

Group of developersTypical Workflow - GitHub–someone else creates a repository on GitHub (OTHERUSER)–on your GitHub account, fork the repository–git clone git@github.com:USER/repos.git–git remote add git–git checkout -b bug1138–do work/add/commit–git push origin bug1138 push to YOUR GitHub repository–On GitHub, issue a Pull request!–git fetch upstream–git merge upstream/master09/23/16CS360Pacific University37

Last time .repository at// public or masterzeus/home/YOU/gitTest.git1. git init --baremaster2. git clone4. git cloneyour localrepository (#1)your local repository(#2)3. git push5. git pushmaster09/23/166. git fetchCS360Pacific Universitymaster38

Forks and branches Correct usage: I used stingHorrible mess: I did not use branches tManager-Example-CCS360Pacific University39

repository atgithub.com/GROUP/REPOSmaster09/23/16CS360Pacific University40

repository atgithub.com/GROUP/REPOSrepository cific University41

repository atgithub.com/GROUP/REPOSrepository atgithub.com/USER/REPOSoriginmastermastergit clone git@github.com:USER/REPOS.gityour localrepository09/23/16CS360Pacific Universitymaster42

repository atgithub.com/GROUP/REPOSrepository it remote add upstream git@github.com:GROUP/REPOS.gityour localrepository09/23/16CS360Pacific Universitymaster43

repository atgithub.com/GROUP/REPOSrepository it branchgit checkout# do workgit addgit commit09/23/16CS360Pacific Universityyour localrepositorybug1138master44

repository atgithub.com/GROUP/REPOSrepository streambug1138git push origin bug1138your localrepository09/23/16CS360Pacific Universitymaster45

repository atgithub.com/GROUP/REPOSrepository masteruse web browser to merge(if no conflicts)bug1138your localrepository09/23/16CS360Pacific Universitymaster46

repository atgithub.com/GROUP/REPOSrepository use web browser to merge(if no conflicts)ORgit clone # to group owner’s local machinegit checkout -b pullreq bug1138git pull github.com/USER/REPOS bug1138# fix merge conflictgit add/git commitgit checkout masteryour localgit merge --no-ff pullreq bug1138repositorygit push origin master09/23/16masterCS360Pacific Universitybug1138master47

repository atgithub.com/GROUP/REPOSrepository masterbug1138git fetch upstream mastergit merge upstream/mastergit branch -d bug1138your localrepository09/23/16CS360Pacific Universitymaster48

repository at github.com/USER/REPOSrepository atgithub.com/GROUP/REPOSgit branch -d bug1138bug1138upstreamoriginmastermastergit push origin masteryour localrepository09/23/16CS360Pacific Universitymaster49

Forks and branches Correct usage: I used stingHorrible mess: I did not use branches tManager-Example-CCS360Pacific University50

209/23/16CS360Pacific University51

Providers GitHub–free ( ), proprietary (not open)–Go get a GitHub account and email me your usernameGitLab09/23/16–open b.com/gitlab-org/gitlab-ce/CS360Pacific University52

CI usingtravis-ci/09/23/16CS360Pacific University53

Practice: DUE: Monday, noon Build a new local git repository in /Documents/CS360Practice PUNetID Add the file test.c that prints Hello World Add the file test.txt that states “test.c prints Hello World” Add a .gitignore file that ignores *.o files and theexecutable named test Commit all the files. Tag this revision as “INIT COMMIT” Make a branch for a feature add: FA Name09/23/16–add code to test.c that prints your name–update test.txt to document your change–commit all the files to the branchCS360Pacific University54

Practice Switch back to master and make a branch for a feature add:FA Git–add code to test.c that prints I love git–update test.txt to document your change–commit al lthe files to the branch Switch back to master Merge the FA Name branch to master Tag this commit as ADDED NAME Merge the FA Git branch to master–resolve any merge conflicts! Tag this commit as ADDED GITLOVE Don’t delete the branches09/23/16CS360Pacific University55

Submit your practice! git log --decorate full --name-status punetid git log.txt cd . tar czf CS360 Git PUNetID.tar.gz CS360Practice PUNetID scp CS360 Git PUNetID.tar.gz YOU@zeus.cs.pacificu.edu: ssh YOU@zeus.cs.pacificu.edu submit cs360f16 CS360 Git PUNetID.tar.gz Leave clear, useful commit messages. You can work on–your own machine–a lab machine–ssh to zeus.cs.pacificu.edu 09/23/16note: zeus likely does not have a Documents folder!CS360Pacific University56

commit 2a50c7dffddffb8c59356945bf77f881f78f4145 (HEAD - refs/heads/master,tag: refs/tags/ADDED GITLOVE)Merge: 68099dd 8bce123fixed merge conflictscommit 68099dd6347be782f7af2c40c6fbe6bb64b3d32a (tag: refs/tags/ADDED NAME)Merge: f75bfa4 cb0828dMerge branch 'FA NAME'commit 8bce1233f6f9be1673a2ec840dcdd7a518a20633 (refs/heads/FA Git)added I love gitMMtest.ctest.txtcommit cb0828d1e773c40c81171666517c1402e03488e0 (refs/heads/FA NAME)prints my nameMMtest.ctest.txtcommit f75bfa4892eb8fc8dc534c71ccbd931a75c6bbeb (tag: refs/tags/INIT COMMIT)initial commitAAA.gitignoretest.ctest.txt

commit 7b78d4ede35747f2b108390c3d5a0c5178fef0b0 (HEAD - refs/heads/master)Merge: eccc9ea 007f0f5ADDED GITLOVEcommit 007f0f548929096e0db791071e642bcd4d976247 (refs/heads/FA git)ADDED GITLOVEMMtest.ctest.txtcommit eccc9ea2246ab2f8abfb890cb99e0c04a17fd54d (refs/heads/FA Name)ADDED NAMEMMtest.ctest.txtcommit 1e63953f391a17b389005de635c53c3263b68a5cINIT COMMITAAA.gitignoretest.ctest.txt

09/23/16 CS360 Pacific University 10 Typical Workflow - Single User – git init builds the repository (.git directory) the repository is in your local working directory – create .gitignore list types of files to not put into version control – any file that is generated: *.obj, *.o, *.class, *.pyc – Create files! Do work! – git