• git学习笔记


    比较全且易懂的git学习网站

    https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013743256916071d599b3aed534aaab22a0db6c4e07fd0000

    分布式版本库和集中式版本库的区别

    集中式版本库

    如SVN

    通过与中心服务器的连接执行所有操作,必须联网

    分布式版本库

    如Git

    1、快速,本地客户机操作,不联网也不影响工作,离线工作,DVCS 比 Subversion 快大约3-10倍

    2、可以实现非常灵活的工作流(传统的集中式工作流 + 特殊工作流 + 特殊工作流和集中式工作流的组合)

    3、安全性更高,因为每个人电脑里都是完整的版本库,坏了复制一份即可,CVCS中央服务器出问题就GG      

    4、两台电脑互相访问不了或某一台电脑未开机不能执行复制(交换修改)时,通常有一台电脑充当中央服务器 

     5、分支管理

    git常用命令

    创建版本库

    git init

    192:gitblit liqiang$ cd gitTest
    192:gitTest liqiang$ git init
    Initialized empty Git repository in /Users/liqiang/Desktop/gitblit/gitTest/.git/
    192:gitTest liqiang$ 

    在指定目录执行git init 命令 该目录下的所有目录及文件都受git管理

    查看状态

    git status

    192:gitTest liqiang$ mkdir files
    192:gitTest liqiang$ vi hello.txt
    192:gitTest liqiang$ git status

    创建了一个files目录 并在files目录下增加了一个hello.txt文件通过git status可以看到还没有add

    忽略文件

    我们使用idea开发,会有很多target和maven的一些文件这些文件是不需要提交的所以在根目录创建.gitignore

    #忽略所有.svn目录
    .svn/
    #忽略所有target目录
    target/
    #忽略所有.idea目录
    .idea/
    #忽略所有.iml文件
    *.iml

    添加和提交

    git add git commit

    这2个是组合命令 先add将文件提交到暂存区  再commit 提交到分支  git默认会创建一个master分支

    192:gitTest liqiang$ git add files
    192:gitTest liqiang$ git status
    On branch master
    
    No commits yet
    
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
    
        new file:   files/hello.txt

    执行status后再查看状态 显示还没有提交   git add . 可以提交所有

    192:gitTest liqiang$ git commit files -m '创建了一个hello文件'
    [master (root-commit) 224f10f] 创建了一个hello文件
     1 file changed, 1 insertion(+)
     create mode 100644 files/hello.txt
    192:gitTest liqiang$ git status
    On branch master
    nothing to commit, working tree clean
    192:gitTest liqiang$ 

    执行commit就成功提交到本地仓库 或者可以到指定目录执行git add . (空格+.) 提交目录下面所有文件

    查看提交详情

    查看提交的详情: git log -

    查看日志

    git log

    192:gitTest liqiang$ git log
    commit 224f10fd409bc7f8f83167167c42d05e36a5bb24 (HEAD -> master)
    Author: liqiang <www.liqiand@qq.com>
    Date:   Fri Jan 4 13:50:16 2019 +0800
    
        创建了一个hello文件

    可以看到刚才的提交记录

    查看某个分支日志

     git log $分支名/tag名/远程分

    版本回退 

    伪造测试数据 对文件进行2此修改并提交 查看日志

    192:gitTest liqiang$ git log
    commit 3708002e7618ca88f349cdec3517b0b1f9611413 (HEAD -> master)
    Author: liqiang <www.liqiand@qq.com>
    Date:   Fri Jan 4 13:58:24 2019 +0800
    
        文件内容修改为hell3
    
    commit 209a176fa3413ac5f3528c2da1ab7126802160e3
    Author: liqiang <www.liqiand@qq.com>
    Date:   Fri Jan 4 13:57:08 2019 +0800
    
        文件内容修改为hell2
    
    commit 224f10fd409bc7f8f83167167c42d05e36a5bb24
    Author: liqiang <www.liqiand@qq.com>
    Date:   Fri Jan 4 13:50:16 2019 +0800
    
        创建了一个hello文件

    或者加上--pretty=oneline 使日志一行显示

    192:gitTest liqiang$ git log --pretty=oneline
    3708002e7618ca88f349cdec3517b0b1f9611413 (HEAD -> master) 文件内容修改为hell3
    209a176fa3413ac5f3528c2da1ab7126802160e3 文件内容修改为hell2
    224f10fd409bc7f8f83167167c42d05e36a5bb24 创建了一个hello文件
    192:gitTest liqiang$ 

     回退到上一个版本

    192:gitTest liqiang$ git reset --hard HEAD^
    HEAD is now at 209a176 文件内容修改为hell2

    打开文件可以看到变成了hello2

    如果要回退到指定版本呢只需要hard参数加上要回退的版本号

    $  git reset --hard 3708002e7618ca88f349cdec3517b0b1f9611413
    HEAD is now at 3708002 文件内容修改为hell3

     git每次提交 都会为每次提交保留一个版本  版本回退只是将指针指向回退的版本

    当版本回退 head 重新指向了 hello2

    强制合并到远程同名分支 让远程分支代码跟本地一样

    git push origin HEAD --force

    撤销add 

    192:files liqiang$ git add hello.txt
    192:files liqiang$ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        modified:   hello.txt

    通过将文件修改通过git add到暂存区 如何撤销提交(我工作中经常遇到这样的需求)

    192:files liqiang$ git reset HEAD hello.txt
    Unstaged changes after reset:
    M    files/hello.txt
    192:files liqiang$ 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:   hello.txt

    git reset HEAD 不加文件信息 则是 撤销所有暂存区

    git reset --soft HEAD^

    撤销本地修改 

    git checkout -- filepathname

    192:files liqiang$ git checkout -- hello.txt 

    删除文件/目录

    git rm filename   git rm -r directoryName

    92:gitTest liqiang$ git rm hello.txt
    92:gitTest liqiang$ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        deleted:    files/hello.txt

    如果需要撤销与上面撤销修改一致

    撤销Untracked files

    # 删除 untracked files
    git clean -f
     
    # 连 untracked 的目录也一起删掉
    git clean -fd
     
    # 连 gitignore 的untrack 文件/目录也一起删掉 (慎用,一般这个是用来删掉编译出来的 .o之类的文件用的)
    git clean -xfd
     
    # 在用上述 git clean 前,墙裂建议加上 -n 参数来先看看会删掉哪些文件,防止重要文件被误删
    git clean -nxfd
    git clean -nf
    git clean -nfd

    添加远程仓库 

    $ git remote add [远程仓库名字(一般叫origin)] [远程仓库地址] $ git push -u origin master

    $ git remote add  本地仓库与远程仓库关联

    $ git push -u origin master  推送到远程仓库

    可以使用github 我这里是本地搭建了一个git服务器 gitblit

    192:gitTest liqiang$ git remote add origin http://admin@127.0.0.1:1234/r/gitTest.git
    192:gitTest liqiang$ git push -u origin master
    Password for 'http://admin@127.0.0.1:1234': 
    To http://127.0.0.1:1234/r/gitTest.git
     ! [rejected]        master -> master (fetch first)
    error: failed to push some refs to 'http://admin@127.0.0.1:1234/r/gitTest.git'
    hint: Updates were rejected because the remote contains work that you do
    hint: not have locally. This is usually caused by another repository pushing
    hint: to the same ref. You may want to first integrate the remote changes
    hint: (e.g., 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    192:gitTest liqiang$ failed to push some refs to

     报错是因为我上面创建版本库 勾上了添加README文件 origin不是一个独立的版本库了。。如果使用clone 将没有问题 

    这个时候需要执行

    $ git pull origin master --allow-unrelated-histories

    合并origin和本地版本库

    $ git commit -m '合并'

    最后推送到远程origin

    $ git push -u origin master

    PUll回退

    1、git reflog 

    2、git reset --hard   <COMMIT_ID> 或者 git reset --hard HEAD@{2}

    clone远程分支

      git clone

    $ git clone [git地址]

    强制推送

    git push -force

    git分支管理

    切换远程分支并创建本地分支

    git checkout -b feature/6.5.5.5_liqiang origin/feature/6.5.5.5

    创建并切换分支

    192:gitTest liqiang$ git checkout -b dev
    Switched to a new branch 'dev'
    192:gitTest liqiang$ git branch
    * dev
      master
    192:gitTest liqiang$ 

    git branch为查看当前分支  上面当前指向dev分支

    当我们再dev上面做任何修改都不会影响其他分支

    切换分支

    192:gitTest liqiang$ git checkout master
    Switched to branch 'master'
    Your branch is up to date with 'origin/master'.
    192:gitTest liqiang$ git branch
      dev
    * master
    192:gitTest liqiang$ 

    解决冲突 

    dev分支将hello.txt改为并推送到远程仓库

    hello2
    hhhhhh
    ~      

    再切换到master将hell.txt改为并推送到远程仓库

    hello5
    llllll

    执行$ git merge [分支名字]将指定分支合并到当前分支

    192:files liqiang$ git branch
      dev
    * master
    192:files liqiang$ 

    当前分支是master  执行git merge [分支名字] 就是讲指定分支合并到当前分支

    192:files liqiang$ git merge dev
    Auto-merging files/hello.txt
    CONFLICT (content): Merge conflict in files/hello.txt
    Automatic merge failed; fix conflicts and then commit the result.

    存在冲突无法自动合并  git告诉我们hello.txt冲突了 需要我们手动合并

    192:files liqiang$ git status
    On branch master
    Your branch is up to date with 'origin/master'.
    
    You have unmerged paths.
      (fix conflicts and run "git commit")
      (use "git merge --abort" to abort the merge)
    
    Unmerged paths:
      (use "git add <file>..." to mark resolution)
    
        both modified:   hello.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")

    git status会告诉我们冲突的文件

    查看文件  冲突的都用====标记起来

    <<<<<<< HEAD
    hello5
    llllll
    =======
    hello2
    hhhhhh
    >>>>>>> dev
    ~                

    Head为 当前分支  dev为dev内容 手动修改 选择要保留的部分 

    hello5
    hhhhhh

    重新add  commit push即可

    Fast forward模式

    当我们使用git merge dev进行合并时会将master直接将指针指向 dev 实现快速合并

    使用--no-ff -m "merge with no-ff" 参数时会禁用Fast forward模式 会重新生成一个新的提交记录 并将master指向他

    $ git merge --no-ff -m "merge with no-ff" dev

    如:

     

     执行命令创建一个dev分支

    $ git checkout -b dev

    上面会创建并切换分支 所以HEAD指向dev

    我们再dev分支进行修改提交操作

    192:files liqiang$ git commit -m '修改hello文件'
    [dev 2ade7d5] 修改hello文件

    Fast forward合并分支

    直接快速切换将maste指向合并时dev的版本记录线

    Fast forward合并

    会多一次commit并将master指针指向他

    bug分支

    工作中用得比较多  常常会遇到 自己本地改动了大量代码 线上有bug需要立即更改部署  但是更改后会被本地修改代码影响

    我们将hello.txt改为

    hello5
    hhhhhh
    fffff
    fffff
    fffff
    fffff
    ffffff
    fff
    ffff
    ffffff
    192:files liqiang$ git stash save '备份'
    Saved working directory and index state On master: 备份

    这个时候暂存区 和工作区都会被还原成分支的最新版本  同时把自己修改的数据备份起来

    这个时候我们就可以修改bug并并提交

    通过git stash list可以查看我们的备份 

    192:files liqiang$ git stash list
    stash@{0}: On master: 备份

    改完bug后通过

    $ git stash apply stash@{0}

    则可以还原

    修改分支名字 

    1.删除远程分支

    git push --delete origin [分支名字]

    2.重命名本地分支

    git branch -m [修改的分支名字] [新增分支名字]

    3.重新提交新分支

    git push origin [新分支名字]

    删除本地分支

    git branch -D 分支名字

    解除关联重新关联新的仓库

    liqiangdeMacBook-Pro:canal-canal-1.1.4 liqiang$ git remote rm origin
    liqiangdeMacBook-Pro:canal-canal-1.1.4 liqiang$ git remote add origin http://git.tuna.1919.cn/kh/soon/canal-canal-1.1.4.git
    liqiangdeMacBook-Pro:canal-canal-1.1.4 liqiang$ git push -u origin master
  • 相关阅读:
    10,EasyNetQ-发布确认
    9,EasyNetQ-版本化消息
    一个小程序云开发的项目,图书借还系统
    利用canvas对图片进行切割
    微信小程序添加卡券到微信卡包,使用wx.addCard()方法传参及整体流程
    git合并时忽略某个文件
    小程序接入云通信IM
    小程序插件使用wx.createSelectorQuery()获取不到节点信息
    小程序插件开发流程及注意事项
    小米6使用charles抓包https
  • 原文地址:https://www.cnblogs.com/LQBlog/p/10219959.html
Copyright © 2020-2023  润新知