Git建立远程/本地服务器和Git命令的使用

Github 远程仓库

1、github.com 注册账户

2、在github上创建仓库

3、生成本地ssh key

[root@localhost ~]# ssh-keygen -t rsa -C '[email protected]' # 邮箱要与github上注册的相同

[root@localhost ~]# cat .ssh/id_rsa.pub

ssh-rsa
AAAAB3NzaC1yc2EAAAADAQABAAABAQDVThfq4brrlsPGtAknVB0TLPx+7Dd3qlxTbSIrUOsGC5Y8JuNqVTlIntZB
4oNj8cSQrWvec9CKm0a8o7WwaJIiqpxurz+YpQHP2KbapftKIxsX4hPf/z+p0El1U6arQa35/xmNsq+cJLH/bDdR
G+EMDhuCBmjVZOlLj/hEdeIT6s56AnnCkaWoF+sq58KCF7Tk54jRbs/YiyE4SN7FuA70r+07sA/uj0+lmuk4E190
KtQUELhjX/E9stivlqiRhxnKvVUqXDywsjfM8Rtvbi4Fg9R8Wt9fpd4QwnWksYUoR5qZJFYXO4hSZrUnSMruPK14
xXjDJcFDcP2eHIzKgLD1 [email protected]

4、复制以上的公钥,在github 中添加ssh key

5、测试

[root@localhost ~]# yum install git
........
[root@localhost ~]# ssh -T [email protected]
The authenticity of host 'github.com (13.250.177.223)' 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.250.177.223' (RSA) to the list of known hosts.
Hi meteor! You've successfully authenticated, but GitHub does not provide shell access.

6、在本地添加远程仓库

[root@localhost ~]# git config --global user.name 'maiya_by'
[root@localhost ~]# git config --global user.email '[email protected]'
[root@localhost ~]# git config --global color.ui true
[root@localhost ~]# git config --list | tail -3
user.name=maiya_by
[email protected]
color.ui=true
[root@localhost ~]#
[root@localhost ~]# ls .gitconfig
.gitconfig
[root@localhost ~]# cat .gitconfig
[user]
name = maiya_by
email = [email protected]
[color]
ui = true

7、连接远程仓库听方法

[root@localhost python1804]# git remote -v
origin [email protected]:meteor/python1804.git (fetch)
origin [email protected]:meteor/python1804.git (push)

[root@localhost python1804]#git remote rm origin(如果连接远程的方式不是ssh,可以删除重新添加)

[root@localhost ~]# git remote add origin [email protected]:meteor/python1804.git

git remote add origin https://github.com/meteor/python1804.git
git push -u origin master

建立本地Git 服务器

[root@localhost ~]# useradd git ------------------创建一个git用户
[root@localhost ~]# mkdir /git‐root/
[root@localhost ~]# cd /git‐root/
[root@localhost git‐root]# git init ‐‐bare shell.git --------------初始化仓库
[root@localhost git‐root]# chown ‐R git.git shell.git ----------把文件的属主和属组改成git
[root@localhost git‐root]# su ‐ git --------------切换成git用户

生成密钥

[git@localhost ~]$ ssh‐keygen ‐t rsa ----------------生成密钥
[git@localhost ~]$ cd .ssh/
[git@localhost .ssh]$ cp id_rsa.pub authorized_keys-----------新建授权公钥串
[git@localhost .ssh]$ exit

克隆仓库

[root@localhost ~]# cd /opt/
[root@localhost opt]# git clone [email protected]:/git‐root/shell.git-----------克隆本地仓库
Cloning into 'shell'...

新建一个测试文件,进行推送

[root@localhost opt]# cd shell/
[root@localhost shell]# vim test1.sh
[root@localhost shell]# git add test1.sh
[root@localhost shell]# git commit ‐m 'first commit'
[root@localhost shell]# git push origin master

git常用的命令

git

git init #------------------初始化仓库
git clone https://github.com/kennethreitz/requests.gitCloning into 'requests'... #-----克隆建立远程库
git clone [email protected]:/git-root/liudelong.git #-------克隆建立本地库

git add * #------------提交

git commit -m "describe" #---------------对提交任务进行描述

git push origin master #----------------上传

查看状态

git status #------------------查看git 的状态

git status -s #-------------------查看git 的简要信息
显示信息:
A .gitignore
MM test2.py #------------------第一个M表示staging有修改,第二个M表示working directory有修改

git diff #检查数据是否一致

git diff #-------------默认git diff 只检查第二个标志位(即检查working directory与staging的数据是否一致

git diff HEAD #-------------------指针,检查working directory与history是否一致,HEAD指针就指向最后一次提交的内容

git diff --stat #-----------------输出简要信息

git diff --staged #-----------------表示检查stage与history中的数据是否一致

下载

git reset #----------------下载历史history中的最后一个版本致stage中

git checkout #--------------------把历史区的文件恢复到工作区-下载stage中的数据到working directory

git checkout HEAD #------------------从history中直接下载历史版本到working directory

git commit -am 'third commit' #-----------------直接由working directory提交到history

删除数据:

git rm file #--------------------删除文件

git rm --cached test2.py #---------------仅删除staging中的文件,此命令主要用来修改文件名

stash暂存

git stash #-------------暂存当前正在进行的工作

git stash list #----------------查看之前的暂存区

git stash pop #----------------取出上次的暂存

branch分支

git branch #----------------查看分支 (*表示当前的 branch)

git branch newidea #----------------创建分支

git checkout newidea #---------------切换新的分支

cat .git/HEAD #----------------查看当前HEAD指向newidea分支

ls .git/refs/heads/ #----------查看现有分支的存储情况

cat .git/refs/heads/* #---------------查看两个分支都指向同一个commit history

git branch -d newidea #-----------------删除指定分支

git checkout -b newcode #------------------参数-b 作用:检查如果没有branch则新建立

git merge newcode #--------------合并分支

git merge bugfix #--------------合并分支Auto-merging test1.py

猜你喜欢

转载自www.linuxidc.com/Linux/2018-05/152153.htm