• Git-Notes


    1.Git安装,直接在官网下载安装即可。

    2.Git配置,使用config选项,配置名字和邮箱,如下所示

    C:Users1yyg>git config --global user.name "zhumuxian"
    
    C:Users1yyg>git config --global user.email "495472831@qq.com"
    

    3.创建仓库,使用init选项

    F:webim>git init
    Initialized empty Git repository in F:/webim/.git/
    

    4.添加文件和提交,使用add和commit选项

    F:webim>git add index.js  //将文件添加到暂存区
    
    F:webim>git commit -m "add index.js" //提交到master分支
    [master 92d277f] add index.js
     1 file changed, 888 insertions(+)
     create mode 100644 index.js

    5.查看仓库当前状态,使用status选项

    F:webim>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:   index.js

    6.对比当前版本文件和上个版本文件的差异,使用diff选项

    F:webim>git diff index.js
    diff --git a/index.js b/index.js
    index 06d9b64..0c6787e 100644
    --- a/index.js
    +++ b/index.js
    @@ -19,6 +19,8 @@ $(function(){
    
            var originDemension = {};
    
    +
    +
            var videoExtReg = /^.(?=mpg|3gp|vob|rmvb|mov|flv|avi|wmv|mp4|mkv)/i,
                    musicExtReg = /^.(?=flac|ape|wav|mp3|aac|ogg|wma)/i,
                    wordExtReg = /^.(?=doc|docx)/i;

    7.对于修改过的文件,再次使用add,commit命令提交即可

    8.查看仓库提交日志,使用log选项,搭配--pretty=oneline更清晰

    F:webim>git log --pretty=oneline
    973ef9dcb5c4cc3de12fa4e3668a16ff3adb2992 add nihao
    c8fbe43e78d3155262e427aa1df246156563cf23 add license.txt
    de0edd4c3fc56fdb74fd87ca9dfa0acb82847faa index.js
    92d277f2dbe720179530962b11d785c448c5296a add index.js
    5954ab28619985ab0ad768602578466dc92399fd del index.js
    cc4370f6424daf34c34f9b90196ab56857d61f47 add index.js

    9.撤销版本,使用reset选项

    git reset --hard HEAD^ //HEAD^后面必须跟一个空格,HEAD代表当前版本,HEAD^当前版本的上个版本,一次类推,也可以使用HEAD~[0-9..]来代替
    
    git reset --hard commit_id //使用commitId来撤销提交,commitId可以使用git log后者 git reflog来查看

    10.查看所有命令,使用reflog选项

    F:webim>git reflog
    973ef9d HEAD@{0}: reset: moving to HEAD^
    78ae302 HEAD@{1}: reset: moving to HEAD
    78ae302 HEAD@{2}: reset: moving to HEAD
    78ae302 HEAD@{3}: commit: add heheda again
    973ef9d HEAD@{4}: reset: moving to HEAD~1
    9fb4130 HEAD@{5}: commit: add heheda
    973ef9d HEAD@{6}: commit: add nihao
    c8fbe43 HEAD@{7}: commit: add license.txt
    de0edd4 HEAD@{8}: commit: index.js
    92d277f HEAD@{9}: commit: add index.js
    5954ab2 HEAD@{10}: commit: del index.js
    cc4370f HEAD@{11}: commit (initial): add index.js

    11.撤销修改

    情况一:工作区中的内容修改了,但是没有添加到暂存区,可以利用checkout -- file来撤销改动
    F:webim>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:   license.txt
    
    F:webim>git checkout -- license.txt
    
    F:webim>git status
    On branch master
    
    情况二:
    工作区的改动添加进了暂存区,但是没有提交,可以利用reset HEAD file来撤销修改,然后在使用git checkout -- file来恢复
    F:webim>git reset HEAD license.txt
    Unstaged changes after reset:
    M       license.txt
    F:webim>git checkout -- license.txt

    12.删除文件,使用rm选项,然后commit提交即可

    F:webim>git rm license.txt
    rm 'license.txt'
    F:webim>git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
            deleted:    license.txt
    
    F:webim>git commit -m "del license file"
    [master 5c258d3] del license file
     1 file changed, 2 deletions(-)
     delete mode 100644 license.txt

     13.添加远程仓库

    由于没有自己的git服务器,这里使用github的服务,首先创建ssh key,采用rsa加密,主要是github用来确认推送者的身份,如下所示

    $ ssh-keygen -t rsa -C "495472831@qq.com"
    Generating public/private rsa key pair.
    Enter file in which to save the key (/c/Users/1yyg/.ssh/id_rsa):
    Created directory '/c/Users/1yyg/.ssh'.
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /c/Users/1yyg/.ssh/id_rsa.
    Your public key has been saved in /c/Users/1yyg/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:jDCsKX+gz2uoiLMYoBuphe7bK7KH4hTgsDkb3N6Gnks 495472831@qq.com

     然后将id_rsa.pub的内容复制到github网站个人设置里面的new ssh key中。接着使用git remote add添加远程库,该库必须在github中存在

    F:webim>git remote add webim https://github.com/zhu495472831/webim.git

    接着在推送内容分支内容之前,使用git pull 先拉取最新的内容

    F:webim>git pull webim master
    //如果遇到fatal: refusing to merge unrelated histories错误,可以添加 --allow-unrelated-histories选项解决
    F:webim>git pull webim master --allow-unrelated-histories

    然后就可以推送内容了

    F:webim>git push webim master
    Counting objects: 3, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (3/3), done.
    Writing objects: 100% (3/3), 3.20 KiB | 0 bytes/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To https://github.com/zhu495472831/webim.git
       ef0c6db..1e4b6fa  master -> master

    14.克隆仓库,使用git clone

    F:zmxgittest>git clone git@github.com:zhu495472831/webim.git
    Cloning into 'webim'...
    The authenticity of host 'github.com (192.30.253.112)' can't be establ
    RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 'github.com,192.30.253.112' (RSA) to the li
    n hosts.
    remote: Counting objects: 27, done.
    remote: Compressing objects: 100% (16/16), done.
    remote: Total 27 (delta 4), reused 23 (delta 3), pack-reused 0R
      51% (14/27)
    Receiving objects: 100% (27/27), 12.45 KiB | 0 bytes/s, done.
    Resolving deltas: 100% (4/4), done.

    15.分支,分支有利于多人协同作业

    创建分支git branch name或者git checkout -b name创建并切换,查看分支git branch,切换分支git checkout branchName,删除分支git branch -d

    F:webim>git branch
    * master
    
    F:webim>git branch dev
    
    F:webim>git branch
      dev
    * master
    
    F:webim>git checkout dev
    Switched to branch 'dev'
    
    F:webim>git branch
    * dev
      master

    删除分支

    F:webim>git branch dev2
    
    F:webim>git branch
    * dev
      dev2
      master
    
    F:webim>git branch -d dev2
    Deleted branch dev2 (was 1e4b6fa).
    
    F:webim>git branch
    * dev
      master

    在创建分支之后,在分支上对文件进行的操作,别的分支感知不到,可以使用merge合并分支,便可以

    F:webim>git merge dev
    Updating 346ce30..d17e615
    Fast-forward
     hello.txt | 3 ++-
     1 file changed, 2 insertions(+), 1 deletion(-)

     在合并分支的时候,可以指定--no-ff参数,表示禁用fast forward模式,这样在合并分支之后,删除分支时,分支的信息就不会丢失

    F:webim>git merge --no-ff -m "merge dev width no-ff" dev
    Merge made by the 'recursive' strategy.
     hello.txt | 3 ++-
     1 file changed, 2 insertions(+), 1 deletion(-)

    16.存储现场,当在一个分支上进行开发的时,来了一个优先级更高的bug需要修复,这时不想提交未完成的代码,就可以使用stash命令来保存现场

    F:webim>git stash list
    stash@{0}: On dev: hello.txt
    
    F:webim>git stash pop
    On branch dev
    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:   hello.txt

    git stash 保存     git stash save [message] 保存并备注       git stash list查看所有stash        git stash clear清空所有stash     git stash apply  stash@{n} 还原某个stash    git stash pop还原最近的一次stash

    17 查看绑定的远程仓库信息

    F:webim>git remote -v
    webim   https://github.com/zhu495472831/webim.git (fetch)
    webim   https://github.com/zhu495472831/webim.git (push)

    18 创建和远程分支关联的分支

    F:webim>git checkout -b dev webim/dev
    Branch dev set up to track remote branch dev from webim.
    Switched to a new branch 'dev'
    //也可以使用branch --track或--set-upstream-to关联
    F:webim>git branch --set-upstream dev webim/dev
    The --set-upstream flag is deprecated and will be removed. Consider using --trac
    k or --set-upstream-to
    Branch dev set up to track remote branch dev from webim.

    19 从远程库拉取内容

    F:webim>git pull
    Already up-to-date.

    20 打标签,使用tag命令

    git tag:查看当前分支所有tag

    git tag name:创建标签name,还可以指定commitid,例如git tag v1.0 63cbab46a467236a290fd4fb56439755f83ac49e,另外可以为标签加上描述,如git tag v1.0 -a -m "release 1.0"

     git show tag_name:查看某个标签的详细信息

    F:webim>git tag v1.0
    
    F:webim>git tag
    v1.0
    
    F:webim>git show v1.0
    commit 63cbab46a467236a290fd4fb56439755f83ac49e
    Author: zhumuxian <495472831@qq.com>
    Date:   Fri Oct 14 14:13:03 2016 +0800
    
        add nizhidema
    
    diff --git a/hello.txt b/hello.txt
    index 8ef3fc0..09f6427 100644
    --- a/hello.txt
    +++ b/hello.txt
    @@ -1,2 +1,3 @@
     hello world
    -this is a worla <A3><A1><A3><A1>
     No newline at end of file
    +this is a worla <A3><A1><A3><A1>
    +<C4><E3>ֵ<B5><C3><C2><F0>
     No newline at end of file

     21 忽略文件,通过使用.gitignore文件配置,在git根目录新建一个名为.gitignore的文件,然后推送到github,内容如下所示

    #images
    *.jpg
    *.png
    *.gif
    *.swf
    *.html

    22 配置别名,使用git config alias命令配置命令的别名

    使用命令配置如下

    F:webim>git config --global alias.st status
    F:webim>git st
    On branch master
    Your branch is up-to-date with 'webim/master'.
    nothing to commit, working tree clean

     

  • 相关阅读:
    文件处理
    基本数据类型
    Python简介和入门
    了解计算机的发展历程
    工具类(MailUtils)发邮件
    文件上传和下载
    JavaWeb过滤器
    JavaWeb监听器
    JavaWeb的分页
    JdbcUtils(内部使用c3p0得到连接池对象datasource)的第三次修改---完成事务的处理
  • 原文地址:https://www.cnblogs.com/zmxmumu/p/5953940.html
Copyright © 2020-2023  润新知