Git基础教程

Posted 天堂的鸽子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Git基础教程相关的知识,希望对你有一定的参考价值。

本教程为学习笔记,github作为最受欢迎的资源库,不可不学!详细教程参见:廖雪峰的官方网站Git教程系列。准备花两篇幅搞定实战总结,闲言碎语少说,脚踏实地求真!

1,Git入门

      Git是目前世界上最先进的分布式版本控制系统(没有之一)。
 ·    1)在Windows上安装Git,安装包详见:https://git-for-windows.github.io安装成功后:

    2)基本配置
        配置邮箱和账户名:
1 Administrator@WIN-9S4D59CISAA MINGW64 ~(master)
2 $ git config --global user.name "zhangbc"
3 
4 Administrator@WIN-9S4D59CISAA MINGW64 ~(master)
5 $ git config --global user.email "zhangbochengcheng189@163.com"
        创建版本库:版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改、删除,Git都能跟踪,以便任何时刻都可以追踪历史,或者在将来某个时刻可以“还原”。
 1  Administrator@WIN-9S4D59CISAA MINGW64 ~(master)
 2  $ cd F:
 3 
 4  Administrator@WIN-9S4D59CISAA MINGW64 /f
 5  $ mkdir learngit
 6 
 7  Administrator@WIN-9S4D59CISAA MINGW64 /f
 8  $ cd learngit/
 9 
10  Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit
11  $ pwd
12  /f/learngit
       初始化:
1  Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit
2  $ git init
3  Initialized empty Git repository in F:/learngit/.git/
4  
5  Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
6  $
 
     把文件添加到版本库:
     使用Windows要特别注意:千万不要使用Windows自带的记事本编辑任何文本文件。
原因是:Microsoft开发记事本的团队使用了一个非常弱智的行为来保存UTF-8编码的文件,他们自作聪明地在每个文件开头添加了0xefbbbf(十六进制)的字符,你会遇到很多不可思议的问题,比如,网页第一行可能会显示一个“?”,明明正确的程序一编译就报语法错误,等等,都是由记事本的弱智行为带来的。
建议:使用Notepad++代替记事本,记得把Notepad++的默认编码设置为UTF-8 without BOM即可。
1 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
2 $ git add readme.md
3 
4 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
5 $ git commit -m "wrote a readme file."
6 [master (root-commit) a25e8e4] wrote a readme file.
7 1 file changed,42 insertions(+)
8 create mode 100644 readme.md
     修改文件后,查看状态:
1 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
2 $ git status
3 On branch master
4 Changes not staged for commit:
5 (use "git add <file>..." to update what will be committed)
6 (use "git checkout -- <file>..." to discard changes in working directory)
7 modified: readme.md
8 no changes added to commit (use "git add" and/or "git commit -a")
      查看修改详情:
 1 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
 2 $ git diff readme.md
 3 diff --git a/readme.md b/readme.md
 4 index 33a3876..46dd97d100644
 5 --- a/readme.md
 6 +++ b/readme.md
 7 @@-40,3+40,6@@
 8 git提交到github:
 9 ssh-keygen -C ‘zhangbocheng189@163.com’-t rsa
10 回到GitHub个人首页,点击AccountSettings-> SSH PublicKeys->Add another public key。title 可以随便取名字,Key里面添加的内容为 id_rsa.pub 文件内所有的代码。然后点击Apply即可。
11 +
12 +Git is a distributed version control system.
13 +Git is free software.
14 \\ No newline at end of file
    查看日志:
 1 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
 2 $ git log
 3 commit 6fa7dc43a0aa553ae0a3cf9b9a6c9b2757a8e556
 4 Author: zhangbc <zhangbochengcheng189@163.com>
 5 Date:   Sun Mar 5 23:21:18 2017 +0800
 6     wrote a readme file.
 7 commit a25e8e46a364a39e52a36cec1bb94bf58921fae4
 8 Author: zhangbc <zhangbochengcheng189@163.com>
 9 Date:   Sun Mar 5 23:07:56 2017 +0800
