Git 101 Presentation Prepared by: Leonardo Parro Jr.
Git 101• Topics: – Introduction - Types of Version Control System - Git Basics – Git vs SVN – Git Set-up – Basic Git commands – References
Introduction• About Version ControlWhat is version control, and why should you care? Versioncontrol is a system that records changes to a file or set of filesover time so that you can recall specific versions later.
• Types of Version Control Systems1. Local Version Control Systems
• Types of Version Control Systems2. Centralized Version Control SystemsThere is a single central repository and allof the changes that are made to thedocuments are saved in that repository.
• Types of Version Control Systems3. Distributed VersionControl SystemsThere is a peer-to-peer approach that clients cansynchronize with by exchanging patches frompeer to peer. Clients can make changes in therepositories and those changes will be localto them unless they synchronizewith someone else.
• Git Basics- Snapshots, not differencesMost other systems (CVS, SVN, Perforce, etc) store information as a list of file-based changes. These systemskeep information as a set of files and the changes made to each file over time.In Git, it thinks of its data more like set of snapshots of a mini file system. Everytime you commit, it takes apicture of what all your files look like at the moment and stores a reference to that snapshot.
• Git Basics- Every operation is localMost operations in Git only need local files & resources to operate- Git has integrityEverything in Git is check-summed (SHA-1 hash) before it is stored & is referred to by thatchecksum. This is 40-character string composed of hexadecimal characters (0-9 and a-f) andcalculated based on the contents of a file or directory structure.SHA-1 hash ex.: 24b9da6552252987aa493b52f8696cd6d3b00373- The 3 Git StatesGit has three main states that your files can reside in: committed, modified, and staged.Committed means that the data is safely stored in your local database. Modified means that youhave changed the file but have not committed it to your database yet. Staged means that youhave marked a modified file in its current version to go into your next commit snapshot.
• Git Basics- The 3 main sections of a Git project: Git directory, working directory, staging area.The Git directory is where Git stores the metadata and object database for your project.The working directory is a single checkout of one version of the project. These files are pulled outof the compressed database in the Git directory and placed on disk for you to use or modify.The staging area (or Index) is a simple file, generally contained in your Git directory, that storesinformation about what will go into yournext commit.- Git workflow1. You modify files in your working directory.2. You stage the files, adding snapshots of them to your staging area.3. You do a commit, which takes the files as they are in the staging area and stores that snapshotpermanently to your Git directory.
- Git Remote Repositories workflow
• Git vs. SVN- With Git, clients can commit changes to their localized repositories as new revisions while being offline. However, SVN does notprovide this facility as user must be online in order to push to the repository from the working copy.- Git’s complete copy of the data is stored locally in the client’s system so it is extremely fast when compared to SVN.- In Git, data copies are stored locally in clients systems. The number of backups available is the same as the number of users onany repository. With SVN, if there is a data loss in the central repository, it will be gone forever.- Lightweight Branches: Frictionless Context SwitchingGit DVCS is based on the concept of branching. The working directory of a developer is itself a branch. In Git, we can easily viewthe working directories of developers while they are modifying two or more unrelated files at the same time as different branchesstemming from the same common base revision of the project. With SVN, there is almost no concept of branchingEach feature, each idea, each bugfix – you can easily create a new branch quickly, do a few commits on that branch and theneither merge it into your mainline work or throw it away. You don’t have to mess up the mainline just to save your experimentalideas, you don’t have to be online to do it and most importantly, you can context switch almost instantly.- In Git, commits are not sequential.A large number of users can commit or push data to the same repository. If someone wants to push work in a Git repository, thenthere is no need to worry about data lost or immediate merging of others changes.- Git allows its users to have control over the merging of data in synchronized repositories. Merges are always pulled by someoneand nobody can push to commit merges in someone else’s repository.- Git keeps track of contents while SVN keeps record of files. Because Git keeps track of contents, whenever there is even a smallchange in content it tracks it as a separate change. Because of this, the history of a single file in Git is split.- Git will not allow you to checkout a subdirectory. Instead, the user will have to checkout the whole repository. In SVN, checkoutsat subdirectory level are possible.
• Git Set-up (assume remote host is Github)1. Download & install latest version of git: http://git-scm.com/2. For GitHub use, set-up ssh keysReference: http://help.github.com/mac-set-up-git/Check SSH set-up to GitHub: $ ssh -T firstname.lastname@example.org. Configure personal identity (~/.gitconfig)Every Git commit uses this information, and it’s immutably baked into the commits you passaround:$ git config --global user.name "John Doe"$ git config --global user.email email@example.comAdditional note: Using GitHub w/ multiple accounts- http://net.tutsplus.com/tutorials/tools-and-tips/how-to-work-with-github-and-multiple-accounts/4. Set-up global .gitignore file (~/.gitignore)A global .gitignore file can also be used by adding one to your global git config. For example, youmight create the file ~/.gitignore_global and add some rules to it. To add this to your config, rungit config --global core.excludesfile ~/.gitignore_global
• Git Set-up (assume remote host is Github)4. Set-up global .gitignore file (~/.gitignore)Create the file ~/.gitignore_global and add some rules to it. To add this to your config,run git config --global core.excludesfile ~/.gitignore_global5. Creating & Getting a Git RepositoryReference: http://help.github.com/create-a-repo/- Initializing a Repository in an Existing DirectoryIf you’re starting to track an existing project in Git, you need to go to the project’s directory andtype: $ git initThis creates a new subdirectory named .git that contains all of your necessary repository files — aGit repository skeleton. If you want to start version-controlling existing files (as opposed to anempty directory), you should probably begin tracking those files and do an initial commit.$ git add .$ git add README$ git commit -m initial project version
5. Creating & Getting a Git Repository• Cloning an existing repositoryIf you want to get a copy of an existing Git repository - the command you need is git cloneYou clone a repository with git clone [url]. For example, if you want to clone facebook-ios-sdk,you can do so like this:$ git clone https://github.com/facebook/facebook-ios-sdk.git
• Git Basic commands- get the manual page (mangpage) help for any Git commands:$ git help <verb>$ git <verb> --help$ man git-<verb>- create new git repository$ git initcheckout a repository$ git clone /path/to/repositoryAdd & commit- add files to the Index (staging area)$ git add <filename>$ git add *to actually commit the changes use,$ git commit -m Commit messagenote: the file is committed to the HEAD, but not in your remote repository yet.Pushing changes- send changes to your remote repository,$ git push origin <branch>
• Git Basic commands• Branching- Branches are used to develop features isolated from each other. The master branch is the"default" branch when you create a repository. Use other branches for development and mergethem back to the master branch upon completion.create a new branch named "feature_x" and switch to it using$ git checkout -b feature_x- switch to other branches$ git checkout origin <other_branch_name>
• Git Basic commands• Update & merge- update your local repository to the newest commit$ git pull origin <branch_name>note: git pull will fetch & merge remote changes- Merge another branch into your active branch (ex. development)$ git merge <branch_to_merge>-When merging, git would auto-merge changes. Unfortunately, theres some cases youllencounter merge conflicts. Youre responsible to merge those conflicts manually by editing thefiles shown by git.- Preview differences$ git diff <source_branch> <target_branch>
• Git Basic commands• Tagging- create tag for software releases$ git tag 1.0.0 1b2e1d63ffnote: 1b2e1d63ff stands for the first 10 characters of the commit ID you want to reference your tag.- show logs$ git logReplace local changes- revert changes$ git checkout -- <filename>- fetch the latest history from the server$ git fetch origin- save the current state in the clipboard$ git stash… work in other branch..bug fixing.. etc..go back to the saved state$ git stash popMerge conflicts- Run merge conflict resolution tools to resolve merge conflicts$ git mergetool