git命令操作

本地库操作

初始化本地仓库

  • 初始化命令

    git init

$ work % cd workspace
$ workspace % mkdir WebService 创建文件夹
$ workspace % git init 初始化
Initialized empty Git repository  jackworkspace
$ workspace %
  • 初始化后的效果

    会在初始化后的目录中生成一个.git隐藏文件夹

$ workspace % cd .git
$ .git % ls
HEAD		branches	config		description	hooks		info		objects		refs
$ .git % ls -l
total 
-rw-r--r--    jack  staff    Sep   HEAD
drwxr-xr-x    jack  staff    Sep   branches
-rw-r--r--    jack  staff   Sep   config
-rw-r--r--    jack  staff    Sep   description
drwxr-xr-x   jack  staff   Sep   hooks
drwxr-xr-x    jack  staff    Sep   info
drwxr-xr-x    jack  staff   Sep   objects
drwxr-xr-x    jack  staff   Sep   refs
$ .git %

注意:.git目录中的文件不要删除也不要修改,否则git不能正常工作。

设置签名

  • 形式

    用户名:XXX

    Email地址:XXX@gmail.com

  • 作用

    只是为了区分成员身份,不会给邮件地址发送邮件。而且邮件地址可以是不存在地址。

  • 注意

    这里的签名和远程仓库的邮件地址密码没有任何关系

  • 命令

    $ WebService % git config user.name njzy
    $ WebService % git config user.email njzy@2020.com
    $ WebService %
    $ WebService % git config --global user.name njzy_global
    $ WebService % git config --global user.email njzy_global@2020.com
    $ WebService %
  1. 系统用户级别

    登录当前系统的用户范围

    git config --global user.name 用户名

    git config --global user.email 邮箱地址

  1. 项目级别/仓库级别

    仅在当前本地仓库有效

    git config user.name 用户名

    git config user.email 邮箱地址

签名保存位置

1.项目级别

当前项目下的.git文件夹下的config文件中。

查看命令:cat .git/config

$ WebService % cat .git/config 
[core]
  repositoryformatversion = 
  filemode = 
  bare = 
  logallrefupdates = 
  ignorecase = 
  precomposeunicode = 
[user]
  name = njzy 
  email = com 
$ WebService %

2.系统用户级别

当前用户文件夹下的.gitconfig文件中。

查看命令:cat .gitconfig

$ ~ % cd ~ 切换到当前用户根目录
$ ~ % pwd  查看当前位置
jack
$ ~ % cat .gitconfig 查看全局签名信息
[user]
  name = njzy_global 被设置的全局用户名
  email = njzy_global@.com 被设置的全局邮件地址
$ ~ %

项目级别和系统用户级别的签名的优先(就近原则)

1.> 两个都设置情况下

项目级别优先于系统用户级别

2.> 只设定系统用户级别签名

以系统用户级签名别为准

3.> 只设定项目级别

以项目级别为准

4.> 两个都没有设定的话不允许。

查看git状态

  • 作用
    确认git的暂存区,本地仓库的情况。

  • 命令
    git status

添加操作

  • 目的

    把内容从工作区添加到暂存区,也叫追踪。

  • 命令

    1.添加单个文件

    git add 文件名

    2.添加多个文件

    1.> git add 文件名1 文件名2 文件名3 ....

    2.> git add -A

    3.> git add .

    详细参考git帮助文档

    : git add [<options>] [--] <pathspec>...
    
        -n, 
        -v, 
    
        -i, 
        -p, 
        -e, 
        -f, 
        -u, 
        
        -N, 
        -A,
  • 使用例

    为了清除git各个阶段的状态,在进行添加操作之前先查看一下git的状态。

    (下面例子是从工作区文件的创建到追加暂存区的过程)

    1.查看工作区被初始化后的状态

    $ WebService % git status
    On branch master
    
    No commits yet 
    
    nothing  commit (create/copy files     track) 
    $ WebService %

    2.创建文件testGIt.txt文件

    $ WebService %  testGit.txt
    $ WebService %  testGit.txt
    //查看文件内容
    hello !
    this  my  git test  !
    $ WebService %

    3.查看文件创建完后git状态

    $ WebService % git status
    On branch master
    
    No commits yet //本地仓库没有乐意提交的东西。
    
    Untracked files: //没有追加跟踪的文件
      (     what will be committed) //使用git  <> 命令可以追加文件到暂存区
    	testGit.txt //刚才新创建的文件,此时文件名字体为红色
    
     added   but untracked files  (   track) //使用git 命令
    $ WebService %

    4.将文件添加到暂存区并查看状态

    $ WebService % git add testGit.txt
    $ WebService % git status
    On branch master
    
    No commits yet
    
    Changes  be committed:
      (   unstage) 
    	 file:   testGit.txt 
    
    $ WebService %

