git是分布式的仓库,我们不需要把代码上传或更新到某个特定的服务器上,所以它不需要依赖网络,我们可以在本地创建一个git仓库。

安装命令:

yum install -y git

创建git仓库:

  1. [root@localhost ~]# mkdir /data/gitroot
  2. [root@localhost ~]# cd /data/gitroot
  3. [root@localhost /data/gitroot]# git init
  4. 初始化空的 Git 版本库于 /data/gitroot/.git/
  5. [root@localhost /data/gitroot]# ll -a
  6. 总用量 0
  7. drwxr-xr-x 3 root root 17 1月 12 18:38 .
  8. drwxr-xr-x 11 root root 144 1月 12 18:38 ..
  9. drwxr-xr-x 7 root root 111 1月 12 18:38 .git # 会生成一个.git目录
  10. [root@localhost /data/gitroot]#

创建一个新的文件,然后随便写些东西:

  1. [root@localhost /data/gitroot]# vim Hello.java
  2. class Hello{
  3. public static void main(String[] s){
  4. System.out.println("Hello World!");
  5. }
  6. }

把刚刚创建的文件添加到git仓库里,然后进行上传:

  1. [root@localhost /data/gitroot]# git add Hello.java
  2. [root@localhost /data/gitroot]# git commit -m "add new file Hello.java"
  3. [master(根提交) 1387815] add new file Hello.java
  4. Committer: root <root@localhost.localdomain>
  5. 您的姓名和邮件地址基于登录名和主机名进行了自动设置。请检查它们正确 # 如果你没有设置姓名和邮件地址就会有这段提示
  6. 与否。您可以通过下面的命令对其进行明确地设置以免再出现本提示信息:
  7. git config --global user.name "Your Name"
  8. git config --global user.email you@example.com
  9. 设置完毕后,您可以用下面的命令来修正本次提交所使用的用户身份:
  10. git commit --amend --reset-author
  11. 1 file changed, 9 insertions(+)
  12. create mode 100644 Hello.java
  13. # 为了避免老是打印提示信息,可以随便设置一下这两项信息
  14. [root@localhost /data/gitroot]# git config --global user.name "zero"
  15. [root@localhost /data/gitroot]# git config --global user.email none@none.com

接着再次修改文件中的内容,然后进行提交:

  1. [root@localhost /data/gitroot]# echo "class hello{}" >> Hello.java
  2. [root@localhost /data/gitroot]# git add Hello.java
  3. [root@localhost /data/gitroot]# git commit -m "add Hello.java agin"
  4. [master 8d77f14] add Hello.java agin
  5. 1 file changed, 1 insertion(+), 1 deletion(-)
  6. [root@localhost /data/gitroot]#

git status命令可以查看当前仓库中的状态,比如是否有改动的文件等:

  1. [root@localhost /data/gitroot]# git status
  2. # 位于分支 master
  3. 无文件要提交,干净的工作区
  4. [root@localhost /data/gitroot]#

git diff命令可以对比某个文件本次修改了什么内容,相比较仓库里面的版本:

  1. [root@localhost /data/gitroot]# echo "class emm{}" >> Hello.java
  2. [root@localhost /data/gitroot]# git diff Hello.java
  3. diff --git a/Hello.java b/Hello.java
  4. index 2935899..2bac836 100644
  5. --- a/Hello.java
  6. +++ b/Hello.java
  7. @@ -8,3 +8,4 @@ class Hello{
  8. }
  9. class hello{}
  10. +class emm{} # 该文件相比较仓库里面的版本多了这行代码
  11. [root@localhost /data/gitroot]#

