参考:https://yq.aliyun.com/articles/619351
文章目录
用户配置
git config [--gloal] user.name '张三' # 暂时[永久]配置git提交的用户名
git config [--gloal] user.email '123@qq.com' #暂时[永久]配置git提交的邮箱
git config --global --list #查看global全局配置(包括用户、邮件配置)
git config user.name #查看当前用户
git config user.email #查看当前邮箱
Https (clone/push)记住密码
#永久记住密码
git config --global credential.helper store
#当前用户下 .gitconfig文件中
[credential]
helper = store
git config –-global credential.helper cache # 记住15分钟
git config credential.helper ‘cache –timeout=3600’ #记住1小时
git clone 报错SSL certificate problem: unable to get local issuer certificate
系统判断这个行为会造成不良影响,所以进行阻止,只要设置跳过SSL证书验证就可以了,那么用命令:
git config --global http.sslVerify false
SSH方式(clone/push)记住密码
ssh-keygen -t rsa -C "your_email@youremail.com"
#一直点确定
cd ~/.ssh/
#生成公私钥id_rsa和id_rsa.pub
#将id_rsa.pub内容添加至git平台中
下次clone、push使用ssh方式即可
~/.ssh/下有known_hosts生成
ssh -T -i ~/.ssh/id_rsa.pub git@gitee.com/github.com #验证是否配置成功
# 注意! 一个系统用户公钥只能配置对应一个GitHub/gitee 账户
Hi ****! You've successfully authenticated, but GitHub does not provide shell access.
配置成功,需要使用ssh方式clone、push
基本命令
git init #创建本地仓库
git clone #仓库远程地址
git add <dir/file> #添加、修改文件操作到暂存区
git rm #将删除文件添加到暂存区,add也可以
git log #查看commit日志
git status #工作区文件状态
remote
git remote [-v] #查看本地仓库关联的远程仓库
#将当前仓库与远程仓库关联,且远程仓库 别名 为origin
git remote add origin <远程仓库地址>
git remote rm origin
git remote rename origin old-origin # 改名
git remote prune origin # 同步远程分支, 清除本地不存在的远程分支
更改commit注释
https://www.jianshu.com/p/098d85a58bf1
log
查看提交日志
git log
git log --pretty=oneline # 一行显示提交记录
git log [-2] # 最近两次提交
commit
git commit -m "comment" #提交到本地仓库, 可以不加 " "
git commit -am "comment" # git add + git commit
`更改commit注释`
git commit --amend # 更改已经commit的注释
#更改多个commit点注释
1. `-注意,有未commit或stash的内容-`
2. git rebase -i HEAD~2 # 更改最近2次commit点
3. #然后更改pick ----> edit,保存退出
4. git commit --amend # 更改注释
5. git rebase --continue # 继续
6. # 重复 3,4修改每一个c要更改的commit
`合并commit点`
7. `-注意,不要有未commit或stash的内容-`
8. git rebase -i HEAD~2 # 更改最近2次commit点,从旧向新(不包括当前commit 点)
9. 或 git rebase -i commit_id # commit_id之后提交的所有commit点,从旧向新(不包括commit_id 点)
10. # pick 目标commit点,可选多个,squash/s 需要合并commit点,可选多个
11. # 若需要合并多次,如下:
12. # git会依次提示2次,更改合并后commit注释
` 不包含rebase -i commit点
pick 4ace906 modify_2_3 # 旧
squash 640559c add_4 # |
pick 2c917b3 add_5 # |
squash 6d062de add_6 # 新
`
-- pick #选择commit点
--squash/s #和上一个commit点合并
--edit #编辑commit点
Note: 如果存在冲突,先git status查看冲突 --> 修正冲突 --> git add . --> git rebase --continue
合并多个commit点
- 当分支有很多commit点时,可以将多个commit点进行合并
git reset <commit_id> # 回退到制定commit点
git add . + git commit -m "msg" # commit更改内容
git pull origin master --rebase # 合并master分支,且合并操作不生成commit
# 处理冲突
git add . + git commit -m "msg"
git push origin local_branch:origin_branch --force # 如果远程分支已经存在,则加上--froce可已将远程分支的commit点也合并
操作撤销
暂存区–>工作区
git restore [./filename/dir] # 工作区更改删除(未add)
git restore --staged [./filename/dir] #暂存区操作撤销至工作区(已add, 未commit)
git checkout -- [./filename/dir] #工作区删除文件操作清除(未add)
仓库区–>暂存区
soft #仅将head调整至指定版本,版本后的 [工作区,暂存区内容保留],[仓库区内容撤销到暂存区]
mixed #默认,跳转到指定版本,版本后的 [工作区,暂存区内容保留],但[暂存区,仓库区]内容撤销到工作区
hard #所有内容均恢复到指定版本, 不保留工作区和暂存区内容
git reset --hard HEAD^ 回退到上个版本
git reset --hard HEAD~1回到上个版本
git reset --hard 版本号, 跳转到指定的版本号
# git revert是用一次新的commit来回滚之前的commit
git revert <commit_id> # 撤销指定的版本,撤销也会作为一次提交进行保存
# git revert 和 git reset的区别
- git revert是用一次新的commit来回滚之前的commit,此次提交之前的commit都会被保留;
- git reset是回到某次提交,提交及之前的commit都会被保留,但是此commit id之后的修改都会被删除
仓库区–>工作区
git checkout <commit id> #HEAD跳转到commit id版本处,并创建临时缓存分支,临时分支上不要提交改动, 很可能丢失
`HEAD detached at <commit id> `
stash 暂存操作
stash@{0}: xxx: 1b008a5 update # 新
stash@{1}: xxx: da6f01c update # |
stash@{2}: xxx: 2135308 mml转换分支自测 # 旧
git stash [save|-m "savemessage"]#将暂存区修改暂时存储起来
git stash list #查看暂存的信息
git stash pop [stash@{$num}|num] #应用并删除指定版本的暂存内容,默认最近一次
git stash apply stash@{$num}|num #应用指定版本的暂存内容
git stash show [stash@{$num}|num] #展示改动,默认展示最近一次存储更改
git stash show -p [stash@{$num}|num] #展示具体改动,默认展示最近一次存储更改
git stash drop stash@{$num}|num # 删除某次暂存
git stash clear #清空暂存栈
branch 分支操作
checkout
git checkout <branch> # 切换分支
# 在分支基础上(默认当前分支)创建并切换到新分支,分支为远程分支时新分支自动追踪
git checkout -b <develop1> [local_branch|origin/develop]
git checkout <origin/develop> # 切换到远程分支,但不创建本地分支
git checkout <develop> # 本地没有 develop分支时,等价于 git checkout -b develop origin/develop
switch
git switch <develop> # == git checkout <develop>
git switch -c <develop> # == git checkout -b
fetch
fetch 更新远程分支,用于查看远程分支和本地分支有何区别,再决定是否合并
git pull = git fetch + get merge
git fetch origin # 将远程分支本地缓存全部更新
`一般使用如下`
git fetch <origin> <remote branch> # 更新当前分支追踪的 远程分支本地缓存
git fetch <origin> <remote branch>:<local branch> # 将服务器远程分支拉到本地分支,本地分支不存在就创建(并不不会创建追踪关系)
git diff <branch> # 查看当前分支和指定分支差别
git diff origin/<branch> # 查看当前分支和追踪的远程分支 本地缓存
git merge origin/<branch> # 远程分支缓存合并到当前分支
pull
# 1. 更新所有远程分支本地缓存(但是并没有将远程分支本地缓存合并到其他本地分支中) 2. 拉取当前分支的远程分支合并到当前分支
git pull
git pull <origin> <remote branch> #远程分支和当前分支合并, 并更新远程分支本地缓存
git pull <origin> <remote branch>:<local branch> # 拉远程分支与本地分支合并,并更新远程分支本地缓存
`--allow-unrelated-histories 允许远程分支和本地分支版本不一致`
push
git push [local branch] # 推送本地分支(默认当前)
git push -u origin --all(只用设置一次)# 推送所有分支到远程,并创建追踪 同名远程分支
git push --force # 强行push(如果只更改了commit注释需要强行push)
# push本地分支到origin仓库 远程分支,
# 远程分支不存在则创建,未指定remote branch则默认和local branch同名
git push <origin> -u <local_branch>[:remote_branch]
`-u 同时追踪远程分支`
git push --set-upstream origin <local_branch> # 当远程分支不存在时,push本地分支并创建同名的远程分支
# 删除远程仓库分支
git push origin -d f_test
git push <origin> :<remote_branch>
# fatal: refusing to merge unrelated histories,拒绝合并不相关的历史
git pull origin master –allow-unrelated-histories
git push
branch
git branch #查看本地分支
git branch -a #查看所有分支
git branch -vv # 查看local/remote分支对应关系
git branch -u origin/** # 分支追踪远程分支
git branch --set-upstream-to=origin/test # 分支追踪远程分支
git branch -m oldname newname #分支更名
merge
# 本地develop分支合并到当前分支
git merge develop
# 远程分支本地缓存合并到当前分支
git merge origin/develop
#将指定版本合并到当前分支
git cherry-pick 版本号
delete
git branch -d f_test #删除本地分支(必须和上游分支merge)
git branch -D f_test #不检查merge状态删除本地分支
# 删除远程仓库分支
git push <origin> -d <remote_branch>
git push <origin> :<remote_branch>
cherry-pick
- 将将指定版本合并到当前分支
git cherry-pick 版本号
tag 标签操作
- tag是git版本库的一个标记,对应某次commit,主要用于发布版本的管理,一个版本发布之后,打上 v1, v2标签
- branch对应一系列commit
查看tag
git tag
# 查看远程tag
git ls-remote --tags <origin>
git show <tag_name> # 查看tag详细信息
git checkout <tag_name> # 切换到tag对应commit点
创建tag
- tag创建后还要推送到仓库
git tag <tag_name> # 创建tag
git tag -a <tag_name> -m 'commit msg' # 创建并添加注释信息
推送tag
- tag创建后还要推送到仓库
git push origin <tag_name> # 推送tag
git push origin --tags # 推送所有tag
删除tag
git tag -d <tagName> # 本地 tag 的删除
git push origin :<tagName> # 远程 tag 的删除
.gitignore文件不生效
git rm -r --cached .
git add .
git commit -a -m"update .gitignore"