git用法

作者: Karl_2c80 | 来源:发表于2018-11-27 16:21 被阅读0次

    安装git

    apt-get install git
    yum install git

    创建版本库

    mkdir learngit
    cd learngit
    git init
    
    git add <file>
    git commit -m <message>
    

    版本回退

    git status   # 查看仓库当前状态
    git diff readme.txt    # 查看修改内容
    
    git log        # 查看提交历史,以便确定回退到哪个版本
    git log --pretty=oneline
    git reflog     # 查看命令历史,以便确定回到未来哪个版本 
    
    git reset --hard HEAD^      # HEAD指当前版本,HEAD^指上一个版本
    git reset --hard <commit_id>
    

    工作区和暂存区

    1. 工作区 Working Directory
    2. 版本库 Repository
    3. 暂存区 Stage

    撤销修改

    git checkout -- readme.txt      # 丢弃工作区的修改
    # 1) 一种是文件修改后未被放到暂存区,撤销修改就回到和版本库相同的状态
    # 2) 一种是文件被添加到暂存区后又做了修改,撤销修改就回到添加到暂存区后的状态
    # 总之就是让文件回到最近一次 git add 或 git commit 时的状态
    ## git checkout -- file命令中的--很重要,没有--,就变成了“切换到另一个分支”的命令,我们在后面的分支管理中会再次遇到git checkout命令
    
    git reset HEAD <file>     # 把暂存区的修改撤销(unstage),重新放回工作区
    

    删除文件

    # 先添加一个文件testnew到Git并提交
    [root@KARL learngit]# touch testnew
    [root@KARL learngit]# git add testnew
    [root@KARL learngit]# git commit -m "add testnew"
    [master 10aa3df] add testnew
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 testnew
    
    # 有如下两种情况:
    
    ## 1) 删除文件后发现是误删,可将误删文件恢复到最新版本
    [root@KARL learngit]# rm -rf testnew
    [root@KARL learngit]# ls
    readme.txt
    [root@KARL learngit]# git checkout -- testnew
    [root@KARL learngit]# ls
    readme.txt  testnew
    
    ## 2) 确实要从版本库中删除该文件
    [root@KARL learngit]# rm -rf testnew
    [root@KARL learngit]# ls
    readme.txt
    [root@KARL learngit]# git status
    # On branch master
    # Changes not staged for commit:
    #   (use "git add/rm <file>..." to update what will be committed)
    #   (use "git checkout -- <file>..." to discard changes in working directory)
    #
    #       deleted:    testnew
    #
    no changes added to commit (use "git add" and/or "git commit -a")
    [root@KARL learngit]# git rm testnew
    rm 'testnew'
    [root@KARL learngit]# git status
    # On branch master
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #       deleted:    testnew
    #
    [root@KARL learngit]# git commit -m "remove testnew"
    [master 8f5bf72] remove testnew
     1 file changed, 0 insertions(+), 0 deletions(-)
     delete mode 100644 testnew
    [root@KARL learngit]# git checkout -- testnew
    error: pathspec 'testnew' did not match any file(s) known to git.
    

    远程仓库

    • Git是分布式版本控制系统,同一个Git仓库,可以分布到不同的机器上。怎么分布呢?最早,肯定只有一台机器有一个原始版本库,此后,别的机器可以“克隆”这个原始版本库,而且每台机器的版本库其实都是一样的,并没有主次之分。

    添加远程库

    1. 在本地执行 ssh-keygen -t rsa -b 4096,并将公钥上传至GitHub中的Deploy keys

    "-b 4096" 不能省略,因为在GitHub上添加key时,必须大于2048位

    1. 将本地库推送至远程库
    git remote add origin git@github.com:Karl-luyi/learngit.git     # 关联远程库
    git push -u origin master      # 第一次推送master分支的所有内容
    git push origin master      # 推送最新修改
    
    [root@KARL learngit]# git remote add origin git@github.com:karl-luyi/learngit.git
    [root@KARL learngit]# git push -u origin master
    The authenticity of host 'github.com (13.250.177.223)' can't be established.
    RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
    RSA key fingerprint is MD5:16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.
    Counting objects: 18, done.
    Compressing objects: 100% (13/13), done.
    Writing objects: 100% (18/18), 1.50 KiB | 0 bytes/s, done.
    Total 18 (delta 3), reused 0 (delta 0)
    remote: Resolving deltas: 100% (3/3), done.
    remote:
    remote: Create a pull request for 'master' on GitHub by visiting:
    remote:      https://github.com/Karl-luyi/learngit/pull/new/master
    remote:
    To git@github.com:karl-luyi/learngit.git
     * [new branch]      master -> master
    Branch master set up to track remote branch master from origin.
    

    从远程库克隆

    1. 在GitHub上新建远程库
    2. 从远程库克隆
    [root@KARL learngit]# git clone git@github.com:Karl-luyi/gitskills.git
    Cloning into 'gitskills'...
    Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
    remote: Enumerating objects: 3, done.
    remote: Counting objects: 100% (3/3), done.
    remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    Receiving objects: 100% (3/3), done.
    [root@KARL learngit]# echo $?
    0
    [root@KARL learngit]# ls
    gitskills  readme.txt
    [root@KARL learngit]# cd gitskills/
    [root@KARL gitskills]# ls
    README.md
    [root@KARL gitskills]# cat README.md
    # gitskills
    new remote repository
    
    1. Git支持多种协议,包括https,但通过ssh支持的原生git协议速度最快。
    2. 使用https除了速度慢以外,还有个最大的麻烦是每次推送都必须输入口令,但是在某些只开放http端口的公司内部就无法使用ssh协议而只能用https。

    分支管理

    [root@KARL gitskills]# git checkout -b dev       # 创建并切换,相当于2条命令:1) git branch dev  2) git checkout dev
    Switched to a new branch 'dev'
    
    [root@KARL gitskills]# git branch      # 查看当前分支
    * dev
      master
    
    [root@KARL gitskills]# git checkout master      # 切换至master分支
    Switched to branch 'master'
    
    [root@KARL gitskills]# git merge dev      # 将dev分支上合并到master分支上
    Updating 13079c7..9f63516
    Fast-forward
     readme.txt | 7 +++++++
     1 file changed, 7 insertions(+)
     create mode 100644 readme.txt
    
    [root@KARL gitskills]# git branch -d dev      # 删除dev分支
    Deleted branch dev (was 9f63516).
    [root@KARL gitskills]# git branch
    * master
    
    [root@KARL gitskills]# git remote -v      # 查看远程库信息
    origin  git@github.com:Karl-luyi/gitskills.git (fetch)
    origin  git@github.com:Karl-luyi/gitskills.git (push)
    

    总结
    查看分支:git branch
    创建分支:git branch <name>
    切换分支:git checkout <name>
    创建+切换分支:git checkout -b <name>
    合并某分支到当前分支:git merge <name>
    删除分支:git branch -d <name>

    相关文章

      网友评论

          本文标题:git用法

          本文链接:https://www.haomeiwen.com/subject/iuzwqqtx.html