git 高階用法

平常生活中的git

一般狀況下,在進行git操做的時候,你們最熟悉的流程:css

可能還有的同窗也常常git commit --amend -C head,向上一個commit提交內容。
然而在平常生活中總會有一些其餘對代碼版本管理的需求,好比須要去修改好久以前的一個commit的內容等。html

本文主要記錄一些git相對使用較少,可是很是有用的命令。linux

git rebase

git rebase 有兩個經常使用功能:git

  • 從上游分支獲取最新commit信息,並有機的將當前分支和上游分支進行合併。shell

  • 對當前分支的歷史commit進行修改,合併,刪除等操做windows

場景

  • 你的分支和master衝突app

若是你git merge master就會留下一個merge的commit。在這種狀況下,咱們推薦使用git rebase master,就能不留commit將當前分支和master有機的合併。ssh

  • 以前某次提交,改錯了一個變量spa

若是你修改那個變量而後再提交一個commit,這顯然不是最優的。若是能將某次的提交挑出來修改就行了。固然git提供了這個功能,你可使用git rebase -i [git-hash| head~n],其中git-hash是你要開始進行rebase的commit的hash,而head~n則是從HEAD向前推n個commit.code

當你執行git rebase -i 你會看到以下界面,須要你去選擇對應的commit指定一種操做。下面提示了不少操做項。

git stash

git stash 主要功能是:

  • 暫存當前沒有提交的更改

git stash save 保存當前更改
git stash 保存當前更改
git stash pop 推出以前stash的內容更改
git stash apply 推出以前stash的內容更改

git stash save --keep-index 只stash沒有被add的內容
git stash save --include-untracked stash還未加入git記錄的文件

git stash list --stat 顯示stash堆棧
git stash show stash@{0} 顯示第x次的更改
git stash show 顯示最近一次stash的更改
git stash show --patch 顯示最近一次stash的詳細更改

git stash save "stash msg"

git stash branch new_brach stash@{0}

場景

  • 當作了更改以後可是沒有編寫完,這個時候發現線上有個bug,你須要停下當前分支去修bug,這個時候你能夠提一個commit到當前分支,或則使用git stash

stash屢次是以堆棧的形式進行存儲的。

git filter-branch

git filter-branch的主要功能是

  • 過濾全部提交記錄,進行相應的操做

git filter-branch --tree-filter 'rm -rf .vscode' 刪除全部分支的.vscode文件夾
git filter-branch --tree-filter 'rm -f xxx' xxx不存在,不報錯
git filter-branch --tree-filter 'rm -rf .vscoe' -- --all(全部分支全部提交)

git filter-branch --index-filter 'git rm --cached --ignore-unmatch password.txt' (只檢查password.txt 一個文件)
git filter-branch -f --prune-empty -- --all

場景

  • 你發現你錯誤的將一個不該該提交的文件提交,而後已通過了好久,已經累計了無數次提交。

這個時候就可使用git filter-branch將全部提交所有過濾一遍刪除掉那個不該該提交的文件

cherry-pick

git cherry-pick的主要功能是

  • 從任何分支,抽取提交到當前分支

git cherry-pick git-hash 

git cherry-pick --edit git-hash

pick 多個
git cherry-pick --no-commit git-hash git-hash

git cherry-pick -x git-hash (添加cherry pick from 那個branch)
git cherry-pick --signoff git-hash不修改author

場景

當須要將多個分支上的提交合在一個分支合併到master的時候,顯然若是這3個分支有關聯,一次合併更爲合理。
這個時候就可使用git cherry-pick進行精細化的commit的操做。

work together

git config --global core.autocrlf
linux osx是lf
windows 是crlf

若是你的源文件中是換行符是LF,而autocrlf=true, 此時Git add就會遇到 fatal: LF would be replaced by CRLF 的錯誤。有兩個解決辦法:

  1. 將你的源文件中的LF轉爲CRLF便可【推薦】

  2. 將autocrlf 設置爲 false

若是你的源文件中是換行符是CRLF,而autocrlf=input, 此時git add也會遇到 fatal: CRLF would be replaced by LF 的錯誤。有兩個解決辦法:

  1. 將你源文件中的CRLF轉爲LF【推薦】

  2. 將autocrlf 設置爲true 或者 false

.gitattributes

* text = auto 
*.html text
*.css text

*.jpg binary
*.png binary

*.ssh text eol=lf
*.bat text eol=crlf

一些可能會用到的

git submodule

git submodule add git@example.com:css.gi

git reflog

git log --walk-reflogs

原文連接:http://hiluluke.cn/2017/07/23...

相關文章
相關標籤/搜索