Git初使用基本指南

141 阅读2分钟
# Git初使用

教程参考了该网址:https://blog.csdn.net/Maiduoudo/article/details/96998986

## 码云里的操作

在码云中直接新建仓库:

![新建仓库](https://upload-images.jianshu.io/upload_images/17172207-08c73bba84be5062.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)



创建成功后的代码仓库
![仓库图片](https://upload-images.jianshu.io/upload_images/17172207-97a3a15710a10347.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


## git中的操作

此时码云的操作已经全部搞定,我们转头去git软件进行操作,默认的场景是git软件已经安装并且是第一次使用.

我们打开git软件:

![打开git.png](https://upload-images.jianshu.io/upload_images/17172207-6241521a4e464ea3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


打开后的界面如下:

![git界面](https://upload-images.jianshu.io/upload_images/17172207-8ee7670c40f07236.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


### 第一次的登录操作

第一次的操作:

> $ git config --global user.name "xxx"
>
> $ git config --global user.email "xxxxx@xx.com"
>
> 用户名:"xxx",可以填写您的码云的用户名
>
> 邮箱:"xxxxx@xx.com",可以填写您的码云账号的邮箱,
>
> 用户名和邮箱也可以填写别的(只要是用户名和邮箱格式就OK)。



$ git config --global user.name "风中追风"

$ git config --global user.email "2982756362@qq.com"

![代码图片](https://upload-images.jianshu.io/upload_images/17172207-a1081248464bf1da.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


记得中间有空格,还有$符号的问题

### 生成密钥

> $ ssh-keygen -t rsa -C “xxxx@xxx.xxx”
>
> 生成密钥语句

$ ssh-keygen -t rsa -C “2982756362@qq.com”

输入完这一句就行了,接下来按三下回车,让你输入什么都别输入,**三次回车!!!!!**

![三次回车!!!](https://upload-images.jianshu.io/upload_images/17172207-1c2ccabe78a53b69.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


此时密钥已经生成,使用以下命令查看生成密钥:

> $ cat ~/.ssh/id_rsa.pub

如果你不知道**之前自己电脑是否生成过密钥**,使用该命令查看:

> ```git
> $ cd ~/.ssh
> $ ls
> ```

![密钥](https://upload-images.jianshu.io/upload_images/17172207-882046d00715bafd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

自己把生成的密钥复制,我们回码云进行操作

## 码云里的密钥操作

先把生成的密钥复制后,我们接着回码云里操作

头像下点击设置

![设置](https://upload-images.jianshu.io/upload_images/17172207-12b1435c93189f12.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


点击SSH公钥

![SSH公钥](https://upload-images.jianshu.io/upload_images/17172207-91405cbe010c84f3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


进入SSH公钥的设置界面
![设置界面](https://upload-images.jianshu.io/upload_images/17172207-778197f441d12fe0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


标题随便起,把生成的公钥粘贴到下面,点击确定
![公钥粘贴](https://upload-images.jianshu.io/upload_images/17172207-bf4b80f251740bf1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)



## 传入项目

进入自己新建的空项目地址:

![空项目.png](https://upload-images.jianshu.io/upload_images/17172207-2086ac2d81df9627.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


在自己电脑本地新建个空文件夹,右键git bash

![git bash](https://upload-images.jianshu.io/upload_images/17172207-84f86be12d9413d2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


接着会弹出的命令框,输入该命令打回车

> $ git init

![git init](https://upload-images.jianshu.io/upload_images/17172207-a0c416bc453a139a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


空文件里就会有一个隐藏文件夹.git

![.git](https://upload-images.jianshu.io/upload_images/17172207-9c60f0cbf8661e39.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)



接下来我们把码云的远程项目同步到这个文件夹里

把码云的网址复制

![克隆](https://upload-images.jianshu.io/upload_images/17172207-02577eed94721832.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![复制网址](https://upload-images.jianshu.io/upload_images/17172207-d77154f613bb798c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)



复制网址后在框里输入以下代码

> $ git remote add origin 你复制的网址

$ git remote add origin https://gitee.com/ljy521/kuafuoa.git

![复制网址](https://upload-images.jianshu.io/upload_images/17172207-6f34ed8324c011b0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


接着把码云上的远程仓库中的代码同步到本地

> git pull origin master

![同步](https://upload-images.jianshu.io/upload_images/17172207-d22f7f98f868938c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


接着你会发现本地文件夹多了些东西

![效果](https://upload-images.jianshu.io/upload_images/17172207-b247f3f5f0063240.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


## 上传项目

这时我们去其他地方把代码给粘贴过来,我们给码云仓库上传

本地文件夹多了一堆东西啊

![效果](https://upload-images.jianshu.io/upload_images/17172207-0609954e944d23df.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


接着在命令行里输入,上传到本地master里(此时可不是上传到码云里):

> git add .

注意中间有空格

![添加文件](https://upload-images.jianshu.io/upload_images/17172207-6b564d4335894a43.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


接着我们提交一下

> $ git commit -m "项目架构"
>
> xxx为注释

![提交](https://upload-images.jianshu.io/upload_images/17172207-d69511a231a0fa69.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


最后我们提交到码云的远程的仓库里

>  $ git push origin master
>
> 这是最后从本地的仓库里,上传到远程的仓库里

![输入用户名和密码](https://upload-images.jianshu.io/upload_images/17172207-cf38162dcd64fcd5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

在弹出的框中输入自己**码云的账号和密码**

点击确定

就看到最后的效果了

![最终效果](https://upload-images.jianshu.io/upload_images/17172207-fa06c73353b19b76.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)