花了大概一天半的时间看完git(Git官网)
感谢廖雪峰老师!
廖老师的网址分享了一个git cheat sheet
这里我把它总结成中文,也做了一点添加,方便自己查询
创建
#在当前目录下创建一个空目录
mkdir <content-name>
#克隆现有仓库
#使用ssh前提是以配置好SSH密钥
git clone ssh://uder@damain.com/repo.git
git clone git@github.com:github<user-name>/<repo-namee>.git
#否则直接用地址下载(慢一些)
git clone <url>
#创建新的本地仓库
git init
本地变化
#查看工作目录中已修改的文件
git status
#change to tracked files??什么意思
git diff
#显示工作区和版本库内文件的区别
git diff HEAD --<file-name>
#将所有当下修改添加到暂存区
git add .
#将一些<file>中的改变添加到暂存区
git add -p <file>
#可以一次add多个文件,再commit
#commit all local changes in tracked files??tracked file是什么意思
git commit -a
#提交之前进行的修改
git commit
#更改最后一次提交??不会用
#不要修改已发布的提交
git commit --amend
#提交修改并且对这次提交进行描述
git commit -m "描述内容"
#在工作区删除某文件
rm <file-name>
#在工作区删除某文件,并更新到暂存区
git rm <file-name>
提交历史
#(从最近的提交开始)显示所有提交
git log
#显示最近n次的提交
git log -n
#每次提交信息显示在一行
git log --pretty=oneline
#每次提交信息显示在一行,并且一堆0x中只显示commitid
git log --pretty=oneline --abbrev-commit
#显示特定文件随时间(从最近开始)的变化
git log -p <file>
#显示全局分支提交合并图
git log --graph
#以一行显示分支提交合并图
git log --graph --pretty=oneline --abbrev-commit
#显示所有做过的改变
git reflog
#显示是谁何时何内容修改了<file>文件
git blame <file>
分支&标签
#列出现存所有分支
git branch
#列出所有分支(包括存在后删除的)
git branch -av
#改变HEAD指针,使指向branch分支,两种命令:
git checkeout <branch>
git switch <branch>
#在当前的HEAD指针下创建一个新的分支
git branch <new-branch>
#在当前HRAD下创建新分支并切换到新分支,两种命令:
git checkout -b <new-branch>
git switch -c <new-branch>
#基于远程分支创建一个新的跟踪分支(tracking branch)???
git checkout --track <remote/branch>
#在本地创建一个和远程分支对应的分支
git checkout -b <branch-name> <remote/branch-name>
#将本地分支与远程分支建立连接
git branch --set-upstream <branch-name> <remote/branch-name>
#删除一个本地分支
git branch -d <branch>
#删除一个还没有merge到master上的分支
git branch -D <branch>
#删除远程库的一个分支
git branch -dr <remote/branch>
#显示现存所有tag
git tag
#显示tagname的commit的具体信息
git show <tagname>
#在当前提交上打一个标签
git tag <tag-name>
#在指定的提交(commit-od)上打上标签(tag-name)
git tag <tag-name> <commit-id>
#在提交<commit-id>上创建带有描述的标签
git tag -a <tagname> -m "描述" <commit-id>
#删除本地标签
git tag -d <tagname>
#删除本地和远程库的标签
git push origin :refs/tags/<tagename>
合并&变基
#将分支branch合并到当前的HEAD上
git merge <branch>
#use your confligured merge tool to solve conflicts??
git mergetool
#禁止使用fastforward(直接改变分支指针merge)合并,将HEAD所指地方的状态改为分支branch当前状态并且commit(-m参数后跟这次commit的描述)
git merge --no-ff -m "描述" <branch>
#将某次commit所做的修改“复制到”当前HEAD下(一般用在bug修改时)
git cherry-pick <commit-id>
#将当前HEAD所在分支变基
#不要将以发布的推送变基
git rebase <branch>
#终止变基
git rebase --abort
#解决冲突后继续变基
git rebase --continue
#use your confligured merge tool to solve conflicts
更新&发布
#列出所有现存已配置的远程库
git remote -v
#显示远程库的具体信息
git remote show <remote>
#添加一个新的远程库(地址url),并命名为shortname
git remote add <shortname> <url>
#下载remote的所有改变,但是不要集成到HEAD上
git fetch <remote>
#下载remote的所有改变,并且合并/继承到HEAD上
git pull <remote> <branch>
#将本地的改变发布在远程库
git push <remote> <branch>
#将本地所有标签发布到远程库上
git push <remote> --tags
#将tagname标签发布到远程库上
git push <remote> <tag-name>
撤销
#丢弃当前工作区的所有改变,回到上一个commit时的状态(直接改变HEAD指向)
git reset --hard HEAD^
#丢弃当前工作区的所有改变,回到和某个commit时的状态(直接改变HEAD指向)
git reset --hard <commit>
#丢弃某个文件的改变,回到上一个commit时的状态
git checkout HEAD <file>
#丢弃当前工作区的所有改变,提交一个和某个commit时状态一样的commit
git revert <commit>
#丢弃当前工作区的所有改变,回到上某个commit时的状态,and preserve all changes as unstaged changes并将所有更改保留为未分阶段的更改??
git reset <commit>
#丢弃当前工作区的所有改变,回到上某个commit时的状态,and preserve uncommitted local changes并保留未提交的本地更改??
git reset --keep <commit>
#撤销工作区文件的修改,回到暂存区状态(如果暂存区无内容则回到上一个commit的状态)
git restore <file-name>
#撤销暂存区文件的修改(直接删除了暂存区的文件)
git restore --stage <file-name>
保存工作现场
#把当前工作现场(未commit的)"储藏"起来
git stash
#查看保存的工作现场
git stash list
#恢复到最新保存的工作现场
git stash apply
#删除最新保存的工作现场
git stash drop
#恢复到最新保存的工作现场并删除此保存
git stash pop
#指定恢复到某个工作现场
git stash apply stash@{0}
自定义Git
#为命令设置别名
git config --global alias.<别名> <原名>