一、创建本地仓库并提交代码

  • `git init初始化一个新仓库。`
  • `git add git commit把文件提交到本地仓库。`

二、拉取远程仓库

先使用git remote add 命令添加一个远程仓库,仓库地址:https://github.com/madawang/intely

> git remote add origin https://github.com/madawang/intely
> git remote -v # 查看远程仓库
origin  https://github.com/madawang/intely.git (fetch)
origin  https://github.com/madawang/intely.git (push)

使用git pull 拉取远程文件,会出现以下信息:

> git pull
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
From https://github.com/madawang/intely
 * [new branch]      master     -> origin/master
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 <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
    git branch --set-upstream-to=origin/<branch> master

表示本地分支和远程分支没有建立联系,不能直接pull ,需要先把本地分支和远程分支建立联系:

git branch --set-upstream-to=远程连接名/远程分支的名字 本地分支的名字
git branch --set-upstream 本地分支名字 远程连接名/远程分支名字

对于本地项目使用gitbranch --set-upstream master origin/master 即可。

如果git版本过新,还可能出现以下错误:

fatal: the "--set-upstream" option is no longer supported. Please use "--track" 
or "--set-upstream-to" instead.

原因是--set-upstream 选项不再被支持,需要使用--track 选项:

> git branch --track master origin/master
# 出现下面信息表示关联成功,然后就可以用git pull了。
Branch "master" set up to track remote branch "master" from "origin".
> git pull origin master # git pull
From https://github.com/madawang/intely
 * branch            master     -> FETCH_HEAD
Already up to date

三、提交本地仓库内容到远程仓库

把本地代码先提交到本地仓库,使用git push 把代码提交到github ,会弹出对话框要求输入账号密码。

> git push
Counting objects: 12, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (12/12), 2.14 KiB | 1.07 MiB/s, done.
Total 12 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/madawang/intely.git
   82f3254..f20d28f  master -> master

提交后github 中的文件信息:

最后修改:2018 年 12 月 16 日
如果觉得我的文章对你有用,请随意赞赏