【Git】git常用命令

文章目录

  • [git add](#git add)
  • [git commit](#git commit)
  • [git log](#git log)
  • [git blame](#git blame)
  • [git reflog](#git reflog)
  • [git status](#git status)
  • [git diff](#git diff)
  • [git pull](#git pull)
  • [git push](#git push)
  • [git branch](#git branch)
  • [git tag](#git tag)
  • [git stash](#git stash)
  • [git cherry-pick](#git cherry-pick)
  • [git rebase](#git rebase)
  • [git reset](#git reset)
  • [git status](#git status)
  • [git show](#git show)
  • --contains
  • [git config](#git config)
  • git的下标
  • .gitignore
  • 其他

记录下工作中较为常用的git命令

git add

bash 复制代码
git add [filename] # 添加指定文件到暂存区
git add . # 将当前目录及其子目录下的所有修改添加到暂存区
git rm [filename] # 一次性删除文件并将此次删除操作添加到暂存区,后续再git commit
git mv [filename-original] [filename-renamed] # 一次性重命名文件并将此次重命名操作添加到暂存区,后续再git commit

git commit

bash 复制代码
git commit --amend -m "" # 使用一次新的commit替代上一次的提交,会修改commit的hash值

git log

bash 复制代码
git log --oneline -n 5 # 显示最新的前五条提交log,每条log单行显示
git log --follow [filename] # 显示某个文件的版本历史

git blame

bash 复制代码
git blame [filename] # 以列表的形式显示指定文件的修改记录

git reflog

bash 复制代码
git reflog # 查看所有可用的历史版本记录,包含被回退的记录

git status

bash 复制代码
git status -s # -s参数使回显更简洁

git diff

bash 复制代码
git diff [filename] # 比较指定文件在工作区和暂存区中的区别,其效果等同于GUI工具中的打开指定文件的working tree
git diff HEAD~ # 与上一次提交相比较
git diff [commitId1] [commitId2] # 比较两次提交
git diff [branchName] # 查看工作区与某个分支间的差异
git diff --shortstat "@{10 day ago}" # 以简洁方式显示10天内的提交

git pull

bash 复制代码
git pull origin [branchName] # 拉取远程分支,并将其内容合并更新到本地工作区中

git push

bash 复制代码
git push --set-upstream origin [localBranchName] # 将本地新建的分支push到远端,第一次push的时候使用

git branch

bash 复制代码
git branch -r # -r:remote列出所有远程分支
git branch -a # 列出所有分支,远程+本地,不同颜色区分
git checkout -b [newBranchName] [branchName] # 从本地分支新建一个分支并checkout到新分支
git checkout -b [branchName] [commitId] # 根据指定的commitId新建个分支

git tag

bash 复制代码
git tag -l # 列出tag
git tag -l 'tag*' # 列出所有tag开头的标签
git show [tagName] # 查看某个tag的信息

git stash

bash 复制代码
git stash # 暂存当前所有修改
git stash list # 查看所有暂存内容
git stash pop # 恢复隐藏内容, 弹出栈顶
git stash pop stash@{0} # 恢复指定的隐藏内容

git cherry-pick

bash 复制代码
git cherry-pick [commitId] # 把指定的commitId的修改合并到当前分支

git rebase

bash 复制代码
git rebase [branchName] # 变基到指定的分支
git rebase -i [commitId] # 变基到指定的commitid,回车后在编辑器中修改内容

git reset

bash 复制代码
git reset [commitId] # reset到指定的commit

git status

bash 复制代码
git status -s # -s:更为简洁的回显

git show

bash 复制代码
git show [commitId] # 查看某个commit的修改内容

--contains

bash 复制代码
git tag --contains # 查看当前分支上的tag
git branch --contains [commitId] # 查看包含指定commit的分支,会列出本地所有包含该commit的分支
git tag --contains [commitId] # 查找包含指定commit的标签

git config

bash 复制代码
git config -l # -l:--list, 列出配置项
git config -l --system # 查看系统配置
git config -l --global # 查看当前用户的全局配置

git的下标

bash 复制代码
HEAD@{2} # 对应的就是以HEAD起始的第三次提交,2为下标

.gitignore

bash 复制代码
!开头,表示规范例外场景

其他

git命令行中结合grep命令

相关推荐
__雨夜星辰__4 分钟前
什么是 Git 与 Git※hub(※Git※hub加※速访※问教程)
git·github
家里有只小肥猫3 小时前
新项目git推送 删除
git
小码狐4 小时前
GIT相关
git·idea·基础
csdn_aspnet5 小时前
Git贡献全流程拆解:从Fork到PR的完整指南
git·fork·pr
XuDream6 小时前
idea中忽略idea文件不提交git和取消被 Git 追踪
java·git·intellij-idea
0xCode 小新7 小时前
Git常用操作
git·gitee·github
0vvv07 小时前
bugku-web-source(git泄露
git·ctf
Joy T7 小时前
【Web3】告别Remix:基于Hardhat的智能合约工程化构建与实战
git·web3·node·智能合约·hardhat
晨曦蜗牛7 小时前
Windows 上 Claude Code 报错 “requires git-bash“ 的完整解决方案
windows·git·bash
不想看见4048 小时前
Qt-Creator如何使用Git【步骤】
git