同步git到码云

一、添加ssh公钥

1、生成ssh公钥

ssh-keygen -t rsa -C "用户邮箱"
# Generating public/private rsa key pair...
# 三次回车即可生成 ssh key

2、查看ssh公钥

路径:C:\Users\(PC用户名)\.ssh\id_rsa.pub

或者使用命令行查看:

cat ~/.ssh/id_rsa.pub
同步git到码云_第1张图片

3、测试ssh公钥

ssh -T [email protected]
#返回 Welcome to Git@OSC, yourname! 则生成成功

4、添加ssh公钥

登录码云→修改资料→SSH公钥,将公钥直接粘贴到公钥区间(码云会自动添加标题)

二、创建项目

在码云控制面板创建项目(个人使用码云最主要的是可以创建private项目),按提示完成创建。

三、创建本地代码仓库(前提是已安装git)

1、全局配置

首次使用git时需要配置本地git的用户信息。在代码工程目录下,Git Bush Here,

git config --global user.name "(your user name)"
git config --global user.email "(your email address)"

2、创建git仓库

初始化本地git仓库:

git init
touch README.md
git add README.md //添加README文档
git commit -m "first commit"

3、添加文件

git add -A //添加当前目录所有文件
连接到仓库
git commit -m "添加所有文件" //备注本次操作
下面会显示已添加的文件:

同步git到码云_第2张图片
添加过程中,可以使用

git status


显示文件commit状态

Shawn@VULCAN MINGW64 /d/Qt/Qt5.9.3/workspace/socket_test (master)
$ git status
On branch master
nothing to commit, working tree clean

4、连接到远程仓库

git remote add origin (your gitee repository url)// (码云远程仓库地址)

5、推送文件

 git push -u origin master
有时候会push失败,出现以下错误:
Shawn@VULCAN MINGW64 /d/Qt/Qt5.9.3/workspace/socket_test (master)
$ git push -u origin master
To https://gitee.com/shawnji/xxx.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://gitee.com/shawnji/xxx.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.
可以尝试先强制同步本地和远程文件:
git pull --rebase origin master
成功后会出现以下提示:
Shawn@VULCAN MINGW64 /d/Qt/Qt5.9.3/workspace/socket_test (master)
$ git pull --rebase origin master
warning: no common commits
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://gitee.com/shawnji/xxx
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
First, rewinding head to replay your work on top of it...
Applying: init git repository
再重新push一次:
git push -u origin master
以下提示表示push成功
$ git push -u origin master
Counting objects: 19, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (18/18), done.
Writing objects: 100% (19/19), 18.48 KiB | 0 bytes/s, done.
Total 19 (delta 2), reused 0 (delta 0)
To https://gitee.com/shawnji/xxx.git
   13a7ec9..d7f9360  master -> master
Branch master set up to track remote branch master from origin.
此时已经将本地和远程码云仓库同步完成了。
参考:git官方网站

***

Qt Pull Gitee已有项目出错:

10:08 Running in F:\Project\KEF_DLNA_Player\QtWorkspace\KEF_DLNA_Player: "E:\Program Files (x86)\Git\cmd\git.exe" pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull  

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/ master

The command "E:\Program Files (x86)\Git\cmd\git.exe" terminated with exit code 1.

根据提示,在项目目录下git bash here,使用

git branch --set-upstream-to=origin/master

再通过Qt Pull一次,

10:10 Running in F:\Project\KEF_DLNA_Player\QtWorkspace\KEF_DLNA_Player: "E:\Program Files (x86)\Git\cmd\git.exe" stash save "QtCreator Pull 2018-03-01T10:10:11"
Saved working directory and index state On master: QtCreator Pull 2018-03-01T10:10:11
The command "E:\Program Files (x86)\Git\cmd\git.exe" finished successfully.

10:10 Running in F:\Project\KEF_DLNA_Player\QtWorkspace\KEF_DLNA_Player: "E:\Program Files (x86)\Git\cmd\git.exe" pull
Already up to date.
The command "E:\Program Files (x86)\Git\cmd\git.exe" finished successfully.
成功。顺便再push一下,确保以后push功能正常。

你可能感兴趣的:(git)