git常用指令记录
链接
1. 查看指令列表
git help
打开git config 命令的手册
git help config
指令后加 -h 可查看该指令快速参考,例如
git add -h
usage: git add [<options>] [--] <pathspec>...
-n, --dry-run dry run
-v, --verbose be verbose
...
2. 查看配置
查看所有的配置以及它们所在的文件
git config --list
git config --list --show-origin
3. 基础配置
配置用户信息
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
4. 常用指令
本地初始化git仓库,在当前目录创建.git文件夹
git init
克隆现有的仓库
git clone https://xxxxx [自定义名字]
关联本地和远程仓库
git remote add <shortname> <url>
git remote add: 添加一个远程版本库关联
git remote rm: 删除某个远程版本库关联
git remote -v 查看关联的远程仓库
查看某个远程仓库
git remote show <remote>
从远程仓库中抓取与拉取
git fetch <remote>
将数据下载到你的本地仓库 不会自动合并或修改你当前的工作
设置了跟踪分支可以用 git pull 抓取数据并自动尝试合并到当前所在的分支
推送到远程仓库
git push <remote> <branch>
检查文件状态
git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
检查文件改变细节(本身只显示尚未暂存的改动)
git diff
查看已暂存
git diff --staged
提交更新
git commit -m "xxxxxx"
移除文件
git rm xxx
删除之前修改过或已经放到暂存区的文件,则必须使用强制删除选项 -f
git rm -f
想把文件从 Git 仓库中删除(亦即从暂存区域移除),但仍然希望保留在当前工作目录
git rm --cached xxx
文件移动、重命名
git mv old new
相当于执行
mv old new
git rm old
git add new
分支重命名
1、修改本地分支名称
git branch -m oldBranchName newBranchName
2、将本地分支的远程分支删除
git push origin :oldBranchName
3、将改名后的本地分支推送到远程,并将本地分支与之关联
git push --set-upstream origin newBranchName
查看提交历史
git log
显示每次提交所引入的差异(按 补丁 的格式输出),并限制日志条目数量
git log -p -1
| 选项 | 说明 |
|---|---|
| -p | 按补丁格式显示每个提交引入的差异。 |
| --stat | 显示每次提交的文件修改统计信息。 |
| --shortstat | 只显示 --stat 中最后的行数修改添加移除统计。 |
| --name-only | 仅在提交信息后显示已修改的文件清单。 |
| --name-status | 显示新增、修改、删除的文件清单。 |
| --abbrev-commit | 仅显示 SHA-1 校验和所有 40 个字符中的前几个字符。 |
| --relative-date | 使用较短的相对时间而不是完整格式显示日期(比如“2 weeks ago”)。 |
| --graph | 在日志旁以 ASCII 图形显示分支与合并历史。 |
| --pretty | 使用其他格式显示历史提交信息。可用的选项包括 oneline、short、full、fuller 和 format(用来定义自己的格式)。 |
| --oneline | --pretty=oneline --abbrev-commit 合用的简写。 |
修补提交
git commit --amend
eg: 第二次提交将代替第一次提交的结果
git commit -m 'initial commit'
git add forgotten_file
git commit --amend
Git 别名
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
查看分支
git branch:查看本地所有分支信息
git branch -r:查看远程仓库所有分支
git branch -a:查看本地和远程仓库所有分支
拉取远端分支代码并在本地创建相同分支
git checkout --track origin/xxxx
查看本地分支与远程分支的关联关系
git branch -vv [可选特定本地分支名]
在某个分支基础上创建新分支
git checkout -b [新分支名]
暂存修改内容
# 1. 暂存当前修改
git stash save "暂存说明"
# 2. 创建并切换到新分支(如果需要将修改内容改动到新分支)
git checkout -b new-feature-branch
# 3. 提取暂存内容到新分支
git stash pop
# (可选)查看暂存列表
git stash list
允许合并history
git pull --allow-unrelated-histories
还原全部
git checkout .
还原排除特定文件
# 1. 暂存你要保留的文件
git add app/index.js
# 2. 还原其它所有文件
git checkout . # 或 git restore .
# 3. 恢复你暂存的文件
git reset app/index.js

浙公网安备 33010602011771号