勤奋的搬运工

导航

Mac git 上传到 github

上传本地项目到github

1、初始化本地项目

 进入到你的项目,根目录下git init,会在你的项目的根目录下多出一个.git的文件夹,也许你的mac隐藏了,但是用命令行或者vscode等工具是可以查看的

例子如下:

macdeMacBook-Pro:~ mac$ cd /Applications/Emma/workspace/git_demo/WB_KOA2 
macdeMacBook-Pro:WB_KOA2 mac$ git init
Initialized empty Git repository in /Applications/Emma/workspace/git_demo/WB_KOA2/.git/

2、生成该项目的ssh

进入.git目录下,生成该项目的密钥ssh  密令为ssh-keygen -t rsa -C "你的github邮箱"

接下来会生成 公有/私有的rsa key 

'Enter file in which to save the key (/Users/mac/.ssh/id_rsa): ' 提示你,你的密钥存放到哪里,直接回车为默认地址,地址为括号中的内容;

'Enter passphrase (empty for no passphrase): ' 提示你 确认 ,直接回车就ok ,然后再回车;生成密钥

macdeMacBook-Pro:WB_KOA2 mac$ cd .git
macdeMacBook-Pro:.git mac$ ls
HEAD        config        hooks        objects
branches    description    info        refs
macdeMacBook-Pro:.git mac$ ssh-keygen -t rsa -C "zhou8337626@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/mac/.ssh/id_rsa): 
Created directory '/Users/mac/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/mac/.ssh/id_rsa.
Your public key has been saved in /Users/mac/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:wKtGIICh0nV4CjrO85wQ+JaF8lXh4IL2Ms37SHlHCf4 zhou8337626@163.com
The key's randomart image is:
+---[RSA 2048]----+
|+.  o.o.         |
|+o.o.=o          |
|*o+o.=+          |
|Bo=o= .o.        |
|oB.B...oS        |
| =B.o.o          |
| .=++. E         |
|  .=+ .          |
|   . .           |
+----[SHA256]-----+

3、复制密钥,配置到github里

如果你的是默认地址,直接输入如下命令就可以复制

macdeMacBook-Pro:.git mac$ pbcopy < ~/.ssh/id_rsa.pub

登陆github后,点击右上角的头像,选择settings -> 选择SSH and GPG keys

然后点击按钮new SSH KEY ,title 随意写,把刚才复制的密钥复制到key中即可

4、测试密钥是否匹配到github上

输入如下命令 ssh -T git@github.com   然后命令行 跟我打了一声招呼,Hi fooller , 😁

macdeMacBook-Pro:.git mac$ ssh -T git@github.com
The authenticity of host 'github.com (192.30.255.113)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.255.113' (RSA) to the list of known hosts.
Hi fooller! You've successfully authenticated, but GitHub does not provide shell access.

5、提交代码

进入你的项目根路径,输入git status列出根目录所有文件和文件夹 类似于ls

macdeMacBook-Pro:.git mac$ cd /Applications/Emma/workspace/git_demo/WB_KOA2 
macdeMacBook-Pro:WB_KOA2 mac$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .babelrc
    .editorconfig
    .gitignore
    .postcssrc.js
    .vscode/
    build/
    config/
    debug.log
    echarts-optimize-conf.js
    index.html
    package-lock.json
    package.json
    read.rtf
    src/
    static/
    "\346\265\213\350\257\225\345\234\260\345\235\200.rtf"

nothing added to commit but untracked files present (use "git add" to track)

然后使用git add .babelrc 来添加文件和文件夹 使用 git add . 来添加全部, 来添加到本地的列表中;

使用git commit -m '必填说明' 提交修改生成

使用git remote add '项目别名,随便起' '你github仓库里的ssh地址而不是https地址' 例子如下

macdeMacBook-Pro:WB_KOA2 mac$ git remote add git_test git@github.com:fooller/git_test.git

接下来最重要的一步 就是讲这些操作 推送到github上,使用git push git_test master  解释:git_test为刚才我起的别名,master为分支

macdeMacBook-Pro:WB_KOA2 mac$ git push git_test master

如果报错,错误如下

macdeMacBook-Pro:WB_KOA2 mac$ git push -u git_test master
Warning: Permanently added the RSA host key for IP address '192.30.255.112' to the list of known hosts.
To github.com:fooller/git_test.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:fooller/git_test.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.
macdeMacBook-Pro:WB_KOA2 mac$ git push -u test master
fatal: 'test' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

输入如下命令

macdeMacBook-Pro:WB_KOA2 mac$ git push -f git_test master

提示成功

Counting objects: 161, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (154/154), done.
Writing objects: 100% (161/161), 893.00 KiB | 3.38 MiB/s, done.
Total 161 (delta 8), reused 0 (delta 0)
remote: Resolving deltas: 100% (8/8), done.
To github.com:fooller/git_test.git
 + 9cd1b59...3f3b515 master -> master (forced update)

 

ok了,刷新一下github也许不会出来你刚上传的项目,退出重新登录是一个很好的选择!

 

posted on 2018-02-02 17:08  刚出锅的奶酪  阅读(497)  评论(0编辑  收藏  举报