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 dlee@tagged.com.
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://svn.tagged.com)
● GIT repo (https://git.tagged.com/git)
3. 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.
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 card
Configuration
1. Open a terminal and create a .gitconfig file in your home directory
and then add/edit ~/.gitconfig file as follows:
6. [user]
name = Don Lee
email = dlee@tagged.com
[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
7. Review Board Setup
1. Download any one of the TAR file in http://downloads.
reviewboard.org/releases/RBTools/0.3
2. Extract the files into home directory
3. mv <folder name> RBtools
4. cd ~/RBtools
5. python setup.py install
6. mv <downloaded EGG file> RBtools.egg
7. easy_install RBtools.egg
8. Download git-svn-diff from: https://gist.github.com/44537 and
add it somewhere in your PATH.
Note:
Linux users can install python by
apt-get install python-setuptools
For Windows 7 user, in order to get "apt-get", use apt-cyg instead
svn --force export http://apt-cyg.googlecode.com/svn/trunk/ /bin/
chmod +x /bin/apt-cyg
8. 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://username@git.tagged.
com/git/engteam/<project name>.git
9. 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
10. Sync to SVN/GIT repo
git 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
11. Branching
git 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
12. Switching branch
git 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
13. Add a file
git 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
14. Remove file
git rm <filesnames> remove files in index
git 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
15. Moving file
git 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.
16. Checking status
git st
This command displays the changes so far in the working
directory i.e. what file are modified.
Reverting mistake
git reset --soft commit_id move HEAD to commit id
git reset commit_id move HEAD & index to commit id
git 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
17. Reverting mistake
git 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.
18. 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.
19. 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 list
to 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}
20. Commit change
git ci -a commit all index and WD to branch
git ci commit only index to branch
git ci -m "blah" commit with commit message "blah"
git ci --amend modify the existing commit
Diffing
git di diff WD and index
git di --cached diff index and HEAD
git di master newbranch diff between branches
21. Inspect commits
git 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 well
git lg -2 Show only last 2
git glp Show 1 line for each commit
git 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 dlee
git lg --committer="dlee" committer is dlee
git show <commit id>
output:
{all the code diff for the particular commit}
22. Inspect Git Log
git rl List all the actions you did in git
Remote Repo URL
git remote -v List all the remote repo URL
Find the culprit
git blame <filename> List all the people who change the file.
23. Merging change
git merge <branch name to merge into >
Merges the branch name to the current branch.
Conflict
Git 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.
24. 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.
25. Interactive mode (con't)
Squashing serveral commit into 1
git rebase -i HEAD~ <no. of commits>
Example:
pick 90a4507 pick me
pick 90a4508 squash me
pick 90a4509 squash me
change to
pick 90a4507 pick me
s 90a4508 squash me
s 90a4509 squash me
and then save.
In the next pop-up just put a "#" on the comments you don't want.
26. Interactive mode (con't)
Reverting a file from latest commit while in a branch
git co master <filename>
git commit -a
git 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.
27. Creating patch
git 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.
28. Apply patch
git apply <the diff file>
apply the changes by others.
Please note this doesn't work with existing svn diff
patch file.
29. Review Board
post-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.
30. 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 instead
git push
Deleting branch
git br -D <newBranch>
You work is done, remove the temp branch.
31. Need help ?
git help <command>
If you don't specify the command name, it will just list out all
the commands that are available.
32. FAQ
1) 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: 1
resolution: 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
33. 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, dlee@tagged.com
Nate, nkresge@tagged.com