Git认真学习(2)

来源:互联网 发布:ubuntu删除nvidia驱动 编辑:程序博客网 时间:2024/06/14 14:03

第二部分就是基础了,这部分我会尝试着用国产github,码云来弄,因为可以免费创建private仓库,所以练习之后我就可以上传保存自己写的羞耻小说了哈哈

取得仓库

1.首先我建立一个名为Gittest的仓库,里面有我新建的文件 file_0。如下图:

这里写图片描述


2.然后我得再本地建立一个 本地仓库

$ git init

这句是用来初始化的,初始化后,文件里就有了一个 .git 的目录

$ git clone <仓库地址> <自己设定的clone的文件名>(非必要,如果加上文件名会改变而已,是在同一目录下的)

这里用的是 clone 而不是 checkout ,也就是git要收取 项目的每一个文件的所有版本,也就是任何一个本地客户端都可以重建服务器仓库

实例:

输入:

$ git clone https@...$ git clone https@... newdir

之后的文件结构如下图:

这里写图片描述


记录更新

工作区的文件状态有 4种

这里写图片描述

untracked 未跟踪
unmodified 为修改
modified 修改了
staged 暂存了

$ git status //可以查看文件环境

我们来试一下吧!

如果还在刚才的目录下此命令后:
会显示为:

On branch masterInitial commitUntracked files:  (use "git add <file>..." to include in what will be committed)        Githubtest/nothing added to commit but untracked files present (use "git add" to track)

显示多了一个未跟踪的目录 Gittest,就是我们刚才clone下来的东西,是不是和大家阅读的官网教程不一样呢?没事,进入我们Gittest目录下后,再次运行此命令。果然显示为:

On branch masterYour branch is up-to-date with 'origin/master'.nothing to commit, working directory clean

果然,现在的目录是干净的

我实验后发现,clone远程的仓库,git 会帮你初始化出一个本地仓库的,只要进入clone下来的文件目录就可以,不需要再次执行git init


创建文件

现在我们增加一个文件 file_1,如下图:
这里写图片描述

然后再次运行 git status 命令,显示为 文件file_1.txt是没有跟踪的

On branch masterYour branch is up-to-date with 'origin/master'.Untracked files:  (use "git add <file>..." to include in what will be committed)        file_1.txtnothing added to commit but untracked files present (use "git add" to track)

那我们就跟踪它!

$ git add file_1.txt // 注意一定要是全文件名

果然,显示为有了一个新文件

On branch masterYour branch is up-to-date with 'origin/master'.Changes to be committed:  (use "git reset HEAD <file>..." to unstage)        new file:   file_1.txt

现在的状态是 staged,文件暂存了,add 也就是把文件加到了暂存区


修改文件

那么如果不是创建文件,只是修改了文件呢?,比如我对file_0做了如下修改:

这里写图片描述

此时再次运行 git status,显示出有file_0文件是修改过的

On branch masterYour branch is up-to-date with 'origin/master'.Changes to be committed:  (use "git reset HEAD <file>..." to unstage)        new file:   file_1.txtChanges 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:   file_0

此时我们需要再次 add 一下file_0 文件 git add file_0

此时的状态为:

On branch masterYour branch is up-to-date with 'origin/master'.Changes to be committed:  (use "git reset HEAD <file>..." to unstage)        modified:   file_0        new file:   file_1.txt

可见此时的 新创建的文件 和 修改过的文件,都处于等待 commit状态


忽略文件

如何忽略一些文件,比如我现在创建了 文件 file_3.txt,那么我现在不想file3.txt处于untracked之下?

创建一个 .gitignore 里面用正则写上忽略的顺序,规则如下:

文件 .gitignore 的格式规范如下:
所有空行或者以注释符号 # 开头的行都会被 Git 忽略。
可以使用标准的 glob 模式匹配。
匹配模式最后跟反斜杠(/)说明要忽略的是目录。
要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。

官方给的这个例子很好,注意 ! 只是代表排除,不代表取反

# 此为注释 – 将被 Git 忽略    # 忽略所有 .a 结尾的文件    *.a    # 但 lib.a 除外,除外不代表取反    !lib.a    # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO    /TODO    # 忽略 build/ 目录下的所有文件    build/    # 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt    doc/*.txt

实例:

如果我们这么写:

*3.txt

果然,显示untracked文件的时候,只有 .gitignore 文件,而没有file_3.txt

On branch masterYour branch is up-to-date with 'origin/master'.Changes to be committed:  (use "git reset HEAD <file>..." to unstage)        modified:   file_0        new file:   file_1.txtUntracked files:  (use "git add <file>..." to include in what will be committed)        .gitignore

查看具体修改

有两个命令来比较

$ git diff // 当前文件 vs 暂存区快照$ git diff --cached // 暂存区快照 vs 提交之后的快照

我运行这两个命令之后:

第一个什么都没显示,因为我所有的暂存区的文件都是和当前区域是一样的。.gitignore 此时没有在 track 下,所以还不显示。

第二个命令后,就有了显示了,但是什么意思,现在还不急着搞清楚。


提交

