安装git [root@yt-01 /]# yum install -y git 创建git版本库目录 [root@yt-01 /]# mkdir /data/gitroot/ 初始化gi版本仓库 [root@yt-01 /]# cd /data/gitroot/ [root@yt-01 gitroot]# git init 初始化空的 Git 版本库于 /data/gitroot/.git/ 配置用户名和邮箱 [root@yt-01 gitroot]# git config --global user.email "zhouqunic@163.com" [root@yt-01 gitroot]# git config --global user.name "yuntai" 配置文件 [root@yt-01 gitroot]# cat /root/.gitconfig [user] email = zhouqunic@163.com name = yuntai
建立新文件 [root@yt-01 gitroot]# echo -e "123/234/456/789" > 1.txt [root@yt-01 gitroot]# ll 总用量 4 -rw-r--r-- 1 root root 16 4月 3 14:39 1.txt 把1.txt添加到git仓库 [root@yt-01 gitroot]# git add 1.txt //打标记 [root@yt-01 gitroot]# git commit -m "creat new file 1.txt" //上传 [master(根提交) 44609a5] creat new file 1.txt 1 file changed, 1 insertion(+) create mode 100644 1.txt # 记住,上传文件到仓库,每次都要 git add filename 和 git commit -m "注释" 两句命令 再次变动1.txt [root@yt-01 gitroot]# echo -e "1 new added/END" >> 1.txt [root@yt-01 gitroot]# cat 1.txt 123/234/456/789 1 new added/END 查看当前仓库中的状态,好比是否有改动的文件 [root@yt-01 gitroot]# git status # 位于分支 master # 还没有暂存以备提交的变动: # (使用 "git add <file>..." 更新要提交的内容) # (使用 "git checkout -- <file>..." 丢弃工做区的改动) # # 修改: 1.txt # 修改还没有加入提交(使用 "git add" 和/或 "git commit -a") # 每部操做以后均可以使用“git status”查看当前状态,能够根据提示信息进行后续操做。 不一样版本文件对比 [root@yt-01 gitroot]# git diff 1.txt diff --git a/1.txt b/1.txt index a53950c..49384d8 100644 --- a/1.txt +++ b/1.txt @@ -1 +1,2 @@ 123/234/456/789 +1 new added/END
更新版本 [root@yt-01 gitroot]# git add 1.txt [root@yt-01 gitroot]# git commit -m "add 1.txt again" [master 01803b5] add 1.txt again 1 file changed, 1 insertion(+) 查看版本变动日志 [root@yt-01 gitroot]# git log commit 01803b54e07b9842da0c2c4558fdafd66b05ada6 Author: yuntai <zhouqunic@163.com> Date: Tue Apr 3 15:04:36 2018 +0800 add 1.txt again commit 2ab6b801dd207ef35d102c240173dc69d2ff3dc2 Author: yuntai <zhouqunic@163.com> Date: Tue Apr 3 15:02:00 2018 +0800 add 1.txt again commit 44609a553eef00abe45e45a3c1bd54cc8fe9a402 Author: yuntai <zhouqunic@163.com> Date: Tue Apr 3 14:41:50 2018 +0800 creat new file 1.txt 删除文件2行,并更新版本 [root@yt-01 gitroot]# vim 1.txt [root@yt-01 gitroot]# git add 1.txt [root@yt-01 gitroot]# git commit -m "删除最后2行 " [master 5acf95f] 删除最后2行 1 file changed, 2 deletions(-) 版本变动日志,一行显示 [root@yt-01 gitroot]# git log --pretty=oneline //版本变动日志,一行显示 5acf95fd5e06f1fc01acc0f28f5753610a292a07 删除最后2行 01803b54e07b9842da0c2c4558fdafd66b05ada6 add 1.txt again 2ab6b801dd207ef35d102c240173dc69d2ff3dc2 add 1.txt again 44609a553eef00abe45e45a3c1bd54cc8fe9a402 creat new file 1.txt # 代码表示版本代码 版本回退 [root@yt-01 gitroot]# git reset --hard 2ab6b801dd207 HEAD 如今位于 2ab6b80 add 1.txt again # hard后面跟的代码能够只填写一部分 [root@yt-01 gitroot]# git log --pretty=oneline 2ab6b801dd207ef35d102c240173dc69d2ff3dc2 add 1.txt again 44609a553eef00abe45e45a3c1bd54cc8fe9a402 creat new file 1.txt 撤销版本回退(假如忘了版本代码) [root@yt-01 gitroot]# git reflog //查看全部历史版本 2ab6b80 HEAD@{0}: reset: moving to 2ab6b801dd207 5acf95f HEAD@{1}: commit: 删除最后2行 01803b5 HEAD@{2}: commit: add 1.txt again 2ab6b80 HEAD@{3}: commit: add 1.txt again 44609a5 HEAD@{4}: commit (initial): creat new file 1.txt # 版本回退 [root@yt-01 gitroot]# git log --pretty=oneline 5acf95fd5e06f1fc01acc0f28f5753610a292a07 删除最后2行 01803b54e07b9842da0c2c4558fdafd66b05ada6 add 1.txt again 2ab6b801dd207ef35d102c240173dc69d2ff3dc2 add 1.txt again 44609a553eef00abe45e45a3c1bd54cc8fe9a402 creat new file 1.txt 假如文件1.txt被删除了 [root@yt-01 gitroot]# rm -rf 1.txt [root@yt-01 gitroot]# ls [root@yt-01 gitroot]# git checkout -- 1.txt //恢复原来文件 [root@yt-01 gitroot]# ls 1.txt 假如更改了1.txt,而后git add了,可是没有git commit,不想更新了,想回退到当时版本 [root@yt-01 gitroot]# echo -e "xxxxxx" > 1.txt [root@yt-01 gitroot]# git add 1.txt [root@yt-01 gitroot]# git reset HEAD 1.txt //重置缓存 重置后撤出暂存区的变动: M 1.txt [root@yt-01 gitroot]# cat 1.txt xxxxxx [root@yt-01 gitroot]# git checkout -- 1.txt //恢复原来文件 [root@yt-01 gitroot]# cat 1.txt 123/234/456/789
[root@yt-01 gitroot]# git rm 1.txt //删除文件 rm '1.txt' [root@yt-01 gitroot]# git commit -m "删除1.txt文件" //提交删除 [master 7215021] 删除1.txt文件 1 file changed, 1 deletion(-) delete mode 100644 1.txt [root@yt-01 gitroot]# ls git checkout已经不能恢复了 [root@yt-01 gitroot]# git checkout -- 1.txt error: pathspec '1.txt' did not match any file(s) known to git. 恢复git rm删除的文件 [root@yt-01 gitroot]# git log --pretty=oneline //查看版本日志 721502178db58654977d89635423afaa806e3c27 删除1.txt文件 5acf95fd5e06f1fc01acc0f28f5753610a292a07 删除最后2行 01803b54e07b9842da0c2c4558fdafd66b05ada6 add 1.txt again 2ab6b801dd207ef35d102c240173dc69d2ff3dc2 add 1.txt again 44609a553eef00abe45e45a3c1bd54cc8fe9a402 creat new file 1.txt [root@yt-01 gitroot]# git reset --hard 721502178d //最后的版本已是删除掉的时候,恢复不了 HEAD 如今位于 7215021 删除1.txt文件 [root@yt-01 gitroot]# ls [root@yt-01 gitroot]# git reset --hard 5acf95fd5e06f1fc01acc0f28f5753610a292a07 //回退到对应版本才能够 HEAD 如今位于 5acf95f 删除最后2行 [root@yt-01 gitroot]# ls 1.txt
GitHub官网:github.com 注册帐号并激活,而后开始建立主机的仓库! 建立完成后,添加key: 点击浏览器右上角头像——setting——SSH and GPG keys(选择SSH keys)——在服务器(虚拟机)执行ssh-keygen命令生成密钥对(/root/.ssh/id_rsa-私钥, /root/.ssh/id_rsa.pub-公钥)——将公钥复制到浏览器后点“添加”。git
建立本地版本仓库 [root@yt-01 tmp]# mkdir /tmp/gittest/ [root@yt-01 tmp]# cd gittest/ [root@yt-01 tmp]# echo "# gittest" >> README.md [root@yt-01 gittest]# git add README.md [root@yt-01 gittest]# git commit -m "建立README.md" [master(根提交) f7045bf] 建立README.md 1 file changed, 1 insertion(+) create mode 100644 README.md 远程链接 [root@yt-01 gittest]# git remote add origin git@github.com:zhouqunic/gittest.git [root@yt-01 gittest]# git push -u origin master Counting objects: 3, done. Writing objects: 100% (3/3), 232 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To git@github.com:zhouqunic/gittest.git * [new branch] master -> master 分支 master 设置为跟踪来自 origin 的远程分支 master。
新建文件 [root@yt-01 gittest]# vim 123.txt [root@yt-01 gittest]# cat 123.txt 123 123 123 123 123 123 推送到本地仓库 [root@yt-01 gittest]# git add 123.txt [root@yt-01 gittest]# git commit -m "新建123.txt文件" [master 6007c36] 新建123.txt文件 1 file changed, 3 insertions(+) create mode 100644 123.txt 推送到远程仓库 [root@yt-01 gittest]# git push warning: push.default 未设置,它的默认值将会在 Git 2.0 由 'matching' 修改成 'simple'。若要再也不显示本信息并在其默认值改变后维持当前使用习惯, 进行以下设置: git config --global push.default matching 若要再也不显示本信息并从如今开始采用新的使用习惯,设置: git config --global push.default simple 参见 'git help config' 并查找 'push.default' 以获取更多信息。 ('simple' 模式由 Git 1.7.11 版本引入。若是您有时要使用老版本的 Git, 为保持兼容,请用 'current' 代替 'simple' 模式) Counting objects: 4, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 300 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To git@github.com:zhouqunic/gittest.git f7045bf..6007c36 master -> master # 上面有好多无用的提示信息,为了以后不显示这些信息,根据提示执行命令 [root@yt-01 gittest]# git config --global push.default simple
建立一个本地文件夹,来放克隆的内容 [root@yt-01 gittest]# cd /usr/local/sbin/ [root@yt-01 sbin]# ls 克隆远程仓库 [root@yt-01 sbin]# git clone https://github.com/maicong/LNMP.git //克隆 正克隆到 'LNMP'... remote: Counting objects: 420, done. remote: Total 420 (delta 0), reused 0 (delta 0), pack-reused 420 接收对象中: 100% (420/420), 612.72 KiB | 95.00 KiB/s, done. 处理 delta 中: 100% (224/224), done. [root@yt-01 sbin]# ls LNMP [root@yt-01 sbin]# cd LNMP/ [root@yt-01 LNMP]# ls DBMGT etc home keys LICENSE lnmp.sh README.md source.sh svnserve svn.sh
要是本身的仓库,别人的仓库的话,必须fork到本身的仓库,克隆后才能编辑github
[root@yt-01 sbin]# git clone git@github.com:zhouqunic/LNMP.git 正克隆到 'LNMP'... remote: Counting objects: 420, done. remote: Total 420 (delta 0), reused 0 (delta 0), pack-reused 420 接收对象中: 100% (420/420), 612.72 KiB | 62.00 KiB/s, done. 处理 delta 中: 100% (224/224), done. [root@yt-01 sbin]# ls LNMP [root@yt-01 sbin]# cd LNMP/ [root@yt-01 LNMP]# ls DBMGT etc home keys LICENSE lnmp.sh README.md source.sh svnserve svn.sh [root@yt-01 LNMP]# echo "^^^^" >> lnmp.sh [root@yt-01 LNMP]# git add lnmp.sh [root@yt-01 LNMP]# git commit -m "瞎改" [master c67bc06] 瞎改 1 file changed, 1 insertion(+) # 此时只是提交到了本地仓库
[root@yt-01 LNMP]# git push Warning: Permanently added the RSA host key for IP address '192.30.253.112' to the list of known hosts. Counting objects: 5, done. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 293 bytes | 0 bytes/s, done. Total 3 (delta 2), reused 0 (delta 0) remote: Resolving deltas: 100% (2/2), completed with 2 local objects. To git@github.com:zhouqunic/LNMP.git 027e59a..c67bc06 master -> master