Linux 创建本地仓设置SSH公钥,创建分支,连接远程仓,提交代码流程

一、创建本地仓

1.首先查看下git版本 没有的先下载git

root@ubuntu:/home/code/test# git --version
git version 2.25.1
root@ubuntu:/home/code/test# ^C
root@ubuntu:/home/code/test# 

2.在当前路径中输入git status 查看git本地仓是否建好

root@ubuntu:/home/code# git status
fatal: not a git repository (or any of the parent directories): .git
root@ubuntu:/home/code# 

3.输入git init 创建本地仓 然后进入.git目录中,不要随便乱动里面的配置要不本地仓就会错乱。现在就创建好本地仓啦。

root@ubuntu:/home/code# git init
Initialized empty Git repository in /home/code/.git/
root@ubuntu:/home/code# cd .git/
root@ubuntu:/home/code/.git# ls
branches  config  description  HEAD  hooks  info  objects  refs
root@ubuntu:/home/code/.git# 

二、设置本地仓的用户名和邮件,要不远程仓不认识本地仓,名字和邮箱随便设置。自己知道就OK.比如设置名称为:xiao ming

git config --global user.name "xiao ming"
git config --global user.email "XXX@163.com"

 三、把本地仓和远程仓连接起来,输入git remote -v 查看是否有连接,如下就没有任何连接

root@ubuntu:/home/code/test# git remote -v
root@ubuntu:/home/code/test# 

      输入git remote add origin + //远程仓库地址 比如远程地址:git@gitlab.xxx.cn:xxx/xxxx.git

root@ubuntu:/home/code/test# git remote add origin git@gitlab.xxxx.cn:xxx/xxx.git

root@ubuntu:/home/code/test# git remote -v
origin	git@gitlab.xxx.cn:xxx/xxx.git (fetch)
origin	git@gitlab.xxx.cn:xxx/xxx.git (push)
root@ubuntu:/home/code/test# 

现在已经把远程仓和本地仓建立连接啦,下面把公钥配置下 

四、配置公钥ssh 如果不配置以后每次都得输入gitlab 密码 可以提交代码和下载代码

在需要克隆的路径中输入:ssh-keygen 然后一路按确认键

root@ubuntu:/home/code/test# 
root@ubuntu:/home/code/test# git --version
git version 2.25.1
root@ubuntu:/home/code/test# 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:lC+shDO+tiWyxXUGs3AS4XHxyeW5y91hlaU0/9pSeo8 root@ubuntu
The key's randomart image is:
+---[RSA 3072]----+
|    +.o.  .   o .|
|   . + o = . . +o|
|    + + * o   ..o|
|     = * . .   ..|
|    + + S o   o o|
|   o = + o o o * |
|  . = o   o . = o|
|   +.+         +.|
|  ..o.        E .|
+----[SHA256]-----+
root@ubuntu:/home/code/test# ls

2.然后进入cd root  再进入.ssh文件中

root@ubuntu:~# cd .ssh/
root@ubuntu:~/.ssh# ls
id_rsa  id_rsa.pub  known_hosts
root@ubuntu:~/.ssh# vi id_rsa.pub 
root@ubuntu:~/.ssh# cd /home/

发现出现三个文件,然后进入vi   id_rsa.pub 复制内容,这个是密钥,得把这个密钥复制到gitlab,这样gitlab就会认得本地仓,

 

注意!!!千万不要把密钥放在项目里的Deploy key选项里,这样只能clone 代码,这是只读的,当时我还纳闷明明已经加上去啦,但是就是不能push 代码,当仔细查看提示后,发现放在项目里的公钥是只读的。以下就是我设置只读的公钥。当然如果要只读那也可以按以下这样设置。

 

然后点击AddKey按钮就可以啦

五、拉代码,要不创建分支会出现问题,创建完分支后我们提交代码。输入git clone + 地址 

例如:git clone git@gitlab.xxx.cn:xxx/xxx.git

root@ubuntu:/home/code/test# git clone git@gitlab.xxx.cn:xxx/xxx.git
Cloning into 'new_vehicle'...
remote: Counting objects: 352, done.
remote: Compressing objects: 100% (278/278), done.
remote: Total 352 (delta 109), reused 248 (delta 57)
Receiving objects: 100% (352/352), 11.47 MiB | 211.00 KiB/s, done.
Resolving deltas: 100% (109/109), done.

 

六、创建分支,在远程仓有个主分支master 这个分支是一个工程正式上线的分支,一般正式版本提交到master 分支,但是如果我们平常要修改代码,测试工程一般得重新建个分支用来我们平常修改,经常提交的分支,比如我新建个名称为8.2的分支。我们先测试本地仓有几个分支输入:git branch  可以看到分支总数  然后我们创建分支:git branch 8.2   这时候本地仓还在master分支上,我们切换到8.2分支:git checkout 8.2                      ( 星号指向8.2啦)

root@ubuntu:/home/code/test# git branch
* master
root@ubuntu:/home/code/test# git branch 8.2
root@ubuntu:/home/code/test# git branch
  8.2
* master
root@ubuntu:/home/code/test# git checkout 8.2
Switched to branch '8.2'
root@ubuntu:/home/code/test# git branch
* 8.2
  master

 

七、把代码上传到远程仓 把需要推送的代码放进刚才的路径中,输入 git add +文件名 example:git add 8.2,然后要记录这次提交的版本信息 git commit -a -m "第一次修改"。

root@ubuntu:/home/code/test# git add 8.2/
root@ubuntu:/home/code/test# git commit -a -m "8.2"

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'root@ubuntu.(none)')
root@ubuntu:/home/code/test# git config --global user.email "xxxxxxx@163.com"
root@ubuntu:/home/code/test# git config --global user.name "ThinkPad"
root@ubuntu:/home/code/test# git commit -a -m "8.2"
[master (root-commit) 7e38c96] 8.2
 179 files changed, 25009 insertions(+)
 create mode 100644 8.2/pduVehicle/.vscode/settings.json
 create mode 100755 8.2/pduVehicle/Makefile
 create mode 100755 8.2/pduVehicle/README.en.md
 create mode 100755 8.2/pduVehicle/README.md
 create mode 100755 8.2/pduVehicle/begin.sh
 create mode 100755 8.2/pduVehicle/include/blackBox.h
 create mode 100755 8.2/pduVehicle/include/calculateTask.h

 create mode 100755 8.2/pduVehicle/vehicle
root@ubuntu:/home/code/test# git push
fatal: The current branch 8.2 has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin 8.2

root@ubuntu:/home/code/test# git push --set-upstream origin 8.2
Enumerating objects: 193, done.
Counting objects: 100% (193/193), done.
Delta compression using up to 2 threads
Compressing objects: 100% (187/187), done.
Writing objects: 100% (193/193), 6.99 MiB | 524.00 KiB/s, done.
Total 193 (delta 4), reused 0 (delta 0)
To gitlab.neolix.cn:space/new_vehicle.git
 * [new branch]      8.2 -> 8.2
Branch '8.2' set up to track remote branch '8.2' from 'origin'.
root@ubuntu:/home/code/test# 


 

然后 git push 当出现如图 fatal: The current branch master has no upstream branch.时候说明我远程仓库有多个分支,不知道要上传到哪个分支,这个时候只需要按照提示:git push --set-upstream origin 8.2,输入到终端命令即可。

好啦从新建本地仓到把代码push 到远程仓一个流程已经完成,努力的你可能还会遇到本文中没有的情况,相信聪明的你能完美解决。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值