2. Why Git
• Centralized Version Control System: svn
– can’t develop without connection to VCS server
– this is slow
• Distributed Version Control System: git
– develop locally without connection to VCS server
– repository backup isn’t problem: every clone is also a
backup
• Git
– fast
– less-space for repository
– cheap local branch
2
3. Distributed VCS
3Computer A Computer B
Repository ServerRepository Server
Version 1
Version 2
Local RepositoryLocal Repository
Version 1
Local RepositoryLocal Repository
Version 2
FilesFiles FilesFiles
pull push
checkout commit
…
4. Install Git
• In Ubuntu Linux
– sudo apt-get install git
• In Fedora Linux
– sudo yum install git
• In Windows
– http://msysgit.github.com/
– http://code.google.com/p/tortoisegit/
• In Mac
– http://code.google.com/p/git-osx-installer/
4
5. Configure Git
• Configure the full name to be recorded in any new commit:
git config --global user.name ”Full Name”
• Configure email address to be recorded in any new commit:
git config --global user.email ”user@email.com"
• Always use colors in all git commands:
git config --global color.ui true
• Configure the editor to edit messages when commit or tag:
git config --global core.editor vim
• Specify how many context lines of diff should be displayed:
git config --global gui.diffcontext 5
• Global configuration is in ~/.gitconfig
• For more detailed, please reference “git help config”
5
6. Initialize Local Repository
1. Enter the directory that you want to manage
by git
– Ex. cd project
1. git init
2. git add --all . (or git add --all --force . to alow
adding otherwise ignored files)
3. git commit --message=“Init”
4. Change description of repository: echo
“description” > .git/description
6
7. Import Empty Folder to Repository
• touch empty_path/.gitignore
• Add .gitignore to every empty folder
– find . ( -type d -empty ) -and ( -not -regex
./.git.* ) -exec touch {}/.gitignore ;
• The content of .gitignore
# Ignore everything in this directory
*
# Except this file
!.gitignore
7
8. Build Remote Repository
1. git clone --bare my_project my_project.git
2. Change description of repository: echo
“description” > my_project.git/description
3. Copy my_project.git to remote site
− Ex. Copy to /home/gitserver/my_project.git of
remote site: scp -r my_project.git
gituser@192.168.1.1:/home/gitserver
8
14. Diff
• Compare the working directory with a specified
commit
– git diff commit
– Ex. git diff 1aj93u5
• Compare two of commits
– git diff commit_1 commit_2
– Ex. git diff 1aj93u5 239ann
• Compare the working directory with staged area
– git diff --staged
14
15. Clean
• Remove all untracked directories and files
– git clean -df
• Remove all files included file list in .gitignore
– git clean -x
15
16. Reset
• Back to specified commit
– git reset commit
– Ex. git reset 1aj93u5
• Keep modification in working tree
– git reset HEAD^
• Keep modification in staging area
– git reset HEAD^ --soft
• Destroy all modification
– git reset HEAD^ --hard
• Using gitk 16
17. Tag
• List all tags
– git tag
• Add a new tag
– git tag -a tagname -m “message for tag”
– Ex. git tag -a v0.1 -m “this is v0.1”
• Delete an existing local tag
– git tag -d tagname
• Send all tags to remote repository
– git push --tags
• Delete the tag of remote repository
– git push origin :refs/tags/tag_name
17
18. Branch• List local existing branches
– git branch
• List both local and remote branches
– git branch -a
• Create a new local branch and switch to it
– git checkout -b branch_name
• Create a new local branch starts from remote branch
– git checkout -b branch_name start_poing
– ex. git checkout -b development original/dev_branch
• Switch to an existing branch
– git checkout branch_name
• Delete an existing local branch
– git branch -d branch_name
• Send an branch to remote repository
– git push origin branch_name
• Delete the existing branch of remote repository
– git push origin :branch_name 18
19. Change remote HEAD to point to
another branch
• In remote git repository folder
– git symbolic-ref HEAD refs/heads/branch_name
– git update-server-info
19
20. Rename remote branch
• Rename the branch in the local repository
– git branch -m old_branch new_branch
• Remove the remote branch
– git push origin :old_branch
• Push the new local branch name to remote
– git push origin new_branch
20