git教程(一)

来源:互联网 发布:办公提醒软件 编辑:程序博客网 时间:2024/05/22 02:08

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/learngit11  $ pwd12  /f/learngit
复制代码
       初始化:
1  Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit2  $ git init3  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.md3 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 status3 On branch master4 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.md8 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 rsa10 回到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 +080010     wrote a readme file.
复制代码
1 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)2 $ git log --pretty=oneline3 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 6fa7dc43a0aa553ae0a3cf9b9a6c9b2757a8e5563 HEAD is now at 6fa7dc4 wrote a readme file.
       查看历史版本:
1 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)2 $ git reflog3 6fa7dc4 HEAD@{0}: reset: moving to 6fa7dc43a0aa553ae0a3cf9b9a6c9b2757a8e5564 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.md12 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.md17 18 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)19 $ git status20 On branch master21 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.md11 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.md16 rm 'test.md'17 18 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)19 $ git commit -m "remove test.md"20 [master 1f8e8c7] remove test.md21 1 file changed,45 deletions(-)22 delete mode 100644 test.md23 24 Administrator@WIN-9S4D59CISAA MINGW64 /f/learngit (master)25 $ git status26 On branch master27 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.com11 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 master3 Permission denied (publickey).4 fatal:Could not read from remote repository.5 6 Please make sure you have the correct access rights7 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 -110 debug1: key_load_public:No such file or directory11 debug1: identity file /c/Users/Administrator/.ssh/id_dsa type -112 debug1: key_load_public:No such file or directory13 debug1: identity file /c/Users/Administrator/.ssh/id_dsa-cert type -114 debug1: key_load_public:No such file or directory15 debug1: identity file /c/Users/Administrator/.ssh/id_ecdsa type -116 debug1: key_load_public:No such file or directory17 debug1: identity file /c/Users/Administrator/.ssh/id_ecdsa-cert type -118 debug1: key_load_public:No such file or directory19 debug1: identity file /c/Users/Administrator/.ssh/id_ed25519 type -120 debug1: key_load_public:No such file or directory21 debug1: identity file /c/Users/Administrator/.ssh/id_ed25519-cert type -122 debug1:Enabling compatibility mode for protocol 2.023 debug1:Local version string SSH-2.0-OpenSSH_7.324 debug1:Remote protocol version 2.0, remote software version libssh-0.7.025 debug1: no match: libssh-0.7.026 debug1:Authenticating to github.com:22 as 'git'27 debug1: SSH2_MSG_KEXINIT sent28 debug1: SSH2_MSG_KEXINIT received29 debug1: kex: algorithm: curve25519-sha256@libssh.org30 debug1: kex: host key algorithm: ssh-rsa31 debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC:<implicit> compression: none32 debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC:<implicit> compression: none33 debug1: expecting SSH2_MSG_KEX_ECDH_REPLY34 debug1:Server host key: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY835 debug1:Host'github.com' is known and matches the RSA host key.36 debug1:Found key in/c/Users/Administrator/.ssh/known_hosts:137 debug1: rekey after 134217728 blocks38 debug1: SSH2_MSG_NEWKEYS sent39 debug1: expecting SSH2_MSG_NEWKEYS40 debug1: rekey after 134217728 blocks41 debug1: SSH2_MSG_NEWKEYS received42 debug1: SSH2_MSG_SERVICE_ACCEPT received43 debug1:Authentications that can continue: publickey44 debug1:Next authentication method: publickey45 debug1:Offering RSA public key:/c/Users/Administrator/.ssh/id_rsa46 debug1:Authentications that can continue: publickey47 debug1:Trying private key:/c/Users/Administrator/.ssh/id_dsa48 debug1:Trying private key:/c/Users/Administrator/.ssh/id_ecdsa49 debug1:Trying private key:/c/Users/Administrator/.ssh/id_ed2551950 debug1:No more authentication methods to try.51 Permission denied (publickey).
复制代码
错误原因:使用了不正确的公钥,在安装目录下寻找。
1 Administrator@WIN-9S4D59CISAA MINGW64 ~(master)2 $ find -name *pub3 ./.android/adbkey.pub4 ./.ssh/id_rsa.pub5 ./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 -v3 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.md10 11 Administrator@WIN-9S4D59CISAA MINGW64 /e/DataStructure/dataStructure (master)12 $ git push origin master13 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.git20 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.gitCloning 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 0Receiving objects:100%(33/33),34.74KiB|0 bytes/s,done.Resolving deltas:100%(3/3),done.


作者: zhangbc 
出处: http://www.cnblogs.com/zhangbc/ 

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。


0 0
原创粉丝点击