我们都知道,代码管理仓库最重要的一个功能就是版本控制,通过版本控制,可以进行版本的回退操作:

  1. # 多更改几次Hello.java,然后add,commit
  2. [root@localhost /data/gitroot]# git add Hello.java
  3. [root@localhost /data/gitroot]# git commit -m "ch Hello.java agin"
  4. [master d1cf481] ch Hello.java agin
  5. 1 file changed, 1 insertion(+)
  6. [root@localhost /data/gitroot]# echo "class Hi{}" >> Hello.java
  7. [root@localhost /data/gitroot]# git add Hello.java; git commit -m "ch Hello.java agin"
  8. [master 5341f93] ch Hello.java agin
  9. 1 file changed, 1 insertion(+)
  10. [root@localhost /data/gitroot]# git log # 查看所有的提交记录
  11. commit 5341f93bef4f6d216b48d9cf6acb3a1f7dac7f0c # 这个是该版本的id,进行回退操作时需要使用
  12. Author: zero <none@none.com>
  13. Date: Fri Jan 12 18:59:51 2018 +0800
  14. ch Hello.java agin
  15. commit d1cf48198534e3bd1a7764ce27667f756f4974b5
  16. Author: zero <none@none.com>
  17. Date: Fri Jan 12 18:59:12 2018 +0800
  18. ch Hello.java agin
  19. commit 8d77f141ba84dae557ab42cd9a110c2542e06643
  20. Author: zero <none@none.com>
  21. Date: Fri Jan 12 18:50:07 2018 +0800
  22. add Hello.java agin
  23. commit b576e395c1197a5dc0aa72e584bb54ef9ab66458
  24. Author: root <root@localhost.localdomain>
  25. Date: Fri Jan 12 18:47:52 2018 +0800
  26. add Hello.java agin
  27. commit 1387815eb4f0eeb58966d89d7756a0ac45c3dde8
  28. Author: root <root@localhost.localdomain>
  29. Date: Fri Jan 12 18:44:06 2018 +0800
  30. :
  31. [root@localhost /data/gitroot]# git log --pretty=oneline # 一行显示
  32. 5341f93bef4f6d216b48d9cf6acb3a1f7dac7f0c ch Hello.java agin
  33. d1cf48198534e3bd1a7764ce27667f756f4974b5 ch Hello.java agin
  34. 8d77f141ba84dae557ab42cd9a110c2542e06643 add Hello.java agin
  35. b576e395c1197a5dc0aa72e584bb54ef9ab66458 add Hello.java agin
  36. 1387815eb4f0eeb58966d89d7756a0ac45c3dde8 add new file Hello.java
  37. [root@localhost /data/gitroot]# git reset --hard b576e395c1197a5dc0aa72e584bb54ef9ab66458 # 回退版本
  38. HEAD 现在位于 b576e39 add Hello.java agin
  39. [root@localhost /data/gitroot]# git log --pretty=oneline # 可以看到已经回退到第二个版本了
  40. b576e395c1197a5dc0aa72e584bb54ef9ab66458 add Hello.java agin
  41. 1387815eb4f0eeb58966d89d7756a0ac45c3dde8 add new file Hello.java
  42. [root@localhost /data/gitroot]# git reset --hard 1387815eb # 这个版本ID是可以简写的,取前面的几个字符即可
  43. HEAD 现在位于 1387815 add new file Hello.java
  44. [root@localhost /data/gitroot]# git log --pretty=oneline
  45. 1387815eb4f0eeb58966d89d7756a0ac45c3dde8 add new file Hello.java
  46. [root@localhost /data/gitroot]#

如果回退版本后,发现不合适,想要回退到新版本或者其他历史版本上,可以使用git reflog命令查看所有历史版本:

  1. [root@localhost /data/gitroot]# git reflog # 查看所有历史版本
  2. 1387815 HEAD@{0}: reset: moving to 1387815eb
  3. b576e39 HEAD@{1}: reset: moving to b576e395c1197a5dc0aa72e584bb54ef9ab66458
  4. 5341f93 HEAD@{2}: commit: ch Hello.java agin
  5. d1cf481 HEAD@{3}: commit: ch Hello.java agin
  6. 8d77f14 HEAD@{4}: commit: add Hello.java agin
  7. b576e39 HEAD@{5}: commit: add Hello.java agin
  8. 1387815 HEAD@{6}: commit (initial): add new file Hello.java
  9. [root@localhost /data/gitroot]# git reset --hard 5341f93 # 通过id进行回退
  10. HEAD 现在位于 5341f93 ch Hello.java agin
  11. [root@localhost /data/gitroot]# git log --pretty=oneline # 回退到最新版本了
  12. 5341f93bef4f6d216b48d9cf6acb3a1f7dac7f0c ch Hello.java agin
  13. d1cf48198534e3bd1a7764ce27667f756f4974b5 ch Hello.java agin
  14. 8d77f141ba84dae557ab42cd9a110c2542e06643 add Hello.java agin
  15. b576e395c1197a5dc0aa72e584bb54ef9ab66458 add Hello.java agin
  16. 1387815eb4f0eeb58966d89d7756a0ac45c3dde8 add new file Hello.java
  17. [root@localhost /data/gitroot]#

