文章目录

  • 安装git
  • 单机上使用git
  • 建立GitHub远程仓库
  • 克隆GitHub远程仓库

安装git

[root@linux ~]# yum -y install git

单机上使用git

创建仓库目录:

[root@linux ~]# mkdir /data/git

初始化仓库:

[root@linux ~]# cd !$
[root@linux git]# git init
初始化空的 Git 版本库于 /data/git/.git/

定义使用者身份(用户名和邮箱):

[root@linux git]# git config user.name "asnfy"
[root@linux git]# git config user.email "asnfy@qq.com"

添加新文件到仓库:

#创建新文件
[root@linux git]# cat /etc/passwd > test.txt
#添加add标记
[root@linux git]# git add test.txt
#使用commit处理添加了标记的文件
[root@linux git]# git commit -m " add newfile: test.txt "
[master(根提交) 425d26f]  add newfile: test.txt1 file changed, 33 insertions(+)create mode 100644 test.txt

#与svn用法想通,先对文件添加指定的标记,再通过commit命令执行,-m指定自定义说明,便于日志查看

查看当前仓库中的状态:

[root@linux git]# git status
# 位于分支 master
无文件要提交,干净的工作区

当新增一个文件但未提交到仓库时,状态显示为:

[root@linux git]# echo "123" > new.txt
[root@linux git]# git status
# 位于分支 master
# 未跟踪的文件:
#   new.txt
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

#当产生新文件但未进行标记时,会显示为未跟踪文件

添加add标记后显示为:

[root@linux git]# git add new.txt
[root@linux git]# git status
# 位于分支 master
# 要提交的变更:
#   新文件:    new.txt

#当文件被标记后,会显示为待提交的变更

对比文件本次修改内容与仓库中的区别:

[root@linux git]# echo "aaaaaaaaaa" >> new.txt
[root@linux git]# git diff new.txt
diff --git a/new.txt b/new.txt
index 190a180..34483aa 100644
--- a/new.txt
+++ b/new.txt
@@ -1 +1,2 @@123
+aaaaaaaaaa

#当前修改的new.txt文件相比较于仓库中的new.txt,有新增内容,会在新增内容行首显示+号,有删减内容,会在行首显示-号

撤销标记:

[root@linux git]# git add 1.txt
[root@linux git]# git reset HEAD 1.txt
重置后撤出暂存区的变更:
M   new1.txt

查看git提交记录:

[root@linux git]# git log
commit 277075277c442544ac98f07f2d5f87197ee34f5b
Author: asnfy <asnfy@qq.com>
Date:   Mon Dec 23 14:31:24 2019 +0800update new.txtcommit 3c0b9eaae1984220fb73d20f54a653932e939c5a
Author: asnfy <asnfy@qq.com>
Date:   Mon Dec 23 14:27:51 2019 +0800update new.txtcommit f1eaf59a52b65c512d53487db383e80e0b5f9386
Author: asnfy <asnfy@qq.com>
Date:   Mon Dec 23 14:14:17 2019 +0800新文件:    new.txt

#git日志显示所有变更记录(变更编号,操作用户,时间,自定义的变更说明),空格向下翻页,b键向上翻页,q退出,用法与less命令类似

一行显示git日志(提交记录):

[root@linux git]# git log --pretty=oneline
277075277c442544ac98f07f2d5f87197ee34f5b update new.txt
3c0b9eaae1984220fb73d20f54a653932e939c5a update new.txt
f1eaf59a52b65c512d53487db383e80e0b5f9386 add new.txt

git版本回滚:

[root@linux git]# git reset --hard 3c0b9eaae1984220fb73d20f54a653932e939c5a
HEAD 现在位于 3c0b9ea update new.txt
[root@linux git]# git log --pretty=oneline
3c0b9eaae1984220fb73d20f54a653932e939c5a update new.txt
f1eaf59a52b65c512d53487db383e80e0b5f9386 add new.txt

#回滚版本使用reset - -hard指定变更编码即可,回滚后再次查看提交记录日志,上次的提交记录已消失,当回滚后又想回滚到上次提交的最新版本,可是日志中已经没有那条记录,需要使用reflog命令,查看所有历史变更

查看所有历史变更:

[root@linux git]# git reflog
3c0b9ea HEAD@{0}: reset: moving to 3c0b9eaae1984220fb73d20f54a653932e939c5a
2770752 HEAD@{1}: commit: update new.txt
3c0b9ea HEAD@{2}: commit: update new.txt
f1eaf59 HEAD@{3}: commit: add new.txt

#编号2770752的变更就是之前最新提交的变更,通过git reset --hard 2770752即可回滚到之前提交的最新版本

