Introduc)on to Version Control with Git Pradeep Sivakumar, PhD Sr. Computa5onal Specialist Research Compu5ng, NUIT
Contents 1. What is Version Control? 2. Why use Version control? 3. What is Git? 4. Create a repository 5. Branching and Merging 6. Sharing Work 7. Inspec5ng Changes with Diffs 8. References
What is a Version Control System (VCS)? Database to track the history of a file or a collec5on of files Snapshot can save a version of a project at any 5me. Gives the ability to revert to an earlier version VCS can display differences from the previous version
Why use a Version Control System (VCS)? Collabora5on Storing versions properly Restoring previous versions Backup Understanding/Tracking changes
Version Control System Reference: vogella.com
What is Git? Distributed version control system. Each person has a full copy (clone) of the repository. Every clone has the same func5onality. Reference: vogella.com
The Basic Workflow Repository: Database where VCS stores all versions and metadata local : resides on a local computer as a.git folder remote: resides on a remote server. common base to merge changes. Commit Permanently add files to Git repository Creates a new snapshot
Installa)on Windows h\p://msysgit.github.io/ Mac OS h\ps://code.google.com/p/git- osx- installer/downloads/ list?can=3 Linux yum install git (Fedora, Red Hat) sudo apt-get install git (Ubuntu, Debian)
Configure Git Setup user sedngs $ git config --global user.name "John Doe $ git config --global user.email john@doe.org $ git config --global color.ui auto
How to create a local repository Local repo is created by either cloning a remote repo or ini5alizing an exis5ng project. Star5ng with an unversioned project $ git init Making the first commit $ git add -A $ git commit -m Initial Commit
Cloning a remote project Star5ng with an exis5ng project on a server Clone an open project to make a complete copy to local disk. (h\p, h\ps, SSH) $ git clone psi391@quest.it.northwestern.edu:/home/psi391/hpl-2.1/.git $ git clone https://github.com/gittower/git-crash-course.git SSH, h\ps protocols require a password Each clone is a complete copy with permission to change, create, delete, move, copy, or rename files
Working on a project Working tree Collec5on of files in a repository. If a working tree is modified, the changes need to be added to Git. Staging area Place to store changes in the working tree before the commit Reference: vogella.com
CommiEng changes to Git Get an overview of changes to the project: git status Provides a (verbose) summary of what will be commi\ed Update what will be commi\ed git add/rm <file> Commidng your work git commit -m comment Display project history git log
Best Prac)ces Never commit a half finished project For temporary changes, Git s Stash feature is handy Do not commit files from different projects to the same version Test your changes before commidng and finally, good commen5ng (descrip5ve message for changes) makes a big difference
Branches Created from exis5ng project in Git Each branch (full copy) can be modified independently of its predecessor and other branches One of Git s most powerful features Used in developmental workflows: new features, bug fixes, tes5ng, development, and produc5on.
Working with Branches Create a new branch $ git branch testing Checkout branch and go to the new branch $ git checkout -b contact-form The HEAD pointer points to the current working directory Reference: git- tower.com
Merging Changes Checkout the branch that should receive changes (Target) Merge with the name of the branch with the desired changes. $ git checkout master $ git merge branch1 Merge conflicts: If the same part of a file was modified in different ways, Git won t be able to combine changes.
Sharing work Git allows users to sync local repo with remote repo. A remote repo that doesn t have a working directory is called a bare repo Reference: git- tower.com
Remote Repositories Connec5ng to a remote repo $ git remote add <remote-connection> <remote-repo> Connec5on establishes a rela5onship, but doesn t exchange data Update informa5on about specified remote connec5on $ git fetch <remote-connection> Create a local branch based on the remote branch pointer $ git checkout --track <remote-connection/branch> Upload all new commits from current local HEAD branch to its remote counterpart $ git push <remote-connection> <remote-connection/branch>
Integra)ng Remote Changes To obtain a log of remote changes, inspect them $ git fetch origin $ git log origin/master Integrate these changes into the working copy $ git pull
Diffs - Inspec)ng Changes To see the difference between two branches $ git diff master local_branch To see which author modified a file on a per line basis $ git blame -L 1,2 [filename] To see which changes have been staged $ git diff --cached
Reading Diffs Reference: git- tower.com
Advantages of using Git Granularity In a shared workflow, there is a dis5nc5on between making a change and publishing it Makes offline development possible Data redundancy and replica5on Mul5ple copies on mul5ple machines when compared to a centralized version control system Proper5es such as ignore, stash, staging makes it easier to manage workflows Ability to undo/amend commits
References Git Tower, Learn Version Control with Git. Retrieved from h\p://www.git- tower.com/learn/ebook/command- line/introduc5on Lars Vogel, Git - Tutorial. Retrieved from h\p://www.vogella.com/tutorials/git/ar5cle.html