zoukankan      html  css  js  c++  java
  • 使用Git

    Git的介绍及在开发中的使用

    一、概述:

    git在本地维护了三棵“树”,分别是:

    1、工作目录:

      该目录实际存放文件

    2、暂存区:

      缓冲区,临时保存文件的变动,执行的命令为:

    git add filename
    git add *

      该命令把文件添加到缓存区。

    3、HEDA:

      指向最后一次提交的结果,执行的命令为:

    git commit -m '描述'

      该命令将改动提交到了HEAD。

    上图说明(图片来自廖雪峰老师博客):

    以上就是本地git的大体组成结构,要想将文件提交到远程仓库(github),还需要以下两步:

    1、关联远程仓库:

    git remote add origin <Git Server> (https://githun.com/ahaii/xxx.git)

    origin是默认远程仓库的名字。

    2、推送本地文件到远程仓库:

    git push -u origin master

     第一次推送时使用 -u 参数,git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。

    3、获取远程仓库获取最新版本,有两种方式:

    git pull:

    git pull  origin master #从远程仓库origin的master分支获取最新版本并于本地仓库合并

    git fetch:

    git fetch origin master        #获取远程仓库master分支到本地
    git log -l master ..origin/master  #比较本地master分支和远程获取的master的差别
    git merge origin/master        #合并master分支

    二、使用实例:

    1、在github中创建一个Repository

    2、在setting的deploy中粘贴本地生成的id_rsa.pub

    3、测试ssh远程连接是否成功:

    ssh -T git@github.com

    提示:'You’ve successfully authenticated, but GitHub does not provide shell access' 说明连接成功。

    4、安装git本地客户端:

    使用homebrew工具安装

    1)安装homebrew:(homebrew官网:http://brew.sh/)

    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

    2)安装git:

    brew install git

    5、建立本地git仓库:

    将本地项目根目录变成Git可以管理的仓库:

    git init

    6、将项目所有文件添加到缓存区中:

    git add .

    若只想添加某一个文件,只需将.换成相应文件名,可以多次执行add命令。

    7、设置全局用户和邮箱:

    git config --global user.email "ahaii@sina.com"
    
    git config --global user.name "ahaii"

    8、将缓存区中的文件(变动)commit到HEAD:

    git commit -m '本次提交描述'

    9、本地仓库关联github:

    去github上新创建的repository中复制ssh地址(https://github.com/ahaii/....git)。

    git remote add origin https://github.com/ahaii/testblog.git

    10、上传代码到github仓库:

    git push -u origin master

    会要求输入github的用户名和密码。

    以上就是将本地代码上传到github的过程。

    11、更新当前分支:

    git pull

    12、克隆远程仓库到本地:

    git clone https://github.com/ahaii/xxx.git

    三、分支介绍:

    在创建仓库的时候,会默认创建master主分支。使用分支可以从主线上分离开,分支上的任何更改不会影响主线,开发完成后可以分支合并到主线。

    1、创建分支:

    git branch <branchname>

    2、切换到分支中:

    git checkout <branchname>

    3、创建一个新的分支,并切换过去:

    git checkout -b <branchname>

    4、切回主分支:

    git checkout master

    5、删掉分支:

    git branch -d <branchname>

    6、合并分支:

    git checkout master   #需先切回主分支
    git merge <branchname>

    四、其他常用命令:

    1、显示最近几条提交信息:

    #git log --oneline -3     #显示最近的3次提交信息
    fb7d208 show article detail
    a99d32c reset
    fb05f6b show category aritcle and set url.name

    2、从暂存区删除文件:

    git rm <file>

    3、查看缓存区文件状态: 

    #git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        new file:   hello.txt

    有一个文件在缓存区(执行过git add)

    修改hello.txt文件内容后,再次添加到缓存区。然后查看缓存区状态:

    #git status 
    On branch master
    Your branch is up-to-date with 'origin/master'.
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        new file:   hello.txt
    
    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

    显示缓存区中有一个文件,并且,该文件被修改之后没有再次提交到缓存区。也可以使用-s参数以精简方式查看:

    #git status -s
    AM hello.txt

    显示结果中,第一列A表示文件添加到缓存区,第二列红色M表示文件被修改过后,没有再次添加到缓存区。

    文件被修改后,没有再次添加到缓存区时,查看具体修改内容:

    #git diff
    diff --git a/hello.txt b/hello.txt
    index eb8c108..6a57f2f 100644
    --- a/hello.txt
    +++ b/hello.txt
    @@ -1,3 +1,4 @@
     hello
     world
     .
    +.

    4、查看缓存区文件:

    git ls-files

    5、版本回滚 git reset:

    为了安全,对文件修改后,一般都会添加到版本库中(git add filename、git commit -m 'message')。当不小心修改错了文件,或者误删文件且已提交到了仓库中时(执行了git add 和 git commit),可以使用reset参数将版本库回滚到前一个版本。

     文本被修改前的内容:

    #cat hello.txt 
    hello ...

    修改后文本内容:

    #cat hello.txt 
    hello ...
    world

    提交文本到版本库:

    git add hello.txt
    git commit -m 'add world'

    现在,想回滚到文本被修改之前的状态(实际工作中,很可能会更改多处文本,我们根本记不住):

    查看最近的几次提交信息:

    #git log
    commit 10c040baeaf8084dcbc9c0be91c042d83e27547a
    Author: ahaii <yuliu100@sina.com>
    Date:   Wed Sep 27 13:21:49 2017 +0800
    
        add world
    
    commit afa5f754c9434c2b64919086da4bead6db2eb4d0
    Author: ahaii <yuliu100@sina.com>
    Date:   Wed Sep 27 13:05:03 2017 +0800
    
        add hello.txt

    Git中,使用HEAD表示当前的版本,即git log中现实的第一个ID(这里是10c040baeaf8084dcbc9c0be91c042d83e27547a,add world)。上一个版本是(afa5f754c9434c2b64919086da4bead6db2eb4d0,add hello.txt),用HEAD^表示,前一个版本使用HEAD^^表示。

    现在,回滚到上个版本(没有world):

    #git reset --hard HEAD^
    HEAD is now at afa5f75 add hello.txt

    查看文本内容:

    #cat hello.txt 
    hello ...

    已经没有world了。

    再次查看提交信息:

    #git log
    commit afa5f754c9434c2b64919086da4bead6db2eb4d0
    Author: ahaii <yuliu100@sina.com>
    Date:   Wed Sep 27 13:05:03 2017 +0800
    
        add hello.txt

    最新的一条即afa5f754c9434c2b64919086da4bead6db2eb4d0(add hello.txt)。

    6、删除文件:

    git rm <filename>  #删除缓存区和本地工作目录下的文件
    git rm --cached <filename>    #只删除缓存区内的文件,本地工作目录下文件保留

    五、Git目录下的几种文件状态:

    1、 在仓库目录下创建新的文件,不做任何git提交:

    #git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        test.txt
    
    nothing added to commit but untracked files present (use "git add" to track)

    2、git add提交到缓存区后:

    #git add test.txt 
    #git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        new file:   test.txt

    3、此时,文件在缓存区里创建了快照,根据提示,使用reset HEAD命令可以恢复到未缓存状态(将已缓存的文件移除,回到git add操作之前):

    #git reset HEAD test.txt 
    #git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        test.txt
    
    nothing added to commit but untracked files present (use "git add" to track)

    4、执行git add 后,对文件进行修改,然后查看git状态:

    #echo 'hello' > test.txt
    #git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        new file:   test.txt
    
    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:   test.txt

    可以看到,多了一个 'Changes not staged for commit:' 状态,表示文件被修改过后,没有被放入缓存区(git add)。此时,如果想提交到版本库,需要重新执行git add。(注:只有状态为Changes to be committed)的文件才能被git commit。

    5、如果想撤回,则执行git checkout(回到文件被修改之前,工作目录中文件也会被修改):

    #git checkout -- test.txt
    #git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        new file:   test.txt

    此时,文件内容为空。

    六、遇到的错误:

    1、远程仓库版本比本地高,本地push时报错:

    ! [rejected]        master -> master (fetch first)
    error: failed to push some refs to 'https://github.com/ahaii/python.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.

    解决:先更新本(pull)地版本库,再push。

       或者丢弃远程仓库的版本,强制push: 

    git push -f

    2、本地仓库有修改,没有合并提交,想用远程仓库上面的代码更新本地:

    error: You have not concluded your merge (MERGE_HEAD exists).
    hint: Please, commit your changes before merging.
    fatal: Exiting because of unfinished merge.

    解决:

      1、下载远程库内容,不做合并:

    git fetch --all

      2、reset把HEAD指向刚刚下载的最新版本

    git reset --hard origin/master

    参考资料:https://git-scm.com/book/zh/v2

  • 相关阅读:
    PHP之get请求用php脚本实现
    PHP之download
    PHP之缩略图
    PHP之upload
    文本域<textarea>将 替换成换行
    JSON中获取变量key的值
    emap表格checkbox属性默认勾选
    读取ORACLE数据库中BOLB形式数据在前端预览
    oracle常用语法
    批量删除本地仓库未下载完成的jar文件
  • 原文地址:https://www.cnblogs.com/ahaii/p/6238215.html
Copyright © 2011-2022 走看看