CS 5220 Advanced Topics in Web Programming Version
CS 5220 Advanced Topics in Web Programming Version Control with Git Chengyu Sun California State University, Los Angeles
Roadmap Basic version control concepts and operations Tag, branch, merge, and rebase Remote repositories and collaboration
The Process of Application Development Initial coding Prototype Add more features Release Version 1. 0 New feature development and bug fixes Release Version 2. 0 …
Problems During Development Initial coding Prototype Add more features New feature has broken existing code. Release Version 1. 0 New feature development and bug fixes Release Version 2. 0 … How do we find out which part of the code has been changed? How do we revert back to the previous version?
Problems During Development Initial coding Prototype Add more features Release Version 1. 0 New feature development and bug fixes Customer 1 requests a feature or bug fix. Release Version 2. 0 … Can we give the customer the development version with the bug fix? Do we still have the Version 1. 0 code?
Problems During Development Initial coding Prototype Where do we put in the bug fix? Add more features n Release Version 1. 0 New feature development and bug fixes n Customer 2 requests a feature or bug fix. Release Version 2. 0 … n Version 1. 0 + customer 1’s feature / bug fix Development version
Problems During Development Initial coding Prototype Add more features Release Version 1. 0 New feature development and bug fixes Customer n requests a feature or bug fix. Release Version 2. 0 … How many different versions do we need to maintain?
Problems During Development Initial coding Prototype Add more features Release Version 1. 0 New feature development and bug fixes Release Version 2. 0 … How do we collect all the bug fixes into one release? Put all changes into an intermediate release. Release Version 1. 1
Version Control Systems CVS n n Used to be the most popular / well known / widely used open source version control system Obsolete due to some inherent system limitations Open Source n Git, Subversion, Mercurial … Commercial n Team Foundation Server, Clear. Case, …
Why Git? Popularity Git. Hub
A Little History … 1991 – Linus Torvalds started the development of Linux 2001 – Linux Kernel 2. 5 Release n n n More than 3 million lines of code Hundreds of developers from all over the world Individual use of version control (mostly CVS)
… A Little History 2002 – Bit. Keeper adopted as the version control system for Linux kernel n n Good system for large, distributed software development Proprietary software and restrictive license terms 2005 n n End of the use of Bit. Keeper for Linux kernel development Linus Torvalds created Git (there is no going back to CVS!)
Using Git Command line: git GUI: IDE integration in Eclipse, Intelli. J IDEA, Android Studio, Visual Studio …
Version Control A Project Unmanaged Copy /project file 1 file 2 … Working Copy /project /special folder used by VCS file 1 file 2 … Repository /project file 1 file 2 file 1 /project … file 2 file 1 … file 2 … Versions (Revisions)
Git Init Run git in the project folder n n n Creates a repository for the project (under /. git) Makes the current project folder a working copy (also using /. git) No files are under version control yet
Share Project in Eclipse Right click on the project and select Team Share Project … n n Performs git init for an unmanaged project, or Makes Eclipse aware of the repository for a project that’s already under version control
What Files Should Be Under Version Control? The minimal set of files that needed to build the project Examples n n n Build file, e. g. pom. xml? ? Source code, e. g. java files? ? Generated files, e. g. class files? ? Graphic assets, e. g. images and icons? ? Libraries, e. g. jar files? ? Eclipse project files: . project, . classpath, . settings
The Files That Should Not Be Under Version Control. gitignore /target/ /test-output/ /. project /. classpath /. settings/ See https: //git-scm. com/docs/gitignore for more gitignore patterns.
Commit – Save Changes to Repository Working Copy /project file 1 file 2 [Modified] file 3 [Added] …. commit /project file 1 file 2 file 1 /project … file 2 file 1 … file 2 … Versions (Revisions)
The Git Index A. K. A. The staging area or cache Technically Git only commits files added to the index (i. e. staged) n Command line option and GUI can bypass this intermediate step Working Copy /project file 1 file 2 [Modified] file 3 [Added] …. Index add file 2 [Modified] file 3 [Added] Repository commit
A Few Common Version Control Operations status: check which files are added/modified diff: see the changes revert: undo the changes log: see the commit
Roadmap Basic version control concepts and operations Tag, branch, merge, and rebase Remote repositories and collaboration
Recall an Earlier Version of a Project Repository In any VCS, it’s always possible to recall an earlier version of a project In Git, each commit has an unique ID, which is the SHA-1 hash of the repository after the commit 1 c 96400 2996044 6 da 4 b 3 a 2996044 The version after commit 2996044
Tag – Give Some Versions More Memorable Names Repository 1 c 96400 2996044 Tag “version-1. 0” git tag –a version-1. 0 –m “Tag version 1. 0” Annotated tags for very important versions, e. g. releases Lightweight tags for somewhat important versions, e. g. used as reminders for yourself
Branch – Work in Parallel version-1. 0 master c 1 c 2 c 3 v 1 -c 1 -branch c 4 c 5 c 6 Create a branch based on version 1. 0 to added the feature requested by Customer #1
Merge – Copy Changes Between Branches git merge <branch> Copy the changes from <branch> to the current branch
Fast-Forward Merge … master c 1 c 2 c 3 Created branch. A c 4 c 5 branch. A
… Fast-Forward Merge git merge branch. A c 1 c 2 c 3 master c 4 c 5 branch. A
Three-Way Merge … master c 1 c 2 c 5 “Hello World” c 4 “Hello CS 5220” c 3 git merge branch. A Should we keep “Hello World” or “Hello CS 5220” after the merge? ? branch. A
… Three-Way Merge master “Hello CS 5220” c 1 c 2 c 5 “Hello World” c 4 “Hello CS 5220” c 3 git merge branch. A Should we keep “Hello World” or “Hello CS 5220” after the merge? ? branch. A
Merge Conflict master “Goodbye” c 1 c 2 c 5 “Hello World” c 4 “Hello CS 5220” c 3 branch. A
Manually Resolve Merge Conflict Manually edit the file Add the edited file to Git index to indicate the conflict is resolved
After Merge master c 5 c 1 c 2 c 3 c 4 branch. A c 6 is called a “merge commit’
Rebase … master c 5 c 1 c 2 c 3 branch. A c 4 git rebase master c 6
… Rebase Takes the changes made in c 4 and apply them to c 5 to create c 6 Rewrite history to make it looks like branch. A started from c 5 instead of c 3
About Rebase c 6 has the same content whether it’s from a merge or a rebase Rebase makes for a cleaner history Think of rebase as a merged performed by the person in charge of branch. A instead of the person in charge of master There are some other trickier usage of rebase because of its ability to rewrite history
Roadmap Basic version control concepts and operations Tag, branch, merge, and rebase Remote repositories and collaboration
Centralized VCS vs. Git Centralized VCS Git Repository Working Copy Repository ? ? Working Copy
Git Remote Setup (I) Remote Repository Local Repository Working Copy Working copy does not interact with remote repository directly.
Git Remote Setup (II) Remote Repository Local Repository Working Copy A local repository can interact with many remote repositories.
Create A Remote Repository Create a remote repository on Git. Hub Push the local repository to Git. Hub git remote add origin <url> Add a remote repository called “origin” at the <url> git push -u origin master • Push the local master branch to the master branch in the remote repository called origin • Set up the local master branch as a local tracking branch of the remote tracking branch origin/master
Branch Terminology Local Repository Remote Repository c 1 local tracking branch master remote tracking branch c 2 origin/master c 2 master remote branch foo-branch local non-tracking branch c 3 A “tracking branch” simply tells Git which branch the changes should be pushed to (or fetch/pull from).
Alternative Branch Terminology Local Repository c 1 tracking branch master remote branch c 2 foo-branch local branch Remote Repository origin/master c 3 c 2 master branch in the remote repository
Understand Push – In The Beginning Local Repository c 1 master c 2 Remote Repository c 1 origin/master c 2 master
Understand Push – After A Local Commit Local Repository c 1 c 2 master c 3 Remote Repository c 1 origin/master c 2 master
Understand Push – After Push Local Repository master Remote Repository c 1 c 2 c 3 origin/master c 3 master
Fetch – Somebody Else Pushed A Commit to Remote Local Repository master Remote Repository c 1 c 2 c 3 origin/master c 3 c 4 master
Fetch – After Fetch Local Repository master Remote Repository c 1 c 2 c 3 c 4 origin/master c 4 master
Pull = Fetch + Merge Local Repository master Remote Repository c 1 c 2 c 3 c 4 origin/master c 4 master
Git Clone Create a local repository from a remote repository Git Clone in Eclipse n n n Use Git Perspective The default repository location is under $USER/git (i. e. outside Eclipse workspace) Need to perform a project import after the repository is created
Git. Hub Fork Somebody else’s Account Git. Hub Repository Your Account Fork Repository Clone Local Repository
Collaboration Models Different ways a team of developers collaborate on a project n n Centralized De-centralized
Centralized Model Remote Repository Local Repository Working Copy Developer (Team Leader) Developer
About Centralized Model … Each developer work on their own branches A team leader is in charge of the important branches (e. g. master) Changes can be copied between branches using merge/rebase Similar to using a centralized VCS
… About Centralized Model All developers need write access to the same repository n Repository “collaborators” on Git. Hub
De-Centralized Model Git. Hub Pull Requests Official Repository Remote Repository Local Repository Working Copy Developer (Team Leader) Developer
About De-Centralized Model Each developer work with their own remote repository A team leader maintains the “official” project repository Changes can be exchanged using Git remote operations or Git. Hub Pull Requests The more “git way” of collaboration
Git. Hub Pull Request … Git. Hub Pull Request Official Repository (Upstream Repository) Remote Repository (Forked Repository) fetch Local Repository push pull Local Repository merge Working Copy Developer (Team Leader) commit Working Copy Developer
… Git. Hub Pull Request … A request made by developer A to developer B n n Requesting some changes made by A to be merged into B’s repository Include two branches and some description of the changes w A branch in A’s repository (where the changes are) w A branch in B’s repository (where the changes will be merged to)
… Git. Hub Pull Request Git. Hub support for pull requests n n Email notifications Detection of merge conflicts Easy-to-use web UI for non-conflict merges Instructions for merges with conflicts
Using Multiple Remote Repositories … It’s usually the responsibility of each project member to keep their repository in sync with the official repository (project leaders don’t usually go around sending out pull requests)
… Using Multiple Remote Repositories … git remote add upstream <url> Official Repository (Upstream Repository) Remote Repository (Forked Repository) Local Repository Working Copy Developer (Team Leader) Developer
… Using Multiple Remote Repositories git fetch upstream Official Repository (Upstream Repository) push Local Repository commit Working Copy Developer (Team Leader) Remote Repository (Forked Repository) fetch push Local Repository merge commit Working Copy Developer
Readings Pro Git - https: //git-scm. com/book/en/v 2
- Slides: 64