美文网首页
git 使用

git 使用

作者: drfung | 来源:发表于2017-10-30 17:51 被阅读15次

    Git简介

    Git是目前世界上最先进的分布式版本控制系统(没有之一)。

    很多人都知道,Linus在1991年创建了开源的Linux,从此,Linux系统不断发展,已经成为最大的服务器系统软件了。

    Linus虽然创建了Linux,但Linux的壮大是靠全世界热心的志愿者参与的,这么多人在世界各地为Linux编写代码,那Linux的代码是如何管理的呢?

    事实是,在2002年以前,世界各地的志愿者把源代码文件通过diff的方式发给Linus,然后由Linus本人通过手工方式合并代码!

    你也许会想,为什么Linus不把Linux代码放到版本控制系统里呢?不是有CVS、SVN这些免费的版本控制系统吗?因为Linus坚定地反对CVS和SVN,这些集中式的版本控制系统不但速度慢,而且必须联网才能使用。有一些商用的版本控制系统,虽然比CVS、SVN好用,但那是付费的,和Linux的开源精神不符。

    不过,到了2002年,Linux系统已经发展了十年了,代码库之大让Linus很难继续通过手工方式管理了,社区的弟兄们也对这种方式表达了强烈不满,于是Linus选择了一个商业的版本控制系统BitKeeper,BitKeeper的东家BitMover公司出于人道主义精神,授权Linux社区免费使用这个版本控制系统。

    安定团结的大好局面在2005年就被打破了,原因是Linux社区牛人聚集,不免沾染了一些梁山好汉的江湖习气。开发Samba的Andrew试图破解BitKeeper的协议(这么干的其实也不只他一个),被BitMover公司发现了(监控工作做得不错!),于是BitMover公司怒了,要收回Linux社区的免费使用权。

    Linus可以向BitMover公司道个歉,保证以后严格管教弟兄们,嗯,这是不可能的。实际情况是这样的:

    Linus花了两周时间自己用C写了一个分布式版本控制系统,这就是Git!一个月之内,Linux系统的源码已经由Git管理了!牛是怎么定义的呢?大家可以体会一下。

    Git迅速成为最流行的分布式版本控制系统,尤其是2008年,GitHub网站上线了,它为开源项目免费提供Git存储,无数开源项目开始迁移至GitHub,包括jQuery,PHP,Ruby等等。

    历史就是这么偶然,如果不是当年BitMover公司威胁Linux社区,可能现在我们就没有免费而超级好用的Git了。

    Git使用

    可以使用apt或者yum安装git,安装完成后配置用户名及邮件

    $ git config --global user.name "Your Name"
    $ git config --global user.email "email@example.com"
    

    创建版本库

    fbo@fbo-virtual-machine:~/Documents$ mkdir learngit
    fbo@fbo-virtual-machine:~/Documents$ cd learngit/
    fbo@fbo-virtual-machine:~/Documents/learngit$ ls
    fbo@fbo-virtual-machine:~/Documents/learngit$ pwd
    /home/fbo/Documents/learngit
    fbo@fbo-virtual-machine:~/Documents/learngit$ git init
    Initialized empty Git repository in /home/fbo/Documents/learngit/.git/
    fbo@fbo-virtual-machine:~/Documents/learngit$ ls -al
    total 12
    drwxrwxr-x 3 fbo fbo 4096 9月   5 16:49 .
    drwxr-xr-x 3 fbo fbo 4096 9月   5 16:49 ..
    drwxrwxr-x 7 fbo fbo 4096 9月   5 16:49 .git
    

    版本提交

    fbo@fbo-virtual-machine:~/Documents/learngit$ cat readme.txt 
    Git is a distributed version control system.
    Git is free software.
    fbo@fbo-virtual-machine:~/Documents/learngit$ git status
    On branch master
    
    Initial commit
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        readme.txt
    
    nothing added to commit but untracked files present (use "git add" to track)
    fbo@fbo-virtual-machine:~/Documents/learngit$ git diff readme.txt
    fbo@fbo-virtual-machine:~/Documents/learngit$ git add readme.txt
    fbo@fbo-virtual-machine:~/Documents/learngit$ git status 
    On branch master
    
    Initial commit
    
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
    
        new file:   readme.txt
    
    fbo@fbo-virtual-machine:~/Documents/learngit$ git commit -m "add distributed"
    [master (root-commit) b20d129] add distributed
     1 file changed, 2 insertions(+)
     create mode 100644 readme.txt
    fbo@fbo-virtual-machine:~/Documents/learngit$ git status
    On branch master
    nothing to commit, working directory clean
    
    

    版本变更

    # 修改文件
    fbo@fbo-virtual-machine:~/Documents/learngit$ cat readme.txt 
    Git is a distributed version control system.
    Git is free software distributed under the GPL.
    fbo@fbo-virtual-machine:~/Documents/learngit$ git add readme.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ git commit -m "append GPL"
    [master c4212dc] append GPL
     1 file changed, 1 insertion(+), 1 deletion(-)
    fbo@fbo-virtual-machine:~/Documents/learngit$ git log
    commit c4212dc080da687c79f2b2209c55ba45eaeb9c0a
    Author: drfung <recoba01@163.com>
    Date:   Tue Sep 5 17:00:51 2017 +0800
    
        append GPL
    
    commit b20d1295ce75f787dff7d9f631f4063ca5608bff
    Author: drfung <recoba01@163.com>
    Date:   Tue Sep 5 16:56:38 2017 +0800
    
        add distributed
    
    fbo@fbo-virtual-machine:~/Documents/learngit$ git log --pretty=oneline
    c4212dc080da687c79f2b2209c55ba45eaeb9c0a append GPL
    b20d1295ce75f787dff7d9f631f4063ca5608bff add distributed
    
    # 在Git中,用HEAD表示当前版本,也就是最新的提交3628164...882e1e0(注意我的提交ID和你的肯定不一样),
    # 上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写
    # 成HEAD~100。
    fbo@fbo-virtual-machine:~/Documents/learngit$ git reset --hard HEAD^
    HEAD is now at b20d129 add distributed
    fbo@fbo-virtual-machine:~/Documents/learngit$ cat readme.txt 
    Git is a distributed version control system.
    Git is free software.
    fbo@fbo-virtual-machine:~/Documents/learngit$ git log
    commit b20d1295ce75f787dff7d9f631f4063ca5608bff
    Author: drfung <recoba01@163.com>
    Date:   Tue Sep 5 16:56:38 2017 +0800
    
        add distributed
    fbo@fbo-virtual-machine:~/Documents/learngit$ git reset --hard c4212dc080da687c79f2b2209c55ba45eaeb9c0a
    HEAD is now at c4212dc append GPL
    fbo@fbo-virtual-machine:~/Documents/learngit$ cat readme.txt 
    Git is a distributed version control system.
    Git is free software distributed under the GPL.
    # 显示每次命令的id
    fbo@fbo-virtual-machine:~/Documents/learngit$ git reflog
    c4212dc HEAD@{0}: reset: moving to c4212dc080da687c79f2b2209c55ba45eaeb9c0a
    b20d129 HEAD@{1}: reset: moving to HEAD^
    c4212dc HEAD@{2}: commit: append GPL
    b20d129 HEAD@{3}: commit (initial): add distributed
    
    

    工作区与暂存区

    • 工作区
      你在电脑里能看到的目录
    • 版本库
      工作区里有一个隐藏的目录.git,这个不是工作区,而是Git的版本库。Git版本库里最重要的就是stage(或者叫做index)的暂存区,还有Git为我们自动创建的一个分支master,以及指向master的一个指针叫做HEAD。
    fbo@fbo-virtual-machine:~/Documents/learngit$ cat readme.txt 
    Git is a distributed version control system.
    Git is free software distributed under the GPL.
    Git has a mutable index called stage.
    fbo@fbo-virtual-machine:~/Documents/learngit$ 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")
    # git add 将要提交的所有修改放到暂存去(Stage),然后执行git commit就可以一次性把暂存区的所有修改提交到分支
    fbo@fbo-virtual-machine:~/Documents/learngit$ git add .
    fbo@fbo-virtual-machine:~/Documents/learngit$ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        modified:   readme.txt
    fbo@fbo-virtual-machine:~/Documents/learngit$ git commit -m "understand how srtage works"
    [master 6a371c1] understand how srtage works
     1 file changed, 1 insertion(+)
    # 提交后暂存区就没有任何内容了
    fbo@fbo-virtual-machine:~/Documents/learngit$ git status
    On branch master
    nothing to commit, working directory clean
    
    

    管理修改

    fbo@fbo-virtual-machine:~/Documents/learngit$ vi readme.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ git add readme.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        modified:   readme.txt
    
    fbo@fbo-virtual-machine:~/Documents/learngit$ vi readme.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ cat readme.txt 
    Git is a distributed version control system.
    Git is free software distributed under the GPL.
    Git has a mutable index called stage.
    Git tracks changes of files.
    
    # 这次commit只提交了add的修改,第二次修改并没有被提交
    fbo@fbo-virtual-machine:~/Documents/learngit$ git commit -m "git tracks changes"
    [master 2817224] git tracks changes
     1 file changed, 1 insertion(+)
    fbo@fbo-virtual-machine:~/Documents/learngit$ 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")
    # 查看工作区和版本库里最新版本的区别
    fbo@fbo-virtual-machine:~/Documents/learngit$ git diff HEAD -- readme.txt
    diff --git a/readme.txt b/readme.txt
    index 76d770f..d021562 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -1,4 +1,5 @@
     Git is a distributed version control system.
     Git is free software distributed under the GPL.
     Git has a mutable index called stage.
    -Git tracks changes.
    +Git tracks changes of files.
    +
    # 先add再commit提交第二次修改
    fbo@fbo-virtual-machine:~/Documents/learngit$ git add readme.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ git commit -m "second modified"
    [master d6230e9] second modified
     1 file changed, 2 insertions(+), 1 deletion(-)
    fbo@fbo-virtual-machine:~/Documents/learngit$ git status
    On branch master
    nothing to commit, working directory clean
    

    撤销修改

    fbo@fbo-virtual-machine:~/Documents/learngit$ vi readme.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ 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")
    # git checkout -- <filename> 将文件在工作区的修改全部撤销,回退到git add或者git commit
    fbo@fbo-virtual-machine:~/Documents/learngit$ git checkout -- readme.txt
    fbo@fbo-virtual-machine:~/Documents/learngit$ cat readme.txt 
    Git is a distributed version control system.
    Git is free software distributed under the GPL.
    Git has a mutable index called stage.
    Git tracks changes of files.
    fbo@fbo-virtual-machine:~/Documents/learngit$ vi readme.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ git add readme.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        modified:   readme.txt
    
    ## 将暂存区的修改回退到工作区
    fbo@fbo-virtual-machine:~/Documents/learngit$ git reset HEAD readme.txt
    Unstaged changes after reset:
    M   readme.txt
    fbo@fbo-virtual-machine:~/Documents/learngit$ 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")
    fbo@fbo-virtual-machine:~/Documents/learngit$ git checkout -- readme.txt
    fbo@fbo-virtual-machine:~/Documents/learngit$ cat readme.txt 
    Git is a distributed version control system.
    Git is free software distributed under the GPL.
    Git has a mutable index called stage.
    Git tracks changes of files.
    

    删除文件

    fbo@fbo-virtual-machine:~/Documents/learngit$ git add test.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ git commit -m "add test.txt"
    [master 212bc97] add test.txt
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 test.txt
    fbo@fbo-virtual-machine:~/Documents/learngit$ rm test.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ git status
    On branch master
    Changes not staged for commit:
      (use "git add/rm <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
        deleted:    test.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")
    fbo@fbo-virtual-machine:~/Documents/learngit$ git rm test.txt
    rm 'test.txt'
    fbo@fbo-virtual-machine:~/Documents/learngit$ git commit -m "remove test.txt"
    [master 64bfb31] remove test.txt
     1 file changed, 0 insertions(+), 0 deletions(-)
     delete mode 100644 test.txt
    

    远程仓库

    添加远程仓库

    1. 注册GitHub账号
    2. 创建SSH Key ssh-keygen -t rsa -C "recoba01@163.com"
    3. 登录GitHub,打开"Accout settings", "SSH keys"界面,”Add SSH key“,粘贴id_rsa.pub内容。
    4. 在GitHub上创建learngit的repo
    5. 添加远程仓库git remote add origin git@github.com:drfung/learngit.git
    6. 推送仓库git push -u origin master,第一次推送-u参数推送master分支的所有内容
    7. git push origin master推送最新修改。

    从远程仓库克隆

    1. 登录GitHub,创建一个gitskills的仓库
    2. 勾选Initialize this repository with a README
    3. 本地git clone git@hithub.com:drfung/gitskills.git克隆一个本地仓库

    分支管理

    创建分支

    # -b创建并切换到新的分支,也可git branch dev创建分支,git checkout dev两步完成
    fbo@fbo-virtual-machine:~/Documents/learngit$ git checkout -b dev
    Switched to a new branch 'dev'
    # 查看分支
    fbo@fbo-virtual-machine:~/Documents/learngit$ git branch
    * dev
      master
    fbo@fbo-virtual-machine:~/Documents/learngit$ vi readme.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ git add readme.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ git commit -m "branch test"
    [dev 318cf42] branch test
     1 file changed, 1 insertion(+), 1 deletion(-)
    fbo@fbo-virtual-machine:~/Documents/learngit$ git checkout master
    Switched to branch 'master'
    Your branch is up-to-date with 'origin/master'.
    fbo@fbo-virtual-machine:~/Documents/learngit$ cat readme.txt 
    Git is a distributed version control system.
    Git is free software distributed under the GPL.
    Git has a mutable index called stage.
    Git tracks changes of files.
    
    # 合并dev分支
    fbo@fbo-virtual-machine:~/Documents/learngit$ git merge dev
    Updating 64bfb31..318cf42
    Fast-forward
     readme.txt | 2 +-
     1 file changed, 1 insertion(+), 1 deletion(-)
    fbo@fbo-virtual-machine:~/Documents/learngit$ cat readme.txt 
    Git is a distributed version control system.
    Git is free software distributed under the GPL.
    Git has a mutable index called stage.
    Git tracks changes of files.
    Creating a new branch is quick.
    # 删除dev分支
    fbo@fbo-virtual-machine:~/Documents/learngit$ git branch -d dev
    Deleted branch dev (was 318cf42).
    fbo@fbo-virtual-machine:~/Documents/learngit$ git branch
    * master
    

    解决冲突

    # 创建新的分支feature1
    fbo@fbo-virtual-machine:~/Documents/learngit$ git checkout -b feature1
    Switched to a new branch 'feature1'
    fbo@fbo-virtual-machine:~/Documents/learngit$ vi readme.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ cat readme.txt 
    Git is a distributed version control system.
    Git is free software distributed under the GPL.
    Git has a mutable index called stage.
    Git tracks changes of files.
    Creating a new branch is quick AND simple.
    fbo@fbo-virtual-machine:~/Documents/learngit$ git add readme.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ git commit -m "AND simple"
    [feature1 6095538] AND simple
     1 file changed, 1 insertion(+), 1 deletion(-)
    # 切换到master分支,会提示本地和远程有一个分支commit的差别
    fbo@fbo-virtual-machine:~/Documents/learngit$ git checkout master
    Switched to branch 'master'
    Your branch is ahead of 'origin/master' by 1 commit.
      (use "git push" to publish your local commits)
    fbo@fbo-virtual-machine:~/Documents/learngit$ vi readme.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ cat readme.txt 
    Git is a distributed version control system.
    Git is free software distributed under the GPL.
    Git has a mutable index called stage.
    Git tracks changes of files.
    Creating a new branch is quick & simple.
    fbo@fbo-virtual-machine:~/Documents/learngit$ git add readme.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ git commit -m "& simple"
    [master 9fecaab] & simple
     1 file changed, 1 insertion(+), 1 deletion(-)
    # 尝试合并分支时,由于修改同一个文件出现冲突
    fbo@fbo-virtual-machine:~/Documents/learngit$ git merge feature1
    Auto-merging readme.txt
    CONFLICT (content): Merge conflict in readme.txt
    Automatic merge failed; fix conflicts and then commit the result.
    fbo@fbo-virtual-machine:~/Documents/learngit$ git status
    On branch master
    Your branch is ahead of 'origin/master' by 2 commits.
      (use "git push" to publish your local commits)
    You have unmerged paths.
      (fix conflicts and run "git commit")
    
    Unmerged paths:
      (use "git add <file>..." to mark resolution)
    
        both modified:   readme.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")
    # 修改文件,文件会标识不同分支的差别
    fbo@fbo-virtual-machine:~/Documents/learngit$ cat readme.txt 
    Git is a distributed version control system.
    Git is free software distributed under the GPL.
    Git has a mutable index called stage.
    Git tracks changes of files.
    <<<<<<< HEAD
    Creating a new branch is quick & simple.
    =======
    Creating a new branch is quick AND simple.
    >>>>>>> feature1
    fbo@fbo-virtual-machine:~/Documents/learngit$ vi readme.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ git add readme.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ git commit -m "conflict fixed"
    [master 9832d03] conflict fixed
    # 图形显示git log
    fbo@fbo-virtual-machine:~/Documents/learngit$ git log --graph --pretty=oneline --abbrev-commit
    *   9832d03 conflict fixed
    |\  
    | * 6095538 AND simple
    * | 9fecaab & simple
    |/  
    * 318cf42 branch test
    * 64bfb31 remove test.txt
    * 212bc97 add test.txt
    * d6230e9 second modified
    * 2817224 git tracks changes
    * 6a371c1 understand how srtage works
    * c4212dc append GPL
    * b20d129 add distributed
    fbo@fbo-virtual-machine:~/Documents/learngit$ git merge feature1
    Already up-to-date.
    fbo@fbo-virtual-machine:~/Documents/learngit$ git branch -d feature1
    Deleted branch feature1 (was 6095538).
    

    分支管理策略

    在合并分支时,git会使用Fast Forward模式,这种模式下,删除分支后,会丢掉分支信息。
    我们来实战下禁用--no-ff模式下的git merge

    fbo@fbo-virtual-machine:~/Documents/learngit$ git checkout -b dev
    Switched to a new branch 'dev'
    fbo@fbo-virtual-machine:~/Documents/learngit$ echo hehe > readme.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ git branch 
    * dev
      master
    fbo@fbo-virtual-machine:~/Documents/learngit$ git add readme.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ git commit -m "add merge"
    [dev 7b49549] add merge
     1 file changed, 1 insertion(+), 5 deletions(-)
    fbo@fbo-virtual-machine:~/Documents/learngit$ git checkout master
    Switched to branch 'master'
    Your branch is ahead of 'origin/master' by 4 commits.
      (use "git push" to publish your local commits)
    fbo@fbo-virtual-machine:~/Documents/learngit$ git merge --no-ff -m "merge with no-ff" dev
    Merge made by the 'recursive' strategy.
     readme.txt | 6 +-----
     1 file changed, 1 insertion(+), 5 deletions(-)
     
    fbo@fbo-virtual-machine:~/Documents/learngit$ git log --graph --pretty=oneline --abbrev-commit
    *   1315c36 merge with no-ff
    |\  
    | * 7b49549 add merge
    |/  
    *   9832d03 conflict fixed
    |\  
    | * 6095538 AND simple
    * | 9fecaab & simple
    |/  
    * 318cf42 branch test
    * 64bfb31 remove test.txt
    * 212bc97 add test.txt
    * d6230e9 second modified
    * 2817224 git tracks changes
    * 6a371c1 understand how srtage works
    * c4212dc append GPL
    * b20d129 add distributed
    
    

    bug分支

    Git还提供了一个stash功能,可以把当前工作现场“储藏”起来,等以后恢复现场后继续工作:

    fbo@fbo-virtual-machine:~/Documents/learngit$ touch test.py
    fbo@fbo-virtual-machine:~/Documents/learngit$ git status
    On branch master
    Your branch is ahead of 'origin/master' by 6 commits.
      (use "git push" to publish your local commits)
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        test.py
    
    nothing added to commit but untracked files present (use "git add" to track)
    fbo@fbo-virtual-machine:~/Documents/learngit$ git add test.py 
    # 1. git stash 保存为提交更改
    fbo@fbo-virtual-machine:~/Documents/learngit$ git stash
    Saved working directory and index state WIP on master: 1315c36 merge with no-ff
    HEAD is now at 1315c36 merge with no-ff
    fbo@fbo-virtual-machine:~/Documents/learngit$ git status
    On branch master
    Your branch is ahead of 'origin/master' by 6 commits.
      (use "git push" to publish your local commits)
    nothing to commit, working directory clean
    # 2. 切换到master分支,修改bug
    fbo@fbo-virtual-machine:~/Documents/learngit$ git branch 
      dev
    * master
    fbo@fbo-virtual-machine:~/Documents/learngit$ git checkout master
    Already on 'master'
    Your branch is ahead of 'origin/master' by 6 commits.
      (use "git push" to publish your local commits)
    fbo@fbo-virtual-machine:~/Documents/learngit$ git checkout -b issue-101
    Switched to a new branch 'issue-101'
    fbo@fbo-virtual-machine:~/Documents/learngit$ echo "Git is a free software" >> readme.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ git add readme.txt 
    fbo@fbo-virtual-machine:~/Documents/learngit$ git commit -m "fix bug 101"
    [issue-101 e8eb976] fix bug 101
     1 file changed, 1 insertion(+)
    fbo@fbo-virtual-machine:~/Documents/learngit$ ls
    readme.txt
    fbo@fbo-virtual-machine:~/Documents/learngit$ git checkout master
    Switched to branch 'master'
    Your branch is ahead of 'origin/master' by 6 commits.
      (use "git push" to publish your local commits)
    fbo@fbo-virtual-machine:~/Documents/learngit$ git merge --no-ff -m "merged bug fix 101" issue-101
    Merge made by the 'recursive' strategy.
     readme.txt | 1 +
     1 file changed, 1 insertion(+)
    fbo@fbo-virtual-machine:~/Documents/learngit$ git branch -d issue-101
    Deleted branch issue-101 (was e8eb976).
    # 3. 切回到工作分支
    fbo@fbo-virtual-machine:~/Documents/learngit$ git checkout dev
    Switched to branch 'dev'
    fbo@fbo-virtual-machine:~/Documents/learngit$ git status
    On branch dev
    nothing to commit, working directory clean
    # 4. 列出保存状态
    fbo@fbo-virtual-machine:~/Documents/learngit$ git stash list
    stash@{0}: WIP on master: 1315c36 merge with no-ff
    # 5. 弹出保存状态,用git stash apply恢复,但是恢复后,stash内容并不删除,你需要用git stash drop来删除;
    fbo@fbo-virtual-machine:~/Documents/learngit$ git stash pop
    On branch dev
    Changes to be committed:
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        new file:   test.py
    
    Dropped refs/stash@{0} (5a865508f62f4e1f62edc0f589587a93436e9d70)
    fbo@fbo-virtual-machine:~/Documents/learngit$ ls
    readme.txt  test.py
    fbo@fbo-virtual-machine:~/Documents/learngit$ git stash list
    

    Feature分支

    fbo@fbo-virtual-machine:~/Documents/learngit$ git checkout -b feature-vulcan
    A   test.py
    Switched to a new branch 'feature-vulcan'
    fbo@fbo-virtual-machine:~/Documents/learngit$ touch vulcan.py
    fbo@fbo-virtual-machine:~/Documents/learngit$ git add  vulcan.py 
    fbo@fbo-virtual-machine:~/Documents/learngit$ git status
    On branch feature-vulcan
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        new file:   test.py
        new file:   vulcan.py
    
    fbo@fbo-virtual-machine:~/Documents/learngit$ git commit -m "add feature vulcan"
    [feature-vulcan eb02af8] add feature vulcan
     2 files changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 test.py
     create mode 100644 vulcan.py
    fbo@fbo-virtual-machine:~/Documents/learngit$ git checkout dev
    Switched to branch 'dev'
    fbo@fbo-virtual-machine:~/Documents/learngit$ git branch -d feature-vulcan
    error: The branch 'feature-vulcan' is not fully merged.
    If you are sure you want to delete it, run 'git branch -D feature-vulcan'.
    fbo@fbo-virtual-machine:~/Documents/learngit$ git branch -D feature-vulcan
    Deleted branch feature-vulcan (was eb02af8).
    

    多人协作

    # 查看远程信息
    fbo@fbo-virtual-machine:~/Documents/learngit$ git remote
    origin
    fbo@fbo-virtual-machine:~/Documents/learngit$ git remote -v
    origin  git@github.com:drfung/learngit.git (fetch)
    origin  git@github.com:drfung/learngit.git (push)
    fbo@fbo-virtual-machine:~/Documents/learngit$ git push origin master
    Warning: Permanently added the RSA host key for IP address '192.30.255.112' to the list of known hosts.
    Counting objects: 18, done.
    Compressing objects: 100% (11/11), done.
    Writing objects: 100% (18/18), 1.38 KiB | 0 bytes/s, done.
    Total 18 (delta 7), reused 0 (delta 0)
    remote: Resolving deltas: 100% (7/7), completed with 1 local object.
    To git@github.com:drfung/learngit.git
       64bfb31..d69e698  master -> master
    fbo@fbo-virtual-machine:~/Documents/learngit$ git push origin dev
    Total 0 (delta 0), reused 0 (delta 0)
    To git@github.com:drfung/learngit.git
     * [new branch]      dev -> dev
    

    标签管理

    fbo@fbo-virtual-machine:~/Documents/learngit$ git branch
    * dev
      master
    fbo@fbo-virtual-machine:~/Documents/learngit$ git checkout master
    Switched to branch 'master'
    Your branch is up-to-date with 'origin/master'.
    fbo@fbo-virtual-machine:~/Documents/learngit$ git tag v1.0
    fbo@fbo-virtual-machine:~/Documents/learngit$ git tag
    v1.0
    fbo@fbo-virtual-machine:~/Documents/learngit$ git log --pretty=oneline --abbrev-commit
    d69e698 merged bug fix 101
    e8eb976 fix bug 101
    1315c36 merge with no-ff
    7b49549 add merge
    9832d03 conflict fixed
    9fecaab & simple
    6095538 AND simple
    318cf42 branch test
    64bfb31 remove test.txt
    212bc97 add test.txt
    d6230e9 second modified
    2817224 git tracks changes
    6a371c1 understand how srtage works
    c4212dc append GPL
    b20d129 add distributed
    fbo@fbo-virtual-machine:~/Documents/learngit$ git tag v0.9 7b49549
    fbo@fbo-virtual-machine:~/Documents/learngit$ git tah
    git: 'tah' is not a git command. See 'git --help'.
    
    Did you mean one of these?
        stash
        tag
    fbo@fbo-virtual-machine:~/Documents/learngit$ git tag
    v0.9
    v1.0
    fbo@fbo-virtual-machine:~/Documents/learngit$ git show v0.9
    commit 7b4954922f8471d72d4620b54d1652c886547439
    Author: drfung <recoba01@163.com>
    Date:   Wed Sep 6 15:29:10 2017 +0800
    
        add merge
    
    diff --git a/readme.txt b/readme.txt
    index f10249a..91ca0fa 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -1,5 +1 @@
    -Git is a distributed version control system.
    -Git is free software distributed under the GPL.
    -Git has a mutable index called stage.
    -Git tracks changes of files.
    -Creating a new branch is quick AND simple.
    +hehe
    

    相关文章

      网友评论

          本文标题:git 使用

          本文链接:https://www.haomeiwen.com/subject/szlmpxtx.html