git經常使用命令小結

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
相關文章
相關標籤/搜索