Loading... # 创建 SSH 私钥 因为本地 `Git` 仓库和远程 `github` 仓库之间的传输是通过 `SSH` 加密的,所以先要生成秘钥。 1. 在用户主目录`C:\Users\UserName`下,看看有没有`.ssh` 目录。 如果有,直接接 第 3 步。 2. 如果没有`.ssh` 目录,那么说明你电脑上没有安装`SSH` 远程服务,那么你需要先安装它 SSH 远程服务,具体安装方法请自行上网。一般新一些的系统都自带了 SSH 远程服务,如`Widnows10MacOS 10.0`以上都应该自带了`SSH` 远程服务 。 3. 如果有 ```bash cd .ssh ``` 再输入如下命令 ```bash ssh-keygen -t rsa -C 'xxxxxxx@xxx.com' ``` `xxxxxxx@xxx.com` 为你 `GitHub` 邮箱。 这样 在 `.ssh` 目录下就会增加 `id_rsa` 和 `is_rsa.pub` 两个文件,其中id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人。 其中,公钥,也就是我们需要绑定到 `GitHub` `SSH keys` 的 `Keys` 。 登录 `github` , 打开 `settings ` 中的 `SSH Keys` 页面,然后点击 `Add SSH Key` , `title` 可自定义,在 `Key` 文本框里粘贴 `id_rsa.pub` 公钥的内容。 # 创建远程仓库 登陆 GitHub,然后选择 Repositories 并点击 New,输入 Repository name, 点击 Create repository 即可创建一个新的远程仓库。  # Clone远程仓库到本地 ```bash git clone git@github.com:dwtowen/photo.git ``` 如下: ```bash PS D:\GitHub\test> git clone git@github.com:dwtowen/photo.git remote: Counting objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), 605 bytes | 33.00 KiB/s, done. ``` 进入仓库目录: ```bash cd photo ``` # git init ```bash git init ``` 以上命令必须,会报如下错误: ```bash fatal: Not a git repository (or any of the parent directories): .git ``` # 将一个已有的本地仓库与 github 仓库进行关联 ```bash git remote add origin git@github.com:dwtowen/photo.git ``` 远程仓库的默认名称是origin。 说明:采用 `git@github.com:` 关联提交。如果采用 https方式提交的话,会出现每次提交需要输入用户名和密码。 # 把代码 push 到远程仓库 ```bash git push -u origin master ``` 新建远程仓库并第一次推送 `master` 分支时,加上 `-u` 参数,`Git` 不但会把本地的 `master` 分支内容推送的远程新的 `master` 分支,还会把本地的 `master` 分支和远程的 `master` 分支关联起来,此后提交代码可以省去该参数,即 `git push origin master` 。 如下 ```bash PS D:\GitHub\test\photo> git push origin master Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Delta compression using up to 12 threads Compressing objects: 100% (1/1), done. Writing objects: 100% (2/2), 221 bytes | 221.00 KiB/s, done. Total 2 (delta 0), reused 0 (delta 0), pack-reused 0 To https://github.com/dwtowen/photo.git 6ff31cc..4e50bde master -> master ``` # 修改后的文件 push 到远程仓库前 修改后的文件 push 到远程仓库前 需要执行如下命令,否则 `push` 一直提示最新 `Everything up-to-date`。 git add . 把文件添加到暂存区(stage),采用 `git status` 命令可以查看暂存区存储了哪些修改。 ```bash git add . git commit -m "first commit" ``` `first commit` 可以理解为 更新说明。会显示在 `Github` 仓库文件后边。 说明:空文件夹是不会被提交的。 # 查看远程仓库信息 ```bash git remote -v ``` 效果图如下所示: ```bash PS D:\GitHub\test> git remote -v origin git@github.com:dwtowen/blog.git (fetch) origin git@github.com:dwtowen/blog.git (push) ``` # 把本地的某个 branch push 到远程仓库 ```bash git push origin <branch name> ``` # 获取远程修改到本地 ```bash git pull ``` # 本地创建并切换到远程分支对应的分支 当我们在 `github` 中创建了一个新的分支 `originbranch` 后,本地创建并切换到远程分支对应的分支时我们会输入以下命令: ```bash git remote update git fetch git checkout -b branch-name origin/branch-name ``` # 常见报错 ## 用户名 邮箱配置问题 ``` Commit failed - exit code 128 received, with output: '*** 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: empty ident name (for <>) not allowed' ``` 需要到项目 `.git\config` 文件最后加入 `.git` 文件夹为 隐藏文件夹,修改,你需要显示它。 ```config [user] name = name email = email ``` name = dwtowen email = xxxx@xxx.com 说明: `dwtowen` 为 Github 用户名。 `xxxx@xxx.com` 为你注册 Github 的邮箱。 ## git init ```bash fatal: Not a git repository (or any of the parent directories): .git ``` 这时候你需要运行如下命令 ```bash git init ``` ## ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'github.com:xxxx/blog.git' ``` ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'github.com:dwtowen/blog.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. ``` **原因:** 仓库文件或是本地生成了ReadMe文件;仓库有本地没有; **方法一:** 本地生成ReadMe文件 ``` git pull --rebase origin master ``` ``` git push origin master ``` **方法二:** 强制上传覆盖远程文件 ``` git push -f origin master ``` <div class="tip inlineBlock error"> 这个命令在团队开发的时候最好不要用,否则有可能会被队员打死或被公司开出 </div> End Thanks!^_^ 最后修改:2020 年 12 月 21 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 0 如果觉得我的文章对你有用,请随意赞赏