记录自己在使用git的过程中,所用到的指令。绿色代表参数。
ssh-keygen -t rsa -C "your_email@example.com"
生成ssh密钥
git init
初始化本地代码仓库
git branch branchname
新建branchname分支
git branch -d(-D) branchname
删除branchname分支,-D是强制删除
git checkout branchname
切换到branchname分支
git stash
暂存从上次commit到当前的改动,通常用于不想commit当前内容同时又想回退版本
git stash pop
弹出暂存的改动
git add .
添加当前所有改动到暂存区
git add filename
添加文件filename到暂存区
git commit -m "版本1"
提交缓存区到本地仓库并添加注释"版本1"
git status
查看当前分支改动状态
git log branchname
查看分支branchname的提交日志,如果不加分支名则显示当前分支
git log -p
查看当前分支提交日志,并显示每次提交的具体内容
git log --stat
查看当前分支提交日志,并显示每次提交更改了哪些文件
git reflog
查看所有操作,通常用于撤销版本回退
git diff branchname1 branchname2
对比不同分支差异
git reset --hard headID
硬性重置到headID版本
git reset --soft headID
重置到headID版本,但是不回退代码
git reset --soft HEAD~n
回退版本HEAD,不回退代码,n为倒数第几次commit
git reset --hard HEAD~n
回退版本HEAD和代码,n为倒数第几次commit
git fetch remotehostname remotebranchname
拉取远程主机remotehostname的remotebranchname分支代码到本地
git merge FETCH_HEAD
把拉取的代码合并至当前分支
git pull remotehostname remotebranchname:localbranchname
拉取远程主机remotehostname的remotebranchname分支代码并合并至本地localbranchname分支
git push remotehostname localbranchname:remotebranchname
推送本地localbranchname分支代码到远程主机remotehostname的remotebranchname分支
git remote add remotehostname ssh
添加远程仓库与本地建立连接,名字命名为remotehostname,仓库链接为ssh
git config --global core.pager less
git显示内容的分页设置为less
git commit --amend
修改上一次commit的注释内容
git reset HEAD
刚使用过git add后,可以使用这个取消add的内容
git checkout .
取消当前所有文件改动,恢复到上一次commit
git checkout filename
文件filename恢复到上次commit的状态
git diff > patchname.patch
打包从上次commit开始到当前为止,所有文件改动到patchname.patch里面
git apply patchname.patch
应用补丁patchname.patch到当前工作目录
修改commit提交模板
在~/.gitconfig中添加:
CommitTemplate即为你的提交模板。每次commit不用再使用 -m 选项添加commit注释,而是默认为模板内容,提交时会直接使用vim打开模板,修改完成后保存,则注释自动提交。
git diff file1 file2 ... filen > tmp.patch
添加file1、file2等文件的改动到tmp.patch里
git co file1 file2 ... filen
撤销file1、file2等文件在工作区的改动
git cherry-pick commitID
把commitID指向的commit合并到当前分支。通常在hotfix分支修改完后commit,然后checkout到master分支,再把hotfix分支的修改合并到master分支
git branch --set-upstream-to=origin/master
设置当前分支使用git pull时关联的远程分支,这里remote是origin,分支是master