git撤销删除:

[root@linux git]# ls
1.txt  new.txt  test.txt
[root@linux git]# rm -f new.txt
[root@linux git]# ls
1.txt  test.txt
[root@linux git]# git checkout -- new.txt
[root@linux git]# ls
1.txt  new.txt  test.txt

#当误删后可以通过checkout指定文件名将误删的文件从git仓库恢复,也可以用于将修改过的文件回退到上一次提交的状态

建立GitHub远程仓库

在GitHub创建仓库:

在本机生成秘钥:

[root@linux ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:PN7KUkW6ZJxXyGbgk1MJ1zLEsYcnqf4QsRk24b9zm+8 root@linux
The key's randomart image is:
+---[RSA 2048]----+
|        +*==     |
|       o =@+o    |
|       .@*=+o    |
|       o*@o+     |
|       oS+.      |
|       oo+ .     |
|       .+ + .    |
|      .. + o o   |
|       .o . ooE  |
+----[SHA256]-----+
[root@linux ~]# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDzAgtbiq1f73Cwxm9ra+Z+e5Ki03VFZbn2EhjpDkzjVL7l7Yeux0nJBriM3xjeJHhysFrrbacvMBfaqXpin7i7MQ8+ugGRFSQeSoXlgSq0X7u05LKhLx/Qbut0brBWaI10WchobbjOGsTLoeE0HzaiZj4muFhtAsN8Yvm9BhIfbLPq255s0c4ESLpXqi6gjBDnHaUdLpnD5mqRmyEjhEGiLqnvTNOtMX9Zf5sFi1lwTahvToeMfuQfgC8QgTCXMGGxVafF6uFof4X4TY7B0Fvj05X1Qsoz9+XcWBk4jxOnGeRB8iYxWoupYQQ14cPAYTa+kYqkoGVThA/ru8lv0N// root@linux

在GitHub点击右上角头像旁边的箭头,选择setting,选择SSH and GPG keys创建ssh秘钥:

修改本地git仓库配置文件:

[root@linux ~]# vim /data/git/.git/config

修改url地址为ssh协议地址:


将本地仓库推送远程仓库:

[root@linux ~]# cd /data/git/
[root@linux git]# git remote add origin https://github.com/AsnFy/git_test.git
[root@linux git]# git push -u origin master
The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
RSA key fingerprint is MD5: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)? yes
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
Counting objects: 11, done.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (11/11), 1.58 KiB | 0 bytes/s, done.
Total 11 (delta 0), reused 0 (delta 0)
To git@github.com:AsnFy/git_test.git* [new branch]      master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。

之后推送执行git push即可:

[root@linux git]# touch github.txt
[root@linux git]# git add github.txt
[root@linux git]# git commit -m "add newfile: github.txt"
[master 3c0a54d] add newfile: github.txt1 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 github.txt
[root@linux git]# git push

查看GitHub仓库:

#推送的文件已显示

补充:如果需要使用https验证方式,本地仓库配置文件保持默认即可,只是推送本地仓库文件到GitHub需要验证GitHub用户名和密码

克隆GitHub远程仓库

在GitHub仓库中点击Clone or download获取https/ssh地址:

克隆远程仓库到本地:

[root@linux ~]# git clone https://github.com/AsnFy/git_test.git
正克隆到 'git_test'...
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 14 (delta 1), reused 13 (delta 0), pack-reused 0
Unpacking objects: 100% (14/14), done.

#此步骤不需要验证用户账号密码或ssh秘钥

进入目录即可看到远程仓库中的文件:

[root@linux ~]# cd git_test/
[root@linux git_test]# ls
github.txt  new1.txt  new.txt  test.txt

定义使用者身份:

[root@linux git_test]# git config --global user.name "zhangsan"
[root@linux git_test]# git config --global user.email "zhangsan@test.com"

推送新文件到GitHub:

[root@linux git_test]# echo "hello" > README.md
[root@linux git_test]# git add README.md
[root@linux git_test]# git commit -m "add README.md"
[root@linux git_test]# git push

#此步骤需要验证用户名密码或ssh秘钥,默认是https验证,如需ssh免密认证,参考前面的步骤修改当前仓库下的.git/config文件

查看GitHub:

#新文件README.md已显示

在GitHub创建新文件:

在本地仓库拉取GitHub仓库的新文件:

[root@linux git_test]# git pull
[root@linux git_test]# ls
abc.sh  github.txt  new1.txt  new.txt  README.md  test.txt

#新文件abc.sh拉取成功

