【Git】将本地代码 提交至 远程版本库

本地代码,提交git 远程仓库

1、首先,创建远程仓库或远程仓库已存在

2、在本地代码目录,git init

$ git init
Initialized empty Git repository in D:/JavaCode/springBootStudy/.git/

3、git add --all

4、git commit -m "v1.0.0"

5、git remote add origin https://github.com/gaoStudy/springBootStudy.git
git push -u origin master

问题1:
$ git push -u origin master
To https://github.com/gaoStudy/springBootStudy.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/gaoStudy/springBootStudy.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.

问题2:

$ git pull
warning: no common commits
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/gaoStudy/springBootStudy
 * [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

问题3:

$ git pull origin master
From https://github.com/gaoStudy/springBootStudy
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

1、Git Windows版本太低 --升级最新
2、 
    fatal: refusing to merge unrelated histories

原因分析:
    这种错误一般出现在首次上传代码时. 远程分支和本地分支不一样,
    远程(origin)有 README.md 文件 , 但是本地仓库没有;
    你想push 他提示你先 pull 一下. 你pull 一下, 就提示 :fatal: refusing to merge unrelated histories

流程分析:   
使用git pull提示refusing to merge unrelated histories

创建了一个origin,两个人分别clone

分别做完全不同的提交

第一个人git push成功

第二个人在执行git pull的时候,提示

fatal: refusing to merge unrelated histories

解决方法(不建议采纳):

git pull --allow-unrelated-histories

以上方式:大部分解决方法都是这样,简单粗暴:git pull origin master --allow-unrelated-histories .其实这种方式不好

最总解决方式:

    1. git log -p master..origin/master 查看本地master分支和远程(origin)分支的区别
    2. git pull --rebase origin master 这段命令,会在本地生成一个README.md文件并将 远端代码pull 下来.

    $ git pull --rebase origin master
        From https://github.com/gaoStudy/springBootStudy
         * branch            master     -> FETCH_HEAD
        First, rewinding head to replay your work on top of it...
        Applying: V0.0.1

    3. git push -u origin master 上传代码

gaodevops