scarcoco / projx

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GIT

scarcoco opened this issue · comments

常用配置

# 全局设置
git config --global user.name "scarcoco"
git config --global user.email "cooxacom@163.com"
git config --global core.editor vim
git config --global merge.tool vimdiff

# 查看配置 
git config --list [--global]

# 别名
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status

git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'

# 设置提交模板
git config commit.template .gitmessage

# 设置当前项目
git config user.name scarcoco
git config user.email cooxacom@163.com

修改最近提交记录

# 修改最近一次提交
git commit --amend

# 修改最后一次提交内容和提交人
git commit --amend --author="scarcoco <cooxacom@163.com>"

# 修改最近 3 次
git rebase -i HEAD~3

# 修改状态,如下图
# 单个操作,按提示操作:
git commit --amend
git commit --amend --author="scarcoco <cooxacom@163.com>"

# 修改
git add

git rebase --continue
git rebase --skip

image

git merge / git rebase 对比

git merge

  • 只处理一次冲突;
  • 引入一次合并历史,其他历史记录按 commit 提交顺序;
  • 过程 commit 记录都在,可能不便于后续查找某个提交记录;
  • 分支图不够清晰;

git rebase

  • 改变当前分支从原始分支拉位置;
  • 没有多余的合并历史,合并后 commit 顺序不一定按照原来 commit 的提交顺序;
  • 可能多次解决同一个冲突;
  • 分支相对清爽一些;

git diff

git diff
git diff --staged

git clone

# 单分支
git clone --single-branch -b development ssh://git@git.xxx.com/hello.git

# 指定分支
git clone -b development ssh://git@git.xxx.com/hello.git

零碎知识点

# 获取最近提交 commit id
git rev-parse HEAD

# 获取最近提交 commit id
git rev-parse --short HEAD

# 当前分支名字
git branch | grep \* | cut -d ' ' -f2
git branch | grep '*' | sed 's/* //'

# 去掉分支前的 feature 
git branch | grep '*' | sed 's/* feature\///'

# 比如 feature/SGCDM-135
git symbolic-ref --short HEAD
# 比如 refs/heads/feature/SGCDM-135
git symbolic-ref -q HEAD


# 不保留代码
git reset --hard commitId

# 保留代码在缓存区
git reset --soft commitId

# 保留代码,不在缓存区
git reset --mixed commit

格式化提交信息

参考:typicode/husky#311

[ -f $GIT_DIR/MERGE_MSG ] && exit 0

BRANCH_NAME=$(git branch | grep '*' | sed 's/* feature\///')
if [ "$BRANCH_NAME" != "(no branch)" ]; then
    echo "id: $BRANCH_NAME\ncause: 需求\nsolution: $(cat $1)" > "$1"
    echo "" >> "$1"
fi