Andy Niu �����ĵ�

Andy Niu

Andy Niu Help  1.0.0.0

变量

 git远程分支的删除和同步
 
 git忽略文件权限的检查
 
 本地创建分支,并和远程关联
 
 svn与git对照
 
 log命令
 
 冲突的情况只有两种
 
 我们的开发分支管理
 
 push可能会有冲突
 
 看看fetch做了什么事情
 
 是不是只能merge提交的分支
 
 本地的修改状态
 
 push命令
 
 git命令status
 
 git撤销修改
 
 撤销本地修改
 

详细描述

变量说明

git命令status
1、执行的结果如下:
    niuzibin@ubuntu:~/work/gittest$ git status    
    On branch master
    Changes to be committed:
    (use "git reset HEAD <file>..." to unstage)
    
            modified:   readme.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:   readme.txt
2、解释如下:
    On branch master                工作的分支
    Changes to be committed:        暂存区没有提交的信息
    Changes not staged for commit:  工作区没有添加到暂存区的信息
git忽略文件权限的检查
1、git config core.fileMode false
2、可以查看配置文件
    niuzibin@ubuntu:~/work/FLKDBEnc/FLKCDP$ cat .git/config 
    [core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
参见
git撤销修改
1、分为两种情况:
    第一种情况,工作区做了修改,还没有添加到暂存区。
    第二种情况,工作区做了修改,并且已经添加到暂存区,然后工作区又做了修改。
2、对于第一种情况,直接使用checkout即可。如下:
    niuzibin@ubuntu:~/work/gittest$ 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")
    niuzibin@ubuntu:~/work/gittest$ git diff  
    diff --git a/readme.txt b/readme.txt
    index 4dd9cb5..56b6b46 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -1 +1 @@
    -I am Andy
    +I am Bill
    niuzibin@ubuntu:~/work/gittest$ git checkout readme.txt
    niuzibin@ubuntu:~/work/gittest$ git status
    On branch master
    nothing to commit, working directory clean
    niuzibin@ubuntu:~/work/gittest$ git diff
3、对于第二种情况,版本库是 I am Andy,暂存区是 I am Bill, 工作区是 I am Caroline,如下:
    // 比较暂存区和版本库的差别
    niuzibin@ubuntu:~/work/gittest$ git diff --cached
    diff --git a/readme.txt b/readme.txt
    index 4dd9cb5..56b6b46 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -1 +1 @@
    -I am Andy
    +I am Bill
    
    // 比较工作区和暂存区的差别
    niuzibin@ubuntu:~/work/gittest$ git diff
    diff --git a/readme.txt b/readme.txt
    index 56b6b46..b688830 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -1 +1 @@
    -I am Bill
    +I am Caroline
    
    // 比较工作区和版本库的差别
    niuzibin@ubuntu:~/work/gittest$ git diff HEAD
    diff --git a/readme.txt b/readme.txt
    index 4dd9cb5..b688830 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -1 +1 @@
    -I am Andy
    +I am Caroline
4、撤销工作区的修改,如下:
    niuzibin@ubuntu:~/work/gittest$ git checkout readme.txt
    niuzibin@ubuntu:~/work/gittest$ git diff --cached
    diff --git a/readme.txt b/readme.txt
    index 4dd9cb5..56b6b46 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -1 +1 @@
    -I am Andy
    +I am Bill
    niuzibin@ubuntu:~/work/gittest$ git diff
    niuzibin@ubuntu:~/work/gittest$ git diff HEAD          
    diff --git a/readme.txt b/readme.txt
    index 4dd9cb5..56b6b46 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -1 +1 @@
    -I am Andy
    +I am Bill
5、撤销暂存区的修改,如下:
    niuzibin@ubuntu:~/work/gittest$ git reset HEAD readme.txt
    Unstaged changes after reset:
    M       readme.txt
    niuzibin@ubuntu:~/work/gittest$ git diff --cached
    niuzibin@ubuntu:~/work/gittest$ git diff
    diff --git a/readme.txt b/readme.txt
    index 4dd9cb5..56b6b46 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -1 +1 @@
    -I am Andy
    +I am Bill
6、还需要再次checkout,如下:
    niuzibin@ubuntu:~/work/gittest$ git checkout readme.txt
    niuzibin@ubuntu:~/work/gittest$ git diff --cached
    niuzibin@ubuntu:~/work/gittest$ git diff
7、也就是说,checkout相当于拿暂存区的数据,覆盖工作区。reset HEAD相当于拿版本库的数据,覆盖暂存区。
8、考虑工作区、暂存区、版本库的内容分别为C、B、A
    现在要全部撤销,有两个办法:
    第一个办法:checkout结果为【B、B、A】-->reset HEAD结果为【B、A、A】-->checkout结果为【A、A、A】
    第二个办法:reset HEAD结果为【C、A、A】-->checkout结果为【A、A、A】
git远程分支的删除和同步
1、其他人已经删除某个分支,但是本地还能看到,这个时候fetch报错,如下:
    niuzibin@ubuntu:~/work/FLKDBEnc/FLKCDP$ git branch -a                   
    develop
    * niuzibin
    remotes/origin/HEAD -> origin/develop
    remotes/origin/chenming
    remotes/origin/develop
    remotes/origin/heming
    remotes/origin/master
    remotes/origin/niuzibin
    remotes/origin/patch-1
    remotes/origin/patch-2
    remotes/origin/patch-3
    remotes/origin/patch-4
    remotes/origin/revert-77cdc6fe
    remotes/origin/revert-fa155359
    remotes/origin/tuchengyu
    remotes/origin/tuchengyu2
    remotes/origin/yangyang
    remotes/origin/yogurt
    remotes/origin/ywn
    
    niuzibin@ubuntu:~/work/FLKDBEnc/FLKCDP$ sudo git fetch origin tuchengyu
    fatal: Couldn't find remote ref tuchengyu
    Unexpected end of command stream
2、这个错误的原因是,fetch已经删除的分支
3、如何同步呢?
    执行 git fetch -p,如下:
    From https://192.168.10.99/FLKDBEnc/FLKCDP
    x [deleted]         (none)     -> origin/patch-2
    x [deleted]         (none)     -> origin/patch-3
    x [deleted]         (none)     -> origin/patch-4
    x [deleted]         (none)     -> origin/tuchengyu
    这个时候 git branch -a,就会少一些分支
4、查看分支的关联,如下:
    niuzibin@ubuntu:~/work/FLKDBEnc/FLKCDP$ git branch -vv
    develop  0c0c9a2 [origin/develop: behind 119] Merge branch 'chenming' into 'develop'
    * niuzibin 4463d6a [origin/niuzibin] 删除无用的方法
参见
log命令
1、显示log
    git log
2、显示最近的两次提交log
    git log -2
3、显示每次提交的内容差异
    git log -2 -p
4、显示某个文件的内容差异
    git log -2 -p .gitignore
5、也可以通过show,显示某一个提交的内容差异,如下:
    root@ubuntu:/home/disk1/GitTest# git log -1 -p .gitignore                         
    commit 12fdff5b6cd259cff2952fce978f1555635d4bfc
    Author: nzbbody <[email protected]>
    Date:   Wed Jun 6 10:27:04 2018 +0800
    
        增加忽略的文件
    
    diff --git a/.gitignore b/.gitignore
    index 259148f..645ee14 100644
    --- a/.gitignore
    +++ b/.gitignore
    @@ -30,3 +30,6 @@
    *.exe
    *.out
    *.app
    +
    +# Other temp file
    +*.bak
    root@ubuntu:/home/disk1/GitTest# git show 12fdff5b6cd259cff2952fce978f1555635d4bfc .gitignore
    commit 12fdff5b6cd259cff2952fce978f1555635d4bfc
    Author: nzbbody <[email protected]>
    Date:   Wed Jun 6 10:27:04 2018 +0800
    
        增加忽略的文件
    
    diff --git a/.gitignore b/.gitignore
    index 259148f..645ee14 100644
    --- a/.gitignore
    +++ b/.gitignore
    @@ -30,3 +30,6 @@
    *.exe
    *.out
    *.app
    +
    +# Other temp file
    +*.bak
push可能会有冲突
1、甲修改,commit push
2、乙修改,commit push,报错
    To https://github.com/nzbbody/GitTest
    ! [rejected]        master -> master (fetch first)
    error: failed to push some refs to 'https://github.com/nzbbody/GitTest'
    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.
3、提示,在push之前,先pull一下。我们使用fetch + merge
4、执行
    git fetch origin master
    git merge origin/master
5、merge有冲突,需要解决冲突,add commit push
    root@ubuntu:/home/disk1/GitTest# git add readme.txt 
    root@ubuntu:/home/disk1/GitTest# git commit -m"333444"
    root@ubuntu:/home/disk1/GitTest# git push origin master:master
push命令
1、git push命令用于将本地分支的更新,推送到远程主机。它的格式与git pull命令相似。
    git push <远程主机名> <本地分支名>:<远程分支名>
2、git push origin master
    上面命令表示,将本地的master分支推送到origin主机的master分支。如果master不存在,则会被新建。
svn与git对照
1、svn是集中式,git是分布式
2、svn保存文件的差异,git保存文件的快照,如果没有变化,从上一次引用下来,对于修改的文件,做一个副本,也就是写时拷贝(COW)
3、svn创建分支,相当于整体拷贝,git创建分支,在任意一个提交点(commit point)开启分支
冲突的情况只有两种
1、两个分支都提交了,然后merge时
    办法是:解决冲突+add+commit
2、push的时候,其实也是两个分支都提交了,需要merge
    办法是:解决冲突+add+commit+push
3、你可能会想,master改了,提交。test1改了,先把master分支merge到test1,然后add commit,存在冲突。
    这种情况是不存在的。因为:test1修改了,没有提交的情况下,是不能merge的。
我们的开发分支管理
1、远程有develop和niuzibin,在gitlab执行 merge niuzibin-->develop
2、我的本地有 niuzibin,注意:本地的develop没有意义
    本地的niuzibin分支,push到 origin/niuzibin
3、我在本地修改 niuzibin分支,
4、然后add commit,
5、从origin/develop进行fetch,然后merge,有冲突,解决冲突,再次add commit push
6、注意:4和5是不能颠倒顺序的,本地有修改,必须先提交,再merge
撤销本地修改
1、准备工作如下:
    niuzibin@ubuntu:~/work/gittest$ git init
    Initialized empty Git repository in /home/niuzibin/work/gittest/.git/
    niuzibin@ubuntu:~/work/gittest$ git config --global user.name "niuzibin"        
    niuzibin@ubuntu:~/work/gittest$ git config --global user.email "[email protected]"
    niuzibin@ubuntu:~/work/gittest$ git config user.name
    niuzibin
    niuzibin@ubuntu:~/work/gittest$ git config user.email
    [email protected]
    niuzibin@ubuntu:~/work/gittest$ vi readme.txt
    niuzibin@ubuntu:~/work/gittest$ git add readme.txt 
    niuzibin@ubuntu:~/work/gittest$ git commit -m"add file readme.txt"
    [master (root-commit) 7846ec0] add file readme.txt
    1 file changed, 1 insertion(+)
    create mode 100644 readme.txt
    niuzibin@ubuntu:~/work/gittest$ git log
    commit 7846ec02be0f61138d84b75aa5213c9f4421d4b4
    Author: niuzibin <[email protected]>
    Date:   Mon Apr 9 18:29:47 2018 -0700
    
        add file readme.txt
2、注意:提交的时候,如果没有-m写日志,会提示输入日志。输入之后,Ctrl+o,然后Enter,然后Ctrl+x
3、修改,查看变化
    niuzibin@ubuntu:~/work/gittest$ 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
    
    Untracked files:
    (use "git add <file>..." to include in what will be committed)
    
            1
    
    no changes added to commit (use "git add" and/or "git commit -a")
    niuzibin@ubuntu:~/work/gittest$ git diff
    diff --git a/readme.txt b/readme.txt
    index 4dd9cb5..3f54eb5 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -1 +1,2 @@
    I am Andy
    +hehe
4、撤销本地修改
    niuzibin@ubuntu:~/work/gittest$ git checkout -- readme.txt
5、上面的方法撤销一个文件的修改,如果撤销当前目录下的多个文件修改,使用
    niuzibin@ubuntu:~/work/gittest$ git checkout ./
是不是只能merge提交的分支
1、创建test1分支,修改文件,add、commit
2、转到master分支,也修改文件,这个是时候,diff test1 master,看到的是提交的差异
    注意:diff比较分支,是比较已经提交的分支。
3、先不提交,merge试一下,报错
    error: Your local changes to the following files would be overwritten by merge:
            readme.txt
    Please, commit your changes or stash them before you can merge.
    Aborting
4、提示:要求提交之后,再merge,如下:
    root@ubuntu:/home/disk1/GitTest# git add readme.txt 
    root@ubuntu:/home/disk1/GitTest# git commit -m"master"
    [master 217992a] master
    1 file changed, 1 insertion(+)
    root@ubuntu:/home/disk1/GitTest# git merge test1      
    Auto-merging readme.txt
    CONFLICT (content): Merge conflict in readme.txt
    Automatic merge failed; fix conflicts and then commit the result.
5、这个时候,merge有冲突,需要解决冲突
    解决冲突,然后再进行 add、commit
6、结论:
    当前分支修改后不提交,merge报错,要求先提交,再merge
    提交后再merge,可能会冲突,需要解决冲突,然后add commit
本地创建分支,并和远程关联
1、示例如下:
niuzibin@ubuntu:~/work/FLKDBEnc/FLKCDP$ git branch -a
* develop
  niuzibin
  remotes/origin/HEAD -> origin/develop
  remotes/origin/chenming
  remotes/origin/develop
  remotes/origin/heming
  remotes/origin/master
  remotes/origin/niuzibin
  remotes/origin/patch-1
  remotes/origin/revert-77cdc6fe
  remotes/origin/revert-fa155359
  remotes/origin/tuchengyu2
  remotes/origin/yangyang
  remotes/origin/yogurt
  remotes/origin/ywn
  remotes/origin/ywn1
    
niuzibin@ubuntu:~/work/FLKDBEnc/FLKCDP$ git branch tuchengyu2
niuzibin@ubuntu:~/work/FLKDBEnc/FLKCDP$ git branch -vv
* develop    dc5f079 [origin/develop] Merge branch 'yangyang' into 'develop'
  niuzibin   4463d6a [origin/niuzibin] 删除无用的方法
  tuchengyu2 dc5f079 Merge branch 'yangyang' into 'develop'
  
niuzibin@ubuntu:~/work/FLKDBEnc/FLKCDP$ git branch --set-upstream-to origin/tuchengyu2 tuchengyu2
Branch tuchengyu2 set up to track remote branch tuchengyu2 from origin.
niuzibin@ubuntu:~/work/FLKDBEnc/FLKCDP$ git branch -vv
* develop    dc5f079 [origin/develop] Merge branch 'yangyang' into 'develop'
  niuzibin   4463d6a [origin/niuzibin] 删除无用的方法
  tuchengyu2 dc5f079 [origin/tuchengyu2: behind 14] Merge branch 'yangyang' into 'develop'
参见
本地的修改状态
   工作区----【add】------->暂存区----【commit】-------->版本库
   工作区<---【checkout】---暂存区<---【reset HEAD】-----版本库
看看fetch做了什么事情
1、git fetch origin master,通过fetch前后的文本对照和抓包,发现
    fetch会获取远程分支的commit id,也就是本地的文件FETCH_HEAD,以及远程 origin/master的最新文件内容
2、然后merge origin/master,会合并 origin/master 到 merge
    也就是说,merge origin/master 并没有网络传输
3、git pull origin master = git fetch origin master + merge origin/master
    但是,一般不要使用pull,在背后进行merge总是不好的。
Copyright (c) 2015~2016, Andy Niu @All rights reserved. By Andy Niu Edit.