美文网首页
git使用教程

git使用教程

作者: JUNjianshuZHU | 来源:发表于2018-08-16 18:03 被阅读0次

    参考资料:Git教程

    1. 创建一个空目录
    #添加文件夹
    $ mkdir learngit
    ↓
    #索引到该文件夹
    $ cd learngit
    ↓
    #查看添加结果
    $ pwd
    --->/c/Users/Xiaoji/learngit
    

    1. 通过git init命令把这个目录变成Git可以管理的仓库
    $ git init
    --->Initialized empty Git repository in C:/Users/Xiaoji/learngit/.git/
    

    1. 把文件添加到仓库
    #编写一个readme.txt文件,内容如下:
    Git is a version control system.
    Git is free software.
    #!一定要放到learngit目录下(子目录也行),因为这是一个Git仓库,放到其他地方Git再厉害也找不到这个文件
    ↓
    #用命令git add告诉Git,把文件添加到仓库:
    $ git add readme.txt
    ↓
    # 用命令git commit告诉Git,把文件提交到仓库:
    $ git commit -m 'wrote a readme.txt'  #-m后面输入的是本次提交的说明,可以输入任意内容
    --->
    [master (root-commit) 37424cf] wrote a readme.txt
     1 file changed, 2 insertions(+)  #1 file changed:1个文件被改动,2 insertions:插入了两行内容
     create mode 100644 readme.txt
    
    tips:
    #可以多次add不同的文件,commit可以一次提交很多文件
    $ git add <file>:$ git add file2.txt file3.txt
    
    $ git commit -m "add 2 files."
    

    1. 修改文件内容并提交:
    # 继续修改readme.txt文件,改成如下内容:
    Git is a distributed version control system.
    Git is free software.
    
    #运行git status命令看看结果:
    $ git status
    --->
    On branch master
    Changes not staged for commit:
      (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")  #readme.txt被修改过了,但还没有准备提交的修改
    
    # 用git diff这个命令看看具体修改了什么内容
    $ git diff
    --->
    diff --git a/readme.txt b/readme.txt
    index d8036c1..013b5bc 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -1,2 +1,2 @@
    -Git is a version control system.
    +Git is a distributed version control system.  #添加了一个distributed单词
     Git is free software.
    \ No newline at end of file
    
    # 提交修改后的文件
    $ git add readme.txt
    $ git commit -m "add distributed"
    
    # 再次查看结果:
    $ git status
    --->
    On branch master
    nothing to commit, working tree clean  #Git告诉我们当前没有需要提交的修改,而且,工作目录是干净(working tree clean)的
    
    tips:
    git diff    #是工作区(work dict)和暂存区(stage)的比较
    git diff --cached    #是暂存区(stage)和分支(master)的比较
    git diff HEAD -- file    #比较的是工作区中的文件与版本库中文件的差异
    

    1. 查看修改的历史记录
    $ git log
    --->
    commit 10e3ff13cb1552ae791c84392b8e6eaed78e55f5 (HEAD -> master)
    Author: Xiaoji <Xiaoji@Xiaoji>
    Date:   Thu Aug 16 18:24:59 2018 +0800
    
        append GPL
    
    commit 98f655214d25482fe581f6bfa5d73b83dfa849f4
    Author: Xiaoji <Xiaoji@Xiaoji>
    Date:   Thu Aug 16 18:15:12 2018 +0800
    
        add distributed
    
    commit 37424cf2fb099229724cd19dfd68f63d4d73167d
    Author: Xiaoji <Xiaoji@Xiaoji>
    Date:   Thu Aug 16 17:58:38 2018 +0800
    
        wrote a readme.txt
    -----------------------------------------------------------------------------------------------------
    #简单版历史记录
    $ git log --pretty=oneline
    --->
    10e3ff13cb1552ae791c84392b8e6eaed78e55f5 (HEAD -> master) append GPL
    98f655214d25482fe581f6bfa5d73b83dfa849f4 add distributed
    37424cf2fb099229724cd19dfd68f63d4d73167d wrote a readme.txt
    

    1. 退回到上一个版本
    #HEAD表示当前版本,HEAD^上一个版本,HEAD^^上上一个版本
    #HEAD~100往上100个版本
    $ git reset --hard HEAD^  
    --->
    HEAD is now at 98f6552 add distributed
    

    1. 反悔了上面的退回操作,需要重新找回退回前的版本
    # 1、有之前要退回到commit id:
    $ git reset --hard 10e3ff
    --->
    HEAD is now at 10e3ff1 append GPL
    
    # 2、暂时没有之前的commit id:
    $ git reflog
    --->
    10e3ff1 (HEAD -> master) HEAD@{0}: reset: moving to 10e3ff
    98f6552 HEAD@{1}: reset: moving to HEAD^
    10e3ff1 (HEAD -> master) HEAD@{2}: commit: append GPL
    98f6552 HEAD@{3}: commit: add distributed
    37424cf HEAD@{4}: commit (initial): wrote a readme.txt
    ↓
    ↓
    ↓
    $ git reset --hard 10e3ff
    --->
    HEAD is now at 10e3ff1 append GPL
    

    1. 撤回修改
      a. 文件修改后反悔了,但这时文件还没有git add,需要撤回
    $ git checkout -- readme.txt  #把readme.txt文件在工作区的修改全部撤销,结果是回到和版本库一模一样的状态;
    

    b. 修改的文件已经git add,但还没有git commit,需要撤回

    # 1、先从暂存区撤回修改
    $ git reset HEAD readme.txt
    --->
    Unstaged changes after reset:
    M       readme.txt
    ↓
    ↓
    ↓
    # 1.5、查看状态
    $ git status
    --->
    On branch master
    Changes not staged for commit:      #注意not staged
      (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")
    ↓
    ↓
    ↓
    # 2、撤回修改到最新的commit状态
    $ git checkout -- readme.txt
    

    1. 删除文件,撤回删除
      a. 版本库的文件已从系统中删除,需要在git中确认
    # 将删除修改提交到暂存区
    $ git rm test.txt
    --->
    rm 'test.txt'
    ↓
    ↓
    ↓
    # 向git提交确认删除
    $ git commit -m 'remove test.txt'
    --->
    [master c262779] remove test.txt
     1 file changed, 0 insertions(+), 0 deletions(-)
     delete mode 100644 test.txt
    

    b. 版本库的文件删除错误,还没有git rm,撤回:

    $ git checkout -- test.txt
    

    c. 版本库的文件删除错误,已经git add``````git commit,撤回:

    $ git reset --hard 62adb  #62adb为版本号(修改记录号)
    

    1. 与github建立连接,将本地仓库推送到github
    # 在github创建一个远程仓库后,将本地仓库与之相连接
    $ git remote add origin https://github.com/junzhu-github/learngit.git
    ↓
    ↓
    ↓
    # 把本地库的所有内容推送到远程库上
    $ git push -u origin master  #第一次推送时,要加上了-u参数
    --->
    Enumerating objects: 19, done.
    Counting objects: 100% (19/19), done.
    Delta compression using up to 2 threads.
    Compressing objects: 100% (14/14), done.
    Writing objects: 100% (19/19), 1.45 KiB | 105.00 KiB/s, done.
    Total 19 (delta 4), reused 0 (delta 0)
    remote: Resolving deltas: 100% (4/4), done.
    To https://github.com/junzhu-github/learngit.git
     * [new branch]      master -> master
    Branch 'master' set up to track remote branch 'master' from 'origin'.
    
    # 此后,每次本地提交后,推送最新修改使用:
    $ git push origin master
    
    # 查看远程库的信息
    $ git remote
    --->
    origin
    
    # or 需要更详细信息
    $ git remote -v
    --->
    origin  git@github.com:junzhu-github/gitskills (fetch)
    origin  git@github.com:junzhu-github/gitskills (push)
    
    

    1. 从远程库克隆
    # 在github新建仓库gitskills后,克隆到本地
    $ git clone git@github.com:junzhu-github/gitskills
    --->
    Cloning into 'gitskills'...
    The authenticity of host 'github.com (198.19.0.112)' can't be established.
    RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 'github.com,198.19.0.112' (RSA) to the list of known hosts.
    remote: Counting objects: 3, done.
    remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    Receiving objects: 100% (3/3), done.
    

    1. git分支的创建、使用、删除
    # 创建并切换到分支
    $ git checkout -b dev
    --->
    Switched to a new branch 'dev'
    
    #equal to
    $ git branch dev
    $ git checkout dev
    --->
    Switched to branch 'dev'
    ↓
    ↓
    ↓
    # 查看当前分支
    $ git branch
    --->
    * dev
      master
    ↓
    ↓
    ↓
    # 提交文件修改  # !改变的只是分支上的文件内容,master的文件未改变
    $ git add README.md
    
    $ git status
    --->
    On branch dev
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
            modified:   README.md
    ↓
    ↓
    ↓
    # 把dev分支的工作成果合并到master分支上,master文件内容改变
    $ git merge dev
    --->
    Updating 482d191..1a83f8d
    Fast-forward
     README.md | 3 ++-
     1 file changed, 2 insertions(+), 1 deletion(-)
    ↓
    ↓
    ↓
    # 合并完成后,就可以放心地删除dev分支了
    $ git branch -d dev
    --->
    Deleted branch dev (was 1a83f8d).
    ↓
    ↓
    ↓
    # 删除后,查看branch,就只剩下master分支了
    $ git branch
    --->
    * master
    ↓
    ↓
    ↓
    # 切换回master分支
    $ git checkout master
    --->
    Switched to branch 'master'
    Your branch is up to date with 'origin/master'.
    

    1. 忽略特殊文件
      a. 在Git工作区的根目录下创建一个特殊的.gitignore文件,然后把要忽略的文件名填进去,Git就会自动忽略这些文件
      b. 把.gitignore也提交到Git
      tips: 使用Windows的童鞋注意了,如果你在资源管理器里新建一个.gitignore文件,它会非常弱智地提示你必须输入文件名,但是在文本编辑器里“保存”或者“另存为”就可以把文件保存为.gitignore了。

    1. 配置别名
    # 增加别名
    $ git config --global alias.st status  #--global是针对当前用户起作用的,如果不加,那只针对当前的仓库起作用
    $ git config --global alias.co checkout
    $ git config --global alias.ci commit
    $ git config --global alias.br branch
    $ git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
    
    # 删除别名
    git config --global --unset alias.ci
    
    查看配置文件
    # 当前仓库的local配置文件
    $ cat .git/config 
    
    # 用户配置文件
    $ cat .gitconfig
    

    相关文章

      网友评论

          本文标题:git使用教程

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