Git与GitHub

2018-12-16  本文已影响0人  枕梦_a280

Git的安装和使用

[root@server02 ~]# yum -y install git
[root@server02 ~]# git --version
git version 1.8.3.1
[root@server02 test_git]# git config --global user.name lzp
[root@server02 test_git]# git config --global user.email lzp@localhost.localdaemon
[root@server02 test_git]# git config --global color.ui true
[root@server02 test_git]# git config --list
user.name=lzp
user.email=lzp@localhost.localdaemon
color.ui=true
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
[root@server02 ~]# mkdir /test_git
[root@server02 ~]# cd /test_git/
[root@server02 test_git]# git init
[root@server02 test_git]# ll -a
total 0
drwxr-xr-x   3 root root  18 Dec 16 17:14 .
dr-xr-xr-x. 18 root root 240 Dec 16 17:12 ..
drwxr-xr-x   7 root root 119 Dec 16 17:14 .git

在工作区创建一个示例文本写入数据

[root@server02 test_git]# echo "hello" >> 1.txt
[root@server02 test_git]# ls
1.txt
[root@server02 test_git]# cat 1.txt 
hello

将文本提交至缓存区,查看状态。

[root@server02 test_git]# git add 1.txt
[root@server02 test_git]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   1.txt
#

将缓存区文件提交到本地git仓库,提交后缓存区被清空。

[root@server02 test_git]# git commit -m "A new file 1.txt added"
[master (root-commit) 0d98c2a] A new file 1.txt added
 1 file changed, 1 insertion(+)
 create mode 100644 1.txt

[root@server02 test_git]# git status
# On branch master
nothing to commit, working directory clean
[root@server02 test_git]# cat 1.txt 
hello
[root@server02 test_git]# echo "world" >> 1.txt 
[root@server02 test_git]# cat 1.txt 
hello
world

git status 查看当前状态
[root@server02 test_git]# git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   1.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

git diff 查看有哪些改变
[root@server02 test_git]# git diff 1.txt
diff --git a/1.txt b/1.txt
index ce01362..94954ab 100644
--- a/1.txt
+++ b/1.txt
@@ -1 +1,2 @@
 hello
+world

提交两步
[root@server02 test_git]# git add 1.txt
[root@server02 test_git]# git commit -m "Modefied 1.txt add world"
[master 68d9eb4] Modefied 1.txt add world
 1 file changed, 1 insertion(+)
[root@server02 test_git]# git status
# On branch master
nothing to commit, working directory clean

查看历史记录

[root@server02 test_git]# git log
commit 68d9eb403c11897d4bab540c40366a6ec714fba1
Author: lzp <lzp@local.localdaemon>
Date:   Sun Dec 16 17:48:11 2018 +0800
    Modefied 1.txt add world

commit 0d98c2a91197a95504f5d059bbb325680a8f5457
Author: lzp <lzp@local.localdaemon>
Date:   Sun Dec 16 17:34:34 2018 +0800
    A new file 1.txt added

较简洁的显示
[root@server02 test_git]# git log --pretty=oneline 
68d9eb403c11897d4bab540c40366a6ec714fba1 Modefied 1.txt add world
0d98c2a91197a95504f5d059bbb325680a8f5457 A new file 1.txt added

方法一:

回退到上一个版本:
[root@server02 test_git]# git reset --hard HEAD^
HEAD is now at 0d98c2a A new file 1.txt added
[root@server02 test_git]# cat 1.txt 
hello

回退到前面的几个版本就几个^

方法二:

[root@server02 test_git]# cat 1.txt 
hello
world
[root@server02 test_git]# git log --pretty=oneline 
68d9eb403c11897d4bab540c40366a6ec714fba1 Modefied 1.txt add world
0d98c2a91197a95504f5d059bbb325680a8f5457 A new file 1.txt added

回退到第一个版本
[root@server02 test_git]# git reset --hard HEAD~1
HEAD is now at 0d98c2a A new file 1.txt added
[root@server02 test_git]# cat 1.txt 
hello

方法三:
可以回到任意版本

查看所有操作历史,第一列就是版本号
[root@server02 test_git]# git reflog 
0d98c2a HEAD@{0}: reset: moving to HEAD~1
68d9eb4 HEAD@{1}: reset: moving to 68d9eb4
0d98c2a HEAD@{2}: reset: moving to HEAD^
68d9eb4 HEAD@{3}: commit: Modefied 1.txt add world
0d98c2a HEAD@{4}: commit (initial): A new file 1.txt added

通过版本号来回退
[root@server02 test_git]# git reset --hard 0d98c2a
HEAD is now at 0d98c2a A new file 1.txt added
[root@server02 test_git]# cat 1.txt 
hello

只能撤销工作区的修改操作

对1.txt做修改
[root@server02 test_git]# cat 1.txt 
hello
[root@server02 test_git]# echo "hahahha" >> 1.txt 
[root@server02 test_git]# cat 1.txt 
hello
hahahha

查看状态
[root@server02 test_git]# git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   1.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

撤销这个修改再查看
[root@server02 test_git]# git checkout -- 1.txt
[root@server02 test_git]# cat 1.txt 
hello
[root@server02 test_git]# git status
# On branch master
nothing to commit, working directory clean

将文件删除

