git 高频操作

116 阅读2分钟

远程仓库

添加仓库

git init  // 初始化仓库

git remote add 远程仓库名 远程分支url // 添加仓库

git remote set-url 远程仓库名  远程分支url // 更换远程分支关联地址

git remote rename 远程仓库名  新远程仓库名

查看远程仓库

git remote -v

删除仓库

git remote remove 远程仓库名

配置信息

查看配置

git config --list // 查看所有配置

git config --local --list // 查看本地配置

git config --global --list // 查看全局配置

设置配置

 git config --local user.name xxx  // 设置当前用户名
 git config --local user.email xxx@xxx.com // 设置当前邮箱

 git config --golbal user.name xxx  // 设置全局用户名
 git config --global user.email xxx@xxx.com // 设置全局邮箱

合并分支

没有冲突

git merge 分支名

选取某个commit合并

git cherry-pick commit-id

切换分支

快速切换到上一分支

git checkout -

切换本地分支

git checkout branch 

创建并切换到远程分支

  1. 拉取远程分支并切换
git checkout -t origin/xxxx
  1. 采用此种方法建立的本地分支会和远程分支建立映射关系。
git checkout -b xxxx origin/xxxx

提交代码

git commit -nv
git commit --amend
git commit cherry-pick  xxx

撤销

撤销当前没有暂存的修改

git checkout fileName   // 撤销某个文件修改
git checkout .  // 撤销所有修改

git clean -f test/* // 撤销test下没被track的文件或文件夹
git clean -df  // 删除当前文件夹下没被track的文件或文件夹

撤销到某个commit

git reset HEAD^  // 回到上次commit,保存之后的修改
git reset commitID // 回到commitID状态,保存之后的修改

撤销提交 并且删除

git reset --hard HEAD^  // 回到上次提交状态,丢弃所有已有修改
git reset --hard commmitID  //回到commitID该状态

撤销建立仓库的第一次commit

git update-ref -d HEAD

统计代码量

git log --since="2020-07-01" --before="2020-08-13" --author="wb-wzc589583" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'

查询个人提交信息并导出

git log --author=<yourname> --after=2023-01-01 --grep=DIR:DTS > ./log.txt

恢复已删除的commit版本

git reflog


git reset commitId

修改commit信息

git commit --amend
git rebase <branch name>

# 再需要修改的commit前 将pick改为reword
# wq保存 每条需要修改的commit信息

pick 正常记录
reword 需要修改的commit记录