10     wrote a readme file.
1 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
2 $ git log --pretty=oneline
3 6fa7dc43a0aa553ae0a3cf9b9a6c9b2757a8e556 wrote a readme file.
4 a25e8e46a364a39e52a36cec1bb94bf58921fae4 wrote a readme file.
       在Git中,用HEAD表示当前版本,也就是最新的提交3628164...882e1e0(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100
        恢复版本:
1 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
2 $ git reset --hard HEAD^
3 HEAD is now at a25e8e4 wrote a readme file.
       还原恢复操作:
1 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
2 $ git reset --hard 6fa7dc43a0aa553ae0a3cf9b9a6c9b2757a8e556
3 HEAD is now at 6fa7dc4 wrote a readme file.
       查看历史版本:
1 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
2 $ git reflog
3 6fa7dc4 HEAD@{0}: reset: moving to 6fa7dc43a0aa553ae0a3cf9b9a6c9b2757a8e556
4 a25e8e4 HEAD@{1}: reset: moving to HEAD^
5 6fa7dc4 HEAD@{2}: commit: wrote a readme file.
6 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中。
         撤销修改:
1 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
2 $ git checkout -- readme.md
     命令git checkout -- readme.md意思就是,把readme.md文件在工作区的修改全部撤销,这里有两种情况:
1)是readme.md自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
2)是readme.md已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。
        删除修改:
        (1)删错了,需要恢复:
 1 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
 2 $ rm test.md
 3 
 4 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
 5 $ git status
 6 On branch master
 7 Changes not staged for commit:
 8   (use "git add/rm <file>..." to update what will be committed)
 9   (use "git checkout -- <file>..." to discard changes in working directory)
10 
11         deleted:    test.md
12 
13 no changes added to commit (use "git add" and/or "git commit -a")
14 
15 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
16 $ git checkout -- test.md
17 
18 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
19 $ git status
20 On branch master
21 nothing to commit, working tree clean
        (2)彻底删除:
 1 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
 2 $ rm test.md
 3 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
 4 $ git status
 5 On branch master
 6 Changes not staged for commit:
 7     (use "git add/rm <file>..." to update what will be committed)
 8     (use "git checkout -- <file>..." to discard changes in working directory)
 9 
10 deleted: test.md
11 
12 no changes added to commit (use "git add" and/or "git commit -a")
13 
14 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
15 $ git rm test.md
16 rm \'test.md\'
17 
18 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
19 $ git commit -m "remove test.md"
20 [master 1f8e8c7] remove test.md
21 1 file changed,45 deletions(-)
22 delete mode 100644 test.md
23 
24 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
25 $ git status
26 On branch master
27 nothing to commit, working tree clean

 

3,远程仓库
    1)配置github
        (1)创建SSH Key:
 1 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
 2 $ ssh-keygen -t rsa -C "zhangbocheng189@163.com"
 3 Generating public/private rsa key pair.
 4 Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): zhangbc
 5 Enter passphrase (empty for no passphrase):
 6 Enter same passphrase again:
 7 Your identification has been saved in zhangbc.
 8 Your public key has been saved in zhangbc.pub.
 9 The key fingerprint is:
10 SHA256:ULusTjP9zWYbldd/snUjJIgG1Jwqqzu1QtT7m2I7zXQ zhangbocheng189@163.com
11 The key\'s randomart image is:
12 +---[RSA 2048]----+
13 | .o o |
14 | . = . |
15 | . .o . |
16 | . o ..o... ..|
17 |. + oS. . .o o|
18 | . + ..E o. ..|
19 |. o * * . ....=|
20 | + = *.o . oo..++|
21 | .=.+oo .o+.. |
22 +----[SHA256]-----+
        (2)配置GitHub:

公钥地址:C:\\Users\\Administrator\\.ssh\\id_rsa.pub
     2)添加远程库-->登陆Github,创建一个分库learngit
1 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
2 $ git remote add origin git@github.com:zhangbc/learngit.git
1 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
2 $ git push -u origin master
3 Permission denied (publickey).
4 fatal:Could not read from remote repository.
5 
6 Please make sure you have the correct access rights
7 and the repository exists.
调试:
 1 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
 2 $ ssh -T -v git@github.com
 3 OpenSSH_7.3p1,OpenSSL1.0.2k26Jan2017
 4 debug1:Reading configuration data /etc/ssh/ssh_config
 5 debug1:Connecting to github.com [192.30.253.113] port 22.
 6 debug1:Connection established.
 7 debug1: identity file /c/Users/Administrator/.ssh/id_rsa type 1
 8 debug1: key_load_public:No such file or directory
 9 debug1: identity file /c/Users/Administrator/.ssh/id_rsa-cert type -1
