git经常使用命令操做小结php
1.
ssh链接方式 公钥生成
ssh-keygen -t rsa -C "764432054@qq.com"
在用户家目录下的.ssh目录下生成 id_rsa ,id_rsa.pub 把公钥文件(id_rsa.pub)内容加到github里的sshkey里
2.
配置linux
git config --global user.email "you@example.com"git config --global user.name "Your Name"获取身份信息 git config --global --get user.email warning: LF will be replaced by CRLF | fatal: CRLF would be replaced by LF 遇到这两个错误,是由于Git的换行符检查功能。 core.safecrlf Git提供了一个换行符检查功能(core.safecrlf),能够在提交时检查文件是否混用了不一样风格的换行符 这个功能的选项以下:false - 不作任何检查 warn - 在提交时检查并警告true - 在提交时检查,若是发现混用则拒绝提交 假如你正在Windows上写程序,又或者你正在和其余人合做,他们在Windows上编程,而你却在其余系统上,在这些状况下,你可能会遇到行尾结束符问题. 这是由于Windows使用回车和换行两个字符来结束一行,而Mac和Linux只使用换行一个字符。虽然这是小问题,但它会极大地扰乱跨平台协做。 Git能够在你提交时自动地把行结束符CRLF转换成LF,而在签出代码时把LF转换成CRLF。 用core.autocrlf来打开此项功能,若是是在Windows系统上,把它设置成true,这样当签出代码时,LF会被转换成CRLF: $ git config --global core.autocrlf trueLinux或Mac系统使用LF做为行结束符,所以你不想 Git 在签出文件时进行自动的转换; 当一个以CRLF为行结束符的文件不当心被引入时你确定想进行修正,把core.autocrlf设置成input来告诉 Git 在提交时把CRLF转换成LF,签出时不转换: $ git config --global core.autocrlf input 这样会在Windows系统上的签出文件中保留CRLF,会在Mac和Linux系统上,包括仓库中保留LF。 若是你是Windows程序员,且正在开发仅运行在Windows上的项目,能够设置false取消此功能,把回车符记录在库中: $ git config --global core.autocrlf false
3.
设置发布模式git
git config --global push.default simple/matching Matching'matching'参数是 Git 1.x 的默认行为,其意是若是你执行 git push 但没有指定分支,它将 push 全部你本地的分支到远程仓库中对应匹配的分支。 Simple 而 Git 2.x 默认的是 simple,意味着执行 git push 没有指定分支时,只有当前分支会被 push 到你使用 git pull 获取的代码。
4.
一些经常使用操做
git add filename git add . 表示当前目录
git commit -m '注释' -o filename
git push
git checkout --
git reset HEAD程序员
git rm file (若是工做区或者暂存区有修改,会报err,一个文件在版本库里,在工做区作了修改(未add到暂存区或者已经add到暂存区),直接git rm 不容许)
git commit -m 'delete file' -o filegithub
git rm用于删除一个文件,若是一个文件已经被提交到版本库,不用担忧误删,可是要当心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容编程
也能够这样删除
rm file
git add file
git commit -m 'delete file' -o filewindows
git reflog
查看全部历史 包括回退历史
git log只是查看 从开始到如今的版本历史(不包括回退的) $ git log --pretty=oneline
git reset --hard c5dd3a558005fb12f8 回退到某一版本
5.
分支操做
查看分支
git branch -a(全部分支包括远程分支)
git checkout -b dev
建立并切换到dev分支
至关于
git branch dev
git checkout devapp
在dev分支作了修改后
切换到 master 分支
git checkout devssh
把线上的分支同步到本地(本地没此分支)
好比远程有分支 b1 本地没有
git checkout -b origin/b1 同步远程分支到本地ide
git checkout -b 本地分支名 origin/远程分支名 (本地该分支名分支必须不存在时)
git merge dev 合并dev到当前分支
注意--no-ff参数,表示禁用Fast forward:
git merge --no-ff -m "merge with no-ff" dev
合并后的历史有分支,能看出来曾经作过合并,而fast forward合并就看不出来曾经作过合并
本地分支重命名 git branch -m oldbranchname newbranchname
远程分支重命名 (假设本地分支和远程对应分支名称相同)
a. 重命名远程分支对应的本地分支
git branch -m old-local-branch-name new-local-branch-name
b. 删除远程分支
git push origin :old-local-branch-name
c. 上传新命名的本地分支
git push origin new-local-branch-name: new-local-branch-name
查看分支合并状况
git log --graph --pretty=oneline 提交的完整编号
操做的简易编号显示模式
git log --graph --pretty=oneline --abbrev-commit
临时要修改Bug时,为保证当前工做区看上去是干净的
把当前未完成工做现场储藏起来
git stash
查看储藏的工做区
git stash list
stash@{0}: WIP on b1: 38b2a71 modify p.php
恢复储藏的工做区
git stash apply stash@{0} (恢复后stash里的内容并无删除) 须要用 git stash drop 来删除
git stash pop (恢复储藏的工做区,并删除stash里的内容)
6.列出对应的远程库
git remote -v
origin https://github.com/hkui/test.git (fetch)
origin https://github.com/hkui/test.git (push)
7.将本地分支 推送到远程
在本地新建分支 如(b1) 推送到远程( 远程没有就新建)
git push origin b1(本地):b1(远程)
设置本地与远程之间分支间的通道后就无需 每次都把2边的分支名都写全了
hkui2015@hkui MINGW32 ~/Desktop/osc (b1)
$ git push
fatal: The current branch b1 has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin b1
设置当前本地分支与远程 分支的关联
git push --set-upstream origin b1(远程分支名存在或者不存在)
删除远程分支
git push origin --delete b1(远程分支)
删除本地分支(所有发布到远程仓库或者合并到主分支后才能被删除)
git branch -d branchname
git branch -D branchname (没合并过的分支强力删除)
标签操做
git tag
git tag 查看全部标签
$ git log --pretty=oneline --abbrev-commit c097905 add dev1.txt dev11.txt on branch dev1 a651ed5 Merge branch 'b2' 0f7c6c7 Merge branch 'b1' 99171bd 修改忽略配置文件 d379649 添加自定义忽略文件 c568160 删除了b1.txt 1862163 modify 9833ae7 add a line on b2 branch 0e07e62 modify b1.txt 1a0cfa7 modify b1 24549b6 modify files 9181725 modify files f46e95b add a line on b2.txt 51ef312 add b2.txt on branch b1 55cedea add a line on b1.txt 38fb557 add b1.txt on b1 branch 058c2f7 add a.php cec4d7c Update README.md cfbb500 Update README.md 04ec1f4 Initial commit
对某一次提交打标签 好比 对 "修改忽略配置文件" 此次commit打标签
git tag p1.0 99171bd
标签不是按时间顺序列出,而是按字母排序的
可用git show
$ git show p1.0 commit 99171bde2eddbf67b8860024504bc14556de2978 Author: hkui <764432054@qq.com> Date: Fri Dec 16 15:41:34 2016 +0800 修改忽略配置文件 diff --git a/.gitignore b/.gitignore index e69de29..ef752b0 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +conf.php \ No newline at end of file
View Code
建立带标签说明的tag
$ git tag -a
还能够经过-s用私钥签名一个标签
$ git tag -s tagname -m "signed version 0.2 released" fec145a
删除标签
$ git tag -d tagname
推送某个标签到远程
git push origin
推送全部还没推送的标签到远程
$ git push origin --tags
若是标签已经推送到远程,要删除远程标签就麻烦一点,先从本地删除
$ git tag -d p1.0 Deleted tag 'p1.0' (was 99171bd) 而后,从远程删除。删除命令也是push,可是格式以下: $ git push origin :refs/tags/p1.0 To https://git.oschina.net/hk/code.git - [deleted] p1.0
问题
fatal: the remote end hung up unexpectedly
发生在push命令中,有多是push的文件过大致使
解决方法:
windows:
在 .git/config 文件中加入
[http] postBuffer = 524288000linux: git config http.postBuffer 524288000