Gitee Git bash VSCode操作简易说明
<p><strong><span style="font-size: 18pt">GIT</span></strong></p><p>Git是一个分布式的版本控制系统,只是软件,需要你下载装到电脑上,实现git功能。</p>
<p>Github、Gitee基于git的项目托管平台。Github是国外的,连接速度因人而异;另外Github收费用户才能创建私有项目。</p>
<p><strong><span style="font-size: 18pt">准备内容</span></strong></p>
<ol>
<li>注册码云(Gitee),创建一个项目,得到项目url:https://gitee.com/YourGiteeName/projectname。https://gitee.com/signup</li>
<li>下载git, 默认安装。https://git-scm.com/downloads</li>
<li>下载安装VSCode。https://code.visualstudio.com/</li>
</ol>
<p align="left"><span style="font-size: 18pt"><strong>一、</strong><strong>生成ssh公钥</strong></span></p>
<p><strong>1.打开Git Bash,按如下命令来生成 sshkey:</strong></p>
<p align="left">Administrator@JOY MINGW64 ~</p>
<p align="left"><span style="background-color: rgba(255, 255, 153, 1)"><strong>$ ssh-keygen -t rsa -C <span style="color: rgba(255, 0, 0, 1)">joypoint@qq.com</span></strong></span></p>
<p style="margin-left: 30px" align="left">Generating public/private rsa key pair.</p>
<p style="margin-left: 30px" align="left">Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):</p>
<p style="margin-left: 30px" align="left">Created directory '/c/Users/Administrator/.ssh'.</p>
<p style="margin-left: 30px" align="left">Enter passphrase (empty for no passphrase):</p>
<p style="margin-left: 30px" align="left">Enter same passphrase again:</p>
<p style="margin-left: 30px" align="left">Your identification has been saved in /c/Users/Administrator/.ssh/id_rsa.</p>
<p style="margin-left: 30px" align="left">Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub.</p>
<p style="margin-left: 30px" align="left">The key fingerprint is:</p>
<p style="margin-left: 30px" align="left">SHA256:UoQbxa…… joypoint@qq.com</p>
<p style="margin-left: 30px" align="left">The key's randomart image is:</p>
<p style="margin-left: 30px" align="left">+-------+</p>
<p style="margin-left: 30px" align="left">| .o=.o.+o o .. |</p>
<p style="margin-left: 30px" align="left">| . =o o. |</p>
<p style="margin-left: 30px" align="left">| +. +*+.. . |</p>
<p style="margin-left: 30px" align="left">|. . o o |</p>
<p style="margin-left: 30px" align="left">|.+ + . o |</p>
<p style="margin-left: 30px" align="left">|o = . |</p>
<p style="margin-left: 30px" align="left">|.o . |</p>
<p style="margin-left: 30px" align="left">|.o . |</p>
<p style="margin-left: 30px" align="left">| o |</p>
<p style="margin-left: 30px" align="left">+---------+</p>
<p align="left">还可以用以下命令指定id_rsa的别名,用以配置多个SSH-Key:</p>
<p style="margin-left: 30px" align="left"><strong><em>$ </em>ssh-keygen -t rsa -C 'xxxxx@company.com' -f ~/.ssh/gitee_id_rsa</strong></p>
<p align="left"> </p>
<p><strong>2.查看 public key:</strong></p>
<p align="left">Administrator@JOY MINGW64 ~</p>
<p align="left"><span style="background-color: rgba(255, 255, 153, 1)"><strong>$ cat ~/.ssh/id_rsa.pub</strong></span></p>
<p style="margin-left: 30px" align="left">cat: /c/Users/Administrator/: Is a directory</p>
<p style="margin-left: 30px" align="left">ssh-rsa AAAAB3NzaC1y…… joypoint@qq.com</p>
<p align="left">打开码云SSH公钥管理页面: https://gitee.com/profile/sshkeys</p>
<p style="margin-left: 30px" align="left">填写标题,如:yourname's SSH key</p>
<p style="margin-left: 30px" align="left">复制公钥,如:ssh-rsa UoQbxa……</p>
<p style="margin-left: 30px" align="left">添加后,回到Git Bash中继续其他操作。</p>
<p align="left"> </p>
<p><strong>3.用ssh命令测试是否配置成功:</strong></p>
<p align="left">Administrator@JOY MINGW64 ~</p>
<p align="left"><span style="background-color: rgba(255, 255, 153, 1)"><strong>$ ssh -T git@gitee.com</strong></span></p>
<p style="margin-left: 30px" align="left">The authenticity of host 'gitee.com (120.55.226.24)' can't be established.</p>
<p style="margin-left: 30px" align="left">ECDSA key fingerprint is SHA256:FQGC9…….</p>
<p style="margin-left: 30px" align="left">Are you sure you want to continue connecting (yes/no/)? <span style="background-color: rgba(255, 255, 153, 1)"><strong>Yes</strong></span></p>
<p align="left"> </p>
<p style="margin-left: 30px" align="left">Warning: Permanently added 'gitee.com,120.55.226.24' (ECDSA) to the list of known hosts.</p>
<p style="margin-left: 30px" align="left">Hi JoyPoint! You've successfully authenticated, but GITEE.COM does not provide shell access.</p>
<p align="left"> </p>
<p align="left"><span style="font-size: 18pt"><strong>二、</strong><strong>Git</strong><strong>操作-初始化Git</strong></span></p>
<p align="left">Administrator@JOY MINGW64 ~</p>
<p align="left"><span style="background-color: rgba(255, 255, 153, 1)"><strong>$ git config --global user.name <span style="color: rgba(255, 0, 0, 1)">JoyPoint</span></strong></span></p>
<p align="left">Administrator@JOY MINGW64 ~</p>
<p align="left"><span style="background-color: rgba(255, 255, 153, 1)"><strong>$ git config --global user.email <span style="color: rgba(255, 0, 0, 1)">JoyPoint@qq.com</span></strong></span></p>
<p align="left"><span style="font-size: 18pt"><strong>三、</strong><strong>创建版本库</strong></span></p>
<p><strong>1.首先,选择一个合适的地方,创建一个空目录YourProjName(名字任意):</strong></p>
<p align="left">Administrator@JOY MINGW64 /</p>
<p align="left"><span style="background-color: rgba(255, 255, 153, 1)"><strong>$ cd /c/</strong></span></p>
<p align="left">Administrator@JOY MINGW64 /c</p>
<p align="left"><span style="background-color: rgba(255, 255, 153, 1)"><strong>$ mkdir <span style="color: rgba(255, 0, 0, 1)">helloGIT</span></strong></span></p>
<p align="left">Administrator@JOY MINGW64 /c</p>
<p align="left"><span style="background-color: rgba(255, 255, 153, 1)"><strong>$ cd <span style="color: rgba(255, 0, 0, 1)">helloGIT</span></strong></span></p>
<p align="left"> <strong><em>(在第一次创建并初始化版本库以后,再次需要修改该库时,只需要在git bash中进入该目录即可,并可以用git remote -v查看关联情况)</em></strong></p>
<p align="left"> </p>
<p><strong>2.第二步,通过git init命令把这个目录变成Git可以管理的仓库:</strong></p>
<p align="left">Administrator@JOY MINGW64 /c/helloGIT</p>
<p align="left"><span style="background-color: rgba(255, 255, 153, 1)"><strong>$ git init</strong></span></p>
<p style="margin-left: 30px" align="left">Initialized empty Git repository in C:/helloGIT/.git/</p>
<p align="left"> </p>
<p align="left"><span style="font-size: 18pt"><strong>四、</strong><strong>关联远程仓库</strong></span></p>
<p><strong>1.把一个本地仓库与一个云端Gitee仓库关联:</strong></p>
<p style="margin-left: 30px" align="left">项目地址形式为:https://gitee.com/YourGiteeName/YourProjName.git 或者 git@gitee.com:YourGiteeName/YourProjName.git</p>
<p align="left">Administrator@JOY MINGW64 /c/helloGIT (master)</p>
<p align="left"><span style="background-color: rgba(255, 255, 153, 1)"><strong>$ git remote add origin https://gitee.com/<span style="color: rgba(255, 0, 0, 1)">JoyPoint/helloGIT</span>.git</strong></span></p>
<p style="margin-left: 30px" align="left"># 如果你发现地址关联有错,或想关联其他仓库,可以执行以下命令重新设置关联地址:</p>
<p style="margin-left: 30px" align="left"><strong># $ git remote set-url origin https://gitee.com/<span style="color: rgba(255, 0, 0, 1)">JoyPoint/helloGIT</span>.git</strong></p>
<p style="margin-left: 30px" align="left"># 但一定要注意字母的大小写,以及文本双引号问题!!!</p>
<p><strong>2.查看关联细节:</strong></p>
<p align="left">Administrator@JOY MINGW64 /c/helloGIT (master)</p>
<p align="left"><span style="background-color: rgba(255, 255, 153, 1)"><strong>$ git remote -v</strong></span></p>
<p align="left">origin https://gitee.com/JoyPoint/helloGIT.git (fetch)</p>
<p align="left">origin https://gitee.com/JoyPoint/helloGIT.git (push)</p>
<p align="left"><span style="font-size: 18pt"><strong>五、</strong><strong>同步(拉取)</strong></span></p>
<p style="margin-left: 30px" align="left">同步,也可以称之为拉取,在Git中是非常频繁的操作,为了保证代码一致性,尽可能的在每次操作前进行一次同步操作,在工作目录下执行如下命令:</p>
<p align="left">Administrator@JOY MINGW64 /c/helloGIT (master)</p>
<p align="left"><span style="background-color: rgba(255, 255, 153, 1)"><strong>$ git pull origin master</strong></span></p>
<p style="margin-left: 30px" align="left">remote: Enumerating objects: 6, done.</p>
<p style="margin-left: 30px" align="left">remote: Counting objects: 100% (6/6), done.</p>
<p style="margin-left: 30px" align="left">remote: Compressing objects: 100% (6/6), done.</p>
<p style="margin-left: 30px" align="left">remote: Total 6 (delta 0), reused 0 (delta 0)</p>
<p style="margin-left: 30px" align="left">Unpacking objects: 100% (6/6), done.</p>
<p style="margin-left: 30px" align="left">From https://gitee.com/JoyPoint/helloGIT</p>
<p style="margin-left: 30px" align="left"> * branch master -> FETCH_HEAD</p>
<p style="margin-left: 30px" align="left"> * master -> origin/master</p>
<p align="left"> </p>
<p align="left">Administrator@JOY MINGW64 /c/helloGIT (master)</p>
<p align="left"><span style="background-color: rgba(255, 255, 153, 1)"><strong>$ git status</strong></span></p>
<p style="margin-left: 30px" align="left">On branch master</p>
<p style="margin-left: 30px" align="left">Changes not staged for commit:</p>
<p style="margin-left: 30px" align="left"> (use "git add <file>..." to update what will be committed)</p>
<p style="margin-left: 30px" align="left"> (use "git restore <file>..." to discard changes in working directory)</p>
<p style="margin-left: 30px" align="left"> modified: README.md</p>
<p style="margin-left: 30px" align="left">no changes added to commit (use "git add" and/or "git commit -a")</p>
<p align="left"><span style="font-size: 18pt"><strong>六、</strong><strong>提交</strong></span></p>
<p style="margin-left: 30px" align="left">git作为支持分布式版本管理的工具,它管理的库(repository)分为本地库、远程库。如有本地库源码文件发生修改,需要将修改提交到远程库,这时需要暂存 (add)、提交(commit)、推送(push)三步:</p>
<p align="left">Administrator@JOY MINGW64 /c/helloGIT (master)</p>
<p align="left"><span style="background-color: rgba(255, 255, 153, 1)"><strong>$ git add -A</strong></span></p>
<p style="margin-left: 30px" align="left">warning: LF will be replaced by CRLF in README.md.</p>
<p style="margin-left: 30px" align="left">The file will have its original line endings in your working directory</p>
<p align="left"> </p>
<p align="left">Administrator@JOY MINGW64 /c/helloGIT (master)</p>
<p align="left"><span style="background-color: rgba(255, 255, 153, 1)"><strong>$ git commit -m "</strong><strong>注意名称大小写和全角标点符号</strong><strong>"</strong></span></p>
<p style="margin-left: 30px" align="left"> 注意名称大小写和全角标点符号</p>
<p style="margin-left: 30px" align="left"> 1 file changed, 7 insertions(+), 5 deletions(-)</p>
<p align="left"> </p>
<p align="left">Administrator@JOY MINGW64 /c/helloGIT (master)</p>
<p align="left"><span style="background-color: rgba(255, 255, 153, 1)"><strong>$ git push origin master</strong></span></p>
<p style="margin-left: 30px" align="left">Enumerating objects: 5, done.</p>
<p style="margin-left: 30px" align="left">Counting objects: 100% (5/5), done.</p>
<p style="margin-left: 30px" align="left">Delta compression using up to 4 threads</p>
<p style="margin-left: 30px" align="left">Compressing objects: 100% (3/3), done.</p>
<p style="margin-left: 30px" align="left">Writing objects: 100% (3/3), 416 bytes | 416.00 KiB/s, done.</p>
<p style="margin-left: 30px" align="left">Total 3 (delta 2), reused 0 (delta 0)</p>
<p style="margin-left: 30px" align="left">remote: Powered By Gitee.com</p>
<p style="margin-left: 30px" align="left">To https://gitee.com/JoyPoint/helloGIT.git</p>
<p style="margin-left: 30px" align="left"> babe7af..ee5acbb master -> master</p>
<p align="left"><span style="font-size: 18pt"><strong>七、</strong><strong>VSCode</strong><strong>中使用git</strong></span></p>
<p><strong>1.点击 文件 > 将文件夹添加到工作区 > E:/YourProjName/ 就完成了。</strong></p>
<p align="left">无需任何配置,VSCode自动获取.git配置实现代码管理: 发生变动的文件或代码会有颜色提示。</p>
<p><strong>2.同步远程仓库: </strong></p>
<ul>
<li>选择源控制栏(Source Control)中需要上传的文件,点击+号,暂存 (add);</li>
<li>在[ 消息 (按 Ctrl+Enter 提交) ]中输入注释, 提交(commit); </li>
<li>点击更多动作中的push图标,推送(push)。</li>
</ul>
<p align="left"> _____________________</p>
<div class="cnblogs_Highlighter">
<pre class="brush:javascript;gutter:true;"># Git配置多个SSH-Key
# 在 ~/.ssh 目录下新建一个config文件,添加如下内容(其中Host和HostName填写git服务器的域名,IdentityFile指定私钥的路径)
# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_id_rsa
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa<br>
</pre>
</div>
<p>______________________</p>
<div class="cnblogs_code">
<pre>基于码云的协同开发实践
需求描述如下:
1.有一个基础性工程共享给大家使用,缺省不允许使用者直接提交修改
2.使用者在使用过程中会发现问题,需要及时修改基础工程代码
3.修改的代码需要提交给基础工程管理者审核后合并
4.使用者需要更新最新的基础工程代码并再此上继续工作
基于这个应用场景,可采用码云平台提供的fork和pull request(PR)结合来完成,具体操作步骤如下:
1.首先使用者需要复制一份基础工程到自己的用户下,Fork,在自己的用户下生成工程副本。
2.然后使用git客户端下载自己用户下的工程副本进行使用,进行修改并提交。
3.提交后进入自己用户下的工程副本页面,进入Pull Requests页面,并点击新建Pull Request按钮,创建PR,在创建PR页面中,添加修改的内容说明,并指定审查人员,点击创建。
4.等到基础工程审查人员审查结束后,代码已经进行了合并,有了新的版本,此时使用者可以进入自己的工程副本首页,点击工程名边上的强制刷新按钮获取工程的最新版本。</pre>
</div>
<p>——————————————</p>
<div class="cnblogs_code">
<pre>$ git push origin master
remote: You do not have permission to push to the repository via HTTPS
fatal: Authentication failed for 'https://gitee.com/someuser/someproject.git/'
————————————————————————————————
原因分析:
原因之一:
这是由于没有设置Gitee的SSH公钥。在未设置SSH公钥的情况下,可以使用git clone Gitee上的项目,但是不能git push项目到Gitee上,如果想push项目到Gitee,那么必须配置SSH公钥。
解决方法:
生成公钥和配置公钥,可以参考Gitee帮助:https://gitee.com/help/articles/4191 。
原因之二:
可能是这台电脑以前使用过git,所以windows保存的账号和密码是其他人的,所以需要进行修改账号和密码:
解决方法:
(一)进入控制面板
(二)选择用户账户
(三)选择管理你的凭据
(四)选择Windows凭据
(五)选择git保存的用户信息
(六)选择编辑或者进行删除操作
(七)完成</pre>
</div>
<p>——————————————</p>
<div class="cnblogs_code">
<pre>在同一台电脑上切换不同gitee账号:
1、不同用户账号对应的秘钥对均已在本地生成好,并在gitee.com中添加;
2、.ssh/config文件中IdentityFile修改为对应账号的rsa;
3、git config --global user.name\email 为对应账号用户名和邮箱;
4、检查控制面板-用户账户-管理你的凭据-普通凭据,确保凭证匹配或为空;
5、用ssh -T git@gitee.com检查是否已关联对应账号;如果没有请检查上面几步。
6、后续按照常规步骤关联本地目录和远程库;
7、git pull 后,如出现没有readme.MD文件的问题,可以用$ git pull --rebase origin master再次拉取一次。</pre>
</div>
<p> -------------------------------------------</p>
<div class="cnblogs_code">
<pre>实现同一本地仓库与 Gitee 和 GitHub 两个远程库同步更新
将本地的代码仓库与 Gitee 和 GitHub 两个远程库同时关联,即可实现本地仓库与两个远程库的同步更新
具体方法操作如下:</pre>
<p>1:移除现在旧有的远程服务器origin</p>
<div class="cnblogs_code">
<pre>git remote rm origin</pre>
</div>
<p>2:关联gitosc远程库</p>
<div class="cnblogs_code">
<pre>git remote add gitee https:<span style="color: rgba(0, 128, 0, 1)">//<span style="color: rgba(0, 128, 0, 1)">gitee.com/xxx/xxx.git</span></span></pre>
</div>
<div class="cnblogs_code">
<pre>git push -u gitosc master</pre>
</div>
<p>关联github远程库</p>
<div class="cnblogs_code">
<pre> git remote add github https:<span style="color: rgba(0, 128, 0, 1)">//<span style="color: rgba(0, 128, 0, 1)">github.com/xxx/xxx.git</span></span></pre>
</div>
<div class="cnblogs_code">
<pre>git push -u github master</pre>
</div>
<pre>现在,用git remote -v查看远程库信息,可以看到两个远程库: <br>git remote -v <br>gitee git@gitee.com:xxx/xxx.git (fetch) <br>gitee git@gitee.com:xxx/xxx.git (push) <br>github git@github.com:xxx/xxx.git (fetch) <br>github git@github.com:xxx/xxx.git (push) <br><br>(2)同步更新 如果要推送到 GitHub,使用命令: <br>git push GitHub 分支名 <br>eg:git push GitHub master <br>如果要推送到 Gitee,使用命令: <br>git push Gitee 分支名 <br>可能提示push失败这里可以尝试用强制push: <br>$ git push github master -f <br>/* 由于是初始化项目,并从远程仓库pull,使用强制推送不会对项目造成影响 一般不推荐强制push */ <br><br>至此,本地库就可以同时与多个远程库互相同步</pre>
</div>
<p> ---------------------------------------</p>
<div class="cnblogs_code">
<pre>* 强制拉取
该命令直接放弃所有修改代码,并更新到版本库最新版本代码;
git fetch --all
git reset --hard origin/master
git pull</pre>
</div>
<div class="cnblogs_code">
<pre>* 克隆分支
git clone -b <branchname> <remote-repo-url> <local-repo-name></pre>
</div>
<p> </p>
<hr>
<p> </p>
<p>参考:</p>
<p>https://blog.csdn.net/watfe/article/details/79761741</p>
<p>https://gitee.com/help/articles/4229#article-header0</p><br><br>
来源:https://www.cnblogs.com/JoyPoint/p/11582879.html
頁:
[1]