• Git 基本用法


    Git教程:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

    创建一个仓库

    git init  将当前目录变为git管理的仓库,在成功执行此命令之后,当前目录会出现 .git 文件
    [wangml@iZwz976helaylvgqok97prZ git]$ pwd 
    /home/wangml/git
    [wangml@iZwz976helaylvgqok97prZ git]$ ls
    [wangml@iZwz976helaylvgqok97prZ git]$ git init
    Initialized empty Git repository in /home/wangml/git/.git/
    [wangml@iZwz976helaylvgqok97prZ git]$ ls -a
    .  ..  .git
    git只能跟踪文本文件的变化情况,而对于二进制文件却无能为力,文本文件都是有编码的,建议
    仓库中文件使用同一种编码格式,一般推荐使用UTF-8
     
    将一个文件添加到仓库中,或者提交一个修改(将工作区的文件加入stage暂存区)
    git add filename
    git commit 将文件提交到仓库,而 -m 后的参数表示对于此次提交的说明(将stage暂存区文件加
               入master分支)
    git commit -m "Explain"
    [wangml@iZwz976helaylvgqok97prZ git]$ vim first.txt
    [wangml@iZwz976helaylvgqok97prZ git]$ ls
    first.txt
    [wangml@iZwz976helaylvgqok97prZ git]$ git add first.txt 
    [wangml@iZwz976helaylvgqok97prZ git]$ git commit -m "the first file"
    [master (root-commit) 8a16ecf] the first file
     0 files changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 first.txt
    git status  查看仓库当前状态
    [wangml@iZwz976helaylvgqok97prZ git]$ git status
    # On branch master
    # Changed but not updated:
    #   (use "git add <file>..." to update what will be committed)
    #   (use "git checkout -- <file>..." to discard changes in working directory)
    #
    # modified:   readme.txt
    #
    no changes added to commit (use "git add" and/or "git commit -a")
    git diff  查看仓库与上次提交后有什么不同(工作区文件与master中的区别)
    [wangml@iZwz976helaylvgqok97prZ git]$ git diff
    diff --git a/readme.txt b/readme.txt
    index e69de29..aede7aa 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -0,0 +1,2 @@
    +don't stop don't stop
    +laughing about it
    git log  查看仓库日志(显示从最近到最远的提交日志)
    [wangml@iZwz976helaylvgqok97prZ git]$ git log
    commit 47c7f76657196394d9d776668cb2206c1927e1c9
    Author: wangml <wangml@iZwz976helaylvgqok97prZ.(none)>
    Date:   Fri Aug 3 16:07:39 2018 +0800
        20180803-16:07
    commit bfa08dc84f3f6f0b235f5a99c5ad4993bdfeb1f3
    Author: wangml <wangml@iZwz976helaylvgqok97prZ.(none)>
    Date:   Wed Aug 1 21:49:30 2018 +0800
        add readme.txt
    commit 8a16ecff65e153fb435f4c0fa5060fbc3bac0bc2
    Author: wangml <wangml@iZwz976helaylvgqok97prZ.(none)>
    Date:   Wed Aug 1 21:46:55 2018 +0800
        the first file

     

    git log --pretty=oneline  将每次提交日志用一行显示
    [wangml@iZwz976helaylvgqok97prZ git]$ git log --pretty=oneline
    47c7f76657196394d9d776668cb2206c1927e1c9 20180803-16:07
    bfa08dc84f3f6f0b235f5a99c5ad4993bdfeb1f3 add readme.txt
    8a16ecff65e153fb435f4c0fa5060fbc3bac0bc2 the first file
    上述显示的第一个字段的一长串是用SHA1计算出来的用16进制表示的 commit id(版本号)

    在 git 中可以用 HEAD^ 表示上一个版本,HEAD^^ 表示上上的版本
    或者使用 HEAD~N 表示最近提交的倒数第N个版本

    git reset --hard HEAD~N  回退到倒数第N个版本
    [wangml@iZwz976helaylvgqok97prZ git]$ git reset --hard HEAD~
    HEAD is now at bfa08dc add readme.txt
    [wangml@iZwz976helaylvgqok97prZ git]$ cat readme.txt 
    [wangml@iZwz976helaylvgqok97prZ git]$ 
    退回到上一个版本,可以看到在上个版本中的readme.txt文件中没有任何内容
    但如果此时我们希望不要回退,让其变为readme.txt文件已被添加了内容的版本,我们应该
    怎么办呢?此时我们无法使用 HEAD~N ,因为这个是指向当前版本之前的版本,这时我们就
    需要使用上述的 commit id
     
    git reset --hard id  将仓库设置为 id 表示的版本
    [wangml@iZwz976helaylvgqok97prZ git]$ git reset --hard 47c7f76
    HEAD is now at 47c7f76 20180803-16:07
    [wangml@iZwz976helaylvgqok97prZ git]$ cat readme.txt 
    don't stop don't stop
    laughing about it
    git reflog  记录了我们使用的们一条命令,也可以通过它找到不同版本的 commit id
    [wangml@iZwz976helaylvgqok97prZ git]$ git reflog
    47c7f76 HEAD@{0}: 47c7f76657196394d9d776668cb2206c1927e1c9: updating HEAD
    bfa08dc HEAD@{1}: HEAD~: updating HEAD
    47c7f76 HEAD@{2}: commit: 20180803-16:07
    bfa08dc HEAD@{3}: commit: add readme.txt
    8a16ecf HEAD@{4}: commit (initial): the first file
    注意:Git跟踪并管理的是修改,而非文件

    git checkout -- filename  使filename文件回到最近一次git add/commit时的样子
    这里有两种情况:
    一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样
    的状态;
    一种是readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区
    后的状态。
    一:
    [wangml@iZwz976helaylvgqok97prZ git]$ cat readme.txt 
    don't stop don't stop
    laughing about it
    test for diff
    [wangml@iZwz976helaylvgqok97prZ git]$ vim readme.txt 
    [wangml@iZwz976helaylvgqok97prZ git]$ cat readme.txt 
    don't stop don't stop
    laughing about it
    test for diff
    checkout...
    [wangml@iZwz976helaylvgqok97prZ git]$ git checkout -- readme.txt 
    [wangml@iZwz976helaylvgqok97prZ git]$ cat readme.txt 
    don't stop don't stop
    laughing about it
    test for diff
    二:
    [wangml@iZwz976helaylvgqok97prZ git]$ cat first.txt 
    test for diff
    [wangml@iZwz976helaylvgqok97prZ git]$ vim first.txt 
    [wangml@iZwz976helaylvgqok97prZ git]$ cat first.txt 
    test for diff
    checkout...
    [wangml@iZwz976helaylvgqok97prZ git]$ git add first.txt 
    [wangml@iZwz976helaylvgqok97prZ git]$ vim first.txt 
    [wangml@iZwz976helaylvgqok97prZ git]$ cat first.txt 
    test for diff
    checkout...
    checkout2...
    [wangml@iZwz976helaylvgqok97prZ git]$ git checkout -- first.txt 
    [wangml@iZwz976helaylvgqok97prZ git]$ cat first.txt 
    test for diff
    checkout...
    git reset HEAD filename  可以把暂存区的修改撤销掉(unstage),重新放回工作区
    [wangml@iZwz976helaylvgqok97prZ git]$ cat readme.txt 
    don't stop don't stop
    laughing about it
    test for diff
    checkout...
    [wangml@iZwz976helaylvgqok97prZ git]$ vim readme.txt 
    [wangml@iZwz976helaylvgqok97prZ git]$ cat readme.txt 
    don't stop don't stop
    laughing about it
    test for diff
    checkout...
    reset...
    [wangml@iZwz976helaylvgqok97prZ git]$ git add readme.txt 
    [wangml@iZwz976helaylvgqok97prZ git]$ git status
    # On branch master
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    # modified:   first.txt
    # modified:   readme.txt
    #
    [wangml@iZwz976helaylvgqok97prZ git]$ git reset HEAD readme.txt 
    Unstaged changes after reset:
    M readme.txt
    [wangml@iZwz976helaylvgqok97prZ git]$ git status
    # On branch master
    # modified:   readme.txt
    rm filename      删除一个文件,被删除的文件可以使用git checkout -- filename 恢复
    [wangml@iZwz976helaylvgqok97prZ git]$ rm test.txt 
    [wangml@iZwz976helaylvgqok97prZ git]$ ls
    first.txt  readme.txt
    [wangml@iZwz976helaylvgqok97prZ git]$ git checkout -- test.txt
    [wangml@iZwz976helaylvgqok97prZ git]$ ls
    first.txt  readme.txt  test.txt
    git rm filename + git commit 从版本库中删除一个文件,可以使用 git reset 恢复
    [wangml@iZwz976helaylvgqok97prZ git]$ ls
    first.txt  readme.txt  test.txt
    [wangml@iZwz976helaylvgqok97prZ git]$ git add test.txt 
    [wangml@iZwz976helaylvgqok97prZ git]$ git commit -m "add test.txt"
    [master 7406bd5] add test.txt
     1 files changed, 2 insertions(+), 0 deletions(-)
     create mode 100644 test.txt
    [wangml@iZwz976helaylvgqok97prZ git]$ git rm test.txt 
    rm 'test.txt'
    [wangml@iZwz976helaylvgqok97prZ git]$ ls
    first.txt  readme.txt
    [wangml@iZwz976helaylvgqok97prZ git]$ git commit -m "delete test.txt"
    [master e6a2486] delete test.txt
     0 files changed, 0 insertions(+), 0 deletions(-)
     delete mode 100644 test.txt
    [wangml@iZwz976helaylvgqok97prZ git]$ ls
    first.txt  readme.txt
    [wangml@iZwz976helaylvgqok97prZ git]$ git log
    commit e6a2486a711c71e7703804123008afe1e3aef2a1
    Author: wangml <wangml@iZwz976helaylvgqok97prZ.(none)>
    Date:   Sat Aug 4 16:42:04 2018 +0800
        delete test.txt
    ...
    [wangml@iZwz976helaylvgqok97prZ git]$ git reset --hard 7406bd59979f1207f6dad4538
    HEAD is now at 7406bd5 add test.txt
    [wangml@iZwz976helaylvgqok97prZ git]$ ls
    first.txt  readme.txt  test.txt

    远程库相关:

    git push ...  将本地仓库的commit推送到远程库中
     
    git clone ...  克隆一个远程库到本地

    git branch 分支名  创建一个新的分支
    git checkout 分支名  选择指定的分支
    上述两个命令等价于: git checkout -b 分支名
    [wangml@iZwz976helaylvgqok97prZ git]$ git branch var
    [wangml@iZwz976helaylvgqok97prZ git]$ git checkout var
    Switched to branch 'var'
    git branch  查看当前分支
    [wangml@iZwz976helaylvgqok97prZ git]$ git branch
      master
    * var
    在新创建的分支中做的改变对源分支没有影响,如下示例:在 var 分支中 对test.txt文件
    加以修改并提交到版本库,在回到master分支,查看test.txt文件,却发现test.txt文件没
    有任何改变
    [wangml@iZwz976helaylvgqok97prZ git]$ vim test.txt 
    [wangml@iZwz976helaylvgqok97prZ git]$ cat test.txt 
    branch...  var
    [wangml@iZwz976helaylvgqok97prZ git]$ git add test.txt 
    [wangml@iZwz976helaylvgqok97prZ git]$ git commit -m "branch var..."
    [var d991370] branch var...
     1 files changed, 1 insertions(+), 0 deletions(-)
    [wangml@iZwz976helaylvgqok97prZ git]$ git checkout master
    Switched to branch 'master'
    [wangml@iZwz976helaylvgqok97prZ git]$ cat test.txt 
    [wangml@iZwz976helaylvgqok97prZ git]$ 
    git merge 分支名  合并指定分支到当前所在的分支,如下示例:将 var 分支合并到 当前
    分支 master 中,可以看到test.txt文件内容变得和之前在 var 分支中修改后的内容一样
    git log --graph  查看分支合并图
    [wangml@iZwz976helaylvgqok97prZ git]$ cat test.txt 
    [wangml@iZwz976helaylvgqok97prZ git]$ git branch
    * master
      var
    [wangml@iZwz976helaylvgqok97prZ git]$ cat test.txt 
    [wangml@iZwz976helaylvgqok97prZ git]$ git merge var
    Updating 7406bd5..d991370
    Fast-forward
     test.txt |    1 +
     1 files changed, 1 insertions(+), 0 deletions(-)
    [wangml@iZwz976helaylvgqok97prZ git]$ cat test.txt 
    branch...  var

     

    git branch -d 分支名  删除指定分支
    [wangml@iZwz976helaylvgqok97prZ git]$ git branch
    * master
      var
    [wangml@iZwz976helaylvgqok97prZ git]$ git branch -d var
    Deleted branch var (was d991370).
    [wangml@iZwz976helaylvgqok97prZ git]$ git branch
    * master
    注意:当两个分支都做出了更改,在对分支进行合并时可能会发生冲突,此时需要将发生冲突的文
    件作出修改之后在提交

    git stash  将未提交的工作空间储存起来,待以后恢复后在使用,在使用 git stash 后可
    以看到working directory clean
    [wangml@iZwz976helaylvgqok97prZ git]$ git status
    # On branch var
    # Changed but not updated:
    #   (use "git add <file>..." to update what will be committed)
    #   (use "git checkout -- <file>..." to discard changes in working directory)
    #
    # modified:   new.txt
    #
    no changes added to commit (use "git add" and/or "git commit -a")
    [wangml@iZwz976helaylvgqok97prZ git]$ git stash
    Saved working directory and index state WIP on var: 6fa8864 new file
    HEAD is now at 6fa8864 new file
    [wangml@iZwz976helaylvgqok97prZ git]$ git status
    # On branch var
    nothing to commit (working directory clean)
    git stash list 列出已储存的工作空间
    [wangml@iZwz976helaylvgqok97prZ git]$ git stash list
    stash@{0}: WIP on var: 6fa8864 new file
    git stash apply  恢复到上一个储存的工作空间
    git stash drop   删除已储存的工作空间
    上述两个命令一起用等于 git stash pop
     
    [wangml@iZwz976helaylvgqok97prZ git]$ git stash apply
    # On branch var
    # Changed but not updated:
    #   (use "git add <file>..." to update what will be committed)
    #   (use "git checkout -- <file>..." to discard changes in working directory)
    #
    # modified:   new.txt
    #
    no changes added to commit (use "git add" and/or "git commit -a")
    [wangml@iZwz976helaylvgqok97prZ git]$ git stash drop
    Dropped refs/stash@{0} (fc7113d17f35a62b46cd7142c20ad1efa5f1d6b4)
  • 相关阅读:
    UIView背景渐变三种方法
    Phone漂亮的动画
    图片处理代码片段
    UIProgressView进度条
    UIActivityIndicatorView
    NSTimer定时器类
    iPhone Tableview分批显示数据
    无限剑制
    兔子繁殖(easy)
    植树节
  • 原文地址:https://www.cnblogs.com/lnlin/p/9435921.html
Copyright © 2020-2023  润新知