实习日志(第三周)

来源:互联网 发布:淘宝卖家无货退款处罚 编辑:程序博客网 时间:2024/04/30 02:02

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

创建版本库

  1. 创建空目录
$ mkdir learngit$ cd learngit$ pwd/c/Users/Administrator/learngit//pwd命令显示仓库位于/c/Users/Administrator/learngit目录下
  1. 通过git init命令把这个目录变成Git可以管理的仓库
$ git initInitialized empty Git repository in C:/Users/Administrator/learngit/.git/
  1. 添加文件至仓库
$ git add readme.txt$ git commit -m "wrote a readme file"[master (root-commit) e87659e] wrote a readme file 1 file changed, 2 insertions(+) create mode 100644 readme.txt// -m后面输入的是本次提交的说明,可以输入任意内容

时光机穿梭

  1. 掌握工作区的状态
    $ git statusOn branch masterChanges 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.txtno changes added to commit (use "git add" and/or "git commit -a")//git status命令可以让我们时刻掌握仓库当前的状态,上面的命令告诉我们,readme.txt被修改过了,但还没有准备提交的修改
    $ git diff readme.txtdiff --git a/readme.txt b/readme.txtindex d8036c1..013b5bc 100644--- a/readme.txt+++ b/readme.txt@@ -1,2 +1,2 @@-Git is a version control system.//git diff可以查看修改内容
  2. ###### 版本回退

    $ git log
    commit 9e3fab12f645e3d87e3dc0d6d2261fbafaa0b373
    Author: MaXiaoBin <1010981834@qq.com>
    Date: Tue Sep 12 16:46:31 2017 +0800
    append GPL
    commit d477e5f4dcd62e710f2947cef9027efeaf4bbb28
    Author: MaXiaoBin <1010981834@qq.com>
    Date: Tue Sep 12 16:39:54 2017 +0800
    add distributed
    commit e87659e203a9b222d74cf3991a9b035f4994dc18
    Author: MaXiaoBin <1010981834@qq.com>
    Date: Tue Sep 12 16:05:16 2017 +0800
    wrote a readme file
    //git log命令显示从最近到最远的提交日志


    $ git reset --hard HEAD^
    HEAD is now at ea34578 add distributed
    //获取上一个版本,HEAD表示当前版本,HEAD^表示上一个版本,HEAD~100表示上100个版本

添加远程库

$ git remote add origin git@github.com:username/learngit.git$ git push -u origin master
$ git push origin master推送本地修改至远程库命令
$ git clone git@github.com:michaelliao/gitskills.git克隆别人远程库到本地

分支管理

  1. 查看分支:git branch
  2. 创建分支:git branch
  3. 切换分支:git checkout
  4. 创建+切换分支:git checkout -b
  5. 合并某分支到当前分支:git merge
  6. 删除分支:git branch -d

    分支管理策略
    $ git merge --no-ff -m"merge with no-ff" devMerge made by the 'recursive' strategy.test.txt | 3 ++-1 file changed, 2 insertions(+), 1 deletion(-)合并分支时,加上--no-ff参数就可以用普通模式合并,合并后的历史有分支,能看出来曾经做过合并,而fast forward合并就看不出来曾经做过合并
    $ git log --graph --pretty=oneline --abbrev-commit*   cf3cc2d merge with no-ff|\| * 130265d add merge|/* df3f18d branch test* 867bb49 Test用git log --graph命令可以看到分支合并图
    BUG分支
    $ git stashSaved working directory and index state WIP on dev: 6224937 add mergeHEAD is now at 6224937 add merge可以把当前工作现场“储藏”起来,等以后恢复现场后继续工作
    $ git stash liststash@{0}: WIP on dev: 6224937 add merge查看当前的工作现场
    $ git stash pop# On branch dev# Changes to be committed:#   (use "git reset HEAD <file>..." to unstage)##       new file:   hello.py## 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#Dropped refs/stash@{0} (f624f8e5f082f2df2bed8a4e09c12fd2943bdd40)恢复工作现场,并在恢复的同时把stash内容也删了
    $ git stash apply恢复,但是恢复后,stash内容并不删除$ git stash drop删除stash内容

    修复bug时,我们会通过创建新的bug分支进行修复,然后合并,最后删除;
    当手头工作没有完成时,先把工作现场git stash一下,然后去修复bug,修复后,再git stash pop,回到工作现场。