33
GIT BASICS Presenter: Don Lee

GIT Basics

Embed Size (px)

DESCRIPTION

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 [email protected].

Citation preview

Page 1: GIT Basics

GIT BASICS

Presenter: Don Lee

Page 2: GIT Basics

What is GIT

● 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://svn.tagged.com)● GIT repo (https://git.tagged.com/git)

(local) GIT ===> SVN/GIT (repo)

Page 3: GIT Basics

GIT Vs SVN

● Git clone (url) gives you a complete copy of the entire history of that project.

● Every operation is now done off data on your local disk, meaning that it is both unbeliveably fast and can be done offline.

● No single point of failure (such as SVN server crash), everyone has full backup of the project data.

● It doesn't depend on 1 centralized server, you can add multiple remote repositories to your project.

Page 4: GIT Basics

Three States

● HEAD - last commit snapshot, local repo● Index - proposed next commit snapshot● working directory (WD) - sandbox

Page 5: GIT Basics

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 card

Configuration

1. Open a terminal and create a .gitconfig file in your home directory and then add/edit ~/.gitconfig file as follows:

Page 6: GIT Basics

[user] name = Don Lee email = [email protected][reviewboard] url = https://scm.tagged.com/reviewboard[alias] ci = commit up = update st = status br = branch co = checkout di = diff lg = log --graph --decorate gl = log -p glp = log --graph --oneline --abbrev-commit --decorate rl = reflog llog = log --date=local ol = log --oneline w = whatchanged sta = stash rb = !post-review --parent=trunk --guess-description --username=dlee -o rb-diff = !post-review -n --parent=trunk sdiff = !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

Page 7: GIT Basics

Review Board Setup1. Download any one of the TAR file in http://downloads.

reviewboard.org/releases/RBTools/0.32. Extract the files into home directory3. mv <folder name> RBtools4. cd ~/RBtools5. python setup.py install6. mv <downloaded EGG file> RBtools.egg7. easy_install RBtools.egg8. Download git-svn-diff from: https://gist.github.com/44537 and

add it somewhere in your PATH.Note: Linux users can install python byapt-get install python-setuptools For Windows 7 user, in order to get "apt-get", use apt-cyg insteadsvn --force export http://apt-cyg.googlecode.com/svn/trunk/ /bin/chmod +x /bin/apt-cyg

Page 8: GIT Basics

Getting SVN/GIT repo git svn clone <repo url> <target directory>

Example:

git svn clone svn://svn.tagged.com/web/trunk .

It is getting the repo from web project to current directory.Note: For GIT Repo, use git clone ssh://[email protected]/git/engteam/<project name>.git

Page 9: GIT Basics

