潦草白纸

2015-09-27 git学习

创建


初始化

  1. git init Initialized empty Git repository in <file>

关联

  1. git remote add origin <git@server-name:path/repo-name.git>

克隆

  1. git clone <git@server-name:path/repo-name.git>

从已有远程库中拷贝

查看


状态日志

  1. git status

提交内容日志

  1. git log

分支合并日志

  1. git log --graph

操作步骤日志

  1. git reflog

差异日志

  1. git diff

远程库信息

  1. git remote -v

提交&撤销


提交
提交代码更改分两个阶段:

  1. 从工作目录,提交到stage;
  2. 从stage提交到master;
  • 从工作目录提交到stage

    • 新增
      1. 单个文件:git add <file>
      2. 多个文件:git add -A
    • 删除
      1. 单个文件:git rm <file>
      2. 多个文件:git rm -A

      只提交到stage,而没有提交到master,是不会自动同步到master的

  • 从stage提交到master

    1. git commit

退回
退回也是要分两步:

  1. 从master退回到stage;
  2. 从stage退回到工作目录;
  • 对于还没有提交到stage的
    1. 单个文件:git checkout --<file>
    2. 多个文件:git checkout master/develop

    退回一步取stage中的文件状态,覆盖掉工作目录中文件的状态,跟master完全没关系。

  • 对于已经到达stage的
    1. 单个文件:git reset HEAD file
    2. 多个文件:git reset HEAD^

    把state中的文件状态用master中的覆盖掉,这样就把stage中修改用master的状态覆盖掉了,完全跟工作目录没关系

更新


第一次推送

  1. git push -u origin master

推送最新修改

  1. git push origin master

分支


查看分支

  1. git branch

创建分支

  1. git branch <name>

切换分支

  1. git checkout <name>

创建+切换分支

  1. git checkout -b <name>

合并某分支到当前分支

  1. git merge <name>

合并分支时,如果可能,Git会用Fast forward模式,但这种模式下,删除分支后,会丢掉分支信息

  1. git merge --no-ff -m "merge with no-ff" <name>

因为本次合并要创建一个新的commit,所以加上-m参数,把commit描述写进去

推送分支

  1. git push origin <master>

git rebase与git marge的差异


将当前分支更新为最新的目标分支分支

  1. git rebase <origin>

应用修改提交

  1. git rebaes --continue

终止流程

  1. git rebaes --abort

跳过此版本

丢弃本地修改,慎用

  1. git rebaes --skip

删除分支

  1. git branch -d <name>

bug分支

储藏

  1. git stash

将目前当所有修改用油纸包起来,快速地将工作环境恢复到上一个commit时的状态
并不是不想提交,而是工作进行到一半,线上缺陷来了,没法提交

  • 线上缺陷储藏步骤
    1. git stash //储藏现有工作分支(一般为develop)
    2. git checkout <master> //切换至master分支
    3. git checkout -b <issus-911> //在master分支上创建issus-911分支
    4. git add <fix.java> //添加缺陷修改文件
    5. git commit -m <"fix bug 911"> //提交缺陷修改文件
    6. git checkout <master> //切换回master分支
    7. git merge --no-ff <issus-911> //将issus-911分支合并至master
    8. git checkout <develop> //切换至develop分支
    9. git merge --no-ff <issus-911> //将issus-911分支合并至develop

查看储藏

  1. git stash list

恢复储藏

  1. git stash apply <stash@{0}>

删除储藏

  1. git stash drop

恢复并删除储藏

  1. git stash pop

feature分支

  • 新feature开发步骤
    1. git checkout -b <new feature 119> //在develop分支上创建issus-911分支
    2. git add <feature.java> //添加新文件
    3. git commit -m <"new feature 119"> //提交新文件
    4. git checkout <develop> //切换至develop分支
    5. git merge --no-ff <new feature 119> //将new feature119分支合并至develop
    6. git branck -d <new feature 119> //将new feature119

标签

查看标签

  1. git tag

创建标签

  1. git tag <v1.0>

别名

命令嫌太长了,可以取个别名

  1. git config --global alias.st status
  2. git config --global alias.co checkout
  3. git config --global alias.ci commit
  4. git config --global alias.br branch


来自为知笔记(Wiz)


posted on 2015-09-27 17:00  潦草白纸  阅读(276)  评论(0编辑  收藏  举报

导航