zoukankan      html  css  js  c++  java
  • Git 使用

    官方网站:http://git-scm.com/

    配置

    jackluo@jackluo:~$ git config --global user.name "jackluo"

    jackluo@jackluo:~$ git config --global user.email "net.webjoy@gmail.com"

    jackluo@jackluo:~$ git config --global color.ui true

    jackluo@jackluo:~$ git config --global color.ui true

    jackluo@jackluo:~$ git config --global core.editor "vi"

    从头建立Repository(Demo):

    $ mkdir sandbox

    $   cd sandbox

    $   git init

    第一次 commit (demo):

    $   touch README

    $   git add README

    $   git status

    $  git commit -m "First Commit"

    修改看看Demo

    编辑README 做些变更

    $  git status

    $  git diff

    $  git add .

    (一次加入所有变更跟新增档案,但不包括删除的档案)

    $  git status

    $  git diff --cached

    $  git commit -m "Update README"

    只Commit 部分档案(demo)

    $   touch a.rb

    $   touch b.rb

    $   git add a.rb

    $   git commit "update a"

    $   git add b.rb

    $   git commit "update b"

    修改 a.rb 

    $  git add a.rb

    再次修改 a.rb

    git commit -m "commit a"

    这时commit 的内容是第一次修改当时的内容而已

    只commit 同一档案部分内容(demo)

    $  git add --patch

        -y 加到staging

        -n 不要加到staging

        -s 查以折小一点hunk

    或者用 GUI 例如gitx 来选取

    删除和搬移档案(demo)

    $ git rm a.rb

    $ git mv b.rb c.rb

    $ git add .

    $ git commit "Remove a ,Rename b to c"

    没有copy ?因为Git 是追跟内容,你只要cp 即可,不用担心浪费空间.

    revert (还原 commit 记录)

    新增一笔 commit 来做还原

    例如本来的commit 是新增一行,那么revert commit 就会移除那一行

    $  git revert

    $  git revert HEAD^

    下标签(demo)

    $ git tag foo

    $ git tag foo <SHAI>

    $ git tag bar -m "some message"

    $ git tag 

    $ git tag -d foo

    $ git log 

    $ git log --oneline

    $ git log --oneline --decorate --graph

    git log 很多参数可以用,可以用GUI

    比较差异Diff

    git diff <SHAI>拿 working ree 比较

    砍掉 untracked 档案

    git clean -n 列出打算要清除的档案

    git clean -f 真的清除

    git clean -x 连 gitignore 里列的档案也要清掉

    Git 建立Local Repository

    1. $ mkdir project; cd project
    2. $ git init
    3. $ echo "hello" > hello.txt
    4. $ git add .
    5. $ git commit -m 'initial'

    Git clone 资料, 资料修改后上传

    1. $ git clone http://git.example.com/project.git
    2. $ cd project
    3. $ touch new_file.txt
    4. $ git add .
    5. $ git commit -m 'add new_file.txt'
    6. $ git push origin master
    7. $ git pull # 拉看看有没有更新

    Git clone 资料, 资料修改后上传.(分两个目录测试)

    1. $ mkdir /tmp/a /tmp/b
    2. $ cd /tmp/a
    3. $ git clone http://example.com/project_name.git
    4. $ cd /tmp/b
    5. $ git clone http://example.com/project_name.git
    6. $ echo "hello" > hello.html
    7. $ git add hello.html
    8. $ git commit -m 'add hello.html' # local commit.
    9. $ git push # 推到Server 上.
    10. $ cd /tmp/a
    11. $ git pull # 会看到hello.html

    Local Repository

    建立Local Repository 测试
    1. $ mkdir test; cd test
    2. $ git init
    建立新的branch (new-branch), 并于branch 去新增档案
    1. $ git branch new-branch # master
    2. $ git branch # master 
       * master 
         new-branch
    3. $ git checkout new-branch # 切换到新的branch
    4. $ git branch # new-branch 
         master 
       * new-branch
    测试Git staging area (git add . 之后的修改, 不会被commit 进去)
    1. $ touch new-branch_file.txt # new-branch
    2. $ git add . # new-branch
    3. $ echo "contents." > new-branch_file.txt # new-branch
    4. $ git commit # new-branch, commit的new-branch_file.txt会是空的,因为修改是在git add .之后.
    修改过的资料, 不要commit, 想直接切换到master(使用Git stash 将修改纪录暂存, 将目前new-branch 的资料merge 到mastermaster)
    1. $ git status # new-branch, 会显示new-branch_file.txt 有修改, 尚未commit.
    2. $ git checkout master # new-branch, 切换回master, 会出现错误: "You have local changes"
    3. $ git stash # new-branch, 先把修改的先暂存下来, 先不commit, 之后取出可用git stash pop 或git stash apply
    4. $ git status # new-branch, 会显示nothing to commit (暂时先不丢进commit 里面)
    5. $ git checkout master # master
    6. $ git merge new-branch # master, 会将new-branch_file.txt 的空档案合并进来.
    Git 于master 将档案砍掉, branch 是否还能存取此档案.
    1. $ ls # master, master_file.txt, new-branch_file.txt
    2. $ rm new-branch_file.txt # master, 删掉此档案
    3. $ git checkout new-branch # master, 切换到new-branch, 会出现错误: "pathspec 'branch' did not match any file(s) known to git."
    4. $ git stash # 先把修改的部份存起来(砍掉new-branch_file.txt)
    5. $ ls # master, 此时new-branch_file.txt 出现了. (因为尚未commit, stash 的动作并未做写入)
    6. $ git stash pop # master, 回复刚刚砍掉的状态, new-branch_file.txt 就消失了.
    7. $ git commit -m 'delete new-branch_file.txt in master' -a # 先砍掉.
    切换到Branch, 去跟master 做Merge
    1. $ git checkout new-branch
    2. $ git stash pop # new-branch
    3. $ git merge master # new-branch, 错误: "Entry 'new-branch_file.txt' not uptodate. Cannot merge.", 因为档案有修改.
    4. $ git diff master # 与master 做diff, 发现/dev/null vs file, 所以要把此档案砍掉.
    5. $ rm new-branch_file.txt
    6. $ git merge master # new-branch, 合并完成
    7. $ ls # new-branch, 只剩master_file.txt 这个档案
    由branch(new-branch) 环境和Master 分别建立新的branch (from-branch, from-master), 并测试未commit 资料状况, 新branch 的状态.
    1. $ touch new-branch_file.txt # new-branch, 测试未commit 资料状况, 新branch 的状态.
    2. $ git branch from-branch new-branch # new-branch, 会将new-branch 目前所有状态和资料都复制过去
    3. $ git checkout from-branch # from-branch
    4. $ git status # from-branch, 会看到new-branch_file.txt, 且这个档案尚未commit.
    5. $ git branch from-master master # from-branch, 依照master 开from-master 的branch
    6. $ git branch # from-branch 
        from-branch 
        from-master 
        master 
      * new-branch
    7. $ git branch -d from-master # from-branch, 砍掉from-master 的branch
    8. $ git checkout -b from-master master # from-branch, 建立from-master 的branch, 并同时切换过去.
    9. $ git branch # from-master 
        from-branch 
      * from-master 
        master 
        new-branch
    测试由Repository 还原档案内容
    1. $ echo "test" > master_file.txt
    2. $ git checkout master_file.txt # 还原回空档案(Repository 的版本是空档案)
    git pull 出现error: Entry 'filename' not uptodate. Cannot merge. 解法
    1. git stash # 目前目录有修改的资料, 先丢进暂存区
    2. git pull # 合并拉下来的修改
    3. git stash pop # 将修改的暂存区资料取出
    4. 去看unmerge 的部份, 修改完成commit + push 即可.

    Repository 测试

    建立local 端master
    1. $ mkdir /tmp/a /tmp/b
    2. $ cd /tmp/a
    3. $ git clone http://git.example.com/project.git
    4. $ cd project/
    5. $ touch master-file
    6. $ git add .
    7. $ git commit -m 'add master-file'
    8. $ git push origin master
    9. $ git pull
    10. $ cd /tmp/b # 由此处来建立branch
    11. $ git clone http://git.example.com/project.git
    建立Repository 的branch
    1. $ git pull
    2. $ git push origin origin:refs/heads/reps-branch
    3. $ git fetch origin # 更新到最新版本(origin 是Repository 的版本)
    4. $ git branch -r
    5. $ git checkout --track -b reps-branch origin/reps-branch # 抓取reps-branch, 并将此branch 建立于local 的reps-branch
    6. $ git pull
    7. $ git branch 
       * reps-branch 
         master

    测试

    A 操作, 新增一个档案, commit 进入reps-branch, 于reps-branch commit
    1. $ cd /tmp/a/project
    2. $ git pull
    3. $ git push origin origin:refs/heads/reps-branch
    4. $ git fetch origin
    5. $ git branch -r
    6. $ git checkout --track -b reps-branch origin/reps-branch # 抓取reps-branch, 并将此branch 建立于local 的reps-branch
    7. $ git pull
    8. $ git branch 
       * reps-branch 
         master
    9. $ touch reps-branch.txt
    10. $ git add reps-branch.txt
    11. $ git commit -m 'add reps-branch.txt'
    12. $ git push
    13. $ git pull
    B 抓取reps-branch, 并修改资料, 再抓取reps 的branch
    1. $ cd /tmp/b/project
    2. $ git clone http://git.example.com/project.git
    3. $ cd project
    4. $ git fetch origin
    5. $ git pull
    6. $ git checkout --track -b reps-branch origin/reps-branch # 丢到reps-branch 去
    7. $ vim reps-branch.txt # 随便加些内容
    8. $ git add reps-branch.txt
    9. $ git commit -m 'add some content'
    10. $ git push
    11. $ git pull
    A 操作, 更新, 会抓到B commit 的资料(于reps-branch)
      1. $ cd /tmp/a/project
      2. $ git pull # 更新reps-branch.txt 内的资料(B commit 的)
  • 相关阅读:
    学期总结
    C语言I博客作业09
    C语言I博客作业08
    C语言I博客作业07
    C语言I博客作业06
    C语言博客作业04
    C语言I博客作业03
    C语言I博客作业02
    C语言I博客作业01
    学期总结
  • 原文地址:https://www.cnblogs.com/jackluo/p/3126493.html
Copyright © 2011-2022 走看看