• 记录--git命令行上传项目到github仓库


        由于公司一直使用的是的SVN,基本上都是内网,原来的git命令都快忘记了,当然也是自己太懒,平时都是直接拖到github上。今天打开idea后突然看到了原来自己写好的一个项目,就想将它上传到github上,也顺便再复习一下git命令,没想到也是遇到了很多坑,也是参考了几个大神的博客才解决,因此想记录一下。

        因为项目是之前写好的,一直放在idea的工作空间里,git是之前已经下好的,包括github上仓库已经建好,万事俱

    1.首先是打命令行窗口 ,cd到项目的目录中

    yht:~ YHT$ cd /Users/ae/IdeaProjects/CloudDisk 
    yht:CloudDisk YHT$ ls
    HELP.md        mvnw.cmd    src
    mvnw        pom.xml        target

    2.然后在初始化仓库

    yht:CloudDisk YHT$ git init
    Initialized empty Git repository in /Users/ae/IdeaProjects/CloudDisk/.git/

    3.设置用户名和邮箱

    刚开始时就直接add然后commit -m,就会提示让你先输入邮箱和用户名。 下面都有提示该怎么敲命令,还是非常友好的,有时候命令敲错了,漏掉一两个单词也会给出提示。

    #如果没有设置邮箱和名字直接commit会提示你设置
    yht:CloudDisk YHT$ git commit -m "project first commit" *** Please tell me who you are. Run git config --global user.email "you@example.com" git config --global user.name "Your Name" to set your account's default identity. Omit --global to set the identity only in this repository. fatal: unable to auto-detect email address (got 'YHT@yht.(none)')

    忘记了是否设置过,可以用命令查看一下,如果设置过了会显示出来。

    yht:.ssh YHT$ git config --global --list
    user.email=你的邮箱
    user.name=你的名字
    #没有的话设置一下
    yht:.ssh YHT$ git config --global user.email "你的邮箱"
    yht:.ssh YHT$ git config --global user.name "你的名字" 

     4.测试SSH连接

    记得原来生成过一次秘钥,但是上github上的setting中的SSH keys看了一下,发现只有一个SSH Key,好像绑定的是另一个电脑,因此就重新生成一个吧。

    cd /Users/ae/.ssh
    yht:.ssh YHT$ ssh-keygen -t rsa -C "你的邮箱" #回车 #回车 #回车 Your identification has been saved in /Users/ae/.ssh/id_rsa. Your public key has been saved in /Users/ae/.ssh/id_rsa.pub.

    输完命令回车就行 会在.ssh文件下生成一个 id_rsa 和一个 id_rsa.pub文件

    通过cat id_rsa.pub或vi id_rsa.pub 查看该文件,然后复制到 github中的setting-->SSH and GPG keys-->New SSH key,取个你喜欢的名字然后将秘钥复制到里面。

    然后测试SSH连接

    yht:.ssh YHT$ ssh -T git@github.com 
    Hi aoteman
    ! You've successfully authenticated, but GitHub does not provide shell access.

    当时也是没有生成SSH Keys直接commit  然后就一直报错

    git@github.com: Permission denied (publickey).

     

    5.提交代码到远程仓库 

    先设置一下远程仓库的地址,因为我们使用的是SSH连接,在github中点开仓库,然后Clone or download,右上角选择SSH连接,复制一下远程仓库地址。

    #设置远程仓库地址
    yht:CloudDisk $YHT git remote add origin "你的远程仓库地址" #查看远程仓库地址 yht:CloudDisk YHT$ git remote -v origin git@github.com:aoteman/CloudDisk.git (fetch) origin git@github.com:aoteman/CloudDisk.git (push)
    ...
    yht:CloudDisk YHT$ git push -u origin master 

    到此本以为大功告成了,只剩下add、commit、push即可,然而再push的时候再次出现问题。。。

    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.

    翻译过来的意思大概就是远程仓库包含你还没有的文件,你可以在push之前先pull一下,想了一下确实远程仓库中初始化的有README.md文件,不是一个空的仓库,因此使用git pull先拉取到本地。

    拉取完成后总可以提交了吧,然而还是报错

    fatal: refusing to merge unrelated histories 
    (拒绝合并不相关的历史)

    百度了一下说是因为本地仓库并不是从远程仓库中git clone下来的,实质上是两个独立的仓库,确实是这样,我是先在github上创建了一个仓库,又在本地的项目中去git init了,然后找到了一条命令:

    git pull origin master --allow-unrelated-histories(该选项可以合并两个独立启动仓库的历史)

    最后 将本地仓库中的文件推送到远程仓库即可。

    git push -u origin master

     

    6.总结:知识学过了如果不经常使用总是容易忘,平时的工作学习都是阶段性的,很容易忽略一些学过的重要的知识,因此平时要多总结,养成一个好习惯,加油!

  • 相关阅读:
    Centos启动Cassandra交互模式失败:No appropriate python interpreter found
    删除Kafka的topic
    《面向中国资本市场应用的分布式总账白皮书》笔记
    搭建Kafka集群(3-broker)
    【转】矩阵求导计算规则
    二次型求导
    解决: org.iq80.leveldb.DBException: IO error: C:data rie00945.sst: Could not create random access file.
    SSH遇见的问题
    解决:Redis:java.util.NoSuchElementException: Unable to validate object at
    【转】mysql查询结果输出到文件
  • 原文地址:https://www.cnblogs.com/yanht/p/gitnotes-1.html
Copyright © 2020-2023  润新知