Share
Embed
Git Basics
Git Basics presentation by Ramesh Dahiya during PHP Reboot's 'Working with Git' meetup on December 20th, 2014.
Kapil Sharma
December 20, 2014
Transcript
67 slides
-
Short intro Ramesh Dahiya Technical Lead Ansh Systems Pvt. Ltd.
Skype: dahiyabecomp Email:[email protected] Github: https://github.com/credulus Twitter: https://twitter.com/dahiyabecomp Linked in: http://in.linkedin.com/in/rameshdahiya Ramesh Dahiya PHP REBOOT 3 -
Version Control System What is Version Control System? Ramesh Dahiya
PHP REBOOT 4 Version control is a system which records changes to a file or set of files over time so that you can recall specific versions later. Types of version control systems: Local version control systems Centralized version control systems Distributed version control systems -
History Generation Networking Operations Concurrency Examples First None One file
at a time Locks RCS, SCCS Second Centralized Multi-file Merge before commit CVS, SourceSafe, Subversion, Team Foundation Server Third Distributed Changesets Commit before merge Bazaar, Git, Mercurial Ramesh Dahiya PHP REBOOT 5 -
Local VCS Earliest version control system was to copy files
in another directory; Simplest but error prone. ‘rcs’ was one of the initial and most popular Local version control systems. This tool basically works by keeping patch sets from one change to another in a special format of disk. Biggest disadvantage: No collaboration, only single developer could work on that. Ramesh Dahiya PHP REBOOT 6 -
Centralized VCS Centralized version control system solved biggest problem of
collaboration. It have central server which have all files. Examples are CVS, Subversion, MS VSS etc. Biggest disadvantage: Single point of failure. Ramesh Dahiya PHP REBOOT 7 -
Distributed VCS In Distributed VCS, clients don’t just checkout the
latest snapshot of the files but they fully mirror the repository. If server fails, any client copy can be used to restore the server. Example Git, Marcurial, Bazaar, Darcs. Ramesh Dahiya PHP REBOOT 8 -
Disadvantage of CVCS You can’t commit without merge and merging
is a pain. Ramesh Dahiya PHP REBOOT 9 No way to push changes to another user (without submitting to the Central Server) Subversion fails to merge changes when files or directories are renamed. Offline commits are not possible. .svn files pollute local directories. The trunk/tags/branches convention can be considered misleading. Performance and Single point of failure. -
Disadvantages of DVCS If a project contains many large, binary
files that cannot be easily compressed, the space needed to store all versions of these files can accumulate quickly. If a project has a very long history (50,000 changesets or more), downloading the entire history can take long time and disk space. Ramesh Dahiya PHP REBOOT 10 -
History of Git Till 2005, Linux Kernel was using proprietary
DVCS BitKeeper. In 2005, that relationship broke as BitKeeper revoke ‘free of charge’ status and Git borned with goals: Speed Simple design Strong support for non-linear branches Fully distributed Able to handle large projects like Linux Kernel efficiently Ensure Integrity Ramesh Dahiya PHP REBOOT 11 -
Advantages of Git Distributed Fully distributed (Almost) Everything is local
Everything is fast Every clone is a backup Work Offline – No network needed for Performing the diff View file history Committing changes Merging branches Obtaining other version of file Switching branches Ramesh Dahiya PHP REBOOT 12 -
Advantages of Git Immutable (Almost) Never remove the data Ramesh
Dahiya PHP REBOOT 13 -
Advantages of Git Snapshot – Not patches File based delta
storage Ramesh Dahiya PHP REBOOT 14 -
Advantages of Git Snapshot – Not patches Ramesh Dahiya PHP
REBOOT 15 Deltaset in SVN or most other CVCS -
Advantages of Git Snapshot – Not patches Ramesh Dahiya PHP
REBOOT 16 Snapshot in git -
Installation On linux, Git is mostly pre installed. However if
needed, it can be installed with following commands: RedHat based Linux sudo yum install git-core Debian based Linux sudo apt-get install git-core Windows Download installer from code.google.com/p/msysgit Mac brew install git Ramesh Dahiya PHP REBOOT 34 -
Configuration Git have three different configuration levels: System configuration: located
at /etc/gitconfig file. These configuration settings apply to all users. --system option passed to git config command to access system configuration. User specific or global configuration: located at ~/.gitconfig file. This config is applicable to all repositories of single user. --global optn passed tp git config command to access global Repository configuration: located in .git/config file. Apply to single repository. Ramesh Dahiya PHP REBOOT 35 -
git config command Identity git config --global user.name "Kapil Sharma"
git config --global user.email "[email protected]" Editor git config --global core.editor netbeans Ramesh Dahiya PHP REBOOT 36 -
GitHub Github is a free Git server for open source
git repository. git clone https://github.com/kapilsharma/gittestrepo.git Initialize locally git init Create some file, say readme.txt git add readme.txt git commit –m “First commit” git remote add https://github.com/kapilsharma/gittestrepo.git Ramesh Dahiya PHP REBOOT 39 -
Basic Operation Make clone git clone https://github.com/kapilsharma/gittestrepo.git <- Unmodified Make
changes Add a new file, say test1.php. <-Untracked Edit existing file, say test2.php. <- Modified Stage Changes git add test1.php <-Staged git add test2.php <-Staged Commit Git commit –m "Change message" <- Unmodified Ramesh Dahiya PHP REBOOT 40 -
Communicate with origin Origin: Central server from where you checkout
code. (Assum for now) Publish changes on origin git push origin master Get changes from origin git pull origin master Ramesh Dahiya PHP REBOOT 41 -
Gitignore file Some times there are files that we need
locally but don’t want to commit. Example of such files are: Configuration files IDE files like .nbproject, .subline-project Local documents Libraries of framework To command git not to consider these files, we simple create file '.gitignore‘ can commit it at the root. Ramesh Dahiya PHP REBOOT 42 -
Logs Entire (paged) git log Date filtering git log --since=2.weeks
git log --since="2 years 1 day 3 minutes ago“ git log --since='2012-12-01' --until='2012-12-10' Ramesh Dahiya PHP REBOOT 43 -
Quick revision Action Stage Command Stage files Modified -> staged
git add filename Review your changes git status git log git diff Commit Staged -> unmodified git commit –m "message" Stage and commit Modified -> staged ->unmodified git commit –a –m "message" Ramesh Dahiya PHP REBOOT 44 -
Reverting changes Unstage staged file git reset HEAD test.php Unmodify
modified file git checkout – test.php Revert commit git revert 1234a2 Add to last commit git commit --amend Ramesh Dahiya PHP REBOOT 46 -
Removing/Move files To remove a file git rm filename To
remove file from git but keep on harddisk git rm –cached filename To move/rename file git mv filename newfilename Above command is equivalent to mv filename newfilename git rm filename git add newfilename Ramesh Dahiya PHP REBOOT 47 -
Remote git push origin master - git push repository branch
git remote git remote -v git remote add remotename remoteurl git fetch remotename Ramesh Dahiya PHP REBOOT 48 -
Tags Tags is just a marker in revision history, to
mark important points of software, like versions. Listing tags: git tag Listing tags with patterns git tag -l "1.2.*" Git support two type of tags Annotated tags – can give a message with tag. (Lightweight) tags Ramesh Dahiya PHP REBOOT 50 -
Creating tags Lightweight tags git tag tagname Annotated tag git
tag –a tagname –m "message" Ramesh Dahiya PHP REBOOT 51 -
Pushing tags Git tags, when created are local. Like any
other commit, we need to push them to remote repository. Command git push origin tagname If we want to push all local tags, command is git push origin --tags Ramesh Dahiya PHP REBOOT 52 -
Branches Branching means you diverge from the main line of
development and continue to do work without messing with that main line. Branching in most SCM is expansive process. Some people believe branching in Git is its Killer Feature. The way Git branches is incredibly lightweight. Ramesh Dahiya PHP REBOOT 53 -
Branches Lets assume we are working on a project and
already have two commits. ----------------- Now say you have to work on mantis issue 53 (just for example) Lets create iss53 branch. Ramesh Dahiya PHP REBOOT 54 -
Branches Creating branch Ramesh Dahiya PHP REBOOT 55 Create branch
git branch iss53 Switch to branch git checkout iss53 Above two commands can be merged in single command as git checkout –b iss53 -
Branches New branch. ----------------- Now lets make some changes in
new branch and commit. Ramesh Dahiya PHP REBOOT 56 -
Branches New branch after changes. ---------------------- OOPs we got an
urgent fire bug on site. It needs to be fixed immediately. Lets create new branch form master. Ramesh Dahiya PHP REBOOT 57 -
Branches Creating new branch And fix the issue Ramesh Dahiya
PHP REBOOT 58 First go back to master git checkout master Create new branch git checkout –b hotfix We can now fix the issue on hotfix branch. -
Branches New condition. ------------------ Now we need to release hotfix.
However only master should contain the code to be released. So lets merge hotfix branch to master. Ramesh Dahiya PHP REBOOT 59 -
Branches Marge hotfix to master ------------------- Ramesh Dahiya PHP REBOOT
60 First go back to master git checkout master Now merge hotfix to master git merge hotfix -
Branches Hotfix merged ------------------ Now hotfix branch is useless, lets
delete it. Then lets go to iss53 branch and finish development there. Ramesh Dahiya PHP REBOOT 61 -
Branches Development on iss53 branch ------------------- Ramesh Dahiya PHP REBOOT
62 First go back to iss53 branch git checkout iss53 Make some changes there to finish development and commit -
Branches Iss53 branch ------------------ Now development is finished on iss53
branch and we need to merge it back to master. BUT There is a problem. Ramesh Dahiya PHP REBOOT 63 -
Branches Iss53 branch ------------------ Master and iss53 branch do not
have common ancestor. Ramesh Dahiya PHP REBOOT 64 -
Branches Merge iss53 in master Assuming no conflict ------------------- Ramesh
Dahiya PHP REBOOT 65 First go back to master git checkout master Merge iss53 branch git merge iss53 -
Branches Iss53 -> master merge ------------------ And if needed, delete
iss53 branch Ramesh Dahiya PHP REBOOT 66 git branch -d iss53