push代码到github

  • 安装git


    $ git
    The program 'git' is currently not installed. You can install it by typing:
    sudo apt-get install git
    上述表明没有安装git
    安装git: sudo apt-get install git

  • 安装完成后配置信息:


    $ git config --global user.name "Your Name"
    $ git config --global user.email "[email protected]"

  • 配置 SSH
    查看本地是否已经生成了ssh key(包含了id_rsa 以及id_rsa.pub): ~/.ssh
    没有就需要生成ssh key:


    $ ssh-keygen -t rsa -C "[email protected]"

    然后一直回车(有密码则需输入密码)
    查看是否配置成功:

    $ cd ~/.ssh
    $ ls
    $ id_rsa id_rsa.pub

    如果有id_rsaid_rsa.pub两个文件则生成了ssh key,然后通过cat ~/.ssh/id_rsa.pub查看公钥内容,然后添加到github上的SSH Keys 中;

  • 创建本地版本仓库
    1.在合适的位置创建一个空目录


    $ mkdir learngit
    $ cd learngit
    $ pwd
    /Users/desk/learngit

    也可以选择一个已有的项目文件夹, cd到当前文件夹下
    2.通过git命令 git init 把当前目录变成git可以管理的仓库:

    $ git init
    Initialized empty Git repository in /Users/desk/learngit/.git/

  • 提交文件到仓库


    $ git add readme.txt
    $ git commit -m "提交了一个文件"

    上面两步可以合并一步: commit -a 然后再用vim编辑提交说明
    添加全部: git add .

  • 添加远程仓库
    1.先在github上创建一个仓库并拷贝地址如:[email protected]:MrXiaxia/test.git
    2.然后关联本地仓库:$ git remote add origin [email protected]:MrXiaxia/test.git
    3.把本地库的内容推送到远程库: $ git push -u origin master
    4.首次提交推送需加上 -u ,以后不用;
    5.如出现以下错误:


    git push -u origin master
    To [email protected]:MrXiaxia/test.git
    ! [rejected] master -> master (non-fast-forward)
    error: failed to push some refs to '[email protected]:MrXiaxia/test.git'
    hint: Updates were rejected because the tip of your current branch is behind
    hint: its remote counterpart. Integrate the remote changes (e.g.
    hint: 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.

    可以尝试用: git pull 或者git push --force origin master解决

  • 从git上克隆项目到本地

//类似SVN 的 checkout
$ git clone [email protected]:sharlley/test.git
  • 更新代码
//类似SVN 的 update
$ git pull

待续!

你可能感兴趣的:(push代码到github)