$ git commit // 会打开默认的编辑器来输入 修改$ git commit -m "xxx" // xxx 作为description

良好的习惯是 commit 之前,用status确定都已经 add 了,commit 后显示为:

[master 3a68b70] xxx //这个数字是校验和 3 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 file_1.txt

每次commit操作都是一次 快照

更快的commit:如果你只是修改而没有添加文件的话,完全可以跳过add阶段,直接提交快照到git目录下,使用 -a

git commit -a -m "xxx"

删除文件

如果要在git中删除一个文件,要:

先从暂存区删除文件

从工作区删除文件

提交

git rm file_1.txt

显示状态为有一个删除:

On branch masterYour branch is ahead of 'origin/master' by 1 commit.  (use "git push" to publish your local commits)Changes to be committed:  (use "git reset HEAD <file>..." to unstage)        deleted:    file_1.txt

如果不是从命令行删除呢?目录下直接删除 file_0, 状态显示如下:

On branch masterYour branch is ahead of 'origin/master' by 1 commit.  (use "git push" to publish your local commits)Changes to be committed:  (use "git reset HEAD <file>..." to unstage)        deleted:    file_1.txtChanges 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:    file_0

可以看到,这样就相当于 一个没有add的 deleted 操作,也就是此时 暂存区的file_0文件 还是在的

add 之后,状态为:

On branch masterYour branch is ahead of 'origin/master' by 1 commit.  (use "git push" to publish your local commits)Changes to be committed:  (use "git reset HEAD <file>..." to unstage)        deleted:    file_0        deleted:    file_1.txt

此时commit之后,file0,1就不再版本管理之下了。但是之前的版本还是有的。

特殊情况1

但是如果删除之前,修改过,并放在了暂存区,也就是 删除修改并add过的文件

那么就要就要用git rm -f xxx f means force

特殊情况2

还有一种情况,就是要删除git仓库的文件,当然也有暂存区,但是在 工作区保存,加 --cached


移动文件

mv 可以用来 改名

改名测试:

git mv README.md README

状态为:

On branch masterYour branch is ahead of 'origin/master' by 1 commit.  (use "git push" to publish your local commits)Changes to be committed:  (use "git reset HEAD <file>..." to unstage)        renamed:    README.md -> README        deleted:    file_0        deleted:    file_1.txt

提交历史

git log

显示了我不仅这两次的操作,还有之前很多次其他项目的commit

commit b245e5cc0d94570089bfb8de38b87b43c7a5a96eAuthor: xuezhaojun <1340762527@qq.com>Date:   Tue Jun 14 16:53:42 2016 +0800    yyycommit 3a68b703d935c56aa1e3ccf45534876923b7d10aAuthor: xuezhaojun <1340762527@qq.com>Date:   Tue Jun 14 16:07:28 2016 +0800    xxx

其他参数:

$ git log -2 // 只显示最近两次$ git log -p // 显示每次提交的差异 $ git log --stat // 显示其他写作者的commit,文件层面的修改

还有一些关于显示log定制的方法,之后再学习。因为commit的各种显示,都可以在网页版上清除的看出来


撤销操作(非常重要)

修改上次的操作:

先做你的修改,然后add,然后执行

$ git commit -m "zzz" --amend // 修改提交说明,上一次的提交说明变成了 zzz,并且新的快照也暂存了

取消暂存区的文件

新建两个文件 file_5.txt , file_6.txt ,add后,显示为:

On branch masterYour branch is ahead of 'origin/master' by 2 commits.  (use "git push" to publish your local commits)Changes to be committed:  (use "git reset HEAD <file>..." to unstage)        new file:   file_5.txt        new file:   file_6.txt

但是我又不想 commit file_6 了,怎么取消呢!其实上面都有提示了

$ git reset HEAD file6.txt

就可以啦~这样 file_6.txt 就从暂存区跑回来了,进入到 文件的modify的状态

还有一个取消对文件的修改,是很危险的操作,可以把modify的东西去掉,但是这非常可能造成文件损失!!!所以在不熟练的情况下先不用


远程仓库的使用

查看远程仓库

git remote -v (verbose长的,就是顺便显示出远程仓库地址)git remote add _name git://github.com/paulboone/ticgit.git (之后就可以用_name来代替这个仓库了) 

抓取远程仓库的信息

git fetch _name

抓取不是clone,此时是没有复制远程仓库到某个本地仓库的。但是你可以在本地 访问该远程仓库的所有分支了

如果只是clone了一个仓库,那么该远程仓库自动的归于origin名下。

注意:fetch只是抓取,不是合并

关于分支之后再详细说明。


推送到远程仓库

如果在你推送前,已经有人推了好几条上去,那么你的推送就会被驳回。你得先fetch

git push origin master// 这是将本地的master分支,推到 origin 上去

注意:clone会自动使用 master 分支,和 origin 的名字

git remote show origin // 显示远程仓库的状态

远程仓库的改名和删除

git remote rename <name_before> <name_after> //只是改了在本地的简称git remote rm <name>

打标签现在还先不接触,等用到时候再学习

0 0
原创粉丝点击