第一次Git

那一晚的第一次Git

问题

利用git,将本地工程上传到github上

步骤

1.进入要上传的工程的主目录之后
git init

已初始化空的 Git 仓库于 /home/name/Desktop/Repo/.git/

git status

位于分支 master

尚无提交

未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

    code/
    plot/
    test-data/

提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

2.添加需要上传到github的代码到本地仓库
git add --all
git status

位于分支 master

尚无提交

要提交的变更:
  (使用 "git rm --cached <文件>..." 以取消暂存)

    新文件:   code/__pycache__/lib_pkl_sl.cpython-37.pyc
    ...
    新文件:   test-data/main.py

3.运行

git config --global user.email “you@example.com”
git config --global user.name “Your Name”

来设置您账号的缺省身份标识。
如果仅在本仓库设置身份标识,则省略 --global 参数。

对应操作:
git config user.email “you@example.com”
git config user.name “Your Name”

4.commit
git commit -m “The First Commit”

[master (根提交) 69535dd] The First Commit
 25 files changed, 779 insertions(+)
 create mode 100644 code/__pycache__/lib_pkl_sl.cpython-37.pyc
 ...
 create mode 100644 test-data/main.py

这样本地的所有文件就都上传到本地仓库了。如果只需要本地一致,就OK了。接下来的步骤用来同步到线上。
这个时候如果要git push

git push
fatal: 当前分支 master 没有对应的上游分支。
为推送当前分支并建立与远程上游的跟踪,使用

    git push --set-upstream origin master

5.在github上建立仓库并复制Git链接

https://github.com/Name/Repo.git

git remote add origin https://github.com/Name/Repo.git(将本地的仓库关联到github上)
git push --set-upstream origin master(并在过程中输入账号密码)

Username for 'https://github.com': Name
Password for 'https://Name@github.com': 
To https://github.com/Name/Repo.git
 ! [rejected]        master -> master (fetch first)
error: 推送一些引用到 'https://github.com/Name/Repo.git' 失败
提示:更新被拒绝,因为远程仓库包含您本地尚不存在的提交。这通常是因为另外
提示:一个仓库已向该引用进行了推送。再次推送前,您可能需要先整合远程变更
提示:(如 'git pull ...')。
提示:详见 'git push --help' 中的 'Note about fast-forwards' 小节。

报错的原因是,我们在github上建立仓库的时候创建的README.md等文件是本地没有的,这样的不一致导致了更新错误。
7.整合远程变更
git pull --rebase origin master

warning: 没有共同的提交
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 10 (delta 2), reused 0 (delta 0), pack-reused 0
展开对象中: 100% (10/10), 完成.
来自 https://github.com/Name/Repo
 * branch            master     -> FETCH_HEAD
 * [新分支]          master     -> origin/master
首先,回退头指针以便在其上重放您的工作...
应用:The First Commit

ls一下,发现README.md等文件已经出现在本地

user@PC:~/Desktop/Repo $ ls
code  plot  README.md  test-data

8.重新push
git push --set-upstream origin master

Username for 'https://github.com': Name
Password for 'https://Name@github.com': 
枚举对象: 32, 完成.
对象计数中: 100% (32/32), 完成.
使用 6 个线程进行压缩
压缩对象中: 100% (31/31), 完成.
写入对象中: 100% (31/31), 2.77 MiB | 1.32 MiB/s, 完成.
总共 31 (差异 3),复用 0 (差异 0)
remote: Resolving deltas: 100% (3/3), done.
To https://github.com/Name/Repo.git
   a534c5f..ee90998  master -> master
分支 'master' 设置为跟踪来自 'origin' 的远程分支 'master'。

9.上github发现代码已经提交上了。
在这里插入图片描述

参考

如何使用git把本地代码上传(更新)到github上

福利

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值