通过git可以恢复删除的文件,前提是你已经将文件提交到了仓库中。如果不小心把某个文件删除了,而这个文件已经存储在仓库中的话,就可以从仓库恢复这个文件:

  1. [root@localhost /data/gitroot]# rm -f Hello.java
  2. [root@localhost /data/gitroot]# ls
  3. [root@localhost /data/gitroot]# git checkout -- Hello.java # 因为文件已经存储到仓库里了,所以可以从仓库恢复
  4. [root@localhost /data/gitroot]# ls
  5. Hello.java
  6. [root@localhost /data/gitroot]#

如果某个文件进行了修改,add后但没有commit,再想回退到上一次提交的状态,可以使用git reset HEAD filename,再执行git checkout -- filename:

  1. [root@localhost /data/gitroot]# echo "class Car{}" >> Hello.java
  2. [root@localhost /data/gitroot]# git add Hello.java
  3. [root@localhost /data/gitroot]# git reset HEAD Hello.java # 这个命令可以把add标记去掉
  4. 重置后撤出暂存区的变更:
  5. M Hello.java
  6. [root@localhost /data/gitroot]# git checkout -- Hello.java
  7. [root@localhost /data/gitroot]# cat !$
  8. cat Hello.java
  9. class Hello{
  10. public static void main(String[] s){
  11. System.out.println("Hello World!");
  12. }
  13. }
  14. class hello{ }
  15. class emm{}
  16. class Hi{}
  17. [root@localhost /data/gitroot]#

删除仓库中的文件:

  1. [root@localhost /data/gitroot]# git rm Hello.java # 删除仓库中的文件
  2. rm 'Hello.java'
  3. [root@localhost /data/gitroot]# ls
  4. [root@localhost /data/gitroot]# git commit -m "delete Hello.java" # 提交删除操作
  5. [master 86da43d] delete Hello.java
  6. 1 file changed, 12 deletions(-)
  7. delete mode 100644 Hello.java
  8. [root@localhost /data/gitroot]# git checkout -- Hello.java # 这时候就无法从仓库中检出该文件了
  9. error: pathspec 'Hello.java' did not match any file(s) known to git.
  10. [root@localhost /data/gitroot]#

即便删除了仓库中的文件,也是可以通过版本id来恢复的:

  1. [root@localhost /data/gitroot]# git log --pretty=oneline
  2. 86da43d5b2f68985d376f297fc670d16fd473884 delete Hello.java
  3. 5341f93bef4f6d216b48d9cf6acb3a1f7dac7f0c ch Hello.java agin
  4. d1cf48198534e3bd1a7764ce27667f756f4974b5 ch Hello.java agin
  5. 8d77f141ba84dae557ab42cd9a110c2542e06643 add Hello.java agin
  6. b576e395c1197a5dc0aa72e584bb54ef9ab66458 add Hello.java agin
  7. 1387815eb4f0eeb58966d89d7756a0ac45c3dde8 add new file Hello.java
  8. [root@localhost /data/gitroot]# git reset --hard 5341f93
  9. HEAD 现在位于 5341f93 ch Hello.java agin
  10. [root@localhost /data/gitroot]# ls
  11. Hello.java
  12. [root@localhost /data/gitroot]# cat Hello.java
  13. class Hello{
  14. public static void main(String[] s){
  15. System.out.println("Hello World!");
  16. }
  17. }
  18. class hello{ }
  19. class emm{}
  20. class Hi{}
  21. [root@localhost /data/gitroot]#

22.7 建立远程仓库

以上的示例都是在本地使用git仓库,没有涉及到远程仓库的使用。下面演示一下如何连接远程的GitHub仓库:

1.首先到 https://github.com 注册一个账号,我这里已经有账户了,所以直接登录:
git仓库的简单使用

2.登录之后,点击右上角,头像旁边的 + 图标,创建一个自己的repository(仓库):
git仓库的简单使用

3.填写仓库的相关信息:
git仓库的简单使用

4.创建完成,如下,远程仓库就创建好了:
git仓库的简单使用

可以把GitHub上创建的仓库,作为我们的远程服务端。

