前提,你首先要在码云上注册一个账号
下载安装Git
这里选择的是客户端安装版,地址:https://git-scm.com/download
一直下一步即可,安装完成后
桌面右键如下:Git GUI Here(图形用户界面) 和GIT Batch Here(命令行终端)
根据自己的电脑选择相应的Git客户端,如下图,有mac、win、linux三个版本的,我们选择的是win版本的
生成本机的SSH公钥,点击刚才安装的GIT,进入命令行
注意:关于SSH密钥可以参考我的这篇博文ssh-keygen - 认证密钥生成、管理和转换(基于FreeBSD 12.1)
yaosht@yaosht-PC MINGW64 ~
$ cd ~/.ssh
##No such file or directory表示本机没有密钥
bash: cd: /c/Users/yaosht/.ssh: No such file or directory
yaosht@yaosht-PC MINGW64 ~
##生成rsa类型的密钥,注释为giteePublicKey
$ ssh-keygen -t rsa -C "giteePublicKey"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/yaosht/.ssh/id_rsa):
Created directory '/c/Users/yaosht/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
##rsa私钥、公钥保存的文件id_rsa、id_rsa.pub
Your identification has been saved in /c/Users/yaosht/.ssh/id_rsa.
Your public key has been saved in /c/Users/yaosht/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Y1iFrEEPV7+6MoS+m0VXNPQl9qqprGfkPZ9NcyWWhuM giteePublicKey
The key's randomart image is:
+---[RSA 3072]----+
| .o..oo.+ o .|
| .+o. o + + |
| oo o . .|
| .o . o o |
| ..S . + * .|
| .o.o.o * ..|
| . ..o..E .o|
| .oo.+oo +o|
| +o.*+ oo .|
+----[SHA256]-----+
yaosht@yaosht-PC MINGW64 ~
##查看显示公钥
$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDsY6YmPBbN+rvKSHwy8eDdR+lwGJz//tete8R4cy9jbdZKgg1s/tRBAqtoEaeqFGoQ3tQj2VFYtcbHl+uXrqetn3cWtU5UM2ZfECKPBiiMMfPy5APS4e+h9i+kVBigpBJuAEaRWzyqhcw7qWVnlkEIMPMOLE2IBRgHyiPzOiQpQcpU+GSAAU5EdMtC8L6gxhvJ+PRRHhDRIdxvuQuNheR768nsQfUFNF0mFCrUqulN1X25o6nZGiWmab+0mfrsMnwf6gXWiW4xpXnPVZCzhTcoF+xCl4fBZSC37MQUsxE5tv1g1WwQ6231kcrc0K3haz9GDrlCWgO2BrDkqixgq+7iu/cA1xvu/b3TZsoMHMAABiehqSiNPUtpjxe/8QMe2WaOlRuu7UHxerUf0rE+BzB0XKV1+4/lzsxvbn6MpcJ7E4IZswzwp1wZ4y5T1AhCJZ2lIw7h9EqV++hia+SjSWS11Q3v3CKdJoMueEBIC0Y1Obu3/iXUEJKpBvq+BH3R2Ms= giteePublicKey
yaosht@yaosht-PC MINGW64 ~
$
在码云上添加SSH公钥
创建远程仓库,仓库名叫test
我们进行一些GIT的操作
yaosht@yaosht-PC MINGW64 ~
$ git config --global user.name "prince"
yaosht@yaosht-PC MINGW64 ~
$ git config --global user.email "[email protected]"
yaosht@yaosht-PC MINGW64 ~
$ mkdir gitclient
yaosht@yaosht-PC MINGW64 ~
$ cd gitclient
#将gitclient初始化为Git代码库
yaosht@yaosht-PC MINGW64 ~/gitclient
$ git init
Initialized empty Git repository in C:/Users/yaosht/gitclient/.git/
##创建README.md文件
yaosht@yaosht-PC MINGW64 ~/gitclient (master)
$ touch README.md
##添加指定文件到暂存区
yaosht@yaosht-PC MINGW64 ~/gitclient (master)
$ git add README.md
##提交暂存区的指定文件到仓库区
yaosht@yaosht-PC MINGW64 ~/gitclient (master)
$ git commit -m "first commit"
[master (root-commit) 1edf3a8] first commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README.md
##增加一个新的远程仓库,并命名为origin
yaosht@yaosht-PC MINGW64 ~/gitclient (master)
$ git remote add origin https://gitee.com/anna-prince/gitclient.git
yaosht@yaosht-PC MINGW64 ~/gitclient (master)
$ git remote show origin
* remote origin
Fetch URL: https://gitee.com/anna-prince/gitclient.git
Push URL: https://gitee.com/anna-prince/gitclient.git
HEAD branch: master
Remote branch:
master new (next fetch will store in remotes/origin)
Local ref configured for 'git push':
master pushes to master (local out of date)
yaosht@yaosht-PC MINGW64 ~/gitclient (master)
$ git push origin master
To https://gitee.com/anna-prince/gitclient.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://gitee.com/anna-prince/gitclient.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
##由于远程包含您在本地没有的工作,因此更新被拒绝。 这通常是由另一个存储库推送到相同的引用引起的
##出现上述的rejected是因为我们刚在在码云上创建了的gitclient中已经有README.md这个文件,并且还有其他的文件,但是在本地仓库是没有的
##查看远程仓库信息
yaosht@yaosht-PC MINGW64 ~/gitclient (master)
$ git remote -v
origin https://gitee.com/anna-prince/gitclient.git (fetch)
origin https://gitee.com/anna-prince/gitclient.git (push)
##在本地创建了一个test.txt文件
yaosht@yaosht-PC MINGW64 ~/gitclient (master)
$ touch test.txt
yaosht@yaosht-PC MINGW64 ~/gitclient (master)
$ git add test.txt
yaosht@yaosht-PC MINGW64 ~/gitclient (master)
$ git commit -m "test.txt commit"
[master 634c40f] test.txt commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
##推送给远程仓库的时候还是有问题,原因还是上面所述
yaosht@yaosht-PC MINGW64 ~/gitclient (master)
$ git push origin master
To https://gitee.com/anna-prince/gitclient.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://gitee.com/anna-prince/gitclient.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
##我们执行rebase获取远程库与本地同步合并(如果远程库不为空必须做这一步,否则后面的提交会失败)
yaosht@yaosht-PC MINGW64 ~/gitclient (master)
$ git pull --rebase origin master
From https://gitee.com/anna-prince/gitclient
* branch master -> FETCH_HEAD
First, rewinding head to replay your work on top of it...
Applying: first commit
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
CONFLICT (add/add): Merge conflict in README.md
Auto-merging README.md ##如信息所示,自动合并了有冲突的README.md文件
No changes -- Patch already applied.
Applying: test.txt commit
##再次推送给远程仓库
yaosht@yaosht-PC MINGW64 ~/gitclient (master)
$ git push origin master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 272 bytes | 136.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-3.8]
To https://gitee.com/anna-prince/gitclient.git
858d1fa..43d8f11 master -> master
yaosht@yaosht-PC MINGW64 ~/gitclient (master)
$ git status
On branch master
nothing to commit, working tree clean
yaosht@yaosht-PC MINGW64 ~/gitclient (master)
$
简易的命令行入门教程:
Git 全局设置:
git config --global user.name "prince"
git config --global user.email "[email protected]"
创建 git 仓库:
mkdir test
cd test
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://gitee.com/anna-prince/test.git
git push -u origin master
已有仓库?
cd existing_git_repo
git remote add origin https://gitee.com/anna-prince/test.git
git push -u origin master
git命令,可以参考git命令大全