俗話說:世上本沒有坑,踩得人多了静陈,也就有了坑......
接觸Git已經(jīng)有一段時間了燕雁,也寫了一些學(xué)習(xí)心得,這是第四篇關(guān)于Git的文章鲸拥,記錄一下在實踐中踩過的坑拐格。
前三篇博文傳送門:
Git+GitHub,構(gòu)建自己的開源倉庫之Git分支
Git+GitHub刑赶,構(gòu)建自己的開源倉庫之Git命令
Git+GitHub捏浊,構(gòu)建自己的開源倉庫之初識Git
希望我在學(xué)習(xí)中遇到的一些問題,同樣能夠幫助正在學(xué)習(xí)Git的你撞叨。
下面是本文要介紹的Git操作(基于Git Bash命令行的操作):
- 如何創(chuàng)建遠(yuǎn)程分支
- 如何刪除遠(yuǎn)程分支
- 如何克隆遠(yuǎn)程分支
- 如何添加金踪、移除忽略文件
- 添加浊洞、移除忽略文件不生效怎么回事
- 刪除的文件怎么恢復(fù)
如何創(chuàng)建遠(yuǎn)程分支
創(chuàng)建遠(yuǎn)程分支其實很簡單,首先要在本地創(chuàng)建分支热康,然后把這個分支push到遠(yuǎn)程倉庫沛申。
git branch -b develop_test
git push origin develop_test
這樣,遠(yuǎn)程倉庫就可以看見develop_test這個分支了姐军。
如何刪除遠(yuǎn)程分支
通過前面三篇文章的學(xué)習(xí)铁材,相信都知道怎么刪除本地分支了,這里回顧一下:
git branch -d <name>
git branch -D <name>--強(qiáng)制刪除
那么奕锌,對于遠(yuǎn)程倉庫的分支著觉,比如我的GitHub上的一個倉庫,不小心push了一個沒有什么意義的分支上去惊暴,怎么刪除呢饼丘?
git push origin :branch-name
注意冒號前面的空格,意思是push一個空的分支到你要刪除的那個分支上辽话,相當(dāng)于刪除這個分支:
$ git push origin :develop2
To git@github.com:chengshengyang/Login-MVP-Architecture.git
- [deleted] develop2
這對于刪除tag同樣有效肄鸽,把要刪除的分支名字換成對應(yīng)的tag名字即可,在Git v1.7.0 之后油啤,還可以使用這種語法刪除遠(yuǎn)程分支:
git push origin --delete <name>
我們看看輸出和上面的指令一個效果:
$ git push origin --delete develop3
To git@github.com:chengshengyang/Login-MVP-Architecture.git
- [deleted] develop3
然后我GitHub上的develop2和develop3都被刪掉了典徘。
如何克隆遠(yuǎn)程分支
場景是我在A電腦上創(chuàng)建了一個分支develop志笼,現(xiàn)在我回到家里袋马,在自己的B電腦上要把這個develop分支clone下來,在本地做開發(fā)吟温,這樣幽告,我上班有空了可以在公司電腦上倒騰自己的代碼梅鹦,下班前提交到develop,回家把在公司提交的代碼pull下來冗锁,就不用電腦整天背來背去了齐唆。so easy!6澈印蝶念!
git clone默認(rèn)會把遠(yuǎn)程倉庫整個clone下來,但只會在本地默認(rèn)創(chuàng)建一個master分支芋绸。想要clone其他分支媒殉,可以使用checkout命令來把遠(yuǎn)程分支取到本地,并自動建立tracking:
git checkout -b develop origin/develop
嘗試輸入上面命令摔敛,把develop克隆到本地廷蓉,但是失敗了:
$ git checkout -t origin/develop
fatal: Cannot update paths and switch to branch 'develop' at the same time.
Did you intend to checkout 'origin/develop' which can not be resolved as commit?
用git remote show origin命令查看一下遠(yuǎn)程倉庫的狀態(tài):
$ git remote show origin
* remote origin
Fetch URL: git@github.com:chengshengyang/Login-MVP-Architecture.git
Push URL: git@github.com:chengshengyang/Login-MVP-Architecture.git
HEAD branch: master
Remote branches:
develop new (next fetch will store in remotes/origin)
master tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (local out of date)
可以看到develop分支的狀態(tài)是new,而不是跟master分支一樣:tracked。這就是導(dǎo)致問題的原因桃犬。也就是說develop分支還沒有被追蹤刹悴。執(zhí)行git remote update命令:
$ git remote update
Fetching origin
remote: Counting objects: 81, done.
remote: Compressing objects: 100% (28/28), done.
remote: Total 81 (delta 21), reused 12 (delta 12), pack-reused 37
Unpacking objects: 100% (81/81), done.
From github.com:chengshengyang/Login-MVP-Architecture
db7e7b3..82618ec master -> origin/master
* [new branch] develop -> origin/develop
再調(diào)用git remote show origin命令查看
$ git remote show origin
* remote origin
Fetch URL: git@github.com:chengshengyang/Login-MVP-Architecture.git
Push URL: git@github.com:chengshengyang/Login-MVP-Architecture.git
HEAD branch: master
Remote branches:
develop tracked
master tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (local out of date)
這時候develop分支也變成了tracked了,接著執(zhí)行git fetch命令攒暇,無任何輸出(成功)土匀。問題解決。再次執(zhí)行克隆遠(yuǎn)程分支命令
$ git checkout -b develop origin/develop
error: Your local changes to the following files would be overwritten by checkout:
.idea/misc.xml
Please commit your changes or stash them before you can switch branches.
Aborting
提示本地分支有未提交的內(nèi)容形用,先提交就轧,保持工作空間的clean。
chengshengyang@csy-pc MINGW64 ~/AndroidStudioProjects/Login-MVP-Architecture (master)
$ git checkout -b develop origin/develop
Branch develop set up to track remote branch develop from origin.
Switched to a new branch 'develop'
chengshengyang@csy-pc MINGW64 ~/AndroidStudioProjects/Login-MVP-Architecture (develop)
ok田度,終于成功啦妒御,現(xiàn)在我在本地克隆了遠(yuǎn)程倉庫的develop分支,并且切換到develop分支了镇饺。
$ git branch
* develop
master
上述問題的解決方案參考:
如何添加乎莉、移除忽略文件
添加刪除忽略文件都是對.gitignore進(jìn)行編輯,對于ignore的語法奸笤,可以通過Android Studio的“.ignore”插件來學(xué)習(xí)惋啃,這個插件還挺好用的,會教你怎么來寫忽略文件监右,而且會把忽略文件灰色標(biāo)記边灭,一目了然。
忽略文件配置示例:
# 用'#'開始注釋一行.
# 忽略掉所有文件名是 foo.txt 的文件.
foo.txt
# 忽略所有生成的 html 文件,
*.html
# foo.html是個特例秸侣,不添加忽略.
!foo.html
# 忽略所有.o和 .a文件存筏,出了special.o文件.
*.[oa]
!special.o
# 只忽略當(dāng)前目錄下的folder文件和目錄宠互,子目錄的folder不在忽略范圍內(nèi)
/folder
# 只忽略test文件味榛,不忽略test目錄(比如當(dāng)前目錄下有test文件夾、test.jpg予跌、test.png等)
test
!test/
# 只忽略test目錄搏色,不忽略test文件
test/
# 忽略test文件和test目錄
test
添加、移除忽略文件不生效怎么回事
辛辛苦苦編輯完忽略規(guī)則券册,卻發(fā)現(xiàn)設(shè)置的規(guī)則完全沒起作用频轿,或者有的起作用了,有的沒起作用烁焙,怎么回事航邢?原因是.gitignore只能忽略那些原來沒有被track的文件,如果某些文件已經(jīng)被納入了版本管理中骄蝇,則修改.gitignore是無效的膳殷。解決方法就是先把要忽略文件的本地緩存刪除(改成未track狀態(tài)),然后再修改提交.gitignore文件:
git rm -r --cached .idea/modules.xml
git add .gitignore
git commit -m 'update .gitignore'
關(guān)于忽略可以參考:https://github.com/github/gitignore 來設(shè)置忽略規(guī)則九火。
刪除的文件怎么恢復(fù)
最怕的就是文件誤刪赚窃,嚇尿了册招,怎么辦?不要怕勒极,在Git里是掰,永遠(yuǎn)有后悔藥可以吃。
-
1.遠(yuǎn)程倉庫沒刪除辱匿,本地執(zhí)行了add 和 commit键痛,這種情況下,恢復(fù)刪除文件很簡單:
git checkout -- test.txt
文件就恢復(fù)到倉庫的最新版本了掀鹅,可能會丟失上次push后的一些修改散休。
-
2.遠(yuǎn)程倉庫都沒啦,本地刪除后push到遠(yuǎn)程了乐尊。這種情況戚丸,可以從本地的版本恢復(fù),先用git log 查看提交的記錄扔嵌,找到被刪除之前的一個commit-id限府,拷貝,
git checkout commit_id -- path_to_file
把文件恢復(fù)到版本commit-id痢缎。
-
3.如果要查看刪除的文件:
git ls-files --deleted
要恢復(fù)則需要執(zhí)行checkout:
git checkout – <deleted_file>
多個文件同時操作可以使用xargs:
git ls-fies -d | xargs git checkout --
總結(jié)
Git的內(nèi)容還是很多的胁勺,用法也多種多樣,很多操作可能不止一種寫法独旷,各個版本也有所不同署穗,學(xué)習(xí)起來很容易入坑,遇到問題的解決方法也不是唯一的嵌洼,關(guān)鍵是每個命令的實現(xiàn)原理案疲,學(xué)習(xí)Git不能只關(guān)注各種命令,更重要的其實是原理麻养,基友們學(xué)習(xí)的時候要抓住重點褐啡,注重基礎(chǔ)原理的理解。