Git代码托管系统常用命令

101 阅读5分钟

Git基本操作流程

代码拉取

git clone http://xxx.net/xxx.git

git clone git@xxx.net/xxx.git

创建本地分支

git checkout -b new_feature

关联远程分支,创建本地对应分支

git checkout -b local_feature origin/remote_feature

切换本地分支

git checkout local_feature

上传本地修改

git add .
git commit -m "备注"
git push origin remote_feature

拉取最新代码

git pull

从远程其他分支拉取代码

git pull origin remote_feature

本地合并分支

git checkout feature-20201016
git merge release-20201016

重新设置基线

git rebase -i master // 重新将基线设置为master分支

打tag版本号

git tag v1.0
git push
git push origin v0.3

Git分支查看

查看本地分支

git branch

查看远程分支

git branch -r

查看本地远程分支对应关系

git branch -vv

查看所有分支

Git branch -a 

Git分支创建

创建本地分支

git branch local_feature

创建并切换本地分支

git checkout -b local_feature

从远程创建本地分支并切换

git checkout -b local_feature origin/remote_feature

创建远程分支——将本地分支上传

git push origin local_feature:remote_feature

Git分支删除

删除本地分支

git branch -D local_feature

删除除当前分支外其他分支

git branch | xargs git branch -d

删除包含'feature-'的分支

git branch | grep 'feature-*' | xargs git branch -d

删除远程分支

git push origin --delete remote_feature

Git代码上传:

git push <远程主机名> <本地分支名> <远程分支名>

git push origin master:refs/for/master

//将本地的master分支推送到远程主机origin上的对应master分支, origin 是远程主机名。

//第一个master是本地分支名,第二个master是远程分支名。

Git状态查看

  • 代码差异
git diff
  • 版本差异
git status

Git忽略文件

*.mat //依据文件后缀忽略

/hw1/data/ //依据父文件夹忽略

Git TAG标签

git tag // 显示所有tag

git tag -l 'v1.0.*' // 查看某个版本系列的tag

git tag -a v1.0.0 -m "内容:v1.0.0" // 创建tag标签

git show v0.0.6 // 查看某个标签的详情

git push origin v1.0.0 // 推送标签

git tag -d v1.0.0 // 删除标签

git push origin :refs/tags/v1.0.0 // 删除远程标签

Tag标签版本号采用三位数字:

  • 第一位数字为大版本号
  • 第二位数字为迭代小版本包
  • 第三位数字为hotfix热修复版本

Git commit 规范

feat:新功能

fix:修补bug

docs:文档

style:样式(不影响代码运行的变动)

refactor:重构(既不是新增功能,也不是修改BUG的代码变动)

test:增加自测用例

chore:构建过程或辅助工具的变动

WIP:代码还在开发,暂时不能合入(Work In Process)

RFR:代码开发完毕,可以检视合入(Ready For Review)

Git撤销

Git撤销当前修改

git restore <文件>... // 放弃工作区某些文件修改,空格分隔文件命名

git checkout . // 回退到上一次add或commit的状态,所有文件

git checkout -- 文件名 // 回退到上一次add或commit的状态,指定文件

Git执行add操作后撤销

git status //查看当前add登记的文件

git reset HEAD . //撤销上一次全部add的文件

git reset HEAD XXX/XXX/XXX.js //对某一个文件的登记进行插销

Git执行commit操作后撤销

git reset --soft HEAD^

  • HEAD^的意思是上一个版本,也可以写成HEAD~1

  • 如果进行了2次commit,想都撤回,可以使用HEAD~2

  • --soft的意思是不删除工作空间改动代码,撤销commit,不撤销git add .

  • --hard删除工作空间改动代码,撤销commit,撤销git add .

注意完成这个操作后,就恢复到了上一次的commit状态。

Git暂存

git stash // 暂存

git stash list // 查看暂存列表

git stash apply XXX // 应用暂存

git stash pop // 应用暂存,并删除

git stash drop // 删除暂存

Git SubModule子模块管理

添加子模块

git submodule add <仓库地址>

git submodule add <仓库地址> <本地路径>

查看当前子模块信息

git submodule

检出子模块

git submodule init // 初始化

git submodule update // 更新

git submodule init & git submodule update // 组合命令

git submodule update --init --recursive // 组合命令

更新子模块

cd XXX // 进入子模块目录

git pull // 拉取最新代码

git pull origin master // 拉取最新代码

删除子模块

git submodule deinit <module>

git rm -rf <module>

rm -rf .git/modules/<module>

// 删除文件

rm -rf <module>

删除子模块

git rm --cached <本地路径>

SSH KEY 验证配置

  1. 生成ssh key

ssh-keygen -t rsa -C "your.email@example.com" -b 4096

  1. 选择存放路径——默认即可

  2. 设置密码——可以不输入

  3. 复制SSH公钥

    1. macOS

pbcopy < ~/.ssh/id_rsa.pub

  1. GNU/Linux (requires the xclip package):

xclip -sel clip < ~/.ssh/id_rsa.pub

  1. Windows Command Line:

type %userprofile%.ssh\id_rsa.pub | clip

  1. Git Bash on Windows / Windows PowerShell:

cat ~/.ssh/id_rsa.pub | clip

  1. 将SSH公钥输入到git托管地址中

删除本地的key密码:(当配置了ssh key后,仍然需要输入密码,可能是key文件有密码保护)

  • ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]

    • 示例:ssh-keygen -p -P 123456 -N '' -f ~/.ssh/id_rsa

Git不同系统兼容

git config --global core.autocrlf false

git config --global core.safecrlf true

含义:

AutoCRLF

#提交时转换为LF,检出时转换为CRLF

git config --global core.autocrlf true

#提交时转换为LF,检出时不转换

git config --global core.autocrlf input

#提交检出均不转换

git config --global core.autocrlf false SafeCRLF

#拒绝提交包含混合换行符的文件

git config --global core.safecrlf true

#允许提交包含混合换行符的文件

git config --global core.safecrlf false

#提交包含混合换行符的文件时给出警告

git config --global core.safecrlf warn