首页 > 其他 > 详细

git学习笔记

时间:2019-01-04 15:18:23      阅读:221      评论:0      收藏:0      [点我收藏+]

比较全且易懂的git学习网站

https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013743256916071d599b3aed534aaab22a0db6c4e07fd0000

分布式版本库和集中式版本库的区别

集中式版本库

如SVN

技术分享图片

通过与中心服务器的连接执行所有操作,必须联网

分布式版本库

如Git

技术分享图片

1、快速,本地客户机操作,不联网也不影响工作,离线工作,DVCS 比 Subversion 快大约3-10倍

2、可以实现非常灵活的工作流(传统的集中式工作流 + 特殊工作流 + 特殊工作流和集中式工作流的组合)

3、安全性更高,因为每个人电脑里都是完整的版本库,坏了复制一份即可,CVCS中央服务器出问题就GG      

4、两台电脑互相访问不了或某一台电脑未开机不能执行复制(交换修改)时,通常有一台电脑充当中央服务器 

 5、分支管理

git常用命令

创建版本库

git init

192:gitblit liqiang$ cd gitTest
192:gitTest liqiang$ git init
Initialized empty Git repository in /Users/liqiang/Desktop/gitblit/gitTest/.git/
192:gitTest liqiang$ 

在指定目录执行git init 命令 该目录下的所有目录及文件都受git管理

查看状态

git status

192:gitTest liqiang$ mkdir files
192:gitTest liqiang$ vi hello.txt
192:gitTest liqiang$ git status

技术分享图片

 

创建了一个files目录 并在files目录下增加了一个hello.txt文件通过git status可以看到还没有add

添加和提交

git add git commit

这2个是组合命令 先add将文件提交到暂存区  再commit 提交到分支  git默认会创建一个master分支

192:gitTest liqiang$ git add files
192:gitTest liqiang$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   files/hello.txt

执行add后再查看状态 显示还没有提交  

192:gitTest liqiang$ git commit files -m ‘创建了一个hello文件‘
[master (root-commit) 224f10f] 创建了一个hello文件
 1 file changed, 1 insertion(+)
 create mode 100644 files/hello.txt
192:gitTest liqiang$ git status
On branch master
nothing to commit, working tree clean
192:gitTest liqiang$ 

执行commit就成功提交到本地仓库 或者可以到指定目录执行git add . (空格+.) 提交目录下面所有文件

查看日志

git log

192:gitTest liqiang$ git log
commit 224f10fd409bc7f8f83167167c42d05e36a5bb24 (HEAD -> master)
Author: liqiang <www.liqiand@qq.com>
Date:   Fri Jan 4 13:50:16 2019 +0800

    创建了一个hello文件

可以看到刚才的提交记录

版本回退 

伪造测试数据 对文件进行2此修改并提交 查看日志

192:gitTest liqiang$ git log
commit 3708002e7618ca88f349cdec3517b0b1f9611413 (HEAD -> master)
Author: liqiang <www.liqiand@qq.com>
Date:   Fri Jan 4 13:58:24 2019 +0800

    文件内容修改为hell3

commit 209a176fa3413ac5f3528c2da1ab7126802160e3
Author: liqiang <www.liqiand@qq.com>
Date:   Fri Jan 4 13:57:08 2019 +0800

    文件内容修改为hell2

commit 224f10fd409bc7f8f83167167c42d05e36a5bb24
Author: liqiang <www.liqiand@qq.com>
Date:   Fri Jan 4 13:50:16 2019 +0800

    创建了一个hello文件

 

或者加上--pretty=oneline 使日志一行显示

192:gitTest liqiang$ git log --pretty=oneline
3708002e7618ca88f349cdec3517b0b1f9611413 (HEAD -> master) 文件内容修改为hell3
209a176fa3413ac5f3528c2da1ab7126802160e3 文件内容修改为hell2
224f10fd409bc7f8f83167167c42d05e36a5bb24 创建了一个hello文件
192:gitTest liqiang$ 

 

 回退到上一个版本

192:gitTest liqiang$ git reset --hard HEAD^
HEAD is now at 209a176 文件内容修改为hell2

 

打开文件可以看到变成了hello2

如果要回退到指定版本呢只需要hard参数加上要回退的版本号

$  git reset --hard 3708002e7618ca88f349cdec3517b0b1f9611413
HEAD is now at 3708002 文件内容修改为hell3

 git每次提交 都会为每次提交保留一个版本  版本回退只是将指针指向回退的版本

技术分享图片

技术分享图片

当版本回退 head 重新指向了 hello2

撤销add 

192:files liqiang$ git add hello.txt
192:files liqiang$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   hello.txt

通过将文件修改通过git add到暂存区 如何撤销提交(我工作中经常遇到这样的需求)

192:files liqiang$ git reset HEAD hello.txt
Unstaged changes after reset:
M    files/hello.txt
192:files liqiang$ 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:   hello.txt

git reset HEAD 不加文件信息 则是 撤销所有暂存区

撤销本地修改 

git checkout -- filepathname

192:files liqiang$ git checkout -- hello.txt 

删除文件/目录

git rm filename   git rm -r directoryName

92:gitTest liqiang$ git rm hello.txt
92:gitTest liqiang$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    files/hello.txt

如果需要撤销与上面撤销修改一致

添加远程仓库 $ git remote add [远程仓库名字(一般叫origin)] [远程仓库地址] $ git push -u origin master

$ git remote add  本地仓库与远程仓库关联

$ git push -u origin master  推送到远程仓库

可以使用github 我这里是本地搭建了一个git服务器 gitblit

技术分享图片

 

技术分享图片

192:gitTest liqiang$ git remote add origin http://admin@127.0.0.1:1234/r/gitTest.git
192:gitTest liqiang$ git push -u origin master
Password for ‘http://admin@127.0.0.1:1234‘: 
To http://127.0.0.1:1234/r/gitTest.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to ‘http://admin@127.0.0.1:1234/r/gitTest.git‘
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., ‘git pull ...‘) before pushing again.
hint: See the ‘Note about fast-forwards‘ in ‘git push --help‘ for details.
192:gitTest liqiang$ failed to push some refs to

 报错是因为我上面创建版本库 勾上了添加README文件 origin不是一个独立的版本库了。。如果使用clone 将没有问题 

这个时候需要执行

$ git pull origin master --allow-unrelated-histories

合并origin和本地版本库

$ git commit -m ‘合并‘

最后推送到远程origin

$ git push -u origin master

技术分享图片

 

clone远程分支

  git clone

$ git clone [git地址]

git分支管理

 

git学习笔记

原文:https://www.cnblogs.com/LQBlog/p/10219959.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!