Getting the repo (con't)

A faster way to get repo is to copy all the directory/files from a colleague who is already migrated to GIT, and then issue the command above to sync to with the repo

Page 10: GIT Basics

Sync to SVN/GIT repogit svn rebase

This will sync your branch to the latest version in the SVN repo.

Note: For user with GIT repo, use the following instead git pull

Page 11: GIT Basics

Branchinggit br <new branch name>

This command creates a new branch and it would have the same version as the master branch. If you don't input a name, it will just show you existing branch.Ex: git br newBranch

Page 12: GIT Basics

Switching branchgit co <branch name>This command switch to a another branch. Please try to do changes on the branch and leave the master clean because most likely you are doing diff with the master branch later.Example: git co newBranch git br output: master * newBranch (notice the * has change to point to newBranch)

There is a combo command which let you create a new branch AND switches to the new branch git co -b newBranch

Page 13: GIT Basics

Add a filegit add <filesnames>

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 subdirectory and b.php

Page 14: GIT Basics

Remove filegit rm <filesnames> remove files in indexgit rm <filename> --cached remove from staging, file still exists

Example: git rm shared/class/tag/{dao/a.php,b.php} This command will remove files a.php in dao subdirectory and b.php

Page 15: GIT Basics

Moving filegit mv <filesnames> <destination file location>

This command moves files in index.

Example: git mv shared/class/tag/a.php . This command will move file a.php to current directory.

Page 16: GIT Basics

Checking statusgit st

This command displays the changes so far in the working directory 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 id

commit_id if not supply means HEAD. For example git reset will unstage files in index to working dir

Page 17: GIT Basics

Reverting mistakegit co commit_id <filename>Change a working directory file to the a version specify by commit id. If not specified, Ex: git co, it means get HEAD version of all files in current dir.

git co -- <filename or path>Clear working directory for specific file or specific path (recursively) git reset HEAD <filename>Unstage a file git revert HEAD Creates "another" new commit that revert earlier change.

Page 18: GIT Basics

Ignoring file

cd ~vim .gitignore

list out the full path of the filename to ignore

git config --global core.excludesfile ~/.gitignore

It ignore any files you may want the GIT to ignore.

Page 19: GIT Basics

Stashing git sta save "a name identifier"

If you are not ready to commit stuff, but you need to save the stuff that are modify, use the command above.

git sta listto look at all the save list

git sta apply <optional nth list>This would just dump out the specific stash.Example: git stash apply stash@{0}

Page 20: GIT Basics

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 commit

Diffinggit di diff WD and indexgit di --cached diff index and HEADgit di master newbranch diff between branches

Page 21: GIT Basics

Inspect commitsgit lg List all the commit ids and description in that branch in decending order with the 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 weeks git lg --since="2011-09-01" since September 1st 2011 git lg --until="2011-09-01" until September 1st 2011 git lg --author="dlee" author is dleegit lg --committer="dlee" committer is dlee

git show <commit id>output:{all the code diff for the particular commit}

Page 22: GIT Basics

Inspect Git Loggit rl List all the actions you did in git

Remote Repo URLgit remote -v List all the remote repo URL

Find the culpritgit blame <filename> List all the people who change the file.

Page 23: GIT Basics

Merging changegit merge <branch name to merge into >

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 one is meld.

git mergetool

trigger a interactive UI where you can click on which one you want to merge.

Page 24: GIT Basics

Interactive mode git rebase -i HEAD~<no. commit involved> This will trigger interactive mode which will allow you to play with the commit message, squash serveral commits 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 commit's 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.

Page 25: GIT Basics

Interactive mode (con't)Squashing serveral commit into 1

git rebase -i HEAD~ <no. of commits>

Example:pick 90a4507 pick me

pick 90a4508 squash mepick 90a4509 squash me

change to

pick 90a4507 pick mes 90a4508 squash mes 90a4509 squash me

and then save.In the next pop-up just put a "#" on the comments you don't want.

Page 26: GIT Basics

Interactive mode (con't)Reverting a file from latest commit while in a branch

git co master <filename> git commit -agit rebase -i HEAD~2 squash the two commit into one. Note there is an easier way:git co master <filename>git commit --amend Checkout the master copy of filename, commit it into the branch and then squash the commits.

Page 27: GIT Basics

Creating patchgit format-patch <commit id> ~ <no. of commit>

If you changes is the latest commit, commit id shall be HEAD.

git rb-diff > patch.diff

This creates a SVN patch diff.

Page 28: GIT Basics

Apply patch

git apply <the diff file>

apply the changes by others.Please note this doesn't work with existing svn diff patch file.

Page 29: GIT Basics

Review Boardpost-review

Submitting code review to review board is easy with just type the command above. However, you still need to enter the group, review name and etc. The diff portion is handled for you already. (Note for GIT-repo, you might need to remove --parent=trunk in .gitconfig)

post-review -r <revision number in RB>

If you want to update diff in review board, just is type the command above with the revision number in RB.

Page 30: GIT Basics

Push to SVN or GIT git svn dcommit

this shall submit the changes to the SVN repo. Note: For GIT Repo user, use the following command insteadgit push

Deleting branchgit br -D <newBranch>You work is done, remove the temp branch.

Page 31: GIT Basics

Need help ?git help <command>

If you don't specify the command name, it will just list out all the commands that are available.

Page 32: GIT Basics

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 --hard

2)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/rebaseall'

3) 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 <name>) or commit your changes (i.e. git ci -a) before you 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

Page 33: GIT Basics

Free Assistance

For those who are interested to transition to GIT next week (limited time offer), we will sit with you and help you to set up.

Don, [email protected], [email protected]