Git 笔记

2017-11-11  本文已影响0人  鐘濤

背景

昨晚我建了个远程库,专门放笔记的。因为只是写md,又没写代码,所以就直接用vs code去编辑了。可vs code自带的git又不知道怎么用,然后我就只好打git命令行,添加远程库等等。可是搞了半天没弄好。

今天我就复现了昨晚的场景。

昨晚是这样的,我在码云新建了个私有库,码云会自动添加md文件嘛。
而我本地是这样的:

mkdir notes
cd notes
mkdir 1711
git init //  初始化新仓库

然后就用vs code新建了md文件开始写笔记,写完了想放到码云上。不得已我就开始打命令行了

git remote add origin https://gitee.com/远程库名
git add [fileName]  //  添加文件
git commit -m [messages]  //  commit
git fetch origin master  //  取回远程库的文件到本地
git merge origin/master  //  合并,结果出错了
//  fatal: refusing to merge unrelated histories
//  加多这一段就ok :--allow-unrelated-histories
git merge origin/master --allow-unrelated-histories
git push origin master  //  推送到远程仓库

我还想有其他骚操作

git rm README.md
git commit -m 删除了README.md
git push origin master
mkdir dir
cd dir
touch 1.md 2.md 3.md
git add *.md
git commit -m 添加文件夹
git push origin master
git mv oldFileName newFileName 
git commit -m 改文件名
git push origin master
//  添加
git remote add origin https://gitee.com/仓库名
//  删除
git remote rm origin
//  重命名
git remote rename origin hhh
//  查看远程主机名
git remote
//  查看远程主机名和对应的地址
git remote -v

Git的文件是有三种状态的,已修改,已暂存,已提交。对于所在位置:工作区work,暂存区stage,仓库repository。

怎么看文件的状态呢?用命令行git status

$ 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:   "1711/git\347\254\224\350\256\260.md"
        modified:   README.md

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:   "1711/git\347\254\224\350\256\260.md"

怎么理解这一段,首先说了你跟远端的分支master是最新的,然后下面有一些文件需要被commit提交,还有一些你改动了的文件,需要用git add放入到 stage暂存区

这个意思就是说,工作区(.git文件所在的目录)里头的git笔记.md修改了,但没有放入暂存区stage,而README.md文件虽然修改了,但已经放入暂存区stage了,就差commit提交到仓库了。

图中只有一个master分支

这里有三个版本,最新的是f30ab

图中有两个分支,master,testing

图中有两个分支,master和testing 然后HEAD指向的是我们当前所在的分支,怎么看我们所在分支是哪个呢。命令行git branch,带*星号的就是所在分支,如下:

git branch
    * master
    testing

新建分支合并分支运用场景

比方说:我们有一个项目,已经上线了,也就是说已经放到生产环境了。

这时候我们有新需求,为了不影响已上线的产品,我们开了一个分支,我们就可以在这里开发,然后测试完再推到主线上。

假如我们线上的产品有个严重的bug,这时候我们就回到主线分支,然后再新建一个分支出来去修复bug,修好了再推到主线上。

//  新建一个分支 newBranchName 并且切换到该分支上
git checkout -b newBranchName
//  相当于执行如下命令
git branch newBranchName
git checkout newBranchName

//  如下例子:
$ git checkout -b develop master
Switched to a new branch 'develop'
$ git branch
* develop
  master

利用分支进行开发工作的流程

master 为主线分支,主分支只用于发布大版本更新。develop 分支为日常开发分支。

若不加 --no-ff 参数则是这样的

Git执行"快进式合并"(fast-farward merge)

加上参数 --no-ff

加上参数 `--no-ff`

未完待续

参考资料

上一篇下一篇

猜你喜欢

热点阅读