Tài Liệu Git Tiếng Việt

Embed Size (px)

DESCRIPTION

Tài Liệu Git Tiếng Việt online từ cơ bản đến nâng cao

Citation preview

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 1

    Mc lc Gii thiu v Git .................................................................................................................... 4

    i vi c gi ...................................................................................................................... 4

    iu kin tin ................................................................................................................... 4

    C bn v Git ....................................................................................................................... 4

    VCS h thng qun l phin bn .................................................................................... 4

    H thng kim sot phin bn phn phi .......................................................................... 5

    Cc li th ca Git............................................................................................................. 5

    Ngun min ph v m ............................................................................................ 5

    Tc nhanh v nh gn ........................................................................................ 6

    D phng (sao lu) n ............................................................................................. 6

    An ton cao ................................................................................................................ 6

    Khng yu cu mt phn cng mnh ................................................................... 6

    Phn nhnh d dng hn ........................................................................................ 6

    Cc thut ng ca DVCS ................................................................................................. 7

    Kho commit ni b .................................................................................................... 7

    Th mc lm vic v Staging hoc Index............................................................. 7

    Blobs ........................................................................................................................... 8

    Cy - Trees ................................................................................................................ 9

    K thc - Commits .................................................................................................... 9

    Cc nhnh - Branches ............................................................................................. 9

    Th - Tags .................................................................................................................. 9

    M phng - Clone ..................................................................................................... 9

    Pull ............................................................................................................................. 10

    Push .......................................................................................................................... 10

    HEAD ........................................................................................................................ 10

    Revision .................................................................................................................... 10

    URL ........................................................................................................................... 10

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 2

    Ci t mi trng Git......................................................................................................... 11

    Ci t Git client .............................................................................................................. 11

    Ty chnh mi trng Git ................................................................................................ 11

    Thit lp tn ngi s dng.................................................................................. 12

    Thit lp email id ..................................................................................................... 12

    Trnh cc commit sp nhp khi pull .................................................................... 12

    Mu ni ..................................................................................................................... 12

    Thit lp b son mc nh ................................................................................... 12

    Thit lp cng c sp nhp mc nh .................................................................. 12

    Lit k cc thit lp Git ........................................................................................... 13

    Vng i Git ........................................................................................................................ 13

    Hot ng Create trong Git ................................................................................................. 14

    To mt ti khon s dng mi ...................................................................................... 14

    To mt repository rng .................................................................................................. 15

    Cp kha chung/ring ..................................................................................................... 15

    Thm cc kha vo cc kha c y quyn ................................................................ 16

    y (Push) cc thay i ti repository ............................................................................. 17

    Hot ng Clone trong Git .................................................................................................. 19

    Thc hin thay i trong Git ................................................................................................ 20

    Review thay i trong Git .................................................................................................... 22

    Commit trong Git ................................................................................................................. 24

    Hot ng Push trong HTML .............................................................................................. 26

    Hot ng Update trong Git ................................................................................................ 28

    Ty chnh cc chc nng ang tn ti ............................................................................. 28

    Thm chc nng mi ..................................................................................................... 31

    Gi ra nhng thay i mi nht....................................................................................... 33

    Hot ng Stash trong Git ................................................................................................... 35

    Hot ng Move trong Git ................................................................................................... 36

    Hot ng Rename trong Git .............................................................................................. 38

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 3

    Hot ng Delete trong Git .................................................................................................. 39

    Sa li trong Git .................................................................................................................. 41

    Tr li nhng thay i cha c commit ...................................................................... 41

    D b nhng thay i t khu vc t chc ...................................................................... 42

    Di chuyn im tr HEAD vi git reset ............................................................................ 43

    Soft ............................................................................................................................ 44

    Ty chn mixed ....................................................................................................... 46

    Ty chn hard .......................................................................................................... 46

    Hot ng Tag trong Git ...................................................................................................... 47

    To cc tag ..................................................................................................................... 47

    Quan st cc tag ............................................................................................................. 48

    Xa cc tag ..................................................................................................................... 49

    Hot ng Patch trong Git ................................................................................................... 49

    Qun l nhnh trong Git ...................................................................................................... 52

    To nhnh ...................................................................................................................... 52

    Chuyn i gia cc nhnh ............................................................................................ 53

    Cch tt to nhnh v chuyn i gia cc nhnh ..................................................... 54

    Xa mt nhnh ............................................................................................................... 54

    t li tn cho mt nhnh ............................................................................................... 55

    Sp nhp hai nhnh ........................................................................................................ 55

    Rebase cc nhnh .......................................................................................................... 59

    X l Conflict trong Git ........................................................................................................ 60

    Thc hin cc thay i trong nhnh wchar_support ........................................................ 60

    Thc hin cc thay i trong nhnh master .................................................................... 61

    X tr cc conflict ............................................................................................................. 64

    X l cc conflict ............................................................................................................. 64

    Cc Platform khc nhau trong Git ....................................................................................... 67

    Repository trc tuyn trong Git ............................................................................................ 67

    To repository GitHub ..................................................................................................... 67

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 4

    Hot ng push .............................................................................................................. 68

    Hot ng pull ................................................................................................................. 69

    Ti liu tham kho v Git ..................................................................................................... 70

    Cc ng link hu ch v Git ........................................................................................ 70

    Gii thiu v Git Git l mt h thng qun l m ngun phn phi v kim sot phin bn phn tn vi s nhn

    mnh v tc . Git ln u c thit k v pht trin bi Linus Torvalds cho pht trin ht nhn

    Linux. N l mt phn mm min ph c phn phi theo cc iu khon ca GNU phin bn 2.

    Phn hng dn ny gii thch cch s dng Git cho iu khin phin bn d n trong mi

    trng phn tn trong khi lm vic v pht trin cc ng dng da trn hoc khng da trn web.

    Lot bi hng dn ca chng ti da trn ngun ti liu ca: Tutorialspoint

    i vi c gi Cc phn hng dn ny s gip ngi mi bt u tm hiu cc chc nng c bn ca h thng

    qun l phin bn Git. Sau khi hon thnh cc phn hng dn ny, bn s thy trnh ca mnh

    mc va phi trong vic s dng h thng kim sot phin bn Git, v t bn c iu kin

    nng cao trnh chuyn mn.

    iu kin tin Chng ti gi nh rng bn ang s dng Git x l tt c cc d n v Java hoc Non-Java. V

    vy n s tt nu bn tip xc vi vng i pht trin phn mm v kin thc v pht trin cc

    ng dng da trn web hoc khng da trn web.

    C bn v Git VCS h thng qun l phin bn Version Control System (VCS) l mt phn mm m gip cc nh pht trin phn mm lm vic

    cng nhau v duy tr mt lch s y cc cng vic m h lm.

    Di y l cc chc nng ca mt VCS:

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 5

    Cho php cc nh pht trin phn mm cng lm vic vi nhau

    Khng cho php ghi ln cc thay i ca nhau

    Duy tr mt lch s ca mi phin bn.

    Di y l cc loi VCS:

    H thng kim sot phin bn tp trung (CVCS).

    H thng kim sot phin bn phn phi/phn cp (DVCS).

    Trong chng ny chng ta s ch tp trung vo h thng qun l phin bn phn phi v c bit

    trn Git.

    H thng kim sot phin bn phn phi H thng kim sot phin bn tp trung (CVCS) s dng mt my ch lu gi tt c cc file v

    cho php cc team cng tc vi nhau. Nhng nhc im ln nht ca CVCS cng l im tht

    bi ca n, tc l, s tht bi ca cc my ch trung tm. Tht khng may l, nu my ch trung

    tm b hng trong mt gi, th trong sut qung thi gian khng ai c th cng tc c vi ai

    c. V ngay c trong trng hp xu nht, nu a ca my ch trung tm b hng v s sao lu

    khng c thc hin, bn s mt ton b lch s ca d n. Ti y, h thng qun l phin bn

    phn phi xut hin.

    Cc client DVCS khng ch kim tra c cc nh chp mi nht ca cc th mc m h cn quan

    st c tt c repository tr ca d n. Nu server b hng, cc kho d tr ca cc client c th

    sao mt bn sao y cho server khi phc li n. Git khng ph thuc vo server trung tm

    v l l do ti sao bn c th thc hin nhiu thao tc khi bn ang offline. Bn c th y thc

    cc thay i, to cc nhnh, xem cc bn ghi v thc hin cc hot ng khc khi bn ang offline.

    Bn cn kt ni mng ch cng b nhng thay i ca bn v a nhng thay i mi nht vo

    d n.

    Cc li th ca Git Ngun min ph v m

    Git c cng b di giy php ngun m ca GPL. N c sn min ph trn mng. Bn c th

    s dng Git qun l cc d n thch hp m khng phi tr bt k ng no. Nh l mt ngun

    m, bn c th ti m ngun ca n v cng c th thc hin cc thay i theo yu cu ca bn.

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 6

    Tc nhanh v nh gn

    Khi hu ht cc thao tc c thc hin trong ni b, n mang li li ch rt ln v tc . Git

    khng ph thuc vo server, l l do ti sao m khng cn s tng tc vi server t xa cho

    mi thao tc. Phn ct li ca Git c vit bng C, m c th trnh c cc chi ph lin quan

    n thi gian chy vi ngn ng bc cao khc. Mc d Git phn nh ton b repository tr, kch

    thc ca cc d liu trn cc client l nh. iu ny cho thy s hiu qu ca Git trong vic nn

    v lu tr d liu trn cc client.

    D phng (sao lu) n

    Vic mt d liu l him khi xy ra khi m c rt nhiu bn sao ca n. D liu hin din bt k

    client no, do n c th c s dng trong trng hp hng hoc ngng server.

    An ton cao

    Git s dng mt hm bm (hash funtion) mt m chung c gi l hm bm an ton (SHA1),

    t tn v xc nh cc i tng trong c s d liu ca n. Mi tp tin v commit c kim tra

    tm tt v thu c kt qu ti thi gian kim tra. iu ny ng rng, n khng th thay i tp

    tin, ngy thng v thng bo commit v bt k d liu khc t c s d liu Git m khng hiu bit

    v Git.

    Khng yu cu mt phn cng mnh

    Trong trng hp CVCS, server trung tm cn mnh phc v cc yu cu ca ton team.

    i vi nhng team nh, n khng phi l mt vn , nhng khi kch thc team pht trin, th

    nhng hn ch ca phn cng server c th lm hiu sut ca cng vic thay i theo hng tiu

    cc. Trong trng hp DVCS, cc nh pht trin khng tng tc vi server tr khi h cn cng

    b nhng thay i thc hin. Tt c cc vic u din ra trn cc client, v th phn cng ca

    server c th thc s l vn n gin.

    Phn nhnh d dng hn

    CVCS s dng k thut sao chp r, nu chng ta to ra mt nhnh mi, n s sao chp tt c cc

    code ti nhnh mi, v th n tn thi gian, khng hiu sut. Ngoi ta, vic xa v sp nhp ca

    cc nhnh trong CVCS l phc tp v tn thi gian. Nhng qun l nhnh vi Git l rt n gin.

    N ch mt mt vi giy to, xoa, v nhp cc nhnh.

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 7

    Cc thut ng ca DVCS Kho commit ni b

    Mi cng c VCS cung cp mt ni lm vic ring nh l bn sao lm vic. Cc nh pht trin

    thc hin nhng thay i trong ni lm vic ring v sau khi commit ca h, nhng thay i tr

    thnh mt phn ca kho. Git c mt bc tin xa hn bng cch cung cp cho h mt bn sao

    ring ca ton b kho. Ngi s dng c th thc hin nhiu thao tc vi kho ny nh thm, di

    chuyn, i tn file, commit thay i v nhiu thao tc khc.

    Th mc lm vic v Staging hoc Index

    Th mc lm vic l ni cc file c kim tra. Trong CVCS, cc nh pht trin thng to cc

    thay i v commit cc thay i ca h mt cch trc tip ti kho cha. Nhng Git s dng mt

    phng thc khc. Git khng theo di tng file sa i. Bt c khi no bn th hin commit mt

    thao tc, Git tm kim file trong khu vc t chc (staging area). Ch khi no nhng file c mt trong

    khu vc t chc ny c xem xt commit m khng phi tt c cc file sa i.

    Sau y l tin trnh lm vic c bn ca Git:

    Bc 1 : Bn sa i mt file t th mc lm vic

    Bc 2 : Bn thm file vo khu vc t chc

    Bc 3 : Bn thc hin cc hot ng commit m di chuyn cc file t khu vc t chc. Sau thao

    tc y (push), n lu cc thay i c nh ti kho cha Git.

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 8

    Gi s bn sa i 2 file, tn l sort.c v search.c v bn mun thc hin hai commit khc nhau

    cho mi hot ng. Bn c th thm mt file vo khu vc t chc v thc hin commit. Sau hot

    ng commit u tin, lm li theo phng thc tng t cho file cn li.

    # First commit

    [bash]$ git add sort.c

    # adds file to the staging area

    [bash]$ git commit m Added sort operation

    # Second commit

    [bash]$ git add search.c

    # adds file to the staging area

    [bash]$ git commit m Added search operation

    Blobs

    Blob l vit tt ca Binary Large Object. Mi phin bn ca mt file c i din bi blob. Mt

    blob cha d liu file nhng khng cha bt k siu d liu no v file. N l mt tp tin nh phn,

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 9

    v trong c s d liu Git, n c t trn l SHA1 hash ca file . Trong Git, cc file khng

    c t bng tn. Tt c mi th c t a ch theo ni dung.

    Cy - Trees

    Cy (Tree) l mt i tng, m biu din mt th mc. N gi cc blob cng nh cc th mc

    ph khc. Mt cy l mt file nh phn m gi cc th lin quan n blob v cc cy cng c t

    tn l SHA1 hash ca i tng cy.

    K thc - Commits

    Hot ng commit gi trng thi hin ti ca repository. Mt commit cng c t tn l SHA1

    hash. Bn c th xem xt mt i tng commit nh l mt nt ca danh sch lin kt. Mi i

    tng commit c mt im con tr ti i tng commit gc. T mt commit cho, bn c th

    truy xt tr li bng cch nhn vo im con tr gc xem lch s ca commit . Nu mt

    commit c nhiu commit gc, th khi cc commit c th s c to bi cch sp nhp hai

    nhnh.

    Cc nhnh - Branches

    Cc nhnh c s dng to ra cc tuyn khc ca s pht trin. Theo mc nh, Git c mt

    nhnh ch, m tng t nh thn (trunk) trong Subversion. Thng thng, mt nhnh c to

    lm vic v mt im mi. Mt khi im ny c hon thnh, n c sp nhp li vi nhnh ch

    v chng ta xa nhnh i. Mi nhnh c m ch bi HEAD, m cc im con tr ti commit

    mi nht trong nhnh. Bt c khi no bn thc hin mt commit, HEAD c cp nht bi cc

    commit mi nht .

    Th - Tags

    Cc th ch mt tn c ngha vi mt phin bn xc nh trong kho cha. Cc th l tng t nh

    cc nhnh, nhng s khc nhau l cc th khng thay i c. N c ngha l, th l mt nhnh,

    m khng ai c nh sa chng. Mt khi mt th c to ra cho cc commit c th, ngay c khi

    bn to mt commit mi, n s khng c cp nht. Thng thng, cc nh pht trin to cc

    th cho cng b sn phm.

    M phng - Clone

    Hot ng m phng to bn sao ca repository. Hot ng ny khng ch kim tra vic sao chp,

    m cn phn nh kho ton b repository. Ngi s dng c th thc hin rt nhiu thao tc vi

    repository ni b. Cc mng thi gian ch c tham gia khi cc repository instance ang c

    ng b.

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 10

    Pull

    Hot ng pull sao chp nhng thay i t mt repository instance xa ti kho ni b. Hot ng

    ny c s dng ng b gia hai repository instance. iu ny tng t nh hot ng cp

    nht trong Subversion.

    Push

    Thao tc y (push) sao chp cc thay i t repository ni b ti mt kho xa. N c s dng

    lu cc thay i vnh vin trong repository Git. N tng t nh hot ng commit trong

    Subversion.

    HEAD

    HEAD l mt im con tr, m thng tr vo commit mi nht trong nhnh. Bt c khi no bn

    thc hin mt commit, HEAD c cp nht vi commit mi nht . u ca cc nhnh c lu

    trong .git/refs/heads/directory.

    [CentOS]$ ls -1 .git/refs/heads/

    master

    [CentOS]$ cat .git/refs/heads/master

    570837e7d58fa4bccd86cb575d884502188b0c49

    Revision

    Revision i din cho phin bn ca mt m ngun. Revision trong Git c i din bi cc

    commit. Nhng commit ny c xc nh bi SHA1 secure hash.

    URL

    URL i din cho v tr ca repository Git. Git URL c gi trong mt tp config.

    [tom@CentOS tom_repo]$ pwd

    /home/tom/tom_repo

    [tom@CentOS tom_repo]$ cat .git/config

    [core]

    repositoryformatversion = 0

    filemode = true

    bare = false

    logallrefupdates = true

    [remote "origin"]

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 11

    url = [email protected]:project.git

    fetch = +refs/heads/*:refs/remotes/origin/*

    Ci t mi trng Git Trc khi bn c th s dng Git, bn phi ci t v thc hin mt vi thay i v cu hnh. Di

    y l cc bc ci t Git client trn Ubuntu v Centos Linux.

    Ci t Git client Nu bn ang s dng Debian do GNU/Linux phn phi, lnh apt-get sau l cn thit thc hin.

    [ubuntu ~]$ sudo apt-get install git-core

    [sudo] password for ubuntu:

    [ubuntu ~]$ git --version

    git version 1.8.1.2

    V nu bn ang s dng RPM do GNU/Linux phn phi, bn s dng lnh yum nh di:

    [CentOS ~]$

    su -

    Password:

    [CentOS ~]# yum -y install git-core

    [CentOS ~]# git --version

    git version 1.7.1

    Ty chnh mi trng Git Git cung cp cng c cu hnh git cho php bn thit lp cc cu hnh a dng. Git lu tt c cc

    cu hnh chung trong tp .gitconfig c t trong th mc home ca bn. thit lp nhng gi

    tr cu hnh nh global, bn thm ty chn --global, v nu bn mun b ty chn ny i, th cc

    cu hnh ca bn c xc nh ring cho repository Git hin ti.

    Bn cng c th thit lp cu hnh rng ri cho h thng. Git lu cc gi tr trong tp /etc/gitconfig,

    m cha cu hnh cho mi ngi s dng v kho cha trn h thng. thit lp nhng gi tr

    ny, bn phi c root ng v s dng ty chn --system.

    Khi code trn c bin dch v thc hin, n to kt qu sau:

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 12

    Thit lp tn ngi s dng

    Thng tin ny c s dng bi Git cho mi commit.

    [jerry@CentOS project]$ git config --global user.name "Jerry Mouse"

    Thit lp email id

    Thng tin ny c s dng bi Git cho mi commit.

    [jerry@CentOS project]$ git config --global user.email "[email protected]"

    Trnh cc commit sp nhp khi pull

    Bn pull thay i mi nht t repository xa v nu nhng thay i ny l khc nhau hoc phn k,

    th khi theo mc nh Git to cc commit sp nhp. Chng ta c th trnh c iu ny theo

    cc thit lp sau:

    jerry@CentOS project]$ git config --global branch.autosetuprebase always

    Mu ni

    Cc lnh sau to mu ni trong bng iu khin Git.

    [jerry@CentOS project]$ git config --global color.ui true

    [jerry@CentOS project]$ git config --global color.status auto

    [jerry@CentOS project]$ git config --global color.branch auto

    Thit lp b son mc nh

    Theo mc nh, Git s dng h thng b son mc nh, m c ly t VISUAL hoc EDITOR.

    Bn c th nh mt b son khc bng cch s dng config.

    [jerry@CentOS project]$ git config --global core.editor vim

    Thit lp cng c sp nhp mc nh

    Git khng cung cp mt cng c sp nhp mc nh cho vic tng tc cc thay i i lp nhau

    trong cy lm vic. Bn c th thit lp cng c ny theo cc thit lp sau:

    [jerry@CentOS project]$ git config --global merge.tool vimdiff

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 13

    Lit k cc thit lp Git

    thm tra li cc thit lp Git trong repository ni b, s dng lnh git config list nh di y.

    [jerry@CentOS ~]$ git config --list

    Lnh trn s cho kt qu sau:

    user.name=Jerry Mouse

    [email protected]

    push.default=nothing

    branch.autosetuprebase=always

    color.ui=true

    color.status=auto

    color.branch=auto

    core.editor=vim

    merge.tool=vimdiff

    Vng i Git Trong chng ny, chng ta s bn lun v vng i ca Git. V chng sau, chng ta tm hiu

    qua cc lnh Git cho mi hot ng.

    Tin trnh lm vic tng qut nh sau:

    Bn m phng repository Git nh l bn sao lm vic.

    Bn chnh sa bn sao lm vic bng vic thm/sa cc file.

    Nu cn thit, bn c th cp nht bn sao lm vic bng cch thc hin cc thay i ca

    cc nh pht trin khc.

    Bn xem li cc thay i trc khi commit.

    Bn commit cc thay i. Nu mi th l tt, sau bn y nhng thay i ny ti

    repository.

    Sau khi commit, nu bn nhn ra mt vi th l sai, khi bn c th chnh li cho ng

    cc commit v y cc thay i ny ti repository.

    Di y l hnh nh biu th v tin trnh lm vic:

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 14

    Hot ng Create trong Git Trong chng ny, chng ta s hc cch to mt repository git t xa, t chng ta s cp n

    nh mt Git server. Chng ta cn mt Git server cho php team cng tc vi nhau.

    To mt ti khon s dng mi

    # add new group

    [root@CentOS ~]# groupadd dev

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 15

    # add new user

    [root@CentOS ~]# useradd -G devs -d /home/gituser -m -s /bin/bash gituser

    # change password

    [root@CentOS ~]# passwd gituser

    Lnh trn s to ra kt qu sau:

    Changing password for user gituser.

    New password:

    Retype new password:

    passwd: all authentication token updated successfully.

    To mt repository rng Hy cng chng ti khi chy mt repository mi bng cch s dng lnh init theo sau bi ty

    chn --bare. N khi chy repository m khng l mt th mc lm vic. Theo quy c, repository

    rng ny phi c t tn nh .git.

    [gituser@CentOS ~]$ pwd

    /home/gituser

    [gituser@CentOS ~]$ mkdir project.git

    [gituser@CentOS ~]$ cd project.git/

    [gituser@CentOS project.git]$ ls

    [gituser@CentOS project.git]$ git --bare init

    Initialized empty Git repository in /home/gituser-m/project.git/

    [gituser@CentOS project.git]$ ls

    branches config description HEAD hooks info objects refs

    Cp kha chung/ring Bn hy cng chng ti xem qua tin trnh ca vic nh cu hnh ca mt Git server, tin ch ssh-

    keygen cng cng/private RSA key pair, m chng ta s s dng xc nh ngi s dng.

    M mt terminal v nhp lnh sau v nhn enter cho mi ln nhp. Sau khi hon thnh, n s to

    mt th mc .ssh bn trong th mc home.

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 16

    tom@CentOS ~]$ pwd

    /home/tom

    [tom@CentOS ~]$ ssh-keygen

    Lnh trn s to ra kt qu sau:

    Generating public/private rsa key pair.

    Enter file in which to save the key (/home/tom/.ssh/id_rsa): Press Enter Only

    Created directory '/home/tom/.ssh'.

    Enter passphrase (empty for no passphrase): ---------------> Press Enter Only

    Enter same passphrase again: ------------------------------> Press Enter Only

    Your identification has been saved in /home/tom/.ssh/id_rsa.

    Your public key has been saved in /home/tom/.ssh/id_rsa.pub.

    The key fingerprint is:

    df:93:8c:a1:b8:b7:67:69:3a:1f:65:e8:0e:e9:25:a1 tom@CentOS

    The key's randomart image is:

    +--[ RSA 2048]----+

    | |

    | |

    | |

    |

    .

    |

    | Soo |

    | o*B. |

    | E = *.= |

    | oo==. . |

    | ..+Oo

    |

    +-----------------+

    ssh-keygen to hai kha, u tin l private (i.e., id_rsa) v th hai l public (i.e., id_rsa.pub).

    Ghi ch: Bn ng bao gi chia s kha Private ca bn vi ai khc.

    Thm cc kha vo cc kha c y quyn Gi s c hai nh lp trnh lm vic trn mt d n, tn l Tom v Jerry. C hai c kha generate

    public. Hy cng xem cch s dng cc kha ny xc nhn.

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 17

    Tom nhp kha chung ca anh ta ti server bng cch s dng lnh ssh-copy-id nh di y:

    [tom@CentOS ~]$ pwd

    /home/tom

    [tom@CentOS ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

    Lnh trn s to ra kt qu sau:

    [email protected]'s password:

    Now try logging into the machine, with "ssh '[email protected]'", and check in:

    .ssh/authorized_keys

    to make sure we haven't added extra keys that you weren't expecting.

    Theo cch tng t, Jerry nhp kha chung ti server bng cch s dng lnh ssh-copy-id.

    [jerry@CentOS ~]$ pwd

    /home/jerry

    [jerry@CentOS ~]$ ssh-copy-id -i ~/.ssh/id_rsa [email protected]

    Lnh trn s to ra kt qu sau:

    [email protected]'s password:

    Now try logging into the machine, with "ssh '[email protected]'", and check in:

    .ssh/authorized_keys

    to make sure we haven't added extra keys that you weren't expecting.

    y (Push) cc thay i ti repository Chng ta to ra mt repository rng trn server v cho php s truy cp ca hai ngi. T ,

    Tom v Jerry c th y cc thay i ca h ti repository bng cch thm n vo nh mt iu

    khin t xa.

    Lnh init to mt th mc .git gi siu d liu v repository mi lc n c cu hnh t tp

    .git/config.

    Tom to mt th mc mi, thm tp README, v commit thay i ca anh ta nh l commit u

    tin. Sau khi k tc, anh ta thm tra cc thng bo commit bng cch chy lnh git log.

    [tom@CentOS ~]$ pwd

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 18

    /home/tom

    [tom@CentOS ~]$ mkdir tom_repo

    [tom@CentOS ~]$ cd tom_repo/

    [tom@CentOS tom_repo]$ git init

    Initialized empty Git repository in /home/tom/tom_repo/.git/

    [tom@CentOS tom_repo]$ echo 'TODO: Add contents for README' > README

    [tom@CentOS tom_repo]$ git status -s

    ?? README

    [tom@CentOS tom_repo]$ git add .

    [tom@CentOS tom_repo]$ git status -s

    A README

    [tom@CentOS tom_repo]$ git commit -m 'Initial commit'

    Lnh trn to kt qu sau:

    [master (root-commit) 19ae206] Initial commit

    1 files changed, 1 insertions(+), 0 deletions(-)

    create mode 100644 README

    Tom kim tra thng bo log bng cch thc hin lnh git log.

    [tom@CentOS tom_repo]$ git log

    Lnh trn s to kt qu sau:

    commit 19ae20683fc460db7d127cf201a1429523b0e319

    Author: Tom Cat

    Date: Wed Sep 11 07:32:56 2013 +0530

    Initial commit

    Tom commit thay i ca anh ta ti repository ni b. By gi l thi gian y nhng thay i

    ti repository t xa. Nhng trc , chng ta phi thm repository ny nh mt iu khin t xa,

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 19

    y l mt hot ng mt ln. Sau , anh ta c th y nhng thay i ny ti repository t xa

    mt cch an ton.

    Ghi ch: Theo mc nh, Git ch y ti nhng nhnh kt ni: cho mi nhnh m tn ti trn side

    ni b, side iu khin t xa c cp nht nu mt nhnh vi cng tn tn ti trn . Trong

    phn hng dn ca chng ti, mi khi chng ti y nhng thay i ti nhnh origin master,

    chng ti s dng chnh xc tn nhnh.

    [tom@CentOS tom_repo]$ git remote add origin [email protected]:project.git

    [tom@CentOS tom_repo]$ git push origin master

    Lnh trn s to ra kt qu:

    Counting objects: 3, done.

    Writing objects: 100% (3/3), 242 bytes, done.

    Total 3 (delta 0), reused 0 (delta 0)

    To [email protected]:project.git

    * [new branch]

    master > master

    By gi, nhng thay i ny c commit thnh cng ti repository iu khin t xa.

    Hot ng Clone trong Git Chng ta c mt repository rng trn my ch v Tom cng y phin bn u tin ca anh ta. By

    gi, Jerry c th quan st nhng thay i ca anh ta. Hot ng clone (to bn sao) to mt

    instance ca repository t xa.

    Jerry to mt th mc mi trong th mc home ca anh ta v thc hin hot ng m phng.

    [jerry@CentOS ~]$ mkdir jerry_repo

    [jerry@CentOS ~]$ cd jerry_repo/

    [jerry@CentOS jerry_repo]$ git clone [email protected]:project.git

    Lnh trn s to kt qu sau:

    Initialized empty Git repository in /home/jerry/jerry_repo/project/.git/

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 20

    remote: Counting objects: 3, done.

    Receiving objects: 100% (3/3), 241 bytes, done.

    remote: Total 3 (delta 0), reused 0 (delta 0)

    Jerry thay i th mc ny ti th mc ni b mi v lit k ni dung th mc ca n.

    [jerry@CentOS jerry_repo]$ cd project/

    [jerry@CentOS jerry_repo]$ ls

    README

    Thc hin thay i trong Git Jerry to mt bn sao repository trn my anh ta v quyt nh thc hin cc chui hot ng c

    bn. V th anh ta to tp string.c. Sau khi thm ni dung, string.c s trng ging nh sau:

    #include

    int my_strlen(char *s)

    {

    char *p = s;

    while (*p)

    ++p;

    return (p - s);

    }

    int main(void)

    {

    int i;

    char *s[] =

    {

    "Git tutorials",

    "Tutorials Point"

    };

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 21

    for (i = 0; i < 2; ++i)

    printf("string lenght of %s = %d\n", s[i], my_strlen(s[i]));

    return 0;

    }

    Anh ta bin dch v kim tra code ca anh ta v mi th lm vic bnh thng. By gi anh ta c

    th thm nhng thay i ny ti repository mt cch an ton.

    Git thm hot ng thm file ti khu vc staging.

    [jerry@CentOS project]$ git status -s

    ?? string

    ?? string.c

    [jerry@CentOS project]$ git add string.c

    Git ang ch mt cu hi nh du trc khi t tn file. R rng l cc file ny khng l mt phn

    ca Git, v l ti sao Git khng bit phi lm iu g vi nhng file ny. l ti sao, Git ang

    ch mt cu hi nh du trc khi t tn file.

    Jerry thm file ti khu vc staging, lnh git status s ch cc file c trong khu vc ny.

    [jerry@CentOS project]$ git status -s

    A string.c

    ?? string

    commit nhng thay i, anh ta s dng lnh git commit theo sau bi ty chn m. Nu chng ta

    qun ty chn m. Git s m mt text editor, ti chng ta c th vit thng tin commit nhiu

    dng.

    [jerry@CentOS project]$ git commit -m 'Implemented my_strlen function'

    Lnh trn s to ra kt qu:

    [master cbe1249] Implemented my_strlen function

    1 files changed, 24 insertions(+), 0 deletions(-)

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 22

    create mode 100644 string.c

    Sau khi commit xem chi tit log, anh ta chy lnh git log. N s hin th thng tin ca tt c cc

    commit vi ID k thc, tc gi k thc, ngy commit v SHA-1 hash ca k thc.

    [jerry@CentOS project]$ git log

    Lnh trn s to ra kt qu:

    commit cbe1249b140dad24b2c35b15cc7e26a6f02d2277

    Author: Jerry Mouse

    Date: Wed Sep 11 08:05:26 2013 +0530

    Implemented my_strlen function

    commit 19ae20683fc460db7d127cf201a1429523b0e319

    Author: Tom Cat

    Date: Wed Sep 11 07:32:56 2013 +0530

    Initial commit

    Review thay i trong Git Sau khi kim tra li cc chi tit k thc, Jerry nhn thy rng di chui khng th m, do anh

    ta quyt nh thay i kiu ca chc nng my_strlen.

    Jerry s dng lnh git log quan st cc chi tit log.

    [jerry@CentOS project]$ git log

    Lnh trn s to kt qu sau:

    commit cbe1249b140dad24b2c35b15cc7e26a6f02d2277

    Author: Jerry Mouse

    Date: Wed Sep 11 08:05:26 2013 +0530

    Implemented my_strlen function

    Jerry s dng lnh git show kim tra cc chi tit k thc. Git ch r lnh c ly t ID k thc

    SHA-1 nh l mt tham s.

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 23

    [jerry@CentOS project]$ git show cbe1249b140dad24b2c35b15cc7e26a6f02d2277

    Lnh trn s to kt qu sau:

    commit cbe1249b140dad24b2c35b15cc7e26a6f02d2277

    Author: Jerry Mouse

    Date: Wed Sep 11 08:05:26 2013 +0530

    Implemented my_strlen function

    diff --git a/string.c b/string.c

    new file mode 100644

    index 0000000..187afb9

    --- /dev/null

    +++ b/string.c

    @@ -0,0 +1,24 @@

    +#include

    +

    +int my_strlen(char *s)

    +{

    +

    char *p = s;

    +

    +

    while (*p)

    + ++p;

    + return (p -s );

    +

    }

    +

    Anh ta thay i kiu ca chc nng t int n size_t. Sau khi kim tra code, anh ta duyt li cc

    thay i bng cch chy lnh git diff.

    [jerry@CentOS project]$ git diff

    Lnh trn s to kt qu sau:

    diff --git a/string.c b/string.c

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 24

    index 187afb9..7da2992 100644

    --- a/string.c

    +++ b/string.c

    @@ -1,6 +1,6 @@

    #include

    -int my_strlen(char *s)

    +size_t my_strlen(char *s)

    {

    char *p = s;

    @@ -18,7 +18,7 @@ int main(void)

    };

    for (i = 0; i < 2; ++i)

    {

    - printf("string lenght of %s = %d\n", s[i], my_strlen(s[i]));

    + printf("string lenght of %s = %lu\n", s[i], my_strlen(s[i]));

    return 0;

    }

    Git diff ch k hiu + trc cc dng m thm mi v thm - cho cc dng b xa.

    Commit trong Git Jerry k thc cc thay i v anh ta mun chnh sa cho ng cc k thc va qua ca anh ta.

    Trong trng hp ny, chc nng git amend s gip lm iu ny. Chc nng ny thay i k thc

    va qua bao gm c thng bo k thc ca bn; n to ra mt ID k thc mi.

    Trc khi hot ng ty chnh, anh ta kim tra k thc log.

    [jerry@CentOS project]$ git log

    Lnh trn s to ra kt qu sau:

    commit cbe1249b140dad24b2c35b15cc7e26a6f02d2277

    Author: Jerry Mouse

    Date: Wed Sep 11 08:05:26 2013 +0530

    Implemented my_strlen function

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 25

    commit 19ae20683fc460db7d127cf201a1429523b0e319

    Author: Tom Cat

    Date: Wed Sep 11 07:32:56 2013 +0530

    Initial commit

    Jerry k thc cc thay i mi vi chc nng --amend, v quan st k thc log.

    [jerry@CentOS project]$ git status -s

    M string.c

    ?? string

    [jerry@CentOS project]$ git add string.c

    [jerry@CentOS project]$ git status -s

    M string.c

    ?? string

    [jerry@CentOS project]$ git commit --amend -m 'Changed return type of my_strlen to size_t'

    [master d1e19d3] Changed return type of my_strlen to size_t

    1 files changed, 24 insertions(+), 0 deletions(-)

    create mode 100644 string.c

    By gi git log s ch mt thng bo k thc mi vi ID k thc mi:

    [jerry@CentOS project]$ git log

    Lnh trn s to ra kt qu sau:

    commit d1e19d316224cddc437e3ed34ec3c931ad803958

    Author: Jerry Mouse

    Date: Wed Sep 11 08:05:26 2013 +0530

    Changed return type of my_strlen to size_t

    commit 19ae20683fc460db7d127cf201a1429523b0e319

    Author: Tom Cat

    Date: Wed Sep 11 07:32:56 2013 +0530

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 26

    Initial commit

    Hot ng Push trong HTML Jerry chnh sa cc commit trc bng cch s dng hot ng amend v anh ta sn sng

    push cc thay i. Hot ng push lu d liu vnh cu ti repository Git. Sau mt hot ng push

    thnh cng, cc nh lp trnh khc c th quan st cc thay i ca Jerry.

    Anh ta thc hin lnh git log quan st cc chi tit commit .

    [jerry@CentOS project]$ git log

    Lnh trn s to ra kt qu:

    commit d1e19d316224cddc437e3ed34ec3c931ad803958

    Author: Jerry Mouse

    Date: Wed Sep 11 08:05:26 2013 +0530

    Changed return type of my_strlen to size_t

    Trc khi thc hin thao tc push, anh ta mun duyt li cc thay i ca anh ta, v th anh ta s

    dng lnh git log duyt cc thay i.

    [jerry@CentOS project]$ git show d1e19d316224cddc437e3ed34ec3c931ad803958

    Lnh trn s to ra kt qu:

    commit d1e19d316224cddc437e3ed34ec3c931ad803958

    Author: Jerry Mouse

    Date: Wed Sep 11 08:05:26 2013 +0530

    Changed return type of my_strlen to size_t

    diff --git a/string.c b/string.c

    new file mode 100644

    index 0000000..7da2992

    --- /dev/null

    +++ b/string.c

    @@ -0,0 +1,24 @@

    +#include

    +

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 27

    +size_t my_strlen(char *s)

    +

    {

    +

    char *p = s;

    +

    +

    while (*p)

    + ++p;

    + return (p -s );

    +

    }

    +

    +int main(void)

    +

    {

    + int i;

    + char *s[] =

    {

    + "Git tutorials",

    + "Tutorials Point"

    +

    };

    +

    +

    +

    for (i = 0; i < 2; ++i)

    printf("string lenght of %s = %lu\n", s[i], my_strlen(s[i]));

    +

    +

    return 0;

    +

    }

    Jerry vui mng vi nhng thay i ca anh ta v sn sng push nhng thay i ny.

    [jerry@CentOS project]$ git push origin master

    Lnh trn s to ra kt qu sau:

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 28

    Counting objects: 4, done.

    Compressing objects: 100% (3/3), done.

    Writing objects: 100% (3/3), 517 bytes, done.

    Total 3 (delta 0), reused 0 (delta 0)

    To [email protected]:project.git

    19ae206..d1e19d3 master > master

    Cc thay i ca Jerry c push thnh cng n repository; by gi cc nh lp trnh khc c

    th quan st nhng thay i ny bng cch thc hin hot ng m phng hoc cp nht.

    Hot ng Update trong Git Ty chnh cc chc nng ang tn ti Tom thc hin hot ng m phng v thy mt file mi string.c. Anh ta mun bit ai thm file

    ny vo kho cha v vi mc ch g, v th anh ta chy lnh git log.

    [tom@CentOS ~]$ git clone [email protected]:project.git

    Lnh ny s to ra kt qu sau:

    Initialized empty Git repository in /home/tom/project/.git/

    remote: Counting objects: 6, done.

    remote: Compressing objects: 100% (4/4), done.

    Receiving objects: 100% (6/6), 726 bytes, done.

    remote: Total 6 (delta 0), reused 0 (delta 0)

    Hot ng m phng s to mt th mc mi bn trong th mc lm vic hin ti. Anh ta thay i

    th mc ti th mc va to mi v chy lnh git log.

    [tom@CentOS ~]$ cd project/

    [tom@CentOS project]$ git log

    Lnh trn s to ra kt qu sau:

    commit d1e19d316224cddc437e3ed34ec3c931ad803958

    Author: Jerry Mouse

    Date: Wed Sep 11 08:05:26 2013 +0530

    Changed return type of my_strlen to size_t

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 29

    commit 19ae20683fc460db7d127cf201a1429523b0e319

    Author: Tom Cat

    Date: Wed Sep 11 07:32:56 2013 +0530

    Initial commit

    Sau khi quan st log, anh ta nhn thy rng tp string.c c thm bi Jerry thc hin cc hot

    ng chui c bn. Anh ta mun bit v on code ca Jerry. V th anh ta m string.c trong text

    editor v ngay lp tc tm thy mt bug (li). Trong chc nng my_strlen, Jerry khng s dng

    im tr hng s. V th, anh ta quyt nh chnh sa code ca Jerry. Sau khi thc hin chnh sa,

    on code trng nh sau:

    [tom@CentOS project]$ git diff

    Lnh trn s to ra kt qu sau:

    diff --git a/string.c b/string.c

    index 7da2992..32489eb 100644

    --- a/string.c

    +++ b/string.c

    @@ -1,8 +1,8 @@

    #include

    -size_t my_strlen(char *s)

    +size_t my_strlen(const char *s)

    {

    - char *p = s;

    + const char *p = s;

    while (*p)

    ++p;

    }

    Sau khi kim tra, anh ta commit cc thay i ca anh ta.

    [tom@CentOS project]$ git status -s

    M string.c

    ?? string

    [tom@CentOS project]$ git add string.c

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 30

    [tom@CentOS project]$ git commit -m 'Changed char pointer to const char pointer'

    [master cea2c00] Changed char pointer to const char pointer

    1 files changed, 2 insertions(+), 2 deletions(-)

    [tom@CentOS project]$ git log

    Lnh trn s to ra kt qu sau:

    commit cea2c000f53ba99508c5959e3e12fff493b

    Author: Tom Cat

    Date: Wed Sep 11 08:32:07 2013 +0530

    Changed char pointer to const char pointer

    commit d1e19d316224cddc437e3ed34ec3c931ad803958

    Author: Jerry Mouse

    Date: Wed Sep 11 08:05:26 2013 +0530

    Changed return type of my_strlen to size_t

    commit 19ae20683fc460db7d127cf201a1429523b0e319

    Author: Tom Cat

    Date: Wed Sep 11 07:32:56 2013 +0530

    Initial commit

    Tom s dng lnh git push push cc thay i ca anh ta.

    [tom@CentOS project]$ git push origin master

    Lnh trn s to ra kt qu sau:

    Counting objects: 5, done.

    Compressing objects: 100% (3/3), done.

    Writing objects: 100% (3/3), 336 bytes, done.

    Total 3 (delta 1), reused 0 (delta 0)

    To [email protected]:project.git

    d1e19d3..cea2c00 master > master

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 31

    Thm chc nng mi Trong khi y, Jerry quyt nh thc hin chc nng string compare. V th anh ta chnh sa

    string.c. Sau khi ty chnh, file trng nh sau:

    [jerry@CentOS project]$ git diff

    Lnh trn s to ra kt qu sau:

    index 7da2992..bc864ed 100644

    --- a/string.c

    +++ b/string.c

    30Git Tutorials

    @@ -9,9 +9,20 @@ size_t my_strlen(char *s)

    return (p -s );

    }

    +char *my_strcpy(char *t, char *s)

    +

    {

    +

    char *p = t;

    +

    + while (*t++ = *s++)

    + ;

    +

    +

    return p;

    +

    }

    +

    int main(void)

    {

    int i;

    +

    char p1[32];

    char *s[] =

    {

    "Git tutorials",

    "Tutorials Point"

    @@ -20,5 +31,7 @@ int main(void)

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 32

    for (i = 0; i < 2; ++i)

    printf("string lenght of %s = %lu\n", s[i], my_strlen(s[i]));

    +

    printf("%s\n", my_strcpy(p1, "Hello, World !!!"));

    +

    return 0;

    }

    }

    Sau khi kim tra, anh ta sn sng push thay i ny.

    [jerry@CentOS project]$ git status -s

    M string.c

    ?? string

    [jerry@CentOS project]$ git add string.c

    [jerry@CentOS project]$ git commit -m "Added my_strcpy function"

    [master e944e5a] Added my_strcpy function

    1 files changed, 13 insertions(+), 0 deletions(-)

    Trc khi thc hin hot ng push, anh ta chnh sa commit bng cch quan st cc thng bo

    log.

    [jerry@CentOS project]$ git log

    Lnh trn s to ra kt qu sau:

    commit e944e5aab74b26e7447d3281b225309e4e59efcd

    Author: Jerry Mouse

    Date: Wed Sep 11 08:41:42 2013 +0530

    Added my_strcpy function

    commit d1e19d316224cddc437e3ed34ec3c931ad803958

    Author: Jerry Mouse

    Date: Wed Sep 11 08:05:26 2013 +0530

    Changed return type of my_strlen to size_t

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 33

    commit 19ae20683fc460db7d127cf201a1429523b0e319

    Author: Tom Cat

    Date: Wed Sep 11 07:32:56 2013 +0530

    Initial commit

    Jerry vui mng vi nhng thay i ny v anh ta mun thc hin push nhng thay i .

    [jerry@CentOS project]$ git push origin master

    Lnh trn s to ra kt qu sau:

    To [email protected]:project.git

    ! [rejected]

    master > master (non-fast-forward)

    error: failed to push some refs to '[email protected]:project.git'

    To prevent you from losing history, non-fast-forward updates were rejected

    Merge the remote changes before pushing again. See the 'Note about

    fast-forwards' section of 'git push --help' for details.

    Nhng Git khng cho php Jerry push nhng thay i ca anh ta. Bi v Git nhn nh rng

    repository t xa v repository ni b ca Jerry l khng ng b. V iu , anh ta c th lc mt

    lch s ca d n. trnh m hn n ny, Git qun hot ng ny. By gi, Jerry u tin phi

    cp nht repository ni b v ch khi sau , anh ta c th push nhng thay i ring ny.

    Gi ra nhng thay i mi nht Jerry chy lnh git pull ng b ha repository ni b ca anh ta vi repository t xa.

    [jerry@CentOS project]$ git pull

    Lnh trn s to ra kt qu sau:

    remote: Counting objects: 5, done.

    remote: Compressing objects: 100% (3/3), done.

    remote: Total 3 (delta 1), reused 0 (delta 0)

    Unpacking objects: 100% (3/3), done.

    From git.server.com:project

    d1e19d3..cea2c00 master > origin/master

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 34

    First, rewinding head to replay your work on top of it...

    Applying: Added my_strcpy function

    Sau khi thc hin hot ng pull, Jerry kim tra cc thng bo log v tm thy cc chi tit commit

    ca Tom vi ID commit cea2c000f53ba99508c5959e3e12fff493ba6f69.

    [jerry@CentOS project]$ git log

    Lnh trn s to ra kt qu sau:

    commit e86f0621c2a3f68190bba633a9fe6c57c94f8e4f

    Author: Jerry Mouse

    Date: Wed Sep 11 08:41:42 2013 +0530

    Added my_strcpy function

    commit cea2c000f53ba99508c5959e3e12fff493ba6f69

    Author: Tom Cat

    Date: Wed Sep 11 08:32:07 2013 +0530

    Changed char pointer to const char pointer

    commit d1e19d316224cddc437e3ed34ec3c931ad803958

    Author: Jerry Mouse

    Date: Wed Sep 11 08:05:26 2013 +0530

    Changed return type of my_strlen to size_t

    commit 19ae20683fc460db7d127cf201a1429523b0e319

    Author: Tom Cat

    Date: Wed Sep 11 07:32:56 2013 +0530

    Initial commit

    By gi repository ni b ca Jerry c ng b y vi repository t xa. V th anh ta c

    th push nhng thay i mt cch an ton.

    [jerry@CentOS project]$ git push origin master

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 35

    Lnh trn s to ra kt qu:

    Counting objects: 5, done.

    Compressing objects: 100% (3/3), done.

    Writing objects: 100% (3/3), 455 bytes, done.

    Total 3 (delta 1), reused 0 (delta 0)

    To [email protected]:project.git

    cea2c00..e86f062 master > master

    Hot ng Stash trong Git Gi s bn ang thc hin mt tnh nng mi ca sn phm ca bn. Code ca bn ang trong

    tin trnh thc hin th t nhin mt v khch n thm. Bi v iu ny, bn phi i ra ngoi trong

    mt vi gi. Bn khng th commit phn code ny v cng khng th qung n i u nhng thay

    i ca bn. V th bn cn mt vi khng gian tm thi, m ti ni bn c th gi nhng thay

    i cc b ny v sau quay li commit n.

    Trong Git, hot ng stash gip bn stash nhng file c chnh sa, nhng thay i, v lu

    chng trn mt stack ca nhng thay i cha c hon thnh, v t bn c th ng dng li

    bt c lc no.

    [jerry@CentOS project]$ git status -s

    M string.c

    ?? string

    By gi bn mun chuyn branch do mt v khch n bt ng, nhng bn khng mun commit

    nhng g bn lm trn , v th bn stash nhng thay i i. y nhng ci stash mi ny

    vo trong stack, bn chy lnh git stash.

    [jerry@CentOS project]$ git stash

    Saved working directory and index state WIP on master: e86f062 Added my_strcpy function

    HEAD is now at e86f062 Added my_strcpy function

    By gi th mc lm vic ca bn sch trn v tt c nhng thay i c gi trn mt stack.

    Hy cng thm tra li n vi lnh git status.

    [jerry@CentOS project]$ git status -s

    ?? string

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 36

    By gi bn c th chuyn branch mt cch an ton v lm bt c iu g. Chng ta c th quan

    st danh sch ca nhng thay i c stash bng cch s dng lnh git stash list.

    [jerry@CentOS project]$ git stash list

    stash@{0}: WIP on master: e86f062 Added my_strcpy function

    Gi s bn gii quyt xong vic vi v khch hng mi n v bn quay tr li lm on code

    ang d dang ca mnh, bn ch cn thc hin ln git stash pop, di chuyn nhng thay i t

    stack v t chng vo th mc lm vic hin ti.

    [jerry@CentOS project]$ git status -s

    ?? string

    [jerry@CentOS project]$ git stash pop

    Lnh trn s to ra kt qu sau:

    # On branch master

    # Changed but not updated:

    # (use "git add ..." to update what will be committed)

    # (use "git checkout -- ..." to discard changes in working directory)

    #

    #

    modified: string.c

    #

    # Untracked files:

    # (use "git add ..." to include in what will be committed)

    #

    #

    string

    no changes added to commit (use "git add" and/or "git commit -a")

    Dropped refs/stash@{0} (36f79dfedae4ac20e2e8558830154bd6315e72d4)

    [jerry@CentOS project]$ git status -s

    M string.c

    ?? string

    Hot ng Move trong Git

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 37

    Nh tn gi cp, hot ng move di mt th mc hoc mt file t mt v tr ny n mt v

    tr khc. Tom quyt nh di chuyn code ngun vo trong th mc src. Cu trc th mc c

    chnh sa ny s xut hin nh sau:

    [tom@CentOS project]$ pwd

    /home/tom/project

    [tom@CentOS project]$ ls

    README string string.c

    [tom@CentOS project]$ mkdir src

    [tom@CentOS project]$ git mv string.c src/

    [tom@CentOS project]$ git status -s

    R string.c > src/string.c

    ?? string

    to ra cc thay i c nh (vnh vin), chng ti push cu trc th mc c chnh sa ti

    repository t xa m cc nh lp trnh khc c th thy n.

    [tom@CentOS project]$ git commit -m "Modified directory structure"

    [master 7d9ea97] Modified directory structure

    1 files changed, 0 insertions(+), 0 deletions(-)

    rename string.c => src/string.c (100%)

    [tom@CentOS project]$ git push origin master

    Counting objects: 4, done.

    Compressing objects: 100% (2/2), done.

    Writing objects: 100% (3/3), 320 bytes, done.

    Total 3 (delta 0), reused 0 (delta 0)

    To [email protected]:project.git

    e86f062..7d9ea97 master > master

    Trong th mc ni b ca Jerry, trc khi thc hin hot ng pull, n s ch cu trc th mc c.

    [jerry@CentOS project]$ pwd

    /home/jerry/jerry_repo/project

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 38

    [jerry@CentOS project]$ ls

    README string string.c

    Nhng sau khi pull xong, cu trc th mc s c cp nht. By gi, Jerry c th nhn thy th

    mc src v cc file c mt bn trong th mc .

    [jerry@CentOS project]$ git pull

    remote: Counting objects: 4, done.

    remote: Compressing objects: 100% (2/2), done.

    remote: Total 3 (delta 0), reused 0 (delta 0)

    Unpacking objects: 100% (3/3), done.

    From git.server.com:project

    e86f062..7d9ea97 master > origin/master

    First, rewinding head to replay your work on top of it...

    Fast-forwarded master to 7d9ea97683da90bcdb87c28ec9b4f64160673c8a.

    [jerry@CentOS project]$ ls

    README src string

    [jerry@CentOS project]$ ls src/

    string.c

    Hot ng Rename trong Git Ti gi, c Tom v Jerry u ang s dng cc lnh bng tay bin dch d n ca h. By gi,

    Jerry quyt nh to Makefile cho d n v cng t mt tn cho file string.c.

    [jerry@CentOS project]$ pwd

    /home/jerry/jerry_repo/project

    [jerry@CentOS project]$ ls

    README src

    [jerry@CentOS project]$ cd src/

    [jerry@CentOS src]$ git add Makefile

    [jerry@CentOS src]$ git mv string.c string_operations.c

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 39

    [jerry@CentOS src]$ git status -s

    A Makefile

    R string.c > string_operations.c

    Git ang ch R trc tn file ch rng file c t li tn.

    i vi hot ng commit, Jerry s dng ui -a, m lm cho git commit t ng tm cc file

    c chnh sa.

    [jerry@CentOS src]$ git commit -a -m 'Added Makefile and renamed strings.c to

    string_operations.c '

    [master 94f7b26] Added Makefile and renamed strings.c to string_operations.c

    1 files changed, 0 insertions(+), 0 deletions(-)

    create mode 100644 src/Makefile

    rename src/{string.c => string_operations.c} (100%)

    Sau khi commit, anh ta push nhng thay i ca anh ta ti repository.

    [jerry@CentOS src]$ git push origin master

    Lnh trn s to ra kt qu sau:

    Counting objects: 6, done.

    Compressing objects: 100% (3/3), done.

    Writing objects: 100% (4/4), 396 bytes, done.

    Total 4 (delta 0), reused 0 (delta 0)

    To [email protected]:project.git

    7d9ea97..94f7b26 master > master

    By gi, cc nh lp trnh khc c th quan st nhng thay i bng cch cp nht repository ni

    b ca h.

    Hot ng Delete trong Git Tom cp nht repository ni b v tm m nh phn c bin dch trong th mc src. Sau khi quan

    st thng bo commit, anh ta nhn ra rng m ny c thm bi Jerry.

    [tom@CentOS src]$ pwd

    /home/tom/project/src

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 40

    [tom@CentOS src]$ ls

    Makefile string_operations string_operations.c

    [tom@CentOS src]$ file string_operations

    string_operations: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked

    (uses

    shared libs), for GNU/Linux 2.6.18, not stripped

    [tom@CentOS src]$ git log

    commit 29af9d45947dc044e33d69b9141d8d2dad37cc62

    Author: Jerry Mouse

    Date: Wed Sep 11 10:16:25 2013 +0530

    Added compiled binary

    VCS ch c s dng gi cc code ngun v khng biu din c cc m nh phn. V th,

    Tom quyt nh dn cc file ny t trong repository. Vi cc thao tc xa hn, anh ta s dng lnh

    git rm.

    [tom@CentOS src]$ ls

    Makefile string_operations string_operations.c

    [tom@CentOS src]$ git rm string_operations

    rm 'src/string_operations'

    [tom@CentOS src]$ git commit -a -m "Removed executable binary"

    [master 5776472] Removed executable binary

    1 files changed, 0 insertions(+), 0 deletions(-)

    delete mode 100755 src/string_operations

    Sau khi commit, anh ta push nhng thay i ti repository.

    [tom@CentOS src]$ git push origin master

    Lnh trn s to ra kt qu sau:

    Counting objects: 5, done.

    Compressing objects: 100% (3/3), done.

    Writing objects: 100% (3/3), 310 bytes, done.

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 41

    Total 3 (delta 1), reused 0 (delta 0)

    To [email protected]:project.git

    29af9d4..5776472 master > master

    Sa li trong Git l con ngi th ai cng phm sai lm. V th mi VCS u cung cp mt tnh nng sa li

    ti mt im no . Git cung cp mt tnh nng m chng ta c th s dng undo cc chnh

    sa m lm trn repository ni b.

    Gi s ngi s dng tnh c thc hin mt vi thay i ti repository ni b v sau mun undo

    nhng thay i ny. Trong nhng trng hp nh vy, hot ng revert ng mt vai tr quan

    trng.

    Tr li nhng thay i cha c commit Gi s Jerry tnh c chnh sa mt file t trong repository ni b ca anh ta. Nhng anh ta mun

    undo li ty chnh ny. gii quyt tnh hung ny, chng ta s dng lnh git checkout. Chng ta

    c th s dng lnh ny tr li nhng ni dung ca mt file.

    [jerry@CentOS src]$ pwd

    /home/jerry/jerry_repo/project/src

    [jerry@CentOS src]$ git status -s

    M string_operations.c

    [jerry@CentOS src]$ git checkout string_operations.c

    [jerry@CentOS src]$ git status s

    Xa hn na, chng ta c th s dng lnh git checkout t c cc file b xa t repository ni

    b. Gi s Tom xa mt file t repository ni b v anh ta mun s dng file ny sau . Chng ta

    c th thc hin c iu ny bng cch s dng lnh ging trn.

    [tom@CentOS src]$ pwd

    /home/tom/top_repo/project/src

    [tom@CentOS src]$ ls -1

    Makefile

    string_operations.c

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 42

    [tom@CentOS src]$ rm string_operations.c

    [tom@CentOS src]$ ls -1

    Makefile

    [tom@CentOS src]$ git status -s

    D string_operations.c

    Git ang ch k t D trc tn file. iu ny ch rng file c xa t repository ni b.

    [tom@CentOS src]$ git checkout string_operations.c

    [tom@CentOS src]$ ls -1

    Makefile

    string_operations.c

    [tom@CentOS src]$ git status -s

    Ghi ch: Chng ta c th thc hin tt c nhng hot ng ny trc hot ng commit.

    D b nhng thay i t khu vc t chc Chng ta nhn thy khi chng ta chng ta thc hin mt hot ng thm, cc file di chuyn t

    repository ni b ti khu vc t chc. Nu mt ngi s dng tnh c chnh sa mt file v thm

    n vo trong khu vc t chc, anh ta c th tr li nhng thay i, bng cch s dng lnh git

    checkout.

    Trong Git, c mt im con tr HEAD m lun lun tr ti commit mi nht. Nu bn mun undo

    mt s thay i t khu vc t chc, th khi bn c th s dng lnh git checkout, nhng vi

    lnh ny, bn phi cung cp thm mt tham s, i.e., im tr HEAD. tham s con tr commit thm

    vo ny ch th lnh git checkout reset cy lm vic v cng d b nhng thay i c t

    chc.

    Gi s Tom chnh sa mt file t repository ni b. Nu chng ta quan st trng thi ca file ny,

    n s ch rng file ny c chnh sa nhng khng thm vo trong khu vc t chc.

    tom@CentOS src]$ pwd

    /home/tom/top_repo/project/src

    # Unmodified file

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 43

    [tom@CentOS src]$ git status -s

    # Modify file and view its status.

    [tom@CentOS src]$ git status -s

    M string_operations.c

    [tom@CentOS src]$ git add string_operations.c

    Git status ch rng file hin din trong khu vc t chc, by gi tr li n bng cch s dng lnh

    git command v quan st trng thi ca file c tr li.

    [tom@CentOS src]$ git checkout HEAD -- string_operations.c

    [tom@CentOS src]$ git status -s

    Di chuyn im tr HEAD vi git reset Sau khi thc hin mt s thay i, bn c th quyt nh d b nhng thay i ny. Lnh git reset

    c s dng reset hoc tr li nhng thay i. Chng ta c th thc hin ba kiu khc nhau

    ca hot ng reset.

    S di ch tin trnh ca lnh git reset.

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 44

    Soft

    Mi nhnh c mt im tr HEAD, m tr vo commit mi nht. Nu chng ta s dng lnh git

    reset vi ty chn --soft theo sau bng ID commit, th khi n s ch reset im tr HEAD m

    khng ph hy bt c th g.

    Tp .git/refs/heads/master gi ID commit ca im tr HEAD. Chng ta c th thm tra n bng

    cch s dng lnh git log-1.

    [jerry@CentOS project]$ cat .git/refs/heads/master

    577647211ed44fe2ae479427a0668a4f12ed71a1

    By gi, quan st cc ID commit mi nht, m s kt ni vi ID commit trn.

    [jerry@CentOS project]$ git log -2

    Lnh trn s to ra kt qu sau:

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 45

    commit 577647211ed44fe2ae479427a0668a4f12ed71a1

    Author: Tom Cat

    Date: Wed Sep 11 10:21:20 2013 +0530

    Removed executable binary

    commit 29af9d45947dc044e33d69b9141d8d2dad37cc62

    Author: Jerry Mouse

    Date: Wed Sep 11 10:16:25 2013 +0530

    Added compiled binary

    Hy cng chng ti reset im tr HEAD.

    [jerry@CentOS project]$ git reset --soft HEAD~

    By gi chng ta va reset im tr HEAD tr li sau mt v tr. Chng ta kim tra li ni dung ca

    tp .git/refs/heads/master file.

    [jerry@CentOS project]$ cat .git/refs/heads/master

    29af9d45947dc044e33d69b9141d8d2dad37cc62

    ID commit t file c thay i, by gi thm tra n bng cch kim tra thng bo commit.

    jerry@CentOS project]$ git log -2

    Lnh trn s to ra kt qu sau:

    commit 29af9d45947dc044e33d69b9141d8d2dad37cc62

    Author: Jerry Mouse

    Date: Wed Sep 11 10:16:25 2013 +0530

    Added compiled binary

    commit 94f7b26005f856f1a1b733ad438e97a0cd509c1a

    Author: Jerry Mouse

    Date: Wed Sep 11 10:08:01 2013 +0530

    Added Makefile and renamed strings.c to string_operations.c

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 46

    Ty chn mixed

    Lnh git reset vi ty chn --mixed tr li nhng thay i t trong khu vc t chc m cha c

    commit. N ch tr li nhng thay i t khu vc t chc. Nhng thay i thc s i vi bn sao

    lm vic ca mt file khng b nh hng. Lnh git reset mc nh tng ng vi git reset --

    mixed.

    Ty chn hard

    Nu bn s dng ty chn --hard vi lnh git reset, n s xa khu vc t chc; n s reset im

    tr HEAD ti nhng commit mi nht ca ID commit c th v xa cc thay i file ni b.

    Chng ta kim tra ID commit.

    [jerry@CentOS src]$ pwd

    /home/jerry/jerry_repo/project/src

    [jerry@CentOS src]$ git log -1

    Lnh trn s to kt qu sau:

    commit 577647211ed44fe2ae479427a0668a4f12ed71a1

    Author: Tom Cat

    Date: Wed Sep 11 10:21:20 2013 +0530

    Removed executable binary

    Jerry chnh sa mt file bng cch thm mt li bnh dng n ti v tr u ca file.

    [jerry@CentOS src]$ head -2 string_operations.c

    /* This line be removed by git reset operation */

    #include

    Anh ta thm tra n bng cch s dng lnh git status.

    [jerry@CentOS src]$ git status -s

    M string_operations.c

    Jerry thm file c chnh sa ny ti khu vc t chc v thm tra n vi lnh git status.

    [jerry@CentOS src]$ git add string_operations.c

    [jerry@CentOS src]$ git status

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 47

    Lnh trn s to kt qu sau:

    # On branch master

    # Changes to be committed:

    # (use "git reset HEAD ..." to unstage)

    #

    #

    modified: string_operations.c

    #

    Git status ang ch rng file ang hin din trong khu vc t chc. By gi, reset im tr HEAD

    vi ty chn --hard.

    [jerry@CentOS src]$ git reset --hard 577647211ed44fe2ae479427a0668a4f12ed71a1

    HEAD is now at 5776472 Removed executable binary

    Lnh git reset thc hin thnh cng, m s tr li file t khu vc t chc cng nh d b bt k

    nhng thay i cc b no thc hin vi file.

    [jerry@CentOS src]$ git status -s

    Git status ang ch rng file c d b khi khu vc t chc.

    [jerry@CentOS src]$ head -2 string_operations.c

    #include

    Lnh head cng ch rng hot ng reset d b nhng thay i cc b.

    Hot ng Tag trong Git Hot ng tag cho php cung cp tn ngha cho cc phin bn c th trong repository. Gi s

    Tom v Jerry quyt nh ghp tag ghi vo code d n ca h m h sau c th truy cp n

    d dng.

    To cc tag Hy cng chng ti ghi tag HEAD hin ti bng cch s dng lnh git tag. Tom cung cp mt tn

    tag vi ty chn -a v cung cp mt thng tin tag vi ty chn -m.

    tom@CentOS project]$ pwd

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 48

    /home/tom/top_repo/project

    [tom@CentOS project]$ git tag -a 'Release_1_0' -m 'Tagged basic string operation code' HEAD

    Nu bn mun ghi tag mt commit c th, th khi bn s dng ID commit chnh xc thay v im

    tr HEAD. Tom s dng lnh sau push tag vo trong khu vc d b.

    [tom@CentOS project]$ git push origin tag Release_1_0

    Lnh trn s to ra kt qu sau:

    Counting objects: 1, done.

    Writing objects: 100% (1/1), 183 bytes, done.

    Total 1 (delta 0), reused 0 (delta 0)

    To [email protected]:project.git

    * [new tag]

    Release_1_0 > Release_1_0

    Quan st cc tag Tom to ra cc tag. By gi, Jerry c th quan st tt c cc tag c sn bng cch s dng lnh git

    tag vi ty chn -I.

    [jerry@CentOS src]$ pwd

    /home/jerry/jerry_repo/project/src

    [jerry@CentOS src]$ git pull

    remote: Counting objects: 1, done.

    remote: Total 1 (delta 0), reused 0 (delta 0)

    Unpacking objects: 100% (1/1), done.

    From git.server.com:project

    * [new tag]

    Release_1_0 > Release_1_0

    Current branch master is up to date.

    [jerry@CentOS src]$ git tag -l

    Release_1_0

    Jerry s dng lnh git show theo sau bi tn tag ca n quan st chi tit hn v tag.

    [jerry@CentOS src]$ git show Release_1_0

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 49

    Lnh trn s to ra kt qu sau:

    tag Release_1_0

    Tagger: Tom Cat

    Date: Wed Sep 11 13:45:54 2013 +0530

    Tagged basic string operation code

    commit 577647211ed44fe2ae479427a0668a4f12ed71a1

    Author: Tom Cat

    Date: Wed Sep 11 10:21:20 2013 +0530

    Removed executable binary

    diff --git a/src/string_operations b/src/string_operations

    deleted file mode 100755

    index 654004b..0000000

    Binary files a/src/string_operations and /dev/null differ

    Xa cc tag Tom s dng lnh sau xa cc tag t repository lu ni b cng nh t xa.

    [tom@CentOS project]$ git tag

    Release_1_0

    [tom@CentOS project]$ git tag -d Release_1_0

    Deleted tag 'Release_1_0' (was 0f81ff4)

    # Remove tag from remote repository.

    [tom@CentOS project]$ git push origin :Release_1_0

    To [email protected]:project.git

    - [deleted]

    Release_1_0

    Hot ng Patch trong Git

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 50

    Patch l mt file vn bn, m ni dung ca n tng t vi git diff, nhng song song vi code, n

    cng c siu d liu v cc commit nh ID commit, ngy thng, thng bo commit. Chng ta c

    th to ra mt patch t cc commit v ngi khc c th p dng chng vo repository ca h.

    Jerry thc hin chc nng strcat cho d n ca anh ta. Jerry c th to mt patch ca code ca

    anh ta v gi n cho Tom. Sau , anh ta c th p dng patch nhn c vo code ca anh ta.

    Jerry s dng lnh git format-patch to mt patch cho cc commit mi nht. Nu bn mun to

    mt patch cho mt commit c th, th khi bn s dng COMMIT_ID vi lnh format-patch.

    [jerry@CentOS project]$ pwd

    /home/jerry/jerry_repo/project/src

    [jerry@CentOS src]$ git status -s

    M string_operations.c

    ?? string_operations

    [jerry@CentOS src]$ git add string_operations.c

    [jerry@CentOS src]$ git commit -m "Added my_strcat function"

    [master b4c7f09] Added my_strcat function

    1 files changed, 13 insertions(+), 0 deletions(-)

    [jerry@CentOS src]$ git format-patch -1

    0001-Added-my_strcat-function.patch

    Lnh trn to ra cc tp .patch bn trong th mc lm vic hin ti. Tom c th s dng patch

    chnh sa file ca anh ta. Git cung cp hai lnh p dng cc patch l git am v git apply, theo

    cch ring bit. Git apply chnh sa cc file ni b m khng to ra commit, trong khi git am chnh

    sa file v cng to commit.

    p dng patch v to commit, bn s dng lnh sau:

    [tom@CentOS src]$ pwd

    /home/tom/top_repo/project/src

    [tom@CentOS src]$ git diff

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 51

    [tom@CentOS src]$ git status s

    [tom@CentOS src]$ git apply 0001-Added-my_strcat-function.patch

    [tom@CentOS src]$ git status -s

    M string_operations.c

    ?? 0001-Added-my_strcat-function.patch

    Patch ny c p dng thnh cng, by gi chng ta c th quan st cc chnh sa bng cch

    s dng lnh git diff.

    [tom@CentOS src]$ git diff

    Lnh trn to ra kt qu sau:

    diff --git a/src/string_operations.c b/src/string_operations.c

    index 8ab7f42..f282fcf 100644

    --- a/src/string_operations.c

    +++ b/src/string_operations.c

    @@ -1,5 +1,16 @@

    #include

    +char *my_strcat(char *t, char *s)

    diff --git a/src/string_operations.c b/src/string_operations.c

    index 8ab7f42..f282fcf 100644

    --- a/src/string_operations.c

    +++ b/src/string_operations.c

    @@ -1,5 +1,16 @@

    #include

    +char *my_strcat(char *t, char *s)

    +

    {

    +

    char *p = t;

    +

    +

    +

    while (*p)

    ++p;

    +

    while (*p++ = *s++)

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 52

    + ;

    + return t;

    +

    }

    +

    size_t my_strlen(const char *s)

    {

    const char *p = s;

    @@ -23,6 +34,7 @@ int main(void)

    {

    Qun l nhnh trong Git Hot ng nhnh cho php to cc tuyn khc nhau ca s pht trin. Chng ta c th s dng

    hot ng ny phn nhnh tin trnh pht trin vo hai hng khc nhau. V d, chng ti cng

    b mt sn phm phin bn 6 v chng ti mun to ra mt nhnh pht trin cc tnh nng 7.0

    m c th c gi cch bit vi s sa li trong phin bn 6.0.

    To nhnh Tom to mt nhnh mi bng cch s dng lnh git branch . Chng ta c th to mt nhnh mi

    t mt nhnh tn ti. Chng ta c th s dng mt commit hoc mt th c th nh l im bt

    u. Nu bt k ID commit c th no khng c cung cp, th khi nhnh s c to ra vi

    HEAD nh l im bt u.

    [jerry@CentOS src]$ git branch new_branch

    [jerry@CentOS src]$ git branch

    * master

    new_branch

    Mt nhnh mi c to ra; Tom s dng lnh git branch lit k cc nhnh c sn. Git ch mt

    du hoa th trc khi kim tra nhnh hin ti.

    Hnh di y miu t hot ng to nhnh:

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 53

    Chuyn i gia cc nhnh Jerry s dng lnh git checkout chuyn i gia cc nhnh:

    [jerry@CentOS src]$ git checkout new_branch

    Switched to branch 'new_branch'

    [jerry@CentOS src]$ git branch

    master

    * new_branch

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 54

    Cch tt to nhnh v chuyn i gia cc nhnh v d trn, chng ta s dng hai lnh ring r to v chuyn i gia cc nhnh. Git cung

    cp ty chn -b vi lnh checkout; hot ng ny to mt nhnh mi v ngay lp tc chuyn i

    n nhnh mi.

    [jerry@CentOS src]$ git checkout -b test_branch

    Switched to a new branch 'test_branch'

    [jerry@CentOS src]$ git branch

    master

    new_branch

    * test_branch

    Xa mt nhnh Mt nhnh c th c xa bng cch s dng ty chn -D vi lnh git branch. Nhng trc khi

    xa mt nhnh ang tn ti, bn chuyn ti nhnh khc.

    Jerry hin ti ang trn nhnh test_branch v anh ta mun d b nhnh . V th anh ta chuyn

    sang nhnh khc v xa nhnh nh di y:

    [jerry@CentOS src]$ git branch

    master

    new_branch

    * test_branch

    [jerry@CentOS src]$ git checkout master

    Switched to branch 'master'

    [jerry@CentOS src]$ git branch -D test_branch

    Deleted branch test_branch (was 5776472).

    By gi, Git s ch c hai nhnh.

    [jerry@CentOS src]$ git branch

    * master

    new_branch

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 55

    t li tn cho mt nhnh Jerry quyt nh thm s h tr cho cc k t rng ri trong d n cc chui hot ng ca anh ta.

    Anh ta to mt nhnh mi, nhng tn nhnh khng c cung cp chnh xc. V th anh ta thay

    i tn nhnh bng cch s dng ty chn -m theo sau bi old branch name v new branch name.

    [jerry@CentOS src]$ git branch

    * master

    new_branch

    [jerry@CentOS src]$ git branch -m new_branch wchar_support

    By gi, lnh git branch s ch tn nhnh mi.

    [jerry@CentOS src]$ git branch

    * master

    wchar_support

    Sp nhp hai nhnh Jerry thc hin mt chc nng tr li di chui ca mt chui k t rng. Mt code mi s

    xut hin nh di y:

    [jerry@CentOS src]$ git branch

    master

    * wchar_support

    [jerry@CentOS src]$ pwd

    /home/jerry/jerry_repo/project/src

    [jerry@CentOS src]$ git diff

    Lnh trn s to ra kt qu sau:

    t a/src/string_operations.c b/src/string_operations.c

    index 8ab7f42..8fb4b00 100644

    --- a/src/string_operations.c

    +++ b/src/string_operations.c

    @@ -1,4 +1,14 @@

    #include

    +#include

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 56

    +

    +size_t w_strlen(const wchar_t *s)

    +

    {

    +

    const wchar_t *p = s;

    +

    +

    while (*p)

    + ++p;

    + return (p - s);

    +

    }

    Sau khi kim tra, anh ta commit v push nhng thay i ca anh ta ti nhnh mi.

    [jerry@CentOS src]$ git status -s

    M string_operations.c

    ?? string_operations

    [jerry@CentOS src]$ git add string_operations.c

    [jerry@CentOS src]$ git commit -m 'Added w_strlen function to return string lenght of wchar_t

    string'

    [wchar_support 64192f9] Added w_strlen function to return string lenght of wchar_t string

    1 files changed, 10 insertions(+), 0 deletions(-)

    Ghi ch rng Jerry ang push nhng thay i ny ti nhnh mi, l ti sao anh ta s dng

    nhnh tn l wchar_support thay cho nhnh master.

    [jerry@CentOS src]$ git push origin wchar_support

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 57

    * [new branch]

    wchar_support -> wchar_support

    Sau khi commit nhng thay i, nhnh mi s xut hin nh sau:

    Tom t m v nhng g Jerry ang lm trong nhnh t nhn ca cu ta v anh ta kim tra log t

    nhnh wchar_support.

    [tom@CentOS src]$ pwd

    /home/tom/top_repo/project/src

    [tom@CentOS src]$ git log origin/wchar_support -2

    Lnh trn s to ra kt qu sau:

    commit 64192f91d7cc2bcdf3bf946dd33ece63b74184a3

    Author: Jerry Mouse

    Date: Wed Sep 11 16:10:06 2013 +0530

    Added w_strlen function to return string lenght of wchar_t string

    commit 577647211ed44fe2ae479427a0668a4f12ed71a1

    Author: Tom Cat

    Date: Wed Sep 11 10:21:20 2013 +0530

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 58

    Removed executable binary

    Bng cch quan st cc thng bo commit, Tom nhn ra rng Jerry thc hin chc nng strlen cho

    k t m rng v anh ta mun c chc nng tng t trong nhnh master. Thay v thc hin li

    cc bc trn, anh ta quyt nh ly code ca Jerry bng cch sp nhp nhnh ca anh ta vi

    nhnh master.

    [tom@CentOS project]$ git branch

    * master

    [tom@CentOS project]$ pwd

    /home/tom/top_repo/project

    [tom@CentOS project]$ git merge origin/wchar_support

    Updating 5776472..64192f9

    Fast-forward

    src/string_operations.c | 10 ++++++++++

    1 files changed, 10 insertions(+), 0 deletions(-)

    Sau hot ng sp nhp, nhnh master s xut hin nh sau:

    By gi, nhnh wchar_support c nhp vi nhnh master. Chng ta thm tra n bng cch

    quan st thng tin commit hoc quan st cc chnh sa c thc hin trong tp

    string_operation.c.

    [tom@CentOS project]$ cd src/

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 59

    [tom@CentOS src]$ git log -1

    commit 64192f91d7cc2bcdf3bf946dd33ece63b74184a3

    Author: Jerry Mouse

    Date: Wed Sep 11 16:10:06 2013 +0530

    Added w_strlen function to return string lenght of wchar_t string

    [tom@CentOS src]$ head -12 string_operations.c

    Lnh trn s to ra kt qu sau:

    #include

    #include

    size_t w_strlen(const wchar_t *s)

    {

    const wchar_t *p = s;

    while (*p)

    ++p;

    return (p - s);

    }

    Sau khi kim tra, anh ta push nhng thay i code ca anh ta ti nhnh master.

    [tom@CentOS src]$ git push origin master

    Total 0 (delta 0), reused 0 (delta 0)

    To [email protected]:project.git

    5776472..64192f9 master > master

    Rebase cc nhnh Lnh git rebase l mt lnh sp nhp nhnh, nhng im khc bit y l n chnh sa th t

    ca cc commit.

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 60

    Lnh git merge c gng t cc commit t nhnh khc ln trn u ca HEAD ca nhnh ni b

    hin ti. V d, nhnh ni b ca bn c cc commit A>B>C>D v nhnh sp nhp c cc

    commit l A>B>X>Y, th sau lnh git merge s bin i nhnh ni b hin ti thnh mt

    nhnh ging nh A>B>C>D>X>Y

    Lnh git rebase c gng tm ra gc gia nhnh ni b hin ti v nhnh sp nhp. N push cc

    commit ti nhnh ni b bng cch chnh sa th t ca cc commit trong nhnh ni b hin ti.

    V d, hai nhnh c cc commit nh trn, th lnh git rebase s chuyn i nhnh ni b hin ti

    thnh mt nhnh ging nh A>B>X>Y>C>D.

    Khi c nhiu nh lp trnh cng lm vic trn mt repository t xa, bn khng th chnh sa th t

    ca cc commit trong repository ny. Trong tnh hung ny, bn c th s dng hot ng rebase

    t cc commit ni b ca bn trn phn u ca cc commit repository t xa v bn c th

    push nhng thay i.

    X l Conflict trong Git Thc hin cc thay i trong nhnh wchar_support Jerry ang lm vic trn nhnh wchar_support. Anh ta thay i tn ca cc tnh nng v sau khi

    kim tra, anh ta repository nhng thay i ca anh ta.

    [jerry@CentOS src]$ git branch

    master

    * wchar_support

    [jerry@CentOS src]$ git diff

    Lnh trn s to ra kt qu sau:

    diff --git a/src/string_operations.c b/src/string_operations.c

    index 8fb4b00..01ff4e0 100644

    --- a/src/string_operations.c

    +++ b/src/string_operations.c

    @@ -1,7 +1,7 @@

    #include

    #include

    -size_t w_strlen(const wchar_t *s)

    +size_t my_wstrlen(const wchar_t *s)

    {

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 61

    const wchar_t *p = s;

    Sau khi thm tra li code, anh ta repository nhng thay i va thc hin.

    [jerry@CentOS src]$ git status -s

    M string_operations.c

    [jerry@CentOS src]$ git add string_operations.c

    [jerry@CentOS src]$ git commit -m 'Changed function name'

    [wchar_support 3789fe8] Changed function name

    1 files changed, 1 insertions(+), 1 deletions(-)

    [jerry@CentOS src]$ git push origin wchar_support

    Lnh trn s to ra kt qu sau:

    Counting objects: 7, done.

    Compressing objects: 100% (4/4), done.

    Writing objects: 100% (4/4), 409 bytes, done.

    Total 4 (delta 1), reused 0 (delta 0)

    To [email protected]:project.git

    64192f9..3789fe8 wchar_support -> wchar_support

    Thc hin cc thay i trong nhnh master Trong khi y trong nhnh master, Tom cng thay i tn ca cng chc nng v push nhng thay

    i ti nhnh master.

    [tom@CentOS src]$ git branch

    * master

    [tom@CentOS src]$ git diff

    Lnh trn s to ra kt qu:

    diff --git a/src/string_operations.c b/src/string_operations.c

    index 8fb4b00..52bec84 100644

    --- a/src/string_operations.c

    +++ b/src/string_operations.c

    @@ -1,7 +1,8 @@

    #include

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 62

    #include

    -size_t w_strlen(const wchar_t *s)

    +/* wide character strlen fucntion */

    +size_t my_wc_strlen(const wchar_t *s)

    {

    const wchar_t *p = s;

    Sau khi thm tra li diff, anh ta repository nhng thay i ny.

    [tom@CentOS src]$ git status -s

    M string_operations.c

    [tom@CentOS src]$ git add string_operations.c

    [tom@CentOS src]$ git commit -m 'Changed function name from w_strlen to my_wc_strlen'

    [master ad4b530] Changed function name from w_strlen to my_wc_strlen

    1 files changed, 2 insertions(+), 1 deletions(-)

    [tom@CentOS src]$ git push origin master

    Lnh trn s to ra kt qu sau:

    Counting objects: 7, done.

    Compressing objects: 100% (4/4), done.

    Writing objects: 100% (4/4), 470 bytes, done.

    Total 4 (delta 1), reused 0 (delta 0)

    To [email protected]:project.git

    64192f9..ad4b530 master -> master

    Trn nhnh wchar_support, Jerry thc hin chc nng strchr cho chui k t m rng. Sau khi

    kim tra, anh ta repository v push nhng thay i ny ti nhnh wchar_support.

    [jerry@CentOS src]$ git branch

    master

    * wchar_support

    [jerry@CentOS src]$ git diff

    Lnh trn s to ra kt qu sau:

    diff --git a/src/string_operations.c b/src/string_operations.c

    index 01ff4e0..163a779 100644

  • http://vietjack.com/git/index.jsp Copyright vietjack.com

    http://vietjack.com/ Trang chia s cc bi hc online min ph Page 63

    --- a/src/strin