开发工具系列:Git常用命令

常用的设置命令如下:

# 设置全局的用户名和Email
git config --global user.name "[your-name]"
git config --global user.email "[your-email]"

# 在项目目录下执行,为当前项目设置特定的用户信息
git config user.name "[your-name]"

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

git config user.name
# 查看指定配置

初始化:

git init
# 初始化一个新的Git仓库

git clone <repository>
# 克隆远程仓库到本地

添加和提交文件:

git add <file>
# 将文件添加到暂存区,多个文件用空格隔开

git add .
# 将所有修改过的文件添加到暂存区

git add -u
# 将所有修改过的文件添加到暂存区,不包括新文件

git commit -m "<message>"
# 提交暂存区的文件,并附上提交信息

git commit -a
# 将所有修改过的文件添加到暂存区,并提交暂存区的文件

git commit --amend -m "<new commit message>"
# 修改最后一次提交的提交信息

git show <commit>
# 查看指定提交的详细内容

 删除和重命名:

git rm <file>
# 删除工作区文件,并将这次删除放入暂存区

git rm --cached <file>
# 删除暂存区文件,但保留在工作区

git rm -r <dir>
# 删除工作区目录,并将这次删除放入暂存区

git rm -rf <dir>
# 删除工作区目录,不将这次删除放入暂存区

git mv <file-original> <file-renamed>
# 将文件重命名,并将这次重命名放入暂存区

拉取和推送:

git pull
# 拉取远程仓库的变更到本地

git push
# 将本地变更推送到远程仓库

分支:

git branch
# 列出本地分支

git checkout <branch>
# 切换到指定分支

git branch -a
# 列出所有分支(包括远程分支)

git branch <branch-name>
# 创建新分支

git checkout <branch-name>
# 切换到指定分支

git checkout -b <new-branch-name>
# 创建并切换到新分支

git branch -d <branch-name>
# 删除本地分支

git branch -D <branch-name>
# 强制删除本地分支

git merge <branch>
# 合并指定分支到当前分支

git rebase <branch-name>
# 将指定分支的变更合并到当前分支(使用rebase)

git merge --no-commit --squash <branch>
# 合并时只保留与当前分支不同的部分,并将变更合并为一个提交

状态和比较: 

git status
查看当前仓库的状态

git diff
# 比较工作目录和暂存区的差异

git diff --cached
# 比较暂存区和本地仓库的差异

git diff --staged
# 比较暂存区和最新提交的差异

git diff --stat
# 以统计信息的形式显示差异

git diff -w
# 忽略空白字符的差异

git diff <file>
# 比较指定文件在工作目录和暂存区的差异

git diff <commit1>..<commit2>
# 比较两个提交之间的差异

git diff --name-status <commit1>..<commit2>
# 显示变化(包括改动、新增和删除)的文件

git diff <branch1>..<branch2>
# 比较指定分支之间的差异

git diff <commit> -- <file>
# 比较某个提交中的具体文件差异

日志: 

git log
# 查看提交日志

git lg
# 参考文中“设置”部分绑定的美化后的log

git log --follow <file>
# 查看指定文件的提交日志

git log --oneline
# 查看提交日志,仅显示一行

git log --pretty=oneline
# 查看提交日志,仅显示一行

git log --pretty=format:"%h - %an, %ar : %s"
# 查看提交日志,并按照格式显示

git log --graph
# 查看分支合并图

git log --graph --oneline --decorate --all --date=relative
# 查看分支合并图,单号显示,并按照时间排序

git log --since=2.weeks
# 查看最近两周的提交日志

git log --author=<pattern>
# 查看指定作者的提交日志

git log <branch1>..<branch2>
# 查看branch2相对于branch1的提交日志

git log --no-merges
# 查看提交日志,不显示合并提交

git log --grep <pattern>
# 查看提交说明中包含指定关键字的提交日志

git log -p <file>
# 查看指定文件的每次详细修改内容的提交日志

git log -p -2
# 查看最近两次详细修改内容的提交日志

git log --stat
# 查看提交日志,仅显示简要的增改行数统计

git log --summary
# 查看提交日志,仅显示简要的增改行数统计

git log --pretty=short
# 查看提交日志,显示较少的提交信息

git log --pretty=full
# 查看提交日志,显示较多的提交信息

git log --pretty=format:"%h %s" --graph
# 查看提交日志,显示提交的简略信息和分支合并图

标签: 

git tag
# 列出所有标签

git tag <tag-name>
# 在当前commit新建一个tag

git tag <tagname> <commit>
# 在指定commit新建一个tag

git tag -a <tag-name> -m "Your tag message"
# 创建带有附注的标签

git show <tag-name>
# 查看标签信息

git tag -d <tag-name>
# 删除标签

撤销: 

git checkout -- <file>
# 撤销工作区指定文件的修改

git checkout .
# 撤销工作区所有文件的修改

git checkout -- <dir>
# 撤销工作区目录的修改

git reset HEAD <file>
# 将暂存区的指定文件还原成和HEAD相同的状态

git reset --hard HEAD
# 将暂存区和工作区都还原成和HEAD相同的状态

git reset --hard <commit>
# 将暂存区和工作区都还原成和指定commit相同的状态

远程操作: 

git remote -v
# 列出所有远程仓库,并显示URL

git remote add <remote-name> <remote-url>
# 添加远程仓库

git remote remove <remote-name>
# 删除远程仓库

git remote rename <old-remote-name> <new-remote-name>
# 重命名远程仓库

git remote show <remote-name>
# 显示远程仓库的信息

git remote set-url <remote-name> <new-remote-url>
# 修改远程仓库的URL

git fetch <remote-name>
# 获取远程仓库的变化,但不合并

git pull <remote-name> <branch-name>
# 拉取远程分支到本地

git push <remote-name> <local-branch-name>
# 推送本地分支到远程

git push <remote-name> --delete <branch-name>
# 删除远程分支(本地分支不受影响)

git fetch origin <remote-branch-name>:<local-branch-name>
# 将远程分支拉取到本地

git push origin <local-branch-name>:<remote-branch-name>
# 将本地分支推送到远程

git push origin <tag-name>
# 推送单个标签到远程

git push origin --tags
# 推送所有标签到远程

git push origin --delete <tag-name>
# 删除远程标签
  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值