提交操作

  • 目的

    将文件从暂存区提交到本地仓库

  • 命令

    1.提交单个文件

    git commit -m "提交时注释信息" 文件名

    2.提交复数个文件

    git comit -a -m "提交时注释信息"

    更多详细可以参考git帮助文档

    : git commit [<options>] [--] <pathspec>...
    
        -q, --quiet           suppress summary after successful commit
        -v, --         show diff in commit message template
    
    Commit message 
        -F, --       message from 
        --author      override author  commit
        --date          override date  commit
        -, --message 
                              commit message
        -, --reedit-message 
                              reuse   message from specified commit
        -C, --reuse-message 
                              reuse message from specified commit
        --fixup       use autosquash formatted message  fixup specified commit
        --squash      use autosquash formatted message  squash specified commit
        --reset-author        the commit  authored by  now (used with -C/-/--amend)
        -s, --signoff          Signed-off-by:
        -t, --template 
                              use specified template 
        -, --            force  of commit
        --cleanup       how  strip spaces  #comments from message
        --status              include status in commit message template
        -S, --gpg-[=]
                              GPG  commit
    
    Commit contents 
        -, --             commit  changed 
        -i, --include          specified     commit
        --interactive         interactively  
        -, --patch           interactively  
        -, --            commit  specified 
        -n, ---verify       bypass -commit  commit-msg hooks
        --dry-run             show what would  committed
        --short               show status concisely
        --branch              show branch information
        --ahead-behind        compute full ahead/behind 
        --porcelain           machine-readable output
        --long                show status in long format (default)
        -, --null            terminate entries with NUL
        --amend               amend  commit
        ---post-rewrite     bypass post-rewrite hook
        -, --untracked-[=]
                              show untracked , optional  , , . (Defaul )
  • 使用例

    提交文件到本地仓库并查看git状态

    $ WebService % git  -m  testGit.txt
    [ (root-) c970a17]  :  testGit.txt // :  testGit.txt 是提交时候的注释信息
       ,  insertions(+) //改变了一个文件,追加了两行信息。
        testGit.txt
    $ WebService % git             
     branch 
      , working tree clean //暂存区没有可以提交的东西
    $ WebService %

擦除暂存区操作

  • 目的

    擦除暂存区的内容

  • 命令

    git rm --cached 文件名

    $ WebService % git rm 
    rm 'testGit.txt'
    $ WebService % git status
    On branch master
    
    No commits yet
    
    Untracked files:
      (     what will be committed)
    	testGit.txt //没有被追踪的文件
    
     added   but untracked files  (   track)
    $ WebService %

查看历史版本信息

  • 目的

    查看提交版本的历史信息

  • 命令

    1.git log

    显示完整信息,包含提交的版本号,提交用户,提交日期,提交注释等信息。

    2.git log --pretty=oneline

    只包含提交后的完整版本号和提交时的注释信息

    3.git log --oneline

    包含提交后的简略版本号和提交时的注释信息

  • 注意

    只能查看已经被提交(commit)的文件的历史信息。

  • 使用例1

    $ WebService % git log
    //第二次被提交的信息
     fd3c0ff3e01e09bf98d883f97a3b0a9c86 ( -> ) //提交版本的值
    Author: njzy <njzy@com> //提交用户信息
    :   Sat Sep  ::  + //提交日期
    
        this  my  ,  testGit.txt //提交时注释
    //第一次被提交的信息
     c970a176de13abc4d436e4a08df329046ef193e7
    Author: njzy <njzy@com>
    :   Sat Sep  ::  +
    
         :  testGit.txt
    $ WebService %
  • 使用例2

    $ WebService % git  --pretty=oneline
    fd3c0ff3e01e09bf98d883f97a3b0a9c86 (HEAD -> master) this  my second commit, modify testGit.txt
    c970a176de13abc4d436e4a08df329046ef193e7  commi  testGit.txt
    $ WebService %
  • 使用例3

    $ WebService % git  --oneline
    
     (HEAD -> master) this  my second commit, modify testGit.txt
    c970a17  commi  testGit.txt
    $ WebService %

