github简单使用

配置Git

  • 生成ssh key
    在git bash中输入以下命令:
    1. $ ssh-keygen -t rsa -C "your_email@youremail.com"
    后面的your_email@youremail.com改为你在github上注册的邮箱,之后会要求确认路径和输入密码,我们这使用默认的一路回车就行。成功的话会在~/下生成.ssh文件夹,进去,打开id_rsa.pub,复制里面的key。回到github上,进入 Account Settings(账户配置),左边选择SSH Keys,Add SSH Key,title随便填,粘贴在你电脑上生成的key。
    为了验证是否成功,在git bash下输入:
    1. $ ssh -T git@github.com
    如果是第一次的会提示是否continue,输入yes就会看到:You’ve successfully authenticated, but GitHub does not provide shell access 。这就表示已成功连上github。
    接下来我们要做的就是把本地仓库传到github上去,在此之前还需要设置username和email,因为github每次commit都会记录他们。
    1. $ git config --global user.name "your name"
    2. $ git config --global user.email "your_email@youremail.com"

简单使用

  • 获取仓库
    在git bash中输入
    1. git clone https://github.com/your username/your repository.git
  • 更新仓库
    在git bash中输入
    1. git add .
    2. git commit -m 'mark'
    3. git push origin branch
    mark为提交的注释
    branch为仓库分支名称
  • 创建分支
    在git bash中输入
    1. git brach branchname
  • 切换分支
    1. git checkout branchname
  • 查看分支
    1. git brach
  • 分支合并
    首先切换到想要合并到的分枝下,运行git merge命令,例如将dev分支合并到master分支,命令如下:
    1. git checkout master
    2. git merge dev

创建库

首先登录github,然后创建repository.
然后用自己本地代码目录初始化库,若是新库的话,依次执行以下命令:

  1. echo "# myapi" >> README.md
  2. git init
  3. git add README.md
  4. git commit -m "first commit"
  5. git remote add origin https://github.com/Chain-Zhang/myapi.git
  6. git push -u origin master

若是已经存在的库,可执行以下命令:

  1. git remote add origin https://github.com/Chain-Zhang/myapi.git
  2. git push -u origin master

问题及处理方式

在pull代码的时候遇到以下问题

You have not concluded your merge (MERGE_HEAD exists).
Please, commit your changes before you can merge.

错误可能是因为在你以前pull下来的代码没有自动合并导致的。有两种解决方法:

    1. 保留你本地的修改
      1. git merge --abort
      2. git reset --merge
      3. git push
      4. git pull
      合并后记得一定要提交这个本地的合并
      然后在获取线上仓库
    2. down下线上代码版本,抛弃本地的修改
      不建议这样做,但是如果你本地修改不大,或者自己有一份备份留存,可以直接用线上最新版本覆盖到本地
      1. git fetch --all
      2. git reset --hard origin/master
      3. git fetch
原文地址:https://www.cnblogs.com/saryli/p/9911071.html