Git命令

挺全的參考 git學(xué)習(xí)筆記

Your branch is ahead of 'origin/master' by 1 commit.
表明你的本地庫,比遠(yuǎn)程倉多了一個(gè)提交(產(chǎn)生了新的commit)


比較分支信息的話:
git log yourBranchName
或者本地和遠(yuǎn)程端的:
git fetch origin
git diff master origin/master 比較本地和遠(yuǎn)程主分支的區(qū)別
git status是查看你本地倉的信息的


工作區(qū)和版本庫
合并遠(yuǎn)程分枝

git add 命令添加所有改動(dòng)內(nèi)容
git add xx
命令可以將xx文件添加到暫存區(qū),如果有很多改動(dòng)可以通過git add -A .來一次添加所有改變的文件。注意-A選項(xiàng)后面還有一個(gè)句點(diǎn).
git add -A
表示添加所有內(nèi)容,
git add .
表示添加新文件和編輯過的文件不包括刪除的文件;
git add -u
表示添加編輯或者刪除的文件,不包括新添加的文件。

192:FarmHouse_IOS Lemon$ git add git add -A.
error: unknown switch `.'
usage: git add [<options>] [--] <pathspec>...

-n, --dry-run         dry run
-v, --verbose         be verbose

-i, --interactive     interactive picking
-p, --patch           select hunks interactively
-e, --edit            edit current diff and apply
-f, --force           allow adding otherwise ignored files
-u, --update          update tracked files
-N, --intent-to-add   record only the fact that the path will be added later
-A, --all             add changes from all tracked and untracked files
--ignore-removal      ignore paths removed in the working tree (same as --no-all)
--refresh             don't add, only refresh the index
--ignore-errors       just skip files which cannot be added because of errors
--ignore-missing      check if - even missing - files are ignored in dry run

--refresh             don't add, only refresh the index
--ignore-errors       just skip files which cannot be added because of errors
--ignore-missing      check if - even missing - files are ignored in dry run

git push

下面local_branch_name用來指明要推送本地哪個(gè)分支,remote_branch_name用來指明遠(yuǎn)端接收這個(gè)分支后叫什么。
git push origin [local_branch_name]:<remote_branch_name>

  • 嚴(yán)格按照此格式寫命令,否則可能會(huì)出現(xiàn)其他的狀況

git checkout --file 同 git reset HEAD --file
首先需要知道工作區(qū)(working diretory)和暫存區(qū)(Stage)這兩個(gè)概念。工作區(qū)的該概念不僅包含你實(shí)際操作、更改的文件還應(yīng)當(dāng)包括當(dāng)前修改但未add存入暫存區(qū)的文件變化信息,暫存區(qū)的作用則是臨時(shí)存儲(chǔ)文件的變化信息,在git add file操作之后,暫存區(qū)中將記錄file文件上的修改信息。暫存區(qū)的存在更細(xì)化了時(shí)間節(jié)點(diǎn),要知道commit的往往是有重大改變的版本或者是在一次修改工作整體完成之后才使用commit。而在這之間需要保存的修改,自然需要一個(gè)緩存區(qū)暫時(shí)存放。廖雪峰的git教程中在“撤銷修改”部分中,提到了幾種情景和相應(yīng)的git checkout與git reset命令。通過我的分析,在這個(gè)更直接地分析這兩個(gè)命令的含義。

  • git checkout -- file;撤銷對工作區(qū)修改;這個(gè)命令是以最新的存儲(chǔ)時(shí)間節(jié)點(diǎn)(add和commit)為參照,覆蓋工作區(qū)對應(yīng)文件file;這個(gè)命令改變的是工作區(qū)
  • git reset HEAD -- file;清空add命令向暫存區(qū)提交的關(guān)于file文件的修改(Ustage);這個(gè)命令僅改變暫存區(qū),并不改變工作區(qū),這意味著在無任何其他操作的情況下,該命令運(yùn)行之后工作區(qū)中的實(shí)際文件同之前無任何變化

創(chuàng)建配置 .gitignore
每次項(xiàng)目提交都會(huì)有每隔幾秒xcuserstate的改變,而這個(gè)文件是不需要提交的 ,所以需要添加.gitignore對這個(gè)文件進(jìn)行忽略
在當(dāng)前目錄下 git mkdir .gitignore
Vim & Vi 編輯使用

# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control
#
# Pods/

保存之后,你會(huì)發(fā)現(xiàn)這個(gè)文件并未生效

原因是.ignore是后來加進(jìn)來的 local cache里面記錄的這個(gè)文件是不忽略的 所以需要進(jìn)行緩存清除
git rm -r -cache 命令
從版本庫中刪除文件,但不刪除文件

$ git rm --cached iLedger.xcodeproj/project.xcworkspace/xcuserdata/mac.xcuserdatad/UserInterfaceState.xcuserstate
$ git commit -m "Removed the stupid strange file that shouldn't be tracked"
$ git push

不跟蹤設(shè)置成功后 git status



Untracked files 每次都顯示,看著好別扭啊,但是沒有找到處理的辦法,心塞?。。?/p>

另外一點(diǎn)很奇怪的是 .gitignore 文件建錯(cuò)了地方,無論是 rm .gitignore,rmdir .gitignore都刪除不了。最后根據(jù)提示先把 .gitignore清空再執(zhí)行rmdir .gitignore才算搞定,世界終于清靜了

gitHub clone 到本地指定文件夾

#測試連接 github 
ssh -T git@github.com

#執(zhí)行克隆 git clone xxx.git "指定目錄"
git clone git@github.com:LemonChao/NoteBook.git "/Users/zhengchao/Documents/gitRepo"

git add

  • git add -A stages All
  • git add . stages new and modified, without deleted
  • git add -u stages modified and deleted, without new

撤銷git add(綠字變紅字)

git reset HEAD .  //撤銷所有的已經(jīng)add的文件:
git reset HEAD filename  //撤銷某個(gè)文件或文件夾:
git checkout -- 文件  //紅字變無 (撤銷沒add修改,慎用)

同步遠(yuǎn)程分支
1.git clone
2.git pull origin branchname
git fetch 遠(yuǎn)程分支
打算把github上的一個(gè)分支取到本地,直接輸入命令:
Git checkout origin/A
看到http://stackoverflow.com/questions/5989592/git-cannot-checkout-branch-error-pathspec-did-not-match-any-files-kn,

有了如下方法:

先執(zhí)行g(shù)it fetch, 從github上取回新的信息放到本地倉庫中,比如哦我要取的那個(gè)分支在本地并沒有這個(gè)信息,執(zhí)行g(shù)it branch -a 發(fā)現(xiàn)并沒有那個(gè)分支,執(zhí)行g(shù)it fetch后就有了,

然后執(zhí)行
git checkout -b A origin/A

就把遠(yuǎn)程的A取回,并切換到本地的A分支。
不過,貌似下面這條命令就可以完成取服務(wù)器上的信息,并且創(chuàng)建分支A,把服務(wù)器上的分支A取回的操作:

git checkout -t -b A origin/A

`git fetch origin dev` 從服務(wù)器更新dev分支

比較本地分支版本和遠(yuǎn)程版本的不同之處
假定遠(yuǎn)端庫名為 origin(如果沒有手動(dòng)修改的話默認(rèn)就是origin), 你要比較的本地分支為 test, 遠(yuǎn)端分支為 xxx

# 獲取遠(yuǎn)端庫最新信息
$ git fetch origin
 
# 做diff
$ git diff test origin/xxx

CocoaPods 升級(jí)后 push失敗解決
場景:升級(jí)完CocoaPods后第一次push時(shí)讓輸入github賬號(hào)密碼(異?,F(xiàn)象的開始),之后
fatal: unable to access 'https://github.com/CocoaPods/Specs.git/':The requested URL returned error: 403
一看git的遠(yuǎn)程地址被pod弄混了,

$ git remote -v
# 結(jié)果 小樣果不其然
origin  https://github.com/CocoaPods/Specs.git (fetch)
origin  https://github.com/CocoaPods/Specs.git (push)

那就換回來唄

$ git remote set-url origin xxxx(#你的庫的地址)

@結(jié)束

Git diff ^M的消除
這是由于換行符在不同的操作系統(tǒng)上定義的區(qū)別造成的。

Windows用CR LF來定義換行,Linux用LF。CR全稱是Carriage Return ,或者表示為\r, 意思是回車。 LF全稱是Line Feed,它才是真正意義上的換行表示符。為什么Windows添加一個(gè)CR和LF組合表示,我并不清楚。不過如果用git diff的時(shí)候看到^M字符,就說明兩個(gè)文件在換行符上有所差別。

比如從我的Windows開發(fā)的同時(shí)那邊拿來一個(gè)目錄,就會(huì)發(fā)現(xiàn)幾乎所有的文件都被修改過了。其實(shí)并不是這樣,都是由于文件多了CR后造成的。

下面簡單的方法可以讓git diff的時(shí)候忽略換行符的差異:

git config --global core.whitespace cr-at-eol

更好的方法是每個(gè)項(xiàng)目都有一個(gè).gitattributes文件,里面配好了換行符的設(shè)置,參考
https://help.github.com/articles/dealing-with-line-endings

Git diff
直接使用Git diff 可以產(chǎn)看當(dāng)前沒有add 的內(nèi)容修改
查看已經(jīng)add 沒有commit 的改動(dòng) 使用 git diff --cached
git diff HEAD 是上面兩條的合并
git diff 版本號(hào)碼1 版本號(hào)碼2 src : 比較兩個(gè)版本號(hào)碼的src 文件夾的差異

回到git的某個(gè)歷史版本,修改然后commit、push到服務(wù)器上成為最新的版本

需求:只退回到某個(gè)歷史版本修改然后commit、push到服務(wù)器上成為最新的版本。

已經(jīng)push到遠(yuǎn)程的提交是不能修改的。也就是說不能單獨(dú)抽出某個(gè)提交進(jìn)行修改。若一定要修改的話需要git push -f。但是這樣做會(huì)讓你丟失回退點(diǎn)之后的歷史。從你的需求來看,我這有個(gè)折中的方法:

  • 1 先獲得服務(wù)器上最新的代碼并在本地建立對應(yīng)的分支:git clone ... ,git checkout -b <branch> <remote>/<branch>
  • 2 把要返回歷史提交以及之后所有提交 commit ID順序的記錄下來。
  • 3 把該分支返回到歷史上某個(gè)點(diǎn): git reset SHA --hard
  • 4 進(jìn)行文件修改并且對返回點(diǎn)的提交進(jìn)行修改: Modify , git add , git commit --amend
  • 5 用之前記錄下來的commit ID反向cherry pick,目標(biāo)提交之后有多少提交都要拿過來: git cherry-pick SHA
  • 6 對當(dāng)前分支進(jìn)行push,替換到服務(wù)器上的遠(yuǎn)程分支(需要強(qiáng)制push): git push -f <branch>:<branch>

git無法pull倉庫refusing to merge unrelated histories
我在Github新建一個(gè)倉庫,寫了License,然后把本地一個(gè)寫了很久倉庫上傳。
先pull,因?yàn)閮蓚€(gè)倉庫不同,發(fā)現(xiàn)refusing to merge unrelated histories,無法pull
因?yàn)樗麄兪莾蓚€(gè)不同的項(xiàng)目,要把兩個(gè)不同的項(xiàng)目合并,git需要添加一句代碼,在git pull,這句代碼是在git 2.9.2版本發(fā)生的,最新的版本需要添加--allow-unrelated-histories
假如我們的源是origin,分支是master,那么我們 需要這樣寫git pull origin master --allow-unrelated-histories需要知道,我們的源可以是本地的路徑

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容