Github简单使用

2017-08-27

新建github仓库

  1. github新建一个仓库(私有或公有)。
  2. 本地项目下初始化git仓库:git init
  3. 本地项目下新建“.gitignore”文件,忽略某些文件,例如下面”.gitignore”,只跟踪“.py”文件和“.gitignore”文件,其他的全部忽略。
# Ignore everything
*
# But not these files...
!.gitignore
!*.py
# etc...

# ...even if they are in subdirectories
!*/

  1. git add .
  2. git commit -m "initial commit"
  3. git remote add origin git@github.com:username/project_name.git
  4. git push -u origin master

上传至github

git add .
git commit -m "some comments"
git push

删除github中某一个文件

git rm [-r] --cached filename[dirname]
git commit -m "delete some file or directory"
git push

基本操作

  1. git init repository_name— 初始化仓库
  2. git status — 查看仓库的状态
  3. git add filename — 向暂存区中添加文件
    • git add . — 添加所有修改过的文件
  4. git commit -m "some comments" — 保存仓库的历史记录
    • git commit可添加更详细的注释
  5. git log — 查看提交日志
    • git log --pretty=short — 只显示提交信息的第一行
    • git log filename[dirname] — 只显示指定目录、文件的日志
    • git log -p — 显示文件的改动
    • git log --graph — 以图表形式查看分支
  6. git diff — 查看更改前后的差别
    • git diff HERD — 查看工作树和最新提交的差别
    • 可以养成这样一个好习惯:在执行git commit命令之前先执行git diff HERD命令,查看本次提交与上次提交之间有什么差别,等确认完毕后再进行提交。这里的HERD是指向当前分支中最新一次提交的指针。
  7. git pull — 获取最新的远程仓库分支

分支的操作

  1. git branch — 显示分支一览表
  2. git checkout -b feature-name — 创建并切换到新分支feature-name
    • git checkout feature-name — 切换分支
    • git checkout - — 切换回上一个分支
  3. git merge — 合并分支
    1. git checkout master — 首先切换回主分支
    2. git merge --no--ff feature-A — 把feature-A分支合并到主分支master

未完待续…