【Git笔记】Git常用命令

常用

关于Git配置

# 查看当前git环境详细配置
git config -l
# 查看系统config 配置文件在git安装目录/etc/gitconfig
git config --system --list
# 查看当前用户配置 配置文件在~/.gitconfig
git config --global --list
# 配置文件在当前项目的/.git/config
git config --local --list
# 查看所有的配置以及它们所在的文件
git config --list --show-origin
# 查看当前分支(详细)
git config --lis
git config [--local][--global][--system] section.key value
# 设置全局名称
git config --global user.name "Your Name"
# 设置全局邮箱
git config --global user.email "email@example.com"
# 设置当前项目的用户名
git config --local user.name xxxx 
#  配置当前用户的编码项,可以解决中文编码问题
git config --global core.quotepath false
#  配置当前项目不忽略文件大小写,git默认忽略文件名的大小写,这点值得注意
git config --local core.ignorecase false 
# 设置git默认文本编辑器(启动路径文本)
git config --global core.editor "[path]"
# 设置git默认文本编辑器(某个内置编辑器)
git config --global core.editor emacs\vim

Git初始化与克隆

# 在当前目录新建一个仓库
git init
# 在一个目录下新建本地仓库
git init [project-name] 
# 克隆一个远程仓库
git clone [url]
# 克隆一个远程仓库到某个目录
git clone [url] [dir-name]

添加到暂存区修改与提交

# 查看状态
git status
# 查看变更内容
git diff
# 从工作区添加指定文件到暂存区
git add [file-name1] [file-name2] ...
# 添加指定目录到暂存区,包括子目录
git add [directory]
# 将工作区的被修改的文件和新增的文件提交到暂存区,不包括被删除的文件
git add .
# u指update,将工作区的被修改的文件和被删除的文件提交到暂存区,不包括新增的文件
git add -u .
# A指all,将工作区被修改、被删除、新增的文件都提交到暂存区
git add -A .
# 文件改名
git mv [oldname] [newname]
# 删除文件
git rm [file]
# 停止跟踪文件单不删除
git rm --cached [file]
# 递归删除
git rm -r *  
# 提交
git commit -m 'xxx'        
# 修改commit信息 本次
git commit --amend
# 合并上一次提交(用于反复修改)
git commit --amend -m 'xxx'
 # 将add和commit合为一步
git commit -am 'xxx'            

查看提交历史

# 查看提交历史
git log
# 显示x行日志 -x为x行
git log -[number]
# 查看指定文件的提交历史
git log -p [file]
# 以列表的方式查看指定文件的提交历史
git blame [file]
# 显示提交日志及相关变动文件
git log --stat

撤销

# 撤销工作目录中所有未提交文件的修改内容
git reset --hard HEAD
# 撤销置顶的未提交文件的修改内容
git checkout HEAD [file]
# 撤销指定的提交
git revert [commit]

比对

# 显示所有未添加至index的变更
git diff
# 显示所有已添加index但还未commit的变更
git diff --cached
# 比较与上一个版本的差异
git diff HEAD^
# 比较与HEAD版本lib目录的差异
git diff HEAD -- ./lib
 # 比较远程分支master上有本地分支master上没有的
git diff origin/master..master 
 # 只显示差异的文件,不显示具体内容
git diff origin/master..master --stat

远程库

# 查看项目远程仓库
git remote -v
# 添加远程仓库地址
git remote add origin [url]
# 删除远程仓库源
git remote remove origin 
# 获取所有远程分支(不更新本地分支,另需merge)
git fetch
# 获取所有原创分支并清除服务器上已删掉的分支
git fetch --prune
# 列出所有本地标签
git tag
# 基于最新提交常见标签
git tag [tagname]
# 删除标签
git tag -d [tagname]
# -a : 理解为 annotated 的首字符,表示 附注标签
# -m : 指定附注信息
git tag -a [tagname] -m xxxxxx
git tag -a [tagname] [version] -m xxxxx
# 推送标签至远程服务器
git push origin tag [tagname]

分支

# 显示本地分支
git branch
# 显示包含提交50089的分支
git branch --contains [name]
# 显示所有分支
git branch -a
# 删除本地新建分支
git branch -d [name]
# 显示所有原创分支
git branch -r 
# 显示所有已合并到当前分支的分支
git branch --merged
# 显示所有未合并到当前分支的分支
git branch --no-merged 
# 本地分支改名
git branch -m [old] [new]  
# 从当前分支创建新分支并检出
git checkout -b [name]
# 从当前分支创建新分支并检出
git checkout -b [old] [new]
 # 检出已存在的分支
git checkout [name]          
# 检出远程分支并创建本地跟踪分支
git checkout --track [name]
# 检出版本
git checkout v2.0
# 从远程分支develop创建新本地分支devel并检出
git checkout -b devel origin/develop 
# 检出head版本的README文件(可用于修改错误回退)
git checkout -- README 
# 合并远程master分支至当前分支
git merge [name]
# 合并提交的修改
git cherry-pick [name]
# 将当前分支push到远程master分支
git push origin [name]    
# 把所有tag推送到远程仓库
git push --tags
  # 删除分支(本分支修改已合并到其他分支)
git branch -d [name] 
# 强制删除分支
git branch -D [name]

推送/拉取

git pull 
# git允许拉取不关联的历史代码
git pull origin xxxx --allow-unrelated-histories
# git允许推送不关联的历史代码
git push origin xxxx --allow-unrelated-histories

Log 美化

git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

查看个人代码量

[email] 替换成你的提交与邮箱

git log --author="[email]" --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  --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 }';
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值