git常用操作
基础操作
git help <command>
:help
git init
: 初始化一个新的rept,保存在.git文件夹
git status
: 查看目前状态
git add <filename>
: 添加文件
git commit
: 生成一个commit
git log
: 查看history
git log --graph --all --decorate
: 以图的形式可视化log
git log --graph --all --decorate --oneline
: 以图的形式可视化log,每个记录一行
git diff <filename>
: 显示文件与上次commit的不同
git diff <revision> <filename>
: 显示文件与之前版本的不同
git checkout <revision>
: 更新HEAD和目前分支
分支与合并
git branch
: 显示分支
git branch <name>
: 生成一个分支
git checkout -n <name>
: 生成一个分支并转到它,相当于git branch <name> && git checkout <name>
git merge <revision>
:合并一个分支到当前分支
git mergetool
: 使用一个工具解决merge冲突
git rebase
: 分支的依赖分支
远程
git remove
:列出远程
git remove add <name> <url>
: 添加远程
git push <remote> <local branch>:<remote branch>
: 将对象送到远程,并更新远程
git branch --set-upstream-to=<remove>/<remote branch>
: 建立远程和本地分支对相应
git fetch
: 获取远程分支
git pull
: 相当于 git fetch; git merge
git clone
: 下载
撤销
git commit --amend
: 修改一个commit的内容
git reset HEAD -- <file>
: 清空add命令向暂存区提交的关于file文件的修改(Ustage);这个命令仅改变暂存区,并不改变工作区
git checkout -- <file>
:撤销对工作区修改;这个命令是以最新的存储时间节点(add和commit)为参照,覆盖工作区对应文件file
高级
git config
: git 的配置文件
git clone --shallow
: 不带有任何版本历史的clone
git add -p
: 交互式add
git rebase -i
: 交互式rebase
git blame
: 显示每行的修改信息
git stash
: 将所有未提交的修改(工作区和暂存区)保存至堆栈中,用于后续恢复当前工作目录
git bisect
: 二分查找历史
参考
https://missing.csail.mit.edu/2020/version-control/