5.在本地机器上创建密钥对:

  1. [root@localhost ~]# ssh-keygen
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa):
  4. Enter passphrase (empty for no passphrase): # 密码
  5. Enter same passphrase again: # 确认密码
  6. Your identification has been saved in /root/.ssh/id_rsa.
  7. Your public key has been saved in /root/.ssh/id_rsa.pub.
  8. The key fingerprint is:
  9. 0e:17:93:c8:8c:9a:d9:b4:21:6e:72:68:41:fa:79:0a root@localhost.localdomain
  10. The key's randomart image is:
  11. +--[ RSA 2048]----+
  12. | . |
  13. |o + . . |
  14. |... + + + |
  15. | +.O o o |
  16. |E.X + . S |
  17. |.= o + |
  18. | . . |
  19. | |
  20. | |
  21. +-----------------+
  22. [root@localhost ~]# cat .ssh/id_rsa.pub # 复制你的公钥

6.然后给远程的仓库添加密钥认证,保证访问的安全性:
git仓库的简单使用
git仓库的简单使用
git仓库的简单使用

添加完成:
git仓库的简单使用


连接远程仓库

以上已经在GitHub上创建好了一个远程仓库,并且也添加了密钥认证,现在我们就可以在本地上连接这个仓库了。

1.创建一个目录,用于存放和上传仓库文件,也相当于是一个本地仓库:

  1. [root@localhost ~]# mkdir /tmp/example
  2. [root@localhost ~]# cd !$
  3. cd /tmp/example
  4. [root@localhost /tmp/example]#

2.根据GitHub的操作示例进行仓库的初始化:

  1. [root@localhost /tmp/example]# echo "# example" >> README.md # 生成README.md文件
  2. [root@localhost /tmp/example]# git init # 初始化
  3. 初始化空的 Git 版本库于 /tmp/example/.git/
  4. [root@localhost /tmp/example]# ll -a
  5. 总用量 8
  6. drwxr-xr-x 3 root root 33 1月 12 23:17 .
  7. drwxrwxrwt. 9 root root 4096 1月 12 23:12 ..
  8. drwxr-xr-x 7 root root 111 1月 12 23:17 .git
  9. -rw-r--r-- 1 root root 10 1月 12 23:16 README.md
  10. [root@localhost /tmp/example]# git add README.md
  11. [root@localhost /tmp/example]# git commit -m "first commit"
  12. [master(根提交) 4b710bc] first commit
  13. 1 file changed, 1 insertion(+)
  14. create mode 100644 README.md
  15. ## 将本地文件推送到远程仓库上
  16. [root@localhost /tmp/example]# git remote add origin https://github.com/Binary-ZeroOne/example.git
  17. [root@localhost /tmp/example]# git push -u origin master
  18. Username for 'https://github.com': Binary-ZeroOne # 你的github用户名
  19. Password for 'https://Binary-ZeroOne@github.com': # 以及密码
  20. Counting objects: 3, done.
  21. Writing objects: 100% (3/3), 213 bytes | 0 bytes/s, done.
  22. Total 3 (delta 0), reused 0 (delta 0)
  23. To https://github.com/Binary-ZeroOne/example.git
  24. * [new branch] master -> master
  25. 分支 master 设置为跟踪来自 origin 的远程分支 master。
  26. [root@localhost /tmp/example]#

然后再创建一个文件,再次进行推送:

  1. [root@localhost /tmp/example]# vim example.txt
  2. This is a example
  3. [root@localhost /tmp/example]# git add example.txt
  4. [root@localhost /tmp/example]# git commit -m "example commit"
  5. [master aacb77a] example commit
  6. 1 file changed, 1 insertion(+)
  7. create mode 100644 example.txt
  8. [root@localhost /tmp/example]# git push
  9. warning: push.default 未设置,它的默认值将会在 Git 2.0 由 'matching' # 以下是新版本的一些提示信息
  10. 修改为 'simple'。若要不再显示本信息并在其默认值改变后维持当前使用习惯,
  11. 进行如下设置:
  12. git config --global push.default matching
  13. 若要不再显示本信息并从现在开始采用新的使用习惯,设置:
  14. git config --global push.default simple
  15. 参见 'git help config' 并查找 'push.default' 以获取更多信息。
  16. 'simple' 模式由 Git 1.7.11 版本引入。如果您有时要使用老版本的 Git,
  17. 为保持兼容,请用 'current' 代替 'simple' 模式)
  18. Username for 'https://github.com': Binary-ZeroOne
  19. Password for 'https://Binary-ZeroOne@github.com':
  20. Counting objects: 4, done.
  21. Delta compression using up to 4 threads.
  22. Compressing objects: 100% (2/2), done.
  23. Writing objects: 100% (3/3), 287 bytes | 0 bytes/s, done.
  24. Total 3 (delta 0), reused 0 (delta 0)
  25. To https://github.com/Binary-ZeroOne/example.git
  26. 4b710bc..aacb77a master -> master
  27. [root@localhost /tmp/example]#

