本教程为学习笔记,github作为最受欢迎的资源库,不可不学!详细教程参见:廖雪峰的官方网站Git教程系列。准备花两篇幅搞定实战总结,闲言碎语少说,脚踏实地求真!
1,Git入门
Git是目前世界上最先进的分布式版本控制系统(没有之一)。
· 1)在Windows上安装Git,安装包详见:https://git-for-windows.github.io,安装成功后:
2)基本配置
配置邮箱和账户名:
Administrator@WIN-9S4D59CISAA MINGW64 ~(master)
$ git config --global user.name "zhangbc" Administrator@WIN-9S4D59CISAA MINGW64 ~(master)
$ git config --global user.email "zhangbochengcheng189@163.com"
创建版本库:版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改、删除,Git都能跟踪,以便任何时刻都可以追踪历史,或者在将来某个时刻可以“还原”。
Administrator@WIN-9S4D59CISAA MINGW64 ~(master)
$ cd F: Administrator@WIN-9S4D59CISAA MINGW64 /f
$ mkdir learngit Administrator@WIN-9S4D59CISAA MINGW64 /f
$ cd learngit/ Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit
$ pwd
/f/learngit
初始化:
Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit
$ git init
Initialized empty Git repository in F:/learngit/.git/ Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$
把文件添加到版本库:
使用Windows要特别注意:千万不要使用Windows自带的记事本编辑任何文本文件。
原因是:Microsoft开发记事本的团队使用了一个非常弱智的行为来保存UTF-8编码的文件,他们自作聪明地在每个文件开头添加了0xefbbbf(十六进制)的字符,你会遇到很多不可思议的问题,比如,网页第一行可能会显示一个“?”,明明正确的程序一编译就报语法错误,等等,都是由记事本的弱智行为带来的。
建议:使用Notepad++代替记事本,记得把Notepad++的默认编码设置为UTF-8 without BOM即可。
Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ git add readme.md Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ git commit -m "wrote a readme file."
[master (root-commit) a25e8e4] wrote a readme file.
1 file changed,42 insertions(+)
create mode 100644 readme.md
修改文件后,查看状态:
Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ 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.md
no changes added to commit (use "git add" and/or "git commit -a")
查看修改详情:
Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ git diff readme.md
diff --git a/readme.md b/readme.md
index 33a3876..46dd97d100644
--- a/readme.md
+++ b/readme.md
@@-40,3+40,6@@
git提交到github:
ssh-keygen -C ‘zhangbocheng189@163.com’-t rsa
回到GitHub个人首页,点击AccountSettings-> SSH PublicKeys->Add another public key。title 可以随便取名字,Key里面添加的内容为 id_rsa.pub 文件内所有的代码。然后点击Apply即可。
+
+Git is a distributed version control system.
+Git is free software.
\ No newline at end of file
查看日志:
Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ git log
commit 6fa7dc43a0aa553ae0a3cf9b9a6c9b2757a8e556
Author: zhangbc <zhangbochengcheng189@163.com>
Date: Sun Mar 5 23:21:18 2017 +0800
wrote a readme file.
commit a25e8e46a364a39e52a36cec1bb94bf58921fae4
Author: zhangbc <zhangbochengcheng189@163.com>
Date: Sun Mar 5 23:07:56 2017 +0800
wrote a readme file.
Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ git log --pretty=oneline
6fa7dc43a0aa553ae0a3cf9b9a6c9b2757a8e556 wrote a readme file.
a25e8e46a364a39e52a36cec1bb94bf58921fae4 wrote a readme file.
在Git中,用
HEAD
表示当前版本,也就是最新的提交3628164...882e1e0
(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^
,上上一个版本就是HEAD^^
,当然往上100个版本写100个^
比较容易数不过来,所以写成HEAD~100
。 恢复版本:
Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ git reset --hard HEAD^
HEAD is now at a25e8e4 wrote a readme file.
还原恢复操作:
Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ git reset --hard 6fa7dc43a0aa553ae0a3cf9b9a6c9b2757a8e556
HEAD is now at 6fa7dc4 wrote a readme file.
查看历史版本:
Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ git reflog
6fa7dc4 HEAD@{0}: reset: moving to 6fa7dc43a0aa553ae0a3cf9b9a6c9b2757a8e556
a25e8e4 HEAD@{1}: reset: moving to HEAD^
6fa7dc4 HEAD@{2}: commit: wrote a readme file.
a25e8e4 HEAD@{3}: commit (initial): wrote a readme file.
2,Git进阶
1)工作区和暂存区
工作区:即在电脑中能看到的目录,如leangit文件夹就是一个工作区。
版本库:工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库。
Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创建的第一个分支
master
,以及指向master
的一个指针叫HEAD
。 2)管理修改
每次修改,如果不
add
到暂存区,那就不会加入到commit
中。 撤销修改:
Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ git checkout -- readme.md
命令git checkout -- readme.md意思就是,把readme.md文件在工作区的修改全部撤销,这里有两种情况:
1)是readme.md自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;2)是readme.md已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。
删除修改:
(1)删错了,需要恢复:
Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ rm test.md Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ git status
On branch master
Changes 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: test.md no changes added to commit (use "git add" and/or "git commit -a") Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ git checkout -- test.md Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ git status
On branch master
nothing to commit, working tree clean
(2)彻底删除:
Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ rm test.md
Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ git status
On branch master
Changes 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: test.md no changes added to commit (use "git add" and/or "git commit -a") Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ git rm test.md
rm 'test.md' Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ git commit -m "remove test.md"
[master 1f8e8c7] remove test.md
1 file changed,45 deletions(-)
delete mode 100644 test.md Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ git status
On branch master
nothing to commit, working tree clean
3,远程仓库
1)配置github
(1)创建SSH Key:
Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ ssh-keygen -t rsa -C "zhangbocheng189@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): zhangbc
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in zhangbc.
Your public key has been saved in zhangbc.pub.
The key fingerprint is:
SHA256:ULusTjP9zWYbldd/snUjJIgG1Jwqqzu1QtT7m2I7zXQ zhangbocheng189@163.com
The key's randomart image is:
+---[RSA 2048]----+
| .o o |
| . = . |
| . .o . |
| . o ..o... ..|
|. + oS. . .o o|
| . + ..E o. ..|
|. o * * . ....=|
| + = *.o . oo..++|
| .=.+oo .o+.. |
+----[SHA256]-----+
(2)配置GitHub:
公钥地址:C:\Users\Administrator\.ssh\id_rsa.pub
2)添加远程库-->登陆Github,创建一个分库learngit
Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ git remote add origin git@github.com:zhangbc/learngit.git
Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ git push -u origin master
Permission denied (publickey).
fatal:Could not read from remote repository. Please make sure you have the correct access rights
and the repository exists.
调试:
Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ ssh -T -v git@github.com
OpenSSH_7.3p1,OpenSSL1.0.2k26Jan2017
debug1:Reading configuration data /etc/ssh/ssh_config
debug1:Connecting to github.com [192.30.253.113] port 22.
debug1:Connection established.
debug1: identity file /c/Users/Administrator/.ssh/id_rsa type 1
debug1: key_load_public:No such file or directory
debug1: identity file /c/Users/Administrator/.ssh/id_rsa-cert type -1
debug1: key_load_public:No such file or directory
debug1: identity file /c/Users/Administrator/.ssh/id_dsa type -1
debug1: key_load_public:No such file or directory
debug1: identity file /c/Users/Administrator/.ssh/id_dsa-cert type -1
debug1: key_load_public:No such file or directory
debug1: identity file /c/Users/Administrator/.ssh/id_ecdsa type -1
debug1: key_load_public:No such file or directory
debug1: identity file /c/Users/Administrator/.ssh/id_ecdsa-cert type -1
debug1: key_load_public:No such file or directory
debug1: identity file /c/Users/Administrator/.ssh/id_ed25519 type -1
debug1: key_load_public:No such file or directory
debug1: identity file /c/Users/Administrator/.ssh/id_ed25519-cert type -1
debug1:Enabling compatibility mode for protocol 2.0
debug1:Local version string SSH-2.0-OpenSSH_7.3
debug1:Remote protocol version 2.0, remote software version libssh-0.7.0
debug1: no match: libssh-0.7.0
debug1:Authenticating to github.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC:<implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC:<implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1:Server host key: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8
debug1:Host'github.com' is known and matches the RSA host key.
debug1:Found key in/c/Users/Administrator/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1:Authentications that can continue: publickey
debug1:Next authentication method: publickey
debug1:Offering RSA public key:/c/Users/Administrator/.ssh/id_rsa
debug1:Authentications that can continue: publickey
debug1:Trying private key:/c/Users/Administrator/.ssh/id_dsa
debug1:Trying private key:/c/Users/Administrator/.ssh/id_ecdsa
debug1:Trying private key:/c/Users/Administrator/.ssh/id_ed25519
debug1:No more authentication methods to try.
Permission denied (publickey).
错误原因:使用了不正确的公钥,在安装目录下寻找。
Administrator@WIN-9S4D59CISAA MINGW64 ~(master)
$ find -name *pub
./.android/adbkey.pub
./.ssh/id_rsa.pub
./AppData/Roaming/VanDyke/Config/KnownHosts/160.16.205.132[160.16.205.132]22.pub
查看远程库:
Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
$ git remote -v
origin git@github.com:zhangbc/learngit.git (fetch)
origin git@github.com:zhangbc/learngit.git (push)
删除远程库的文件:
Administrator@WIN-9S4D59CISAA MINGW64 /e/DataStructure/dataStructure (master)
$ git rm -f gitReadme.md
rm 'gitReadme.md' Administrator@WIN-9S4D59CISAA MINGW64 /e/DataStructure/dataStructure (master)
$ git commit -m "delete file Gitreadme.md"
[master 20828c2] delete file Gitreadme.md
1 file changed,4 deletions(-)
delete mode 100644 gitReadme.md Administrator@WIN-9S4D59CISAA MINGW64 /e/DataStructure/dataStructure (master)
$ git push origin master
Counting objects:2,done.
Delta compression using up to 4 threads.
Compressing objects:100%(2/2),done.
Writing objects:100%(2/2),236 bytes |0 bytes/s,done.
Total2(delta 1), reused 0(delta 0)
remote:Resolving deltas:100%(1/1), completed with 1local objects.
To github.com:zhangbc/dataStructure.git
c182956..20828c2 master -> master
删除远程库:
zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master)
$ git remote remove origin
3)从远程库克隆
zhangbc@working MINGW64 /d/BaiduYunDownload
$ git clone git@github.com:zhangbc/cnblogs_Scrapy.git
Cloning into 'cnblogs_Scrapy'...
remote:Counting objects:33,done.
remote:Compressing objects:100%(28/28),done.
remote:Total33(delta 3), reused 33(delta 3), pack-reused 0
Receiving objects:100%(33/33),34.74KiB|0 bytes/s,done.
Resolving deltas:100%(3/3),done.