git学习笔记


git可以说是现在最主流的版本控制工具,作为一名今后的程序员,必须要了解怎么使用这款工具。首次接触充满困惑,记录学习笔记,供日后参考及改正。



一、需要了解的基本概念:

1、版本控制是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统。Git保存的不是文件的变化或者差异,而是一系列不同时刻的文件快照。

2、Git是分布式版本控制,在此基础上的gitlab、github,与之相对应的是集中式版本控制如CVS。

3、优缺点。集中式依赖主机,服务器崩溃或无网络连接将无法工作,而分布式在本地拥有项目的全部copy。对于修改代码而言,分布式系统更安全。

 


二、Git的三种状态

Committed:已提交,所有暂存过的更改都提交了,完成储存。换个角度来看也可以称作未修改(Unmodified)。

Modified:对文件做了修改案还没保存的。

Staged:修改后放入暂存区的。

 git学习笔记



三、常用命令:

$ git init  

如果想将文件夹”mygit”及其内容纳入仓库,进入文件夹输入如上命令,该命令将创建一个名为 .git 的子目录,这个子目录含有你初始化的 Git 仓库中所有的必须文件,这些文件是 Git仓库的骨干。Win上使用“gitbash here”,发现.git文件夹并不是可见的。

 

$ git clone + address (+rename)

克隆一个仓库到本地,address可以是httpsssh

 

$ git status

查看仓库内的文件状态,即”git的三种状态,每个文件的状态互不影响。

在仓库内新建文件后,查看状态

Untracked

   On brunch master

   Untracked files:

(use “git add<file>…”  …)

 Filename1

 Filename2

加入trackgit add Filename1track文件或者将修改过的文件纳入暂存区)

提示Changes to be committed,处于staged状态。

 

对filename1进行修改后

提示Changes not stagedfor commmit,处于modified状态。

 

此时filename1同时处于这两种状态,区别在于当前文件是修改后的文件,但是如果执行commit提交的会是修改之前的staged状态文件。

 

$ git commit –m “commit first time”

$ git status

 Nothing to commit,working tree clean

处于committed状态

 

$ git commit –a 跳过add命令把所有tracked文件commit,而不是staged文件。

 

 $ git log查看历史提交–[123]显示最近123次更细  - - stat列出修改文件

        -pretty=oneline 将每个提示放在一行

        -pretty=format:” ….” 按指定格式输出,具体懒得记。

$ git log用法很多,功能也很强大,对需要用到的特殊功能再做学习。

 


四、版本的回滚

$ git log 可以看到以往历史的版本

$ git reset –hard (branchName) (版本编号如5a4c40d90c8c8af0ba5e8fe0f27df89fcbea322c)

回滚到需要的版本

 


 五、分支概念

想要快速上手使用,所以准备着重了解分支

 

分支意味着你可以把工作从开发主线上分离开来,以免影响开发主线。

具体的含义一张图很直观

git学习笔记

版本从”98ca9””34ac2””f30ab”后出现分支分叉情况。

Mastertesting都是分支,但一般会将master作为版本走向的主要分支。

HEAD指针指向当前操作的分支。

新建分支:

$ git branch branchName

切换分支:

$ git checkout branchName

新建一个分支并切换到它:

$ git checkout –b branchName

当前分支与branchName分支合并:

$ git merge branchName

关闭并删除分支:

$ git branch –d branchName(只有当branchName完全被其他分支覆盖后才能成功,否者需要使用-D强制删除)

$ git branch 查看当前所有分支–v附加分支最后修改 –merged –no-merged合并到当前分支或未合并到当前分支下的分支

快进(fast-forward)的概念:当要合并的两个分支,其中一个分支能走到另一个分支,或者说一个分支是另一个分支的早期版本,那么不会进行真正的合并,而是使靠后的分支指针前移。

 

 

六、远程仓库

工作中维护的代码通常都放在远端服务器上,工作中同事们常常需要在多人合作的情况下,频繁的拉取代码,修改优化,上传合并。

 

$ git clone 将仓库克隆到本地。

$ git fetch origin 更新本地仓库。

$ git push origin localBranch:originBranch推送分支到远端。

$ git checkout -b [branch][remotename]/[branch]跟踪分支。

 

远端仓库的多人合作

git学习笔记

简单来说就是

$ git pull

更改

$ git push

 

 

七、一个实际的练习

使用github测试。

先在github上随便找一个仓库,fork到自己号下作为测试的远程仓库。我选择的是

https://github.com/Tencent/libco一个wechat的协程库。在fork后地址为https://github.com/_name_/libco  _name_github账户名。

在本地创建一个文件夹作为测试的存放处。

$ mkdir mygittest

$ cd mygittest

$ git init

 

#克隆目标仓库到本地

$ git clone https://github.com/_name_ /libco     

Cloning into'libco'...

remote: Countingobjects: 249, done.

remote: Compressingobjects: 100% (10/10), done.

remote: Total 249(delta 8), reused 15 (delta 5), pack-reused 231

Receiving objects:100% (249/249), 169.73 KiB | 232.00 KiB/s, done.

Resolving deltas: 100% (138/138), done.

#mygittest文件夹中多出了子文件夹libco

 

$ git status

#提示untracked file

 

$ git add libco/

#对文件做一些更改,存放到master

#随便打开一个文件co_epoll.h,添加一行注释//Im trying to make some good changes sometimes

#保存。

 

$ git add co_epoll.h

$ git commit –m “update file co_epoll.h and add oneline”.

[master(root-commit) 38348ee] change file co_epoll.h and add one line

 50 files changed, 9099 insertions(+)

 

#新建一个分支,打开文件添加//Im trying a time,but nothing I find

$ git checkout -b thebranch

$ git commit -a -m "change the branch"

 

#将改动推到自己的github

$ git remote add origin https://github.com/_name_ /libco

$ git remote -v

origin  https://github.com/Showjy/libco (fetch)

origin https://github.com/Showjy/libco (push)

$ git push originthebranch

Counting objects:34, done.

Delta compressionusing up to 4 threads.

Compressingobjects: 100% (31/31), done.

Writing objects:100% (34/34), 38.58 KiB | 0 bytes/s, done.

Total 34 (delta 9),reused 0 (delta 0)

remote: Resolvingdeltas: 100% (9/9), done.

To https://github.com/Showjy/libco

 * [newbranch]      thebranch -> thebranch

 

#进入github libco 发现新分支thebranch

 

前面做了一个SSH密匙验证。

$ ssh-****** -t rsa

按提示操作生成两个文件id_rsa id_rsa.pub, id_rsa是**,id_rsa.pub 是公钥。进入第二个文件把内容复制下来。在github setting中new SSH key,复制进去。

$ ssh -T [email protected]

Hi _name_ ! You'vesuccessfully authenticated, but GitHub does not provide shell access.

 

此时我在github上对文件进行修改,然后在本地pull,本地文件更新为修改后。

如果在github上修改,在本地fetch而不进行merge,发现有三个branch

* (HEAD detached at origin/thebranch)

  master

  thebranch

当checkout 到origin/thebranch时,本地文件为修改后文件,

而checkout 到thebranch时,本地文件为修改前文件。

这样就比较清楚pull的作用了。


(图片来自https://git-scm.com/)