git安装、使用、建立github远程仓库、克隆远程仓库相关推荐

  1. Git使用(Git安装、建立Git仓库、克隆Git仓库、提交代码至Git仓库)

    目录 1 Git安装 2 建立Git仓库 3 克隆Git仓库 3.1 新建Spring Boot项目 ​3.2 克隆Git仓库 4 提交代码至Git仓库 4.1 Pull ​4.2 Commit 4. ...

  2. Git配置SSH公钥及创建和克隆远程仓库

    gitbash在本地创建ssh key (1)打开c盘下的用户文件夹,可以找到".ssh"这样一个文件夹,现在这个文件夹下是空的. 执行如下命令 ssh-keygen -t rsa ...

  3. 第一:Git安装和使用github(超详解)

    简介 刚开始写了关于如何将本地代码上传到github上,但是有些小伙伴们不清楚如何安装Git,这一篇就给小伙伴们普及一下Git的安装和使用.适合刚开始用git的小白,大神或者大佬请绕道. 实际项目开发 ...

  4. github ssh 配置_Github远程仓库克隆更新本机,SSH协议免密操作配置和注意事项

    Github远程仓库-克隆远程仓库到本机 [重点] 目标 如何克隆远程仓库到本机呢? 小结下载项目的命令: git clone https://gitee.com/kekesam/sassweb777 ...

  5. 【GIT】使用Git命令窗口将本地工程提交至远程GitHub

    目标: 1.解决的问题是如何通过Git命令窗口将本地工程提交至GitHub. 2.方便园友的同时也方便自己以后解决此类问题. 步骤: 1.首先登陆GitHub网站https://github.com/ ...

  6. git安装及github配置

    一.git安装 二.github连接 1.账户信息配置 用户名: git config --global user.name "username" 邮箱 git config -- ...

  7. Mac OS Git 安装

    一.Git是一个分布式的代码版本管理工具.类似的常用工具还有SVN,CVS.最大的特点也是优点在于提供分布式的代码管理 1.分支代码只有一份! 使用过svn的童鞋想必都知道,当我们要开发一个新功能或者 ...

  8. 极客时间《玩转Git三剑客》之GItHub剑客

    文章目录 1.注册github账号 2.配置公私钥 3.在github上创建个人仓库 4.把本地仓库同步到github 5.不同人修改了不同文件如何处理? 6.不同人修改了同文件的不同区域如何处理? ...

  9. 版本控制介绍、Git的安装、TortoiseGit的安装、Git的文件操作、IDEA + 同步到GitHub远程仓库 | 克隆 | 分支 | 忽略文件

    文章目录 版本控制 什么是版本控制 常见的版本控制工具 版本控制分类 Git与SVN的主要区别 Git的历史 Git安装与配置 TortoiseGit的安装 Git基本理论 Git项目搭建 Git文件 ...

最新文章

  1. Android 进阶 Fragment 介绍和使用 (一)
  2. 如何将瀑布流里的图片加链接_「魔幻瀑布」180度全景接片+曝光合成!
  3. 命令 / Linux / 常用的解压缩的命令
  4. boost::allocate_unique相关的测试程序
  5. c语言中读取内存的文件,c++从内存中读取文件内容,内容写到内存 实现文件的内存共享代码实例...
  6. 在win10上使用Vmware安装Mac OS
  7. ASP.NET Core 2.2 : 十六.扒一扒2.2版更新的新路由方案
  8. spring+mybatis+springMVC+redis缓存+mysql+bootstrap+异步提交----联系人小demo
  9. java map 赋值_java Listmap赋值的问题
  10. 实例解说Linux命令行uniq
  11. RPC的负载均衡策略
  12. 程序员的进阶课-架构师之路(5)-队列
  13. 输出100以内所有的质数
  14. 走过 Google 2005年[zt]
  15. 操作系统课后答案第三章
  16. Gamesalad借QQ游戏无线平台进军中国
  17. 小米手机抢购背后的摩尔定律
  18. 【命学释疑之何为命】一个程序员对算命的剖析
  19. springboot+java农家乐系统-住宿-美食-活动vue
  20. libusb ubuntu

热门文章

  1. 【Bug】WindowsPowerShell\profile.ps1
  2. xiaopiu原型设计 记录
  3. big5转utf-8乱码解决方案
  4. 微信小程序实现按钮数组单击更改颜色
  5. Postgresql 的 pg_notify 方法介绍
  6. php本地浏览器调试,php浏览器端调试输出方法
  7. [python] fileinput模块
  8. 微信小程序版 九宫格数独游戏
  9. 华为云ECS下安装MySQL
  10. 宝宝长牙发烧怎么办?会自己退吗?