[root@server02 test_git]# git rm 1.txt
rm '1.txt'
[root@server02 test_git]# ls
[root@server02 test_git]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    1.txt
#

创建一个分支,命名为add。并查看当前分支信息。

[root@server02 test_git]# git branch add

[root@server02 test_git]# git branch
  add
* master              *号表示当前处于主分支中

分支切换

[root@server02 test_git]# git checkout add 
Switched to branch 'add'
[root@server02 test_git]# git branch
* add
  master

在add分支中创建文件并提交至本地git仓库

[root@server02 test_git]# echo "nnnnmmmmddd" >> 99.txt
[root@server02 test_git]# ls
1.txt  99.txt
[root@server02 test_git]# git add 99.txt
[root@server02 test_git]# git commit -m "add add 99.txt"
[add 8115400] add add 99.txt
 1 file changed, 1 insertion(+)
 create mode 100644 99.txt

切换到master分支查看,add分支中的数据对master分支没有影响

[root@server02 test_git]# git checkout master 
Switched to branch 'master'
[root@server02 test_git]# ls
1.txt

合并分支

当前处于master分支
[root@server02 test_git]# git branch 
  add
* master

与add分支合并
[root@server02 test_git]# git merge add
Updating 68d9eb4..8115400
Fast-forward
 99.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 99.txt

查看
[root@server02 test_git]# ls
1.txt  99.txt

当多人同时操作同一文件时容易发生版本冲突,例如,我们在master分支和add分支中同时创建88.txt并写入不同数据,然后进行合并分支操作。

在master分支中
[root@server02 test_git]# git branch 
  add
* master
[root@server02 test_git]# echo "master" >> 88.txt
[root@server02 test_git]# git add 88.txt
[root@server02 test_git]# git commit -m "master add 88.txt"
[master fb50d41] master add 88.txt
 1 file changed, 1 insertion(+)
 create mode 100644 88.txt

在add分支中
[root@server02 test_git]# git checkout add 
Switched to branch 'add'
[root@server02 test_git]# git branch 
* add
  master
[root@server02 test_git]# echo "add" >> 88.txt
[root@server02 test_git]# git add 88.txt
[root@server02 test_git]# git commit -m "add 88.txt"
[add 96416d3] add 88.txt
 1 file changed, 1 insertion(+)
 create mode 100644 88.txt

切换到master进行合并操作
[root@server02 test_git]# git checkout master
Switched to branch 'master'
[root@server02 test_git]# ls
1.txt  88.txt  99.txt
[root@server02 test_git]# git branch 
  add
* master
[root@server02 test_git]# git merge add
Auto-merging 88.txt
CONFLICT (add/add): Merge conflict in 88.txt
Automatic merge failed; fix conflicts and then commit the result.

查看88.txt的内容
[root@server02 test_git]# cat 88.txt 
<<<<<<< HEAD
master
=======
add
>>>>>>> add

解决冲突,将冲突部分做修改

[root@server02 test_git]# sed -ie '/^[^a-z]/d' 88.txt
[root@server02 test_git]# cat 88.txt
master
add

提交

[root@server02 test_git]# git add 88.txt
[root@server02 test_git]# git commit -m "modified 88.txt ok"
[root@server02 test_git]# git branch 
  add
* master
[root@server02 test_git]# git branch -d add
Deleted branch add (was 96416d3).
[root@server02 test_git]# git branch 
* master

GitHub

获取克隆地址


克隆地址.png

创建目录来克隆

[root@server02 /]# mkdir /test
[root@server02 /]# cd /test
[root@server02 test]# ls

[root@server02 test]# git clone https://github.com/LiZhipen/test.git
Cloning into 'test'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
[root@server02 test]# ls
test
[root@server02 test]# cd test/
[root@server02 test]# ls
README.md

提交一个项目文件

创建文件,提交到本地git仓库
[root@server02 test]# echo "hello world" >> index.php
[root@server02 test]# ls
index.php  README.md
[root@server02 test]# git add index.php
[root@server02 test]# git commit -m "create index.php file"
[master 562e59a] create index.php file
 1 file changed, 1 insertion(+)
 create mode 100644 index.php

推到远程git库


推.png
推2.png

生成密钥对


生成秘钥.png

复制公钥

[root@server02 test]# cat /root/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDzERJR8EdnR+OMdVAZ2H0dKuS0pm30/8n50C3e037RnKwfQNTJdPZ8ghrlRBDMD0hsem0vIqe5TYYqW/nJVzdLk9rXvrjzTL8v4nx+NgSbPIC6KPCtsEIVYSpeqnKmnLYs/zULiiDASKFe2quEDb/kejrj3DUfBNHwSCbAMrP8ND81JUZm2X99EUx1zi1G8PD4/yY8mo3P0iZN+TgiOo/uQW0irSJ3VMxYRI3zhTwifE20hndrvlozfVbg2d2H/eZNh8D62NEjIS5haPhSxDrs8ygczXWntc0iYxXjJ/RjpZFCxTr55xvijqusjqRWSaDUsc0PlYWoRM7pFLljEft7 392711464@qq.com
[root@server02 test]# 

粘贴到GitHub


设置.png
shez2.png
shez3.png
设置4.png

选择ssh方式来克隆和推代码


ssh1.png
ssh2.png
测试
ssh3.png
上一篇 下一篇

猜你喜欢

热点阅读