2024-01-04
服务器
git
462

一、安装查看

1.安装 git
yum install -y git
2.设置用户名、邮箱
git config --global user.name jdzor # 配置用户
git config --global user.email 895947580@qq.com # 配置邮箱

git config user.name # 查看用户
git config user.email # 查看邮箱
3.查看分支、仓库
git branch # 本地分支
git branch -r # 远程分支
git branch -a # 远程和本地分支

git remote show origin # 查看远程仓库
git remote -v # 查看 git 地址

二、常用命令

1.创建仓库
git init # 初始化本地仓库
git remote add origin https://gitee.com/xxx/xxx.git # 本地仓库添加远程地址
git remote set-url origin https://gitee.com/xxx/xxx.git # 本地仓库更改远程地址

git status
git add --all
git commit -m "初始化项目" # 提交本地代码
git push -f origin master # 强行推送到远程分支(谨慎使用,远程git代码会强行覆盖)
2.创建、删除分支
git branch jdzor # 创建本地分支
git checkout jdzor # 切换到分支
git checkout -b jdzor # 创建本地分支并切换
git push origin test:dev # 第一次提交时,创建远程 dev 分支,并将本地 test 提交到远程 div

git branch -D jdzor # 删除本地分支
git push origin :jdzor # 删除远程分支
3.创建、删除标签
git tag # 列出所有 tag
git show v1.0 # 查看标签版本信息

git tag v1.0 # 创建标签
git tag -a v1.0 -m "my tag 1.0" # 创建标签写备注
git checkout v1.0 # 切换到标签(与切换分支命令相同)

git tag -d v1.0 # 删除本地标签
git push origin :refs/tags/v1.0 # 删除远程标签

git push origin v1.0 # 推送指定标签到远程
git push origin --tags # 推送所有标签到远程
4.合并分支
git fetch # 拉取远程分支
git fetch origin test # 拉取远程 test 分支
git checkout -b test origin/test # 合并远程分支至本地

git merge origin/master # 合并远程分支至当前分支
git merge jdzor # 合并本地分支至当前分支

git pull origin master # 直接合并远程分支至当前分支
5.上传分支
git status
git diff

git add --all 
git commit -m "xxx"
git push origin jdzor
git push origin test:dev # 提交本地 test 分支到远程 dev 分支

git add --all # 提交所有变化
git add -A # 提交所有变化,git add --all 简写
git add -u # 提交被修改 (modified) 和被删除 (deleted) 文件,不包括新文件 (new)
git add . # 提交新文件 (new) 和被修改 (modified) 文件,不包括被删除 (deleted) 文件
6.克隆、撤销、更新
git clone https://github.com/xxx/xxx.git # 克隆代码
git reset --hard HEAD # 撤销当前所有未提交文件的修改
git merge --abort # 退回本次合并
git reset --hard commit_id # 退回到某次提交前
7.重置所有 commit
# 删除本地 .git 文件夹
git init # 初始化 
git remote add origin https://github.com/xxx/xxx # 添加远程地址 

git add --all # 添加文件
git commit -m '初始化项目' # 提交本地
git push -f origin master # 强制推送 
8.合并多个 commit
git log
git log --author=jdzor # 查询指定用户日志

git rebase -i HEAD~3 # 压缩最近 3 条 commit 为一条,不推荐
git rebase -i commit_id # 想合并的最后一个 commit 的前一个commit
git push -f # wq 保存后 强制推到远程
git rebase --abort # 取消回滚压缩操作

# pick:git 会应用这个补丁,以同样的提交信息(commit message)保存提交
# reword:git 会应用这个补丁,但需要重新编辑提交信息
# edit:git 会应用这个补丁,但会因为 amending 而终止
# squash:git 会应用这个补丁,但会与之前的提交合并
# fixup:git 会应用这个补丁,但会丢掉提交日志
# exec:git 会在 shell 中运行这个命令
9.本地分支 cherry-pick 提交
# 如果需要一个分支的所有代码变动,那么就采用合并(git merge);如果只需要部分代码变动(某几个提交),可以采用 Cherry pick
git cherry-pick commit_id
git cherry-pick --quit
git cherry-pick a..b # a should be older than b, else an error will be thrown error: empty commit set passed
git cherry-pick a..b # a is not included, while b is included
git cherry-pick a^..b # If wish to include a, use command a^..b instead

# 示例
git checkout test
git pull --rebase
git cherry-pick 87a0b6307bfb1e52880771c7294e22f1cd30d5b1 # 本地提交的 commit_id
git push -f # 强制推送到远程 

# 在合并到 test 分支前,从 test 分支拉取一个 test-bak 分支,cherry-pick 到 test-bak 分支,然后提 pull request,把 test-bak 合并到 test( 新建 test-bak 分支主要是防止冲突)
10.配置 ssh
ssh-keygen -t rsa -C "xxxxxx@xxx.com"
# Generating public/private rsa key pair...
# 三次回车即可生成 ssh key

# 将公钥添加至代码托管平台、查看添加是否成功
cat ~/.ssh/id_rsa.pub

ssh -T git@git.oschina.net # oschina
ssh -T git@git.coding.net # coding
ssh -T git@gitee.com # 码云

三、.gitignore 忽略文件

1..gitignore 忽略规则
# 在已忽略文件夹中不忽略指定文件夹
/node_modules/*
!/node_modules/layer/

# 在已忽略文件夹中不忽略指定文件,要忽略的文件夹一定要结尾 /* ,否则规则无法生效
/node_modules/*
!/node_modules/layer/layer.js

# 忽略所有 .a 结尾的文件
*.a
*.[oa]   # 忽略所有以 .o 或 .a 结尾的文件

# 忽略所有 .b 和 .B 结尾的文件,lib.b 除外
*.[bB]
!lib.b

# 忽略 node_modules 文件和 node_modules 目录
node_modules

# 只忽略 node_modules 目录,不忽略 node_modules 文件
node_modules/

# 只忽略 node_modules 文件,不忽略 node_modules 目录
node_modules
!node_modules/

# 忽略 doc/xxx.txt 但不包括 doc/xxx/xxx.txt
doc/*.txt 

# 仅忽略当前目录下的 TODO 文件,不包含 subdir/TODO
/TODO
2.删除本地 git 缓存并提交:
git rm -r --cached . # 命令后面有小数点
git add . # 命令后面有小数点
git commit -m 'update .gitignore' 
标签:

git