历史版本的前进或者回退

查看带head信息的日志

  • 目的

    查看带有指针和hash地址的日志信息,方便进行版本的前进或者后退。

  • 命令

    git reflog

  • 使用例

    $ WebService % git reflog
    f (HEAD -> master) {}: commit:   my second commit, modify testGit.txt
    
    
    c970a17 {}: commit (initial): first commit:new file testGit.txt
    
    $ WebService %

版本的前进或者和回退

基于hash地址的版本指定【推荐】

  • 目的

    对版本进行前进操作或者回退操作

  • 命令

    git reset --hard 指定版本的hash地址

  • 使用例

    从第五个版本跳转到第三个版本

    $ WebService % git reflog 
    d3c9608 (HEAD -> master) {}: commit:   my fifth updata,updata testGit.txt
    e {}: commit:   my fourth commit,updata testGit.txt
    dba7c5 {}: commit:   my third commit,updata testGit.txt
    f {}: commit:   my second commit, modify testGit.txt
    c970a17 {}: commit (initial): first commit:new file testGit.txt
    $ WebService % cat testGit.txt 
    hello !
      my first git test file !
    
      added
    
      my third updata!
    
      my four updata!
    
      my fifth updata!
    $ WebService % git reset --hard dba7c5
    
    HEAD  now at dba7c5   my third commit,updata testGit.txt
    $ WebService % cat testGit.txt 
    hello !
      my first git test file !
    
      added
    
      my third updata!
    
    $ WebService % git reflog 
    dba7c5 (HEAD -> master) {}: reset: moving to dba7c5
    d3c9608 {}: commit:   my fifth updata,updata testGit.txt
    e {}: commit:   my fourth commit,updata testGit.txt
    dba7c5 (HEAD -> master) {}: commit:   my third commit,updata testGit.txt
    f {}: commit:   my second commit, modify testGit.txt
    c970a17 {}: commit (initial): first commit:new file testGit.txt
    $ WebService %

使用^符号(只能进行版本的后退不能前进)

  • 目的

    进行版本的回退

  • 命令

    git reset --hard HEAD^

    (注意:一个^代表倒退一个版本)

  • 使用例

    从第四个版本倒退到一个版本到第三个版本

    $ WebService % git  
        dba7c5 this  my third ,updata testGit.txt
    $ WebService % cat testGit.txt
    hello !
    this  my  git   !
    
    this  added
    
    this  my third updata!
    
    $ WebService %

    倒退两个版本

    $ WebService % git  
        c970a17  :  testGit.txt
    $ WebService % cat testGit.txt
    hello !
    this  my  git   !
    $ WebService %

使用~符号

  • 目的

    退回指定版本

    (只能倒退,但是可以指定指定退几步)

  • 命令

    git reset --hard HEAD~要退的步数

  • 使用例

    从当前版本第五版后退两步到第三个版本

    $ WebService % git  
        dba7c5 this  my third ,updata testGit.txt
    $ WebService % cat testGit.txt
    hello !
    this  my  git   !
    
    this  added
    
    this  my third updata!
    
    $ WebService %

reset三个参数的对比(对比详细图文参照文末图片1,2,3)

--soft参数

  • 说明

    仅在本地仓库移动HEAD指针

  • 命令

    git reset -soft 指定版本号

  • 使用例

    $ WebService % cat testGit.txt 
    hello !
      my first git test file !
    
      added
    
      my third updata!
    
    $ WebService % git reset --soft f
    
    $ WebService % cat testGit.txt 
    hello !
      my first git test file !
    
      added
    
      my third updata!
    
    $ WebService % git status 
    On branch master
    Changes to be committed:
      (use  to unstage)
    	modified:   testGit.txt 
    	
    
    $ WebService %