接着到GitHub的仓库上,可以发现多了两个文件,README.md 和 example.txt:
git仓库的简单使用
git仓库的简单使用
git仓库的简单使用


22.8 克隆远程仓库

以上演示了将本地文件推送到远程仓库,我们也可以将远程仓库给克隆到本地机器上。

1.复制远程仓库的URL链接:
git仓库的简单使用

2.然后到本地机器上执行命令进行克隆:

  1. [root@localhost ~]# cd /home/
  2. [root@localhost /home]# git clone https://github.com/Binary-ZeroOne/example.git # 克隆的命令
  3. 正克隆到 'example'...
  4. remote: Counting objects: 6, done.
  5. remote: Compressing objects: 100% (3/3), done.
  6. remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
  7. Unpacking objects: 100% (6/6), done.
  8. [root@localhost /home]# ls
  9. example
  10. [root@localhost /home]# ll -a example/
  11. 总用量 8
  12. drwxr-xr-x 3 root root 51 1月 12 23:37 .
  13. drwxr-xr-x. 8 root root 88 1月 12 23:37 ..
  14. -rw-r--r-- 1 root root 18 1月 12 23:37 example.txt
  15. drwxr-xr-x 8 root root 152 1月 12 23:37 .git
  16. -rw-r--r-- 1 root root 10 1月 12 23:37 README.md
  17. [root@localhost /home]#

注:公开的仓库是任何人都可以进行克隆的,但是只能克隆不可以对仓库进行写操作。

3.对克隆的文件进行更改,然后再推送到远程的仓库,因为我们是该仓库的所有者,可以进行写操作:

  1. [root@localhost /home]# cd example/
  2. [root@localhost /home/example]# echo "This is a change operation" >> example.txt
  3. [root@localhost /home/example]# git add example.txt
  4. [root@localhost /home/example]# git commit -m "change example.txt"
  5. [master 09b7380] change example.txt
  6. 1 file changed, 1 insertion(+)
  7. [root@localhost /home/example]# git config --global push.default simple
  8. [root@localhost /home/example]# git push
  9. Username for 'https://github.com': Binary-ZeroOne
  10. Password for 'https://Binary-ZeroOne@github.com':
  11. Counting objects: 5, done.
  12. Delta compression using up to 4 threads.
  13. Compressing objects: 100% (2/2), done.
  14. Writing objects: 100% (3/3), 310 bytes | 0 bytes/s, done.
  15. Total 3 (delta 0), reused 0 (delta 0)
  16. To https://github.com/Binary-ZeroOne/example.git
  17. aacb77a..09b7380 master -> master
  18. [root@localhost /home/example]#

3.然后到GitHub上看看是否有更改的内容:
git仓库的简单使用

4.我现在在GitHub上更改这个文件的内容,更改之后同样可以在本地把新内容拉下来:
git仓库的简单使用
git仓库的简单使用

拉到页面下方,点击Commit changes提交更改:
git仓库的简单使用

接着到本地机器上,执行git pull命令,把远程仓库的更改内容拉下来:

  1. [root@localhost /home/example]# git pull
  2. remote: Counting objects: 3, done.
  3. remote: Compressing objects: 100% (3/3), done.
  4. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
  5. Unpacking objects: 100% (3/3), done.
  6. 来自 https://github.com/Binary-ZeroOne/example
  7. 09b7380..b71be6b master -> origin/master
  8. 更新 09b7380..b71be6b
  9. Fast-forward
  10. example.txt | 1 +
  11. 1 file changed, 1 insertion(+)
  12. [root@localhost /home/example]# cat example.txt
  13. This is a example
  14. This is a change operation
  15. This is another change operation # 可以看到文件内容成功更新了
  16. [root@localhost /home/example]#