GIT Intro and usage onWindows - BasicBy Geshan Manandharhttp://geshan.blogspot.com1http://geshan.blogspot.com
Agenda What is git Who uses it, features How to use git on windows Prepare working directory Create public repository Add, commit, pull, push Git ignore Other commands and things to look athttp://geshan.blogspot.com 2
GIT is a free & open source, distributedversion control system. designed to handle everything from smallto very large projects with speed andefficiency. a developer’s friend in a multideveloper environment. a must tool to go back in history to whatyou coded and see the code changesmade in the timeline.3http://geshan.blogspot.com
Who uses GIT Many big names including◦ Linux/Kernel◦ Eclipse◦ Ruby on Rails◦ Android◦ PostgreSQL◦ Git itself.4http://geshan.blogspot.com
Why use Git/ Features Fast Distributed (no central repository) Can work offline Small (file system usage) Has a staging area Access control (with gitosis) Fit for any project size Branching, tags … GitHub.com◦ Reference: http://whygitisbetterthanx.com5http://geshan.blogspot.com
Distributed?? No Centralrepository All developershave a workingcopy and ownprivate repo (.git) Code transfershould be done onnon-private repo.http://geshan.blogspot.com 6Image Source:http://coding.smashingmagazine.com/2011/07/26/modern-version-control-with-git-series/
How to use on windows? Could not find mature GUI tools liketortoise SVN. Integrated in Eclipse (with plugin) andNetBeans (good colorful support). If you are programmer, you are notscared of the command line so: Download MsysGit :http://code.google.com/p/msysgit/downloads/listhttp://geshan.blogspot.com 7
MsysGit for git on Windowshttp://geshan.blogspot.com 8Steps:1. Download the exe file (or the portable one, it will not needinstallation).2. Install msysgit , next … next is your friend here with finish – noneed of any fancy configuration.3. Run the git bash application.
Git bash part of MsysGithttp://geshan.blogspot.com 9
User and line settings for win If you are familiar with ~ of unix systemsthis might be easy. Make sure where your home dir is inyour windows setup like:◦ /c/users/Geshan – for me in Windows 7 Set ~/.bash_profile if you want.(googlebash profiles and dot files, if you want to) Run this: git config --global core.autocrlf true Have a look for new line setting if need be:http://github.com/guides/dealing-with-newlines-in-githttp://geshan.blogspot.com 10
Introduce yourself to GIT execute commands in bash◦ git config --global user.name <name>◦ git config --global user.email <email>◦ (this gets written to your ~/.gitconfig file) Example◦ git config --global user.name “GeshanManadhar”◦ git config --global user.email“email@example.com”http://geshan.blogspot.com 11
Some conceptual clarity You have a working directory and yourprivate repo (a .git folder in your dir) You always push your changes andpull changes from another non-privaterepository.http://geshan.blogspot.com 12Working Dir with gitPublic git repo on file system
Preparing your working dirhttp://geshan.blogspot.com 13Nothing significant just created a directory with a file that’s it.
Preparing working dir 2http://geshan.blogspot.com 14
Working directory made Commands used◦ git init ; to initialize the current dir for gittracking.◦ git status; shows current status◦ git add; put files to stating area Variations like Git add . ; . Is for all changed files Git add /path/to/file ; /c/dir1/file1.txt Git add /path/a.* etchttp://geshan.blogspot.com 15
First commithttp://geshan.blogspot.com 16git commit –m “message”;Files have been put into the local repository (the .git dircreated with git init)All things are in local so we need to push things to a non-private repository. For this example I’ll create a localrepository.Always remember to write concise but meaningfulcommit messages.
Creating public repositoryhttp://geshan.blogspot.com 17A bare passive public repo has been created for pushing code.Remember the path is /c/users/geshan/gitrepos/testgit.gitDifferent command : git init --bare;Now we will link the working directory with this public repo.
Linking working dir with publicrepohttp://geshan.blogspot.com 18Remote repository created in previous slide linked to the working directory.git remote add <name> <path>Path can be:File : /home/geshan/gitrepost/testrepo.gitRemote file: ssh://firstname.lastname@example.org/home/user1/gitrepos/test.gitGitosis: email@example.com:test.git
First push to remote repo git push <repo_name><branch_name> Repo_name is just a alias to the path. git push origin master◦ Default repo when cloned in origin anddefault branch is master.http://geshan.blogspot.com 19
First pull attempt Pull is done to get the changes doneby others in the self working directory. git pull <repo_name> <branch_name> git pull origin masterhttp://geshan.blogspot.com 20
.gitignore file .gitignore file is generally placed in theroot of the working directory. As the name suggests it can ignorecertain files from git. Generally local config is ignored but aback up is kept for others, exampledatabase username password config.http://geshan.blogspot.com 21
Using .gitignorehttp://geshan.blogspot.com 22Scenario:Local databasesettings arestored indatabase.phpfile
Continue to push bkp filehttp://geshan.blogspot.com 23
Pull by a new developerhttp://geshan.blogspot.com 24A newdeveloperwillgenerallyclone theproject,thancreatenew files.-Noticegit remote–vGit branchshowsavailablebranches.
Scenario dev 2 posts code Dev 2 changed test.txt and pushed hiscode to public repo.http://geshan.blogspot.com 25
Dev 1 pulled the changes. Dev 1 can immediately have thechanged code.http://geshan.blogspot.com 26
Gitk to know what changed execute gitk on a git repo root to getthis.http://geshan.blogspot.com 27
Other git commands git log git short log –sne git checkout / git branch –b newbranch git tag v 1.0 –a “test” git fsck git push dev feature1; - testing you.http://geshan.blogspot.com 28
Things to look at Reset the code to old version Branching with git Tagging with git How to handle conflict situation Other git features and commands Check out the git cheat sheet:http://www.cheat-sheets.org/saved-copy/git-cheat-sheet.pdf For details of hashing etc check the gitinternals book.http://geshan.blogspot.com 29
Happy Git-ing Google git See videos on youtube Find some good podcasts on git You will like what you usehttp://geshan.blogspot.com 30