--mixd参数

  • 说明

    在本地仓库移动HEAD指针,重置暂存区。

  • 命令

    git reset --mixed 指定版本号

  • 使用例

    $ WebService % cat testGit.txt //跳转到其他版本之前文件内容
    hello !
    this is my first git test file !
    
    this is added
    
    this is my third updata!
    
    $ WebService % git  
    //指定到指定版本
    Unstaged changes  :
    M	testGit.txt
    $ WebService % git reflog
    //指定版本后的日志
    d3c9608 ( -> ) @{}: : moving  d3c9608
    f @{}: : moving  f
    dba7c5 @{}: : moving  ~
    d3c9608 ( -> ) @{}: : moving  d3c9608
    c970a17 @{}: : moving  ^^
    dba7c5 @{}: : moving  ^
    e @{}: : moving  e
    dba7c5 @{}: : moving  dba7c5
    d3c9608 ( -> ) @{}: : this  my fifth updata,updata testGit.txt
    e @{}: : this  my fourth ,updata testGit.txt
    dba7c5 @{}: : this  my third ,updata testGit.txt
    f @{}: : this  my  ,  testGit.txt
    c970a17 @{}:  ():  :  testGit.txt
    $ WebService % cat testGit.txt  //查看切换版本后的文件内容
    hello !
    this  my  git   !
    
    this  added
    
    this  my third updata!
    
    $ WebService % git 
     branch 
    Changes  staged  :
      (    what will be committed)
      (   discard changes  working )
    	modified:   testGit.txt //此时文件名是红色
    
     changes added   (  / )
    $ WebService %

--hard参数

  • 说明
    在本地仓库移动HEAD指针
    重置暂存区
    重置工作区

  • 命令
    git reset --hard 指定版本

  • 使用例
    参照上面各种版本前进或者后退的例子。

删除文件并找回

  • 前提

    提交到本地库后删除才能找回

  • 删除

    物理删除即可

  • 找回版本

    通过跳转到指定版本命令即可找回删除的内容。

  • 使用例

    新建文件->追加到暂存区->提交到本地仓库->删除文件->添加到暂存区->提交到本地仓库

    $ WebService % vim test2.txt //1.创建一个新的测试文件
    $ WebService % git add test2.txt //2.添加到暂存区
    $ WebService % git  -m  test2.txt //提交到本地仓库
    [ a4e57d]   test2.txt
       ,  insertions(+)
        test2.txt
    $ WebService % ls -l //查看当前文件夹文件
    total 
    -rw-r
    -rw-r
    $ WebService % git  //查看git状态
     branch 
      , working tree clean
    $ WebService %
    $ WebService %
    $ WebService %
    $ WebService % rm test2.txt //本地物理删除文件
    $ WebService % ls -l //删除后确认
    total 
    -rw-r
    $ WebService % git  //查看删除后
     branch 
    Changes  staged  :
      (    what will be committed)
      (   discard changes  working )
    	deleted:    test2.txt //红色字体,表示这个操作没有被添加到暂存区
    
     changes added   (  / )
    $ WebService % git  test2.txt //将删除后的状态添加到暂存区
    $ WebService % git  //查看添加后的git状态
     branch 
    Changes  be committed:
      (   unstage)
    	deleted:    test2.txt //文字为绿色,表示暂存区已被更新。
    
    $ WebService % git  -m  test2.txt //把暂存区内容提交到本地仓库
    [ f8373c4]   test2.txt
       ,  deletions(-)
        test2.txt
    $ WebService % git  //查看git状态
     branch 
      , working tree clean
    $ WebService % ls
    testGit.txt
    $ WebService % git reflog //查看日志信息
    f8373c4 ( -> ) @{}: :   test2.txt ///文件被删除的历史记录
    a4e57d @{}: :   test2.txt
    d3c9608 @{}: : moving  d3c9608
    d3c9608 @{}: : moving  d3c9608
    f @{}: : moving  f
    dba7c5 @{}: : moving  ~
    d3c9608 @{}: : moving  d3c9608
    c970a17 @{}: : moving  ^^
    dba7c5 @{}: : moving  ^
    e @{}: : moving  e
    dba7c5 @{}: : moving  dba7c5
    d3c9608 @{}: : this  my fifth updata,updata testGit.txt
    e @{}: : this  my fourth ,updata testGit.txt
    dba7c5 @{}: : this  my third ,updata testGit.txt
    f @{}: : this  my  ,  testGit.txt
    c970a17 @{}:  ():  :  testGit.txt
    $ WebService % git  
        a4e57d   test2.txt
    $ WebService % ls -l//查看本地文件是否恢复?
    total 
    -rw-r
    -rw-r
    $ WebService %

