310 likes | 553 Vues
Introduction to Git. Chu-Cheng Hsieh chucheng @ ucla.edu. What is git. a distributed revision control and source code management (SCM) system What the hell?. You want a CTRL-Z for your documents. Create a readme file in cs144/ mkdir cs144 cd cs144 echo “This is a readme” >> readme.txt.
E N D
Introduction to Git Chu-Cheng Hsieh chucheng@ucla.edu
What is git • a distributed revision control and source code management (SCM) system • What the hell?
You want a CTRL-Z for your documents • Create a readme file in cs144/mkdir cs144cd cs144echo “This is a readme” >> readme.txt
git in English • Track your changes by making a snapshot (hashcode) • “gitinit” • Create a “.git” folder in your folder to track the changes ~/github/cs144$ gitinit Initialized empty Git repository in /Users/chucheng/github/cs144/.git/
Tell git to track files (put flags on them) • add a file • git add <filename> • add a dir and everything inside this dir • git add <dir> • add everything • git add . ~/github/cs144$ git add readme.txt
Compute a hash and make a snapshot • a8a9469 is the “hash” of the snapshot ~/github/cs144$ git commit -m "Create a readme file" [master (root-commit) a8a9469] Create a readme file 1 file changed, 1 insertion(+) create mode 100644 readme.txt ~/github/cs144$ git log commit a8a946980ffaa1d0fa1c7dfe35f1f18c3fac267c Author: Chu-Cheng Hsieh <chucheng@ucla.edu> Date: Thu Oct 11 21:32:21 2012 -0700 Create a readme file
Now you make a mistake • To see a difference ~/github/cs144$ git diff readme.txt diff --git a/readme.txt b/readme.txt index 24308cb..c920514 100644 --- a/readme.txt +++ b/readme.txt @@ -1 +1 @@ -This is a readme +Oh… I forget submit Project 1 • To rollback to last commit git checkout readme.txt
What if you already make a commit? • Suppose we make a mistake… • To rollback to any point $ git commit -am "Second commit" [master d5b61af] Second commit 1 file changed, 1 insertion(+), 1 deletion(-) $ gitreset --hard a8a946 HEAD is now at a8a9469 Create a readme file $ cat readme.txt This is a readme
“master branch” • Remembering “sha” is stupid… • Create a “pointer” with a name • default “pointer” is master $ gitbranch * master
If you need another pointer for trying something … $ git checkout -b feature-kill-virus Switched to a new branch 'feature-kill-virus’ $ git branch * feature-kill-virus master
Working in a branch • git checkout <branch> $ git commit -am "kill all virus in a branch” [feature-kill-virus 1a2970b] kill all virus in a branch 1 file changed, 2 insertions(+), 2 deletions(-)
branch is a pointer • Every sha only “know” which sha is its predecessor
Branch • Help you know… • where you “were” • give you a convenient to try something but do not have to worry the consequence • Or if later you are happy with things you “try”… you can merge
git merge • git do “smart merge” $ git branch * feature-kill-virus master$ git checkout master$ git branch feature-kill-virus * master$ gitmerge feature-kill-virus Merge made by the 'recursive' strategy. readme.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Some popular branch command • Create a new branch from current branch • git checkout –b <new_branch> • Switch between branch • git checkout <target_branch> • example: git checkout master • To see all branch • git branch • To delete a branch • git branch –d <name>
In a word … • Git is a (super) smart ctrl-z • Every time you make a “commit” taking a snapshot • A snapshot is represented by “sha” • You can roll back to any snapshot (time machine) • You can tell a difference….between different versions • You can create a branch to “try something” • Once you are done you can then “merge back”
What if “.git” can be automatically sync between two persons. • You can see what changes your partner made (and at what time) • You can control-z to any point, compare (diff) between your work and his/her work • But how?
Github? • An online repository • keep a copy of your .git • allow others to “download” your git • github.com/edu github upload download Chucheng Young
You need tell where to sync This is just an “alias” $ git remote add origin https://github.com/chucheng/cs144.git $ git remote -v origin https://github.com/chucheng/cs144.git (fetch) origin https://github.com/chucheng/cs144.git (push) github Chucheng
upload (push)*don’t forget “-u” for the first push $ gitpush -u Username for 'https://github.com': chucheng Password for 'https://chucheng@github.com': To https://github.com/chucheng/cs144.git * [new branch] master -> master github push Chucheng
download (pull)* If there is a conflict, you will see a “diff” results $ git pull Already up-to-date. github pull Chucheng
How to release the power of branch Ref: http://nvie.com/posts/a-successful-git-branching-model/
Develop branch • Have a develop branch, keep your master “clean”
Feature branches • When you want to try some thing (new feature… which might take a while) • create a branch • merge back to develop once you are done • Snapshot in master branch • runnable • Snapshot in develop • able to be compiled without error
Hotfix – Emergency request • What if you need fix a bug for your partner but you are in the middle of doing something • Tag is just a permanent convenient alias for a hash • Using “branch” + “merge” to support multi-tasking
Some reference • Tutorial • http://git-scm.com/book • http://www-cs-students.stanford.edu/~blynn/gitmagic/ • Sites: • http://jonas.nitro.dk/git/quick-reference.html • http://nvie.com/posts/a-successful-git-branching-model/