Git

文章目录
  1. 1. git config
  2. 2. git走代理
  3. 3. commit规范
  4. 4. 命令
    1. 4.1. rebase
    2. 4.2. merge
    3. 4.3. 切远程分支
    4. 4.4. 回滚
    5. 4.5. stash
      1. 4.5.1. 恢复误删的commit
    6. 4.6. 其他
  5. 5. Gitlab

git config

三种级别 【参考资料

  • 项目级别:git config -e
  • 用户级别:git config --global -e
  • 系统级别:git config --system -e

git走代理

  • 如果clash开启了TUN Mode,并且使用git ssh来验证权限,则需新增clash rules不代理22端口的请求(- DST-PORT,22,DIRECT),否则可能会报以下错误

    kex_exchange_identification: Connection closed by remote host
    Connection closed by 198.18.0.251 port 22
    fatal: Could not read from remote repository.
  • 如果使用小飞机等工具的系统代理,则需手动给git config增加配置

    [http "https://github.com"]
    proxy = socks5://127.0.0.1:7890
    [https "https://github.com"]
    proxy = socks5://127.0.0.1:7890

commit规范

  • feat:新功能(feature)
  • fix:修补bug
  • docs:文档(documentation)
  • refactor:重构(即不是新增功能,也不是修改bug的代码变动)
  • test:增加测试

命令

rebase

# 简单的一句话执行完
git pull --rebase origin master --autostash
# 冲突解决完毕后
git rebase --continue
# 暂存dev的代码
git stash

# 更新master代码
git checkout master
git pull

git checkout dev
git rebase master

git push

merge

git fetch
git merge xxx

切远程分支

git branch -a # 查看所有分支
git checkout -b newDev origin/newDev

回滚

# 丢弃变更
git checkout .
git reset --hard 86312f5 # 回退到某个commit,之后的commit将会丢失
git reset --soft 86312f5 # 回退到某个commit并将回退的代码放在暂存区

git revert 86312f5 # 回退到某个commit,相比reset会产生一个新的commit

stash

git stash push -u -m "some msg"
git stash pop


git stash list
git stash apply 1
git stash pop 1
git stash drop 1

git show stash@{0}

恢复误删的commit

# 查看已删除的commit(包括git stash drop, git reset --hard)
# git fsck --unreachable | grep commit
# git fsck --lost-found | grep commit
git reflog
# 查看commit1, commit2, commit3的详细内容
git show -q commit1 commit2 commit3
# 恢复commit
git merge commit1

其他

# 删除分支
git branch -D xxx

# 设置git config
git config --local -e # 对应文件~/myproject/.git/config
git config --global -e # 对应文件~/.gitconfig
git work add  ../new-branch-projecy
git worktree remove ../new-branch-projecy

Gitlab