GIT Basics

  • View

  • Download

Embed Size (px)


Here Don goes over some of the benefits of using GIT as well as some of the basic concepts and methods. Later he goes through the workflow of using GIT. Download his slides here or email him at

Text of GIT Basics

  • 1.GIT BASICSPresenter: Don Lee

2. What is GIT (local) GIT ===> SVN/GIT (repo) GIT is a distributed version control system (DVCS). Our repo can be SVN or GIT It only interface with SVN when read ==> sync to repo write ==> submit to repo With GIT, all changes are store locally. SVN repo (svn:// GIT repo ( 3. GIT Vs SVN Git clone (url) gives you a complete copy ofthe entire history of that project. Every operation is now done off data onyour local disk, meaning that it is bothunbeliveably fast and can be done offline. No single point of failure (such as SVNserver crash), everyone has full backup ofthe project data. It doesnt depend on 1 centralized server,you can add multiple remote repositories toyour project. 4. Three States HEAD - last commit snapshot, local repo Index - proposed next commit snapshot working directory (WD) - sandbox 5. Installation Linux - sudo apt-get install git-core -sudo apt-get install git-svn Mac OS - Download and install from HERE. Windows 7 - Install cywin and enable git*, svn*, perl*, python, wget packages where * means wild cardConfiguration1. Open a terminal and create a .gitconfig file in your home directoryand then add/edit ~/.gitconfig file as follows: 6. [user]name = Don Leeemail =[reviewboard]url =[alias]ci = commitup = updatest = statusbr = branchco = checkoutdi = difflg = log --graph --decorategl = log -pglp = log --graph --oneline --abbrev-commit --decoraterl = reflogllog = log --date=localol = log --onelinew = whatchangedsta = stashrb = !post-review --parent=trunk --guess-description --username=dlee -orb-diff = !post-review -n --parent=trunksdiff = !git-svn-diff[log] date = local[color]ui = auto[color "diff"]whitespace = red reverse[core]whitespace = fix,-indent-with-non-tab,trailing-space,cr-at-eol 7. Review Board Setup 1. Download any one of the TAR file in 2. Extract the files into home directory 3. mv RBtools 4. cd ~/RBtools 5. python install 6. mv RBtools.egg 7. easy_install RBtools.egg 8. Download git-svn-diff from: andadd it somewhere in your PATH.Note:Linux users can install python byapt-get install python-setuptoolsFor Windows 7 user, in order to get "apt-get", use apt-cyg insteadsvn --force export /bin/chmod +x /bin/apt-cyg 8. Getting SVN/GIT repogit svn clone Example:git svn clone svn:// .It is getting the repo from web project to currentdirectory.Note: For GIT Repo, usegit clone ssh:// 9. Getting the repo (cont)A faster way to get repo is to copy all the directory/filesfrom a colleague who is already migrated to GIT, andthen issue the command above to sync to with the repo 10. Sync to SVN/GIT repogit svn rebaseThis will sync your branch to the latest version in theSVN repo.Note: For user with GIT repo, use the followinginsteadgit pull 11. Branchinggit br This command creates a new branch and it would havethe same version as the master branch. If you dont inputa name, it will just show you existing branch.Ex: git br newBranch 12. Switching branchgit co This command switch to a another branch. Please try to do changes on thebranch and leave the master clean because most likely you are doing diff withthe master branch later.Example: git co newBranch git broutput: master * newBranch(notice the * has change to point to newBranch)There is a combo command which let you create a new branch AND switchesto the new branchgit co -b newBranch 13. Add a filegit add This command add files to index.Example:git add shared/class/tag/{dao/a.php,b.php}This command will add to files a.php in dao subdirectoryand b.php 14. Remove filegit rm remove files in indexgit rm --cached remove from staging,file still existsExample:git rm shared/class/tag/{dao/a.php,b.php}This command will remove files a.php in daosubdirectory and b.php 15. Moving filegit mv This command moves files in index.Example:git mv shared/class/tag/a.php .This command will move file a.php to current directory. 16. Checking statusgit stThis command displays the changes so far in the workingdirectory i.e. what file are modified.Reverting mistakegit reset --soft commit_id move HEAD to commit idgit reset commit_id move HEAD & index to commit idgit reset --hard commit_id move ALL to commit idcommit_id if not supply means HEAD. For example git reset willunstage files in index to working dir 17. Reverting mistakegit co commit_id Change a working directory file to the a version specify by commitid. If not specified, Ex: git co, it means get HEAD version of all filesin current dir.git co -- Clear working directory for specific file or specific path (recursively)git reset HEAD Unstage a filegit revert HEADCreates "another" new commit that revert earlier change. 18. Ignoring filecd ~vim .gitignorelist out the full path of the filename to ignoregit config --global core.excludesfile ~/.gitignoreIt ignore any files you may want the GIT to ignore. 19. Stashinggit sta save "a name identifier"If you are not ready to commit stuff, but you need to savethe stuff that are modify, use the command above.git sta listto look at all the save listgit sta apply This would just dump out the specific stash.Example:git stash apply stash@{0} 20. Commit changegit ci -a commit all index and WD to branchgit ci commit only index to branchgit ci -m "blah" commit with commit message "blah"git ci --amend modify the existing commitDiffinggit di diff WD and indexgit di --cached diff index and HEADgit di master newbranch diff between branches 21. Inspect commitsgit lg List all the commit ids and description in that branch in decending order withthe latest commit at the top. The argument can be compound.git gl Show the code diff as wellgit lg -2 Show only last 2git glp Show 1 line for each commitgit lg --since=2.weeks last 2 weeksgit lg --since="2011-09-01" since September 1st 2011git lg --until="2011-09-01" until September 1st 2011git lg --author="dlee" author is dleegit lg --committer="dlee" committer is dleegit show output:{all the code diff for the particular commit} 22. Inspect Git Loggit rl List all the actions you did in gitRemote Repo URLgit remote -vList all the remote repo URLFind the culpritgit blame List all the people who change the file. 23. Merging changegit merge Merges the branch name to the current branch.ConflictGit will try to auto-resolve a conflict if it can, however, when it fails to do so,you can use a conflict resolve tool, there are several out there, one good oneis meld.git mergetooltrigger a interactive UI where you can click on which one you want tomerge. 24. Interactive modegit rebase -i HEAD~This will trigger interactive mode which will allow youto play with the commit message, squash serveralcommits into 1.# Rebase a257c40..90a4507 onto a257c40## Commands:# p, pick = use commit# r, reword = use commit, but edit the commit message# e, edit = use commit, but stop for amending# s, squash = use commit, but meld into previous commit# f, fixup = like "squash", but discard this commits log message# x, exec = run command (the rest of the line) using shell## If you remove a line here THAT COMMIT WILL BE LOST.# However, if you remove everything, the rebase will be aborted. 25. Interactive mode (cont)Squashing serveral commit into 1git rebase -i HEAD~ Example:pick 90a4507 pick mepick 90a4508 squash mepick 90a4509 squash mechange topick 90a4507 pick mes 90a4508 squash mes 90a4509 squash meand then save.In the next pop-up just put a "#" on the comments you dont want. 26. Interactive mode (cont)Reverting a file from latest commit while in a branchgit co master git commit -agit rebase -i HEAD~2 squash the two commitinto one.Note there is an easier way:git co master git commit --amendCheckout the master copy of filename, commit it into the branch andthen squash the commits. 27. Creating patchgit format-patch ~ If you changes is the latest commit, commit id shall beHEAD.git rb-diff > patch.diffThis creates a SVN patch diff. 28. Apply patchgit apply apply the changes by others.Please note this doesnt work with existing svn diffpatch file. 29. Review Boardpost-reviewSubmitting code review to review board is easy with just type thecommand above. However, you still need to enter the group, reviewname and etc. The diff portion is handled for you already. (Note forGIT-repo, you might need to remove --parent=trunk in .gitconfig)post-review -r If you want to update diff in review board, just is type the commandabove with the revision number in RB. 30. Push to SVN or GITgit svn dcommitthis shall submit the changes to the SVN repo.Note: For GIT Repo user, use the following command insteadgit pushDeleting branchgit br -D You work is done, remove the temp branch. 31. Need help ?git help If you dont specify the command name, it will just list out allthe commands that are available. 32. FAQ1) After unpacking the source code and syncing the latest, git st gives a list of changes which I did not modify.resolution: git reset --hard2)Win 7 user, I get a call stack crash stating remap error when I issue a git svn rebase.resolution: Close out of Cygwin (and all cygwin processes) , go to your cygwin bin directory, and type ash /usr/bin/rebaseall3) Why do I get the error ? update-index --refresh: command returned error: 1resolution: You need to either save your current changes using stash (i.e. git sta save ) or commit your changes (i.e. git ci -a) beforeyou can sync with repo.4) How do I commit my changes to repo if I have several commits ?resolution: Git only commit your latest changes, You can squash several commit into one latest commit before submitting to repo.5) How do I see the commits in my local branch ?resolution: git lg 33. Free AssistanceFor those who are interested to transition to GITnext week (limited time offer), we will s