文章目录
安装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@"
添加新文件到仓库:
#创建新文件[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.txtdiff --git a/new.txt b/new.txtindex 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重置后撤出暂存区的变更:Mnew1.txt
查看git提交记录:
[root@linux git]# git logcommit 277075277c442544ac98f07f2d5f87197ee34f5bAuthor: asnfy <asnfy@>Date: Mon Dec 23 14:31:24 +0800update new.txtcommit 3c0b9eaae1984220fb73d20f54a653932e939c5aAuthor: asnfy <asnfy@>Date: Mon Dec 23 14:27:51 +0800update new.txtcommit f1eaf59a52b65c512d53487db383e80e0b5f9386Author: asnfy <asnfy@>Date: Mon Dec 23 14:14:17 +0800新文件: new.txt
#git日志显示所有变更记录(变更编号,操作用户,时间,自定义的变更说明),空格向下翻页,b键向上翻页,q退出,用法与less命令类似
一行显示git日志(提交记录):
[root@linux git]# git log --pretty=oneline 277075277c442544ac98f07f2d5f87197ee34f5b update new.txt3c0b9eaae1984220fb73d20f54a653932e939c5a update new.txtf1eaf59a52b65c512d53487db383e80e0b5f9386 add new.txt
git版本回滚:
[root@linux git]# git reset --hard 3c0b9eaae1984220fb73d20f54a653932e939c5aHEAD 现在位于 3c0b9ea update new.txt[root@linux git]# git log --pretty=oneline 3c0b9eaae1984220fb73d20f54a653932e939c5a update new.txtf1eaf59a52b65c512d53487db383e80e0b5f9386 add new.txt
#回滚版本使用reset - -hard指定变更编码即可,回滚后再次查看提交记录日志,上次的提交记录已消失,当回滚后又想回滚到上次提交的最新版本,可是日志中已经没有那条记录,需要使用reflog命令,查看所有历史变更
查看所有历史变更:
[root@linux git]# git reflog 3c0b9ea HEAD@{0}: reset: moving to 3c0b9eaae1984220fb73d20f54a653932e939c5a2770752 HEAD@{1}: commit: update new.txt3c0b9ea HEAD@{2}: commit: update new.txtf1eaf59 HEAD@{3}: commit: add new.txt
#编号2770752的变更就是之前最新提交的变更,通过git reset --hard 2770752即可回滚到之前提交的最新版本
git撤销删除:
[root@linux git]# ls1.txt new.txt test.txt[root@linux git]# rm -f new.txt [root@linux git]# ls1.txt test.txt[root@linux git]# git checkout -- new.txt[root@linux git]# ls1.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@linuxThe 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 /AsnFy/git_test.git[root@linux git]# git push -u origin masterThe authenticity of host ' (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)? yesWarning: Permanently added ',13.229.188.59' (RSA) to the list of known hosts.Counting objects: 11, pressing 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@: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 /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 0Unpacking objects: 100% (14/14), done.
#此步骤不需要验证用户账号密码或ssh秘钥
进入目录即可看到远程仓库中的文件:
[root@linux ~]# cd git_test/[root@linux git_test]# lsgithub.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@"
推送新文件到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]# lsabc.sh github.txt new1.txt new.txt README.md test.txt
#新文件abc.sh拉取成功