恢复暂存区的文件

  • 说明
    使暂存区文件恢复

  • 命令
    git reset --hard HEAD
    通过上面刷新让三个区保持一致即可

  • 条件
    要恢复的文件提交到了本地库

版本比较

比较工作区和暂存区

  • 命令

    git diff 文件名

  • 使用例

    比较修改后的test2.txt的工作区和暂存区

    工作区,暂存区,本地仓库test2.txt内容一致

    bbbbbbbb
    cccccccc

    修改后的工作区test2.txt

    bbbbbbbb
    cccccccc@@@@@@

    比较后结果

    $ WebService % git diff test2.txt
    diff --git a/test2.txt b/test2.txt
    index 092b923..0c08686 100644
    
    
    
     aaaaaaaa
     bbbbbbbb
    
    
    $ WebService %

    提交到暂存区后再比较

    $ WebService % git  2.txt
    $ WebService % git diff test2.txt
    $ WebService %

工作区和本地仓库版本比较

  • 命令

    git diff 指定版本 [文件名]

  • 指定版本

    HEAD:当前本地仓库的版本

    HEAD^:本地仓库的上一个版本

    HEAD^^:本地仓库的上两个版本

    HEAD~n:本地仓库的上n个版本

    版本号的hash值

  • 注意

    文件名不写的话是所有当前文件夹的所有文件

  • 使用例

    工作区和本地仓库的当前版本比较

    $ WebService % git  
    //先把三个区同步一下,恢复到同一状态
        a4e57d   test2.txt
    $ WebService % git            
     branch 
      , working tree clean
    $ WebService % cat test2.txt
    aaaaaaaa
    bbbbbbbb
    cccccccc
    $ WebService % vim test2.txt //对工作区修改
    $ WebService % git diff  test2.txt//修改后进行比较
    diff 
     b923.c08686 
    
    +++ b/test2.txt
    @@ , +, @@
     aaaaaaaa
     bbbbbbbb
    -cccccccc
    +cccccccc@@@@@@
    $ WebService %

    和本地仓库的上一个版本进行比较

    $ WebService % git diff HEAD^ test2.txt
    diff --git a/test2.txt b/test2.txt
    new file mode 100644
    index 0000000..0c08686
    
    
    
    
    
    
    $ WebService %

分支

分支概述

在版本控制过程中,使用多条线同时推进多个任务。

分支好处

  • 同时推进多个功能开发,提高生产效率。

  • 各个分支在开发过程中,如有某个分支失败,不会对其他分支有影响。失败的分支可以重新获取mastaer分支,进行再次开发。

创建分支

  • 命令

  • 使用例

    git branch 分支名

    $ WebService % git branch hot_fix

查看分支

  • 命令

    git branch -v

  • 使用例

    $ WebService % git branch -v
    hot_fix a4e57d  new 2.txt
    * master  a4e57d  new 2.txt
    //*所在的位值就是我们现在所以在的分支
    $ WebService %

切换分支

  • 说明

    把分支从当前分支切换到其他分支

  • 命令

    git checkout 分支名

  • 使用例

    $ WebService % git checkout hot_fix
    Switched to branch 
    $ WebService % git branch -v
    * hot_fix a4e57d  new 2.txt //现在已经切换到hot_fix分支
    master  a4e57d  new 2.txt
    $ WebService %

合并分支

  • 说明

    把指定分支的内容合并到当前分支

  • 命令

    git merge 要合并内容的分支名

    $ WebService % git branch -v
    
    * hot_fix a360edf hox_fix one 
    master  a4e57d   test2.txt
    $ WebService % git checkout master
    
    Switched to branch 
    $ WebService % git branch -v
    
    hot_fix a360edf hox_fix one 
    * master  a4e57d   test2.txt
    $ WebService % git merge hot_fix
    
    Updating a4e57d..a360edf
    Fast-forward
    test2.txt |  +-
     file changed,  insertion(+),  deletion(-)
    $ WebService % cat test2.txt
    aaaaaaaa
    bbbbbbbb
    cccccccc edit  hox_fix
    $ WebService % git branch -v
    hot_fix a360edf hox_fix one    
    * master  a360edf hox_fix one  
    $ WebService %