10 debug1: key_load_public:No such file or directory
11 debug1: identity file /c/Users/Administrator/.ssh/id_dsa type -1
12 debug1: key_load_public:No such file or directory
13 debug1: identity file /c/Users/Administrator/.ssh/id_dsa-cert type -1
14 debug1: key_load_public:No such file or directory
15 debug1: identity file /c/Users/Administrator/.ssh/id_ecdsa type -1
16 debug1: key_load_public:No such file or directory
17 debug1: identity file /c/Users/Administrator/.ssh/id_ecdsa-cert type -1
18 debug1: key_load_public:No such file or directory
19 debug1: identity file /c/Users/Administrator/.ssh/id_ed25519 type -1
20 debug1: key_load_public:No such file or directory
21 debug1: identity file /c/Users/Administrator/.ssh/id_ed25519-cert type -1
22 debug1:Enabling compatibility mode for protocol 2.0
23 debug1:Local version string SSH-2.0-OpenSSH_7.3
24 debug1:Remote protocol version 2.0, remote software version libssh-0.7.0
25 debug1: no match: libssh-0.7.0
26 debug1:Authenticating to github.com:22 as \'git\'
27 debug1: SSH2_MSG_KEXINIT sent
28 debug1: SSH2_MSG_KEXINIT received
29 debug1: kex: algorithm: curve25519-sha256@libssh.org
30 debug1: kex: host key algorithm: ssh-rsa
31 debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC:<implicit> compression: none
32 debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC:<implicit> compression: none
33 debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
34 debug1:Server host key: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8
35 debug1:Host\'github.com\' is known and matches the RSA host key.
36 debug1:Found key in/c/Users/Administrator/.ssh/known_hosts:1
37 debug1: rekey after 134217728 blocks
38 debug1: SSH2_MSG_NEWKEYS sent
39 debug1: expecting SSH2_MSG_NEWKEYS
40 debug1: rekey after 134217728 blocks
41 debug1: SSH2_MSG_NEWKEYS received
42 debug1: SSH2_MSG_SERVICE_ACCEPT received
43 debug1:Authentications that can continue: publickey
44 debug1:Next authentication method: publickey
45 debug1:Offering RSA public key:/c/Users/Administrator/.ssh/id_rsa
46 debug1:Authentications that can continue: publickey
47 debug1:Trying private key:/c/Users/Administrator/.ssh/id_dsa
48 debug1:Trying private key:/c/Users/Administrator/.ssh/id_ecdsa
49 debug1:Trying private key:/c/Users/Administrator/.ssh/id_ed25519
50 debug1:No more authentication methods to try.
51 Permission denied (publickey).
错误原因:使用了不正确的公钥,在安装目录下寻找。
1 Administrator@WIN-9S4D59CISAA MINGW64 ~(master)
2 $ find -name *pub
3 ./.android/adbkey.pub
4 ./.ssh/id_rsa.pub
5 ./AppData/Roaming/VanDyke/Config/KnownHosts/160.16.205.132[160.16.205.132]22.pub

 

 
查看远程库:
1 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)
2 $ git remote -v
3 origin git@github.com:zhangbc/learngit.git (fetch)
4 origin git@github.com:zhangbc/learngit.git (push)
删除远程库的文件:
 1 Administrator@WIN-9S4D59CISAA MINGW64 /e/DataStructure/dataStructure (master)
 2 $ git rm -f gitReadme.md
 3 rm \'gitReadme.md\'
 4 
 5 Administrator@WIN-9S4D59CISAA MINGW64 /e/DataStructure/dataStructure (master)
 6 $ git commit -m "delete file Gitreadme.md"
 7 [master 20828c2] delete file Gitreadme.md
 8 1 file changed,4 deletions(-)
 9 delete mode 100644 gitReadme.md
10 
11 Administrator@WIN-9S4D59CISAA MINGW64 /e/DataStructure/dataStructure (master)
12 $ git push origin master
13 Counting objects:2,done.
14 Delta compression using up to 4 threads.
15 Compressing objects:100%(2/2),done.
16 Writing objects:100%(2/2),236 bytes |0 bytes/s,done.
17 Total2(delta 1), reused 0(delta 0)
18 remote:Resolving deltas:100%(1/1), completed with 1local objects.
19 To github.com:zhangbc/dataStructure.git
20 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.

 

 



以上是关于Git基础教程的主要内容,如果未能解决你的问题,请参考以下文章

git基础教程(27)git commit --amend撤销方法

VSCode自定义代码片段——git命令操作一个完整流程

git基础教程 Git 简史

git基础教程(26)git rebase -i合并多个提交

VSCode自定义代码片段15——git命令操作一个完整流程

VSCode自定义代码片段15——git命令操作一个完整流程