Git 相关

# http://learngitbranching.js.org/

# config & help & diff
    git config --global user.name "username"
    git config --global user.email [email protected]
    git config --list  # 列出所有配置项
    git config <key>  # 来检查 Git 的某一项配置  e.g: git config user.name
    git help <verb>  # 获取帮助 查看 git 手册  e.g: git help config
    git diff  # 看暂存前后的变化
    # 此命令比较的是工作目录中当前文件和暂存区域快照之间的差异,也就是修改之后还没有暂存起来的变化内容
    git diff --staged/--cached  # 查看已经暂存起来的变化(--staged和--cached是同义词)

# clone
    # 克隆版本库的时候,所使用的远程主机自动被 Git 命名为 origin
    # 该命令会在本地主机生成一个目录,与远程主机的版本库同名
    # 如果要指定不同的目录名,可以将目录名作为 git clone 命令的第二个参数
    git clone <版本库的地址>
    git clone <版本库的地址> <本地目录名>
    git clone -o jQuery https:...  # 克隆的时候,指定远程主机叫做 jQuery

# remote 远程
    git remote  # 列出所有远程主机
    git remote show <主机名>  # 查看该主机的详细信息
    git remote add <主机名> <地址>  # 添加远程主机
    git remote rm <主机名>  # 删除远程主机
    git remote rename <原主机名> <新主机名>  # 改变远程主机名

# status
    git status  # 查看 git 状态

# stash
    git stash  # 将未提交的变化隐藏
    git stash pop  # 将未提交的变化显示

# branch
    git branch -l  # 查看本地分支
    git branch -r  # 查看远端分支
    git branch -a  # 查看所有分支
    git branch -m dev develop  # 重命名本地分支
    git branch -f master HEAD~3  # (强制)移动 master 指向 HEAD 的第 3 次父提交

# checkout
    git checkout master  # 切换到 master 分支
    git checkout -b bug_master  # 新建并切换到新分支

# commit
    git commit -m 'fix'  # 提交带有 fix 的注释
    git commit -am 'fix'  # 提交修改的文件 并带有 fix 注释
    git commit --amend  # 修改当前提交点注释(进入 vim)

# pull
    git pull  # 拉取远端代码 会生成一个新的提交点
    git pull --rebase  # 不会生成新的提交点
    git pull -p  # 同步远端与本地

# push 
    git push origin <branch_name>  # 将本地分支推送到远端
    git push origin :<branch_name>  # 删除远端指定分支

# reset
    git reset --hard HEAD  # 回退到修改之前
    git reset --hard <commit_id>  # 回滚到某个提交点

# revert

# merge
    # merge 操作会在当前的分支增加一个新的提交点 包含有目标分支的所有提交
    # 当前分支会和目标分支建立一个联系
    git merge <branch_name>  # 目标分支(branch_name)合并到当前分支

# rebase
    # rebase 操作会把当前分支的所有提交点 合并到目标分支
    # 只是提交点的复制 当前分支不会和目标分支建立联系
    git rebase <branch_name>  # 合并提交点到目标分支(branch_name)

# log
    git log  # 查看提交日志
    # 一个常用的选项是 -p,用来显示每次提交的内容差异。 你也可以加上-2来仅显示最近两次提交:
    git log -p -2
    git log -S <keyword>  # 搜索提交历史,根据关键词
    git log --stat  # 查看每次提交的简略的统计信息
    git log --pretty=<options>  # 可以指定使用不同于默认格式的方式展示提交历史
    # options: oneline/short/full/fuller/format
    # format,可以定制要显示的记录格式
    git log --pretty=format:"%h - %an, %ar : %s"
    # 3c83e87 - PaddyWang, 12 days ago : website menu add sort

# rm
    git rm --cache ./.DS_Store  # 移除暂存区

# tag
    git tag  # 列出所有tag
    git tag <tag_name>  # 新建一个tag在当前commit
    git tag <tag_name> <commit>  # 新建一个tag在指定commit
    git tag -d <tag_name>  # 删除本地tag
    git push origin :master/tags/<tag_name>  # 删除远程tag

# HEAD
    # HEAD 总是指向最近一次的提交点
git log --pretty=format 常用的选项

| --- | --- | 选项 | 说明 | --- | --- | %H | 提交对象(commit)的完整哈希字串 | %h | 提交对象的简短哈希字串 | %T | 树对象(tree)的完整哈希字串 | %t | 树对象的简短哈希字串 | %P | 父对象(parent)的完整哈希字串 | %p | 父对象的简短哈希字串 | %an | 作者(author)的名字 | %ae | 作者的电子邮件地址 | %ad | 作者修订日期(可以用 --date= 选项定制格式) | %ar | 作者修订日期,按多久以前的方式显示 | %cn | 提交者(committer)的名字 | %ce | 提交者的电子邮件地址 | %cd | 提交日期 | %cr | 提交日期,按多久以前的方式显示 | %s | 提交说明


  • 相对引用

    • 使用 ^ 向上移动一个提交纪录
      • 切换到 bugFix 的父提交
      • git checkout bugFix^
    • 使用 ~<num> 向上移动多个提交纪录
      • git checkout HEAD~3
  • 多个提交点合并

    • git commit -am 'fix1'
    • git commit -am 'fix2'
    • git commit -am 'fix3'
    • git rebase -i HEAD~3
    • 进入 vim 环境, i 编辑模式
    • 将除了第一个 base 全部改为 s , Esc shift + : wq 保存并退出
    • 然后把 commit 的提交信息只保留一个, Esc shift + : wq 保存并退出
    • 最后再 push 就可以了
  • -i: Interactive
  • -f: forcing
        # Git Workflow
        1.  git checkout master
        2.  git pull --rebase
        3.  git checkout -b <feature_branch>|<bug_branch>
        4.  modify and commit
        5.  git rebase -i HEAD~N ?
        6.  git checkout master
        7.  git pull --rebase
        8.  git checkout -b <production_branch>
        9.  git cherry-pick <SHA-one>
        10. git push origin <production_branch>
        11. git checkout master
        12. git branch -d <feature_branch>|<bug_branch>

results matching ""

    No results matching ""