解决合并冲突

  • 说明

    当要合并两个分支的时候,两个分支修改内容不一样,导致不能自动进行合并操作,所以需要手动进行合并操作。

  • 解决思路

    1.删除冲突文件内容的特殊符号

    2.修改冲突内容

    3.修改后的文件添加到暂存区。(git add 文件名)

    4.从暂存区提交到本地仓库。

    (命令:git commit -m "注释") 注:这里的commit命令不能带文件名称参数。

    $ WebService % vim test2.txt //修改mster分支的test2文件
    $ WebService % git add test2.txt
    //把修改后的文件添加到暂存区
    $ WebService % git  test2.txt
    //把暂存区文件提交到本地仓库
    [ a902f1] updata 
       ,  insertion(+)
    $ WebService % git branch -v
    //查看当前分支
      hot_fix a360edf hox_fix one 
    *   a902f1 updata 
    $ WebService %
    $ WebService % git checkout hot_fix
    //切换到hot_fix分支
    Switched  branch 
    $ WebService % git branch -v //查看分支
    * hot_fix a360edf hox_fix one 
      a902f1 updata 
    $ WebService % vim test2.txt //编辑test2.txt
    $ WebService % git  test2.txt //修改后的文件添加到暂存区
      $ WebService % git  -m  test2.txt //提交文件到本地仓库
    [hot_fix ee3ae4c] updata hox_fix
       ,  insertion(+)
    $ WebService %
    $ WebService % git            //合并分支
    -merging test2.txt
    CONFLICT ():  conflict  test2.txt
      ; fix conflicts and then  the result.
    //自动合并分支失败,接下来需要手动修改文件后在进行提交来解决冲突
    $ WebService % ls -l
    total 
    -rw-r
    -rw-r
    $ WebService % vim test2.txt                       //打开文件进行手动合并文件内容
    $ WebService % git 
    //查看git状态
     branch hot_fix //在hot_fix分支上
    You have unmerged paths.//没有合并的路径
      (fix conflicts  run ) //修理冲突并执行
      (    the ) //终止合并
    
    Unmerged paths:
      (   mark resolution) //使用git  <> 命令去标记为解决
    	 modified:   test2.txt
    
     changes added   (  / )
    $ WebService % git  test2.txt
    //解决冲突后的文件添加到暂存区
    $ WebService % git        
     branch hot_fix
    All conflicts  but you  still merging.//所有的冲突已经解决了,但是你仍然处于“合并中”状态。
      (   conclude )
    //使用git  命令去变换”合并中“的状态
    
    Changes  be committed:
    	modified:   test2.txt
    
    $ WebService % git  -m  test2.txt
    fatal: cannot  a   during a merge.
    //注意:在这中特殊场合下的不能再后面使用文件名
    $ WebService % git  -m 
    //去掉文件名再次执行提交。
    [hot_fix d62477] resolve conflict //冲突解决了。
    $ WebService % git 
    //查看git状态
     branch hot_fix
      , working tree clean
    $ WebService % vim test2.txt
    //确认合并后的内容
    $ WebService %

    上面执行完merge命令后,test2.txt文件的内容,

    aaaaaaaa
    bbbbbbbb
    cccccccc edit  hox_fix
    <<<<<<< HEAD  
    eeeeeeee   hox_fix
    =======
    dddddddd   master
    >>>>>>> master

    上面修改后的文件内容

    aaaaaaaa
    bbbbbbbb
    cccccccc edit by hox_fix
    dddddddd  by master
    eeeeeeee  by hox_fix

远程库操作

注册账户

  • 注册github账户

  • 注册码云账户

  • 两个账户任意一个即可

创建项目仓库

  • 创建新仓库

    (在这里不做叙述)

远程仓库别名设定

  • 说明

    起别名的目的为了在推送到远程仓库的时候比较简单,不至于敲很长的地址。

  • 命令

    git remote add 别名 远程仓库地址.git

  • 使用例

    $ WebService % git remote add origin /2019/WebService.git
    $ WebService %

