【引自xjtuhit的博客】
Configuration:配置
列举所有的别名与配置
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git config --list<br></span>
Git 别名配置
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git config --global alias.<handle> <command> git config --global alias.st status<br></span>
设置git为大小写敏感
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git config --global core.ignorecase false<br></span>
User
Help:常用的辅助查询命令
在git 命令行里查看everyday git
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git help everyday<br></span>
显示git常用的帮助命令
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git help -g<br></span>
获取Git Bash的自动补全
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">curl http://git.io/vfhol > ~/.git-completion.bash && echo '[ -f ~/.git-completion.bash ] && . ~/.git-completion.bash' >> ~/.bashrc<br></span>
设置自动更正
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git config --global help.autocorrect 1<br></span>
Remote:远端仓库配置
获取所有远端引用配置
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git remote<br></span>
或者
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git remote show<br></span>
修改某个远端的地址
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git remote set-url origin <URL><br></span>
Repo
查看当前仓库中的所有未打包的objects和磁盘占用
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git count-objects --human-readable<br></span>
从object数据库中删除所有不可达的object
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git gc --prune=now --aggressive<br></span>
Cache:缓存
Track:文件追踪
Info
展示所有被追踪的文件
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git ls-files -t<br></span>
展示所有未被追踪的分支
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git ls-files --others<br></span>
展示所有被忽略的文件
- <span style="font-family:'宋体', SimSun;">git ls-files --others -i --exclude-standard<br>git check-ignore *<br>git status --ignored<br></span>
Manipulation:操作
停止追踪某个文件但是不删除它
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git rm --cached <file_path><br></span>
或者
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git rm --cached -r <directory_path><br></span>
强制删除未被追踪的文件或者目录
- <span style="font-family:'宋体', SimSun;">git clean -f<br>git clean -f -d<br>git clean -df<br></span>
清空.gitignore
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git clean -X -f<br></span>
Changes:修改
Info:信息查看
查看上次提交之后的未暂存文件
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git diff<br></span>
查看准备用于提交的暂存了的修改的文件
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git diff --cached<br></span>
显示所有暂存与未暂存的文件
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git diff HEAD<br></span>
查看最新的文件版本与Stage中区别
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git diff --staged<br></span>
dd:追踪某个修改,准备提交
Stage某个文件的部分修改而不是全部
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git add -p<br></span>
Reset:修改重置
以HEAD中的最新的内容覆盖某个本地文件的修改
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git checkout -- <file_name><br></span>
Stash:贮存
Info:信息查看
展示所有保存的Stashes
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git stash list<br></span>
Manipulation:操作
Save:保存
保存当前追踪的文件修改状态而不提交,并使得工作空间恢复干净
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git stash<br></span>
或者
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git stash save<br></span>
保存所有文件修改,包括未追踪的文件
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git stash save -u<br></span>
或者
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git stash save --include-untracked<br></span>
Apply:应用
应用任何的Stash而不从Stash列表中删除
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git stash apply <stash@{n}><br></span>
应用并且删除Stash列表中的最后一个
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git stash pop<br></span>
或者
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git stash apply stash@{0} && git stash drop stash@{0}<br></span>
删除全部存储的Stashes
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git stash clear<br></span>
或者
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git stash drop <stash@{n}><br></span>
从某个Stash中应用单个文件
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git checkout <stash@{n}> -- <file_path><br></span>
或者
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git checkout stash@{0} -- <file_path><br></span>
Commit:提交
检索某个提交的Hash值
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git rev-list --reverse HEAD | head -1<br></span>
Info:信息查看
List:Commit列表
查看自Fork Master以来的全部提交
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git log --no-merges --stat --reverse master..<br></span>
展示当前分支中所有尚未合并到Master中的提交
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git cherry -v master<br></span>
或者
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git cherry -v master <branch-to-be-merged><br></span>
可视化地查看整个Version树
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git log --pretty=oneline --graph --decorate --all<br></span>
或者
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">gitk -all<br></span>
查看所有在分支1而不在分支2中的提交
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git log Branch1 ^Branch2<br></span>
Files:文件信息
展示直到某次提交的全部文件列表
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git ls-tree --name-only -r <commit-ish><br></span>
展示所有在某次提交中修改的文件
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git diff-tree --no-commit-id --name-only -r <commit-ish><br></span>
展示所有对于某个文件的提交修改
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git log --follow -p -- <file_path><br></span>
Manipulation:关于提交的操作
Apply:Commit确认或者应用
利用cherry-pick将某个分支的某个提交跨分支的应用到其他分支
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git checkout <branch-name> && git cherry-pick <commit-ish><br></span>
提交时候忽略Staging区域
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git commit -am <commit message><br></span>
提交时候忽略某个文件
- <span style="font-family:'宋体', SimSun;">git update-index --assume-unchanged Changelog; <br>git commit -a; <br>git update-index --no-assume-unchanged Changelog<br></span>
撤销某个故意忽略
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git update-index --no-assume-unchanged <file_name><br></span>
将某个提交标记为对之前某个提交的Fixup
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git commit --fixup <SHA-1><br></span>
Reset:将当前分支的HEAD重置到某个提交时候的状态
重置HEAD到第一次提交
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git update-ref -d HEAD<br></span>
丢弃自某个Commit之后的提交,建议只在私有分支上进行操作。注意,和上一个操作一样,重置不会修改当前的文件状态,Git会自动将当前文件与该Commit时候的改变作为Changes列举出来
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git reset <commit-ish><br></span>
Undo&Revert:撤销与恢复某个Commit
以创建一个新提交的方式撤销某个提交的操作
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git revert <commit-ish><br></span>
恢复某个文件到某个Commit时候的状态
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git checkout <commit-ish> -- <file_path><br></span>
Update:修改某个Commit
修改上一个提交的信息
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git commit -v --amend<br></span>
修改提交的作者信息
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git commit --amend --author='Author Name <email@address.com>'<br></span>
在全局的配置改变了之后,修改某个作者信息
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git commit --amend --reset-author --no-edit<br></span>
修改前一个Commit的提交内容但是不修改提交信息
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git add --all && git commit --amend --no-edit<br></span>
Branch:分支
Info:信息查看
获取当前分支名
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git rev-parse --abbrev-ref HEAD<br></span>
Tag
列举当前分支上最常用的标签
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git describe --tags --abbrev=0<br></span>
List:分支枚举
获取所有本地与远程的分支
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git branch -a<br></span>
只展示远程分支
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git branch -r<br></span>
根据某个Commit的Hash来查找所有关联分支
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git branch -a --contains <commit-ish><br></span>
或者
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git branch --contains <commit-ish><br></span>
Changes:某个分支上的修改情况查看
查看两周以来的所有修改
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git log --no-merges --raw --since='2 weeks ago'<br></span>
或者
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git whatchanged --since='2 weeks ago'<br></span>
Merger:合并情况查看
追踪某个分支的上游分支
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git branch -u origin/mybranch<br></span>
列举出所有的分支以及它们的上游和最后一次提交
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git branch -vv<br></span>
列举出所有已经合并进入Master的分支
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git branch --merged master<br></span>
Manipulation:操作
Checkout:检出与分支切换
快速切换到上一个分支
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git checkout -<br></span>
不带历史记录的检出某个分支
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git checkout --orphan <branch_name><br></span>
Remove:分支移除
删除本地分支
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git branch -d <local_branchname><br></span>
删除远程分支
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git push origin --delete <remote_branchname><br></span>
或者
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git push origin :<remote_branchname><br></span>
移除所有已经合并进入Master的分支
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git branch --merged master | grep -v '^\*' | xargs -n 1 git branch -d<br></span>
移除所有在远端已经被删除的远程分支
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git fetch -p<br></span>
或者
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git remote prune origin<br></span>
Update:信息更新
修改当前分支名
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git branch -m <new-branch-name><br></span>
或者
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git branch -m [<old-branch-name>] <new-branch-name><br></span>
rchive:打包
将Master分支打包
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git archive master --format=zip --output=master.zip<br></span>
将历史记录包括分支内容打包到一个文件中
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git bundle create <file> <branch-name><br></span>
从某个Bundle中导入
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git clone repo.bundle <repo-dir> -b <branch-name><br></span>
Merge:合并
Pull&Push:远程分支合并操作
用pull覆盖本地内容
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git fetch --all && git reset --hard origin/master<br></span>
根据Pull的ID拉取某个Pull请求到本地分支
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git fetch origin pull/<id>/head:<branch-name><br></span>
或者
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git pull origin pull/<id>/head:<branch-name><br></span>
Rebase:变基
在Pull时候强制用变基进行操作
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git config --global branch.autosetuprebase always<br></span>
将某个feature分支变基到master,然后合并进master
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git checkout feature && git rebase @{-1} && git checkout @{-2} && git merge @{-1}<br></span>
变基之前自动Stash所有改变
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git rebase --autostash<br></span>
利用变基自动将fixup提交与正常提交合并
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git rebase -i --autosquash<br></span>
利用ReBase将前两个提交合并
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git rebase --interactive HEAD~2<br></span>
Diff&Conflict:差异与冲突
Info:信息查看
列举全部的冲突文件
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git diff --name-only --diff-filter=U<br></span>
在编辑器中打开所有冲突文件
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git diff --name-only | uniq | xargs $EDITOR<br></span>
Workflow:工作流
SubModules:子模块
Info:信息查看
Manipulation:操作
利用SubTree方式将某个Project添加到Repo中
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git subtree add --prefix=<directory_name>/<project_name> --squash git@github.com:<username>/<project_name>.git master<br></span>
更新所有的子模块
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git submodule foreach git pull<br></span>
Work Tree
Manipulation:操作
从某个仓库中创建一个新的Working Tree
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git worktree add -b <branch-name> <path> <start-point><br></span>
从HEAD状态中创建一个新的Working Tree
- <span style="color:rgb(0,0,0);font-family:'宋体', SimSun;"><span style="color:rgb(0,0,0);font-family:'宋体', SimSun;font-size:14px;">git worktree add --detach <path> HEAD</span><span style="margin:0px;padding:0px;color:rgb(192,0,0);font-family:arial, helvetica, sans-serif;font-size:14px;"><br></span></span>