git初体验

参考网站:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

很久以前就想学习git的使用了,可是找了个繁体的教程看的我心都碎了,就放弃了,昨天加入豪哥的群,看到了这个网站,没想到还蛮好的,今天就小试了一下,感觉还不错~

 

1.安装git

恩,我的开发环境是 windows 7,下载的是网站上的国内镜像,直接默认安装。

image好啦~

 

2.文件时光机

总结了一下,有以下这些命令:

创建目录

$ mkdir learngit

显示当前目录路径

$ pwd

/Users/michael/learngit //路径

把这个目录变成Git可以管理的仓库

$ git init

Initialized empty Git repository in /Users/michael/learngit/.git/

把文件添加到仓库

$ git add readme.txt    //没有任何显示,说明添加成功

把添加到仓库的文件提交,-m后跟着本次提交的描述,和SVN很像

$ git commit -m "wrote a readme file"

查看仓库状态,显示被修改的文件

$ 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:   readme.txt

#

no changes added to commit (use "git add" and/or "git commit -a")

查看文件被修改的内容

$ git diff readme.txt

diff --git a/readme.txt b/readme.txt

index 46d49bf..9247db6 100644

--- a/readme.txt

+++ b/readme.txt

@@ -1,2 +1,2 @@

-Git is a version control system.

+Git is a distributed version control system.

Git is free software.

查看提交历史 显示从最近到最远的提交日志

$ git log

commit 3628164fb26d48395383f8f31179f24e0882e1e0 //git生成的版本号

Author: Michael Liao <askxuefeng@gmail.com>

Date:   Tue Aug 20 15:11:49 2013 +0800

    append GPL    //修改日志

查看提交历史 单行显示 从最近到最远的提交日志

$ git log --pretty=oneline

3628164fb26d48395383f8f31179f24e0882e1e0 append GPL

把当前版本回退到上一个版本

$ git reset --hard HEAD^ //HEAD表示当前版本

HEAD is now at ea34578 add distributed

指定回到未来的某个版本

$ git reset --hard 3628164    //3628164是commit id 写前几位就可以啦

HEAD is now at 3628164 append GPL

查看命令历史

$ git reflog

ea34578 HEAD@{0}: reset: moving to HEAD^

3628164 HEAD@{1}: commit: append GPL

ea34578 HEAD@{2}: commit: add distributed

cb926e7 HEAD@{3}: commit (initial): wrote a readme file

丢弃工作区的修改  --很重要,没有--,就变成了“切换到另一个分支”的命令

$ git checkout -- readme.txt

//readme.txt自修改后还没有被放到暂存区,撤销修改就回到和版本库一模一样的状态

//readme.txt已经添加到暂存区后,又作了修改,撤销修改就回到添加到暂存区后的状态

把暂存区的修改撤销掉(unstage),重新放回工作区,丢弃工作区的修改要使用上一个命令。

$ git reset HEAD readme.txt    //用HEAD时,表示最新的版本

Unstaged changes after reset:

M readme.txt

在文件管理器中删除文件

$ rm test.txt

要从版本库中删除该文件

$ git rm test.txt

rm 'test.txt'

$ git commit -m "remove test.txt"

[master d17efd8] remove test.txt

1 file changed, 1 deletion(-)

delete mode 100644 test.txt

把误删的文件恢复到最新版本

$ git checkout -- test.txt

3.github 远程仓库

一、创建SSH KEY

1、首先注册github账号。

2、创建SSH Key:

$ ssh-keygen -t rsa -C "87801036@qq.com"

然后一路回车,使用默认值即可。

3、然后会显示出来这两个SSH Key的存放位置,千万不要手贱移动它。

 

二、登陆github 打开setting --SSH KEY页面,点“Add SSH Key”,填上任意Title,在Key文本框里粘贴id_rsa.pub文件的内容,点“Add Key”即可。

image

 

三、添加远程仓库

1、登陆GitHub,然后,在右上角找到“Create a new repo”按钮,创建一个新的仓库。

image

2、我们根据GitHub的提示,在本地的learngit仓库下运行命令

$ git remote add origin git@github.com:veronica3101/learngit.git

3、把本地库的所有内容推送到远程库上

$ git push -u origin master

 

 

四、从远程库克隆

在github上已有的库,在clone or download里复制SSH KEY

$ git clone git@github.com:veronica3101/gitskills.git

loning into 'gitskills'...

remote: Counting objects: 3, done.

remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0

Receiving objects: 100% (3/3), done.

Checking connectivity... done.

然后就成功啦~啦啦啦~

4.我遇到的坑

1.

$ git remote add origin git@github.com:michaelliao/learngit.git

$ git push -u origin master

fatal: Could not read from remote repository.

出现这个提示有两种情况:

第一种: 我手贱把私钥和公钥文件夹移动了,移回去就好了。

第二种: 仓库里没有任何文件。添加一个然后add,commit以后就好啦。

 

2.

$ git remote add origin git@github.com:veronica3101/learngit1.git

fatal: remote origin already exists.

各种捣腾以后,就变成这样啦~

网友说这样解决:

$ git remote rm origin

亲测有效。

 

3. 和上面的一起发生的

$ git remote rm origin

error: Could not remove config section 'remote.origin'.

恩,有一个亲测有效的解决方法:

打开.git里的config文件,删除[remote "origin"]这一行就可以啦~

 

另外,有两个警告:

1.

$ git push -u origin master

The authenticity of host 'github.com (192.30.252.120)' can't be established.

RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.

Are you sure you want to continue connecting (yes/no)?

廖雪峰同志说:“这是因为Git使用SSH连接,而SSH连接在第一次验证GitHub服务器的Key时,需要你确认GitHub的Key的指纹信息是否真的来自GitHub的服务器,输入yes回车即可。”

2.

Warning: Permanently added 'github.com' (RSA) to the list of known hosts.

看着蛮吓人的,我还以为哪里不对了呢,不过廖雪峰同志告诉我“

Git会输出一个警告,告诉你已经把GitHub的Key添加到本机的一个信任列表里了。这个警告只会出现一次,后面的操作就不会有任何警告了。”

没办法,到目前为止,我觉得他说的对。

 

还有一个另外,哈哈~

知道了一个神奇的命令:

$ ssh -vvv git@github.com

会出来一大堆debug字样,目前还没有弄明白做什么用的,看起来好高端的样子。

 

恩恩,我写完啦~

 

这里附加三个说明,留给我自己哒~

1、石头哥一语惊醒梦中人,我决定全心全意的开始学习前端啦。

2、加入了豪情的跳板群,开启了我的新旅程。

3、这是一个高质量的群,人外有人,天外有天。

posted @ 2016-05-29 21:01  v以恒  阅读(156)  评论(0编辑  收藏  举报