美文网首页Git使用Git
Git新建仓库提交代码

Git新建仓库提交代码

作者: 爱吃馒头的二饼 | 来源:发表于2019-07-12 14:45 被阅读0次

    本机环境安装

    安装git 下载地址

    在Coding上建立仓库

    在项目根目录下鼠标右键打开git bash

    执行输入以下命令

    1. git init

    2. git add .

    3.添加更新说明 git commit -m "First commit"

    4.设置提交地址 git remote add origin 提交地址

    5.提交 git push origin master

    以后再提交代码就无需那么麻烦,只需第2、3、5步

    其中可能遇到 Please tell me who you are 这种返回,输入以下命令

    git config user.name "用户名"
    git config user.email "邮箱"

    可能出现的错误

    1.如果之后在AndroidStudio提交代码时,弹出错误:

    Can't update: no tracked branch

    No tracked branch configured for branch master.
    To make your branch track a remote branch call, for example,
    git branch --set-upstream master origin/master

    Push rejected
       Push to origin/master was rejected
    

    原因是没有指定分支,解决方法就是按提示
    git branch --set-upstream master origin/master

    2.此时可能又出现一个提示:

    The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to

    Branch master set up to track remote branch master from origin.

    我们按着提示重新设置后就可以提交了
    git branch --set-upstream-to origin/master

    image.png

    3.如果出现了 failed to push some refs to 问题:

    error: failed to push some refs to 'git@github.com:hansionit/H-Downloader.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.

    主要原因是github中的README.md文件不在本地代码目录中
    先通过如下命令执行代码合并
    git pull --rebase origin master

    如果无法pull,报错 fatal: refusing to merge unrelated histories
    就改用以下命令
    git pull origin master --allow-unrelated-histories

    然后可以看到本地代码库中多了README.md文件,再次push
    git push origin master

    4.提示:fatal: remote origin already exists

    一般在执行第4步时出现,主要原因是已经添加了远程仓库了,如果需要更换,需要先移除远程仓库
    git remote rm origin

    然后再重新从第4步开始
    git remote add origin 提交地址

    5.提示:

    warning: LF will be replaced by CRLF in 某文件
    The file will have its original line endings in your working directory.
    一般是在执行第2步时出现,但不处理也不会影响提交,主要原因是:
    CRLF 代表CR(Carriage-Return)、LF(Line-Feed) 回车换行
    回车(CR, ASCII 13, \r) 、换行(LF, ASCII 10, \n)
    这两个ACSII字符不会在屏幕有任何输出,但在Windows中广泛使用来标识一行的结束,而在Linux/UNIX系统中只有换行符。
    也就是说在windows中的换行符为 CRLF, 而在linux下的换行符为:LF
    使用git来生成工程后,文件中的换行符为LF, 当执行git add .时,系统提示:LF 将被转换成 CRLF

    解决方法:
    删除刚刚生成的.git文件
    rm -rf .git

    配置core.autocrlf为false
    git config --global core.autocrlf false

    然后重新重第1步开始

    相关文章

      网友评论

        本文标题:Git新建仓库提交代码

        本文链接:https://www.haomeiwen.com/subject/uxgekctx.html