查看远程仓库别名信息

  • 说明

    在设定远程仓库别名后,查看设定是否成功。

  • 命令

    git remote -v

  • 使用例

    $ WebService % git remote -v
        origin	/2019/WebService.git (fetch)
        origin	/2019/WebService.git (push)
    $ WebService %

推送到远程仓库

  • 说明

    把本地仓库的内容上传到远程仓库

  • 命令

    git push 远程库别名 分支名

  • 使用例

    $ WebService % git push origin hot_fix
    Enumerating  , done.
    Counting  % (/), done.
    Delta compression using up to  threads
    Compressing  % (/), done.
    Writing  % (s, done.
    Total  (delta ), reused  (delta )
     Resolving  % (/), done.
     
     Create a pull request   on GitHub by 
          
     
    To 
     * [ branch]      hot_fix -> hot_fix
    $ WebService %

推送发生失败

  • 当推送到远程仓库时莫名发生失败,此时可以进行强行推送

  • 命令

    git push 远程仓库名称.git 分支名 -f

  • 使用例

    $ WebService % git push origin master //首次推送
    To http//github./jack2019/WebService.git
     ! [rejected]        master -> master (fetch )
    error: failed  push some refs   //推送失败
    hin Updates were rejected because the remote contains work that you 
    hin not have locally. This  usually caused by another repository pushing
    hin  the same ref. You may want   integrate the remote 
    hin (.g., ) before pushing again.
    hin See the  in   details.
    $ WebService % git push origin master - //进行强行推送
    Total  (delta ), reused  (delta )
    To http//github./jack2019/WebService.git
     + cfbddc...a902f1 master -> master (forced ) //强行推送成功,可以去github仓库查看是否已经有上传的内容。
    $ WebService %

远程仓库克隆

  • 说明

    当我们想从github或者码云下载他人的项目进行学习阅读的时候,我们可以使用克隆命令。

  • 命令

    git clone 远程仓库地址

  • 注意

    克隆操作在完成下载文件到本地的同时还完成了两件事情。分别是初始化远程仓库别名,初始话本地仓库(也就是git init命令执行过程。)

  • 使用例

    ➜  IdeaProjects git clone 
    Cloning into ...
     Enumerating  , done.
     Counting  % (/), done.
     Compressing  % (/), done.
     Total  (delta ), reused  (delta ), pack-reused 
    Unpacking  % (/克隆成功
    ➜  IdeaProjects cd gitLearnning 
    ➜  gitLearnning (master) ls -al 
    total 
    drwxr-xr-x    jack  staff   Oct   : .
    drwxr-xr-x@  jack  staff   Oct   : ..
    drwxr-xr-x   jack  staff   Oct   : .git
    -rw-r--r--    jack  staff   Oct   : test2.txt
    -rw-r--r--    jack  staff   Oct   : testGit.txt
    ➜  gitLearnning (master) git status 
    On branch master
    Your branch is up to date with . 
    
    nothing to commit, working tree clean
    ➜  gitLearnning (master)

从远程库拉取最新的内容

fetch操作

  • 说明

    远程库的内容被更新后,我们想取得最新的到本地,这时候就用到了fetch命令。如果我们还想和本地的版本进行合并。我们还需要使用merge命令进行合并。

  • 命令

    git fetch 远程仓库别名 分支名

  • 使用例

    $ git fetch origin master 
     Enumerating  , done.
     Counting  % (/), done.
     Compressing  % (/), done.
     Total  (delta ), reused  (delta ), pack-reused 
      Unpacking  % (/), done.
      From 
       * branch            master     -> FETCH_HEAD
         f1a3142..da6a4ee  master     -> origin/master
      
      Window-PC MINGW64 workspacewebservice (master)
      $ ls -l
      total 
      -rw-r--r--  laofan   九月  : test2.txt
      -rw-r--r--  laofan   九月  : testGit.txt
      
      Window-PC MINGW64 workspacewebservice (master)
      $ cat test2.txt
      aaaaaaaa
      bbbbbbbb
      cccccccc edit by hox_fix
      dddddddd add by master
      
      mmmmmmm push after updata!
      
      Window-PC MINGW64 workspacewebservice (master)
      $ git checkout originmaster下,切换分支到origin、master下。
     checking out .
      
      You are   state. You can look around, make experimental
      changes and commit them, and you can discard any commits you make  
      state without impacting any branches by performing another checkout.
      
      If you want to create a  branch to retain commits you create, you may
      do so (now or later) by using -b with the checkout command again. 
      
        git checkout -b <-branch-name>
      
      HEAD is now at da6a4ee... mac commit ,window fetch
      
      Window-PC MINGW64 workspacewebservice ((da6a4ee...))
      $ git branch -v 
      * (HEAD detached at origin/master) da6a4ee mac commit ,window fetch
        master f1a3142 [behind ] push after updata test2.txt 
      
      Window-PC MINGW64 workspacewebservice ((da6a4ee...))
      $ cat test2.txt 
      aaaaaaaa
      bbbbbbbb
      cccccccc edit by hox_fix
      dddddddd add by master
      
      mmmmmmm push after updata
      nnnnnnn macbook add! 
      
      Window-PC MINGW64 workspacewebservice ((da6a4ee...))
      $ git checkout master  
      Previous HEAD position was da6a4ee... mac commit ,window fetch
      Switched to branch 
      Your branch is behind  by  commit, and can be fast-forwarded.
        (use  to update your local branch)
      
      Window-PC MINGW64 workspacewebservice (master)
      $ git branch -v 
      * master f1a3142 [behind ] push after updata test2.txt
      
      Window-PC MINGW64 workspacewebservice (master)
      $ cat test2.txt 
      aaaaaaaa
      bbbbbbbb
      cccccccc edit by hox_fix
      dddddddd add by master
      
      mmmmmmm push after updata!
      
      Window-PC MINGW64 workspacewebservice (master)
      $ git merge originmaster分支内容合并到master
      Updating f1a3142..da6a4ee
      Fast-forward
       test2.txt |  ++-
        file changed,  insertions(+),  deletion(-)
      
      Window-PC MINGW64 workspacewebservice (master)
      $ cat test2.txt 
      aaaaaaaa
      bbbbbbbb
      cccccccc edit by hox_fix
      dddddddd add by master
      
      mmmmmmm push after updata
      nnnnnnn macbook add! 
      
      Window-PC MINGW64 workspacewebservice (master)

pull操作

  • 说明

    pull操作等价于fetch操作 + merge操作

  • 命令

    git pull 远程仓库别名 分支名

  • 使用例

    $ git pull origin master
     Enumerating  , done.
     Counting  % (/), done.
     Compressing  % (/), done.
     Total  (delta ), reused  (delta ), pack-reused 
    Unpacking  % (/), done.
    From 
     * branch            master     -> FETCH_HEAD
       da6a4ee.ed55  master     -> origin/master
    Updating da6a4ee.ed55
    Fast-forward
     test2.txt |  +
      file changed,  insertion(+)
    
    Window-PC MINGW64 workspacewebservice (master)
    $ cat test2.txt
    aaaaaaaa
    bbbbbbbb
    cccccccc edit by hox_fix
    dddddddd add by master
    
    mmmmmmm push after updata
    nnnnnnn macbook add!
    jjjjjjj macbook add2!
    
    Window-PC MINGW64 workspacewebservice (master)
    $

协同开发冲突的解决

  • 说明
    如果不基于远程库最新版本进行修改的话则不能推送,必须先拉取最新的远程库。拉取后发生冲突,则按照“分支冲突解决”的操作即可。

  • 例子
    mac端更新后提交github
    window端不拉取最新的github直接更新进行提交github。此时需要先拉取最新的远程库进行,并进行远程库和本地库的合并。

跨团队协作

  • 说明
    当需要团队外部的人员进行代码的变更时,由于团队外的人没有push的权限,所以需要团队外人员对项目进行fork操作。

  • 正常流程
    1.github用户1fork用户2的项目
    (fork之后,在远程库自动创建一个同样的项目)
    2.clone到本地
    3.本地修改内容
    4.提交到暂存区,本地库
    5.push到github用户1
    6.向用户2申请提交请求
    pull requests
    new pull request
    7.用户2进行pull requestes的处理,并进行merge操作。

git知识补充

  • Q:当不小心对整个系统的文件夹进行git inint操作后该如何取消?
    A:通过命令rm -rf .git对git文件进行删除操作就即可。

  • Q:如何查看帮助文档?
    A:通过命令git help 要查看的命令 进行查看即可。