在此記錄Github中git的基礎指令,如:怎麼將fork項目和源倉庫同步;怎麼刪除分支;拋棄本地修改;上傳本地修改;克隆github項目分支 等。
最近一直在用github,提交文件之類的操作常常需要用到git。所以在此記錄。
這個最簡單:git clone <github-project-url>
<github-project-url>
就是項目所在的github網址,如下圖,則克隆命令為:git clone https://github.com/SpoonLabs/nopol.git
fork項目的話,我沒用git指令,而是直接在github中你想要fork到自己倉庫的項目主頁,點擊fork按鈕即可,如下:
克隆分支使用命令:git clone -b <branch-name> <github-project-url>
如:git clone -b march2017 https://github.com/SpoonLabs/nopol.git
這個比較難。
先描述一下場景 :github上有一個項目Nopol
( https://github.com/SpoonLabs/nopol.git ),我將Nopol這個項目fork到了我的倉庫中( https://github.com/DehengYang/nopol ),然後使用git clone https://github.com/DehengYang/nopol.git
下載到了本地,原本Nopol總共有4個分支,現在Nopol作者給Nopol添加了一個march2017
分支,我想把這個march2017
分支添加到我fork過來的Nopol中,這時候需要怎麼操作呢?
在本地中打開終端(命令行):
git branch -a # 查看我本地的fork項目Nopol的分支;發現沒有march2017git remote -v # 查看這個項目的遠程目錄;發現只有我的github fork項目地址,即 https://github.com/DehengYang/nopol.git ,但是沒有原項目的地址(https://github.com/SpoonLabs/nopol.git)git remote add upstream https://github.com/SpoonLabs/nopol.git # 添加github原項目的地址git remote -v # 再次查看本項目的遠程目錄,發現已經包含原項目地址git branch -a # 這時候再次查看項目分支,發現有remote:march2017 這個分支存在,但是不在本地git fetch upstream march2017 # 從upstream,即Nopol原倉庫地址中拉取march2017分支git checkout march2017 #切換到march2017分支git branch -a #確認是否切換到這個分支
後面幾步我記得不是很清楚了,大概應該就是這樣的(有待實踐中進一步確認)。
參考 [1],[2]。
場景:在本地不小心使用 git branch -b march2017
誤創建了一個新的march2017
分支。現在需要刪除分支,應該如何呢?
如下:
git branch -d march2017
參考 [5] 。
git status #查看哪些文件被修改了git add <file-name> # 把對應的文件添加到緩存區。注意:git add * 表示緩存所有有變化的文件git commit -m "Write your desciption here" # 添加commit描述git push origin master # 如果要上傳分支代碼的話,那麼先要git checkout <branch-name>,然後git push origin <branch-name>
參考 [6] 。
場景:我在本地項目中修改了幾個文件,但是我現在不想要了,想刪除修改,並在本地和github倉庫中的代碼同步。
在終端輸入:
git status # 查看自己修改了什麼文件git checkout . # 如果自己的修改還沒有用git add 緩存,則使用這個來放棄這些尚未緩存的修改git pull origin master # 我沒記錯的話應該是這個指令,就是把github倉庫的項目同步到本地,如果本地沒有修改,會顯示already-up-to-date這類信息
參考 [3] 和 [4]。
[1] Github進行fork後如何與原倉庫同步 https://blog.csdn.net/matrix_google/article/details/80676034
[2] git分支查看及切換 https://blog.csdn.net/qq_26710805/article/details/80674006
[3] git 放棄本地修改 https://www.cnblogs.com/qufanblog/p/7606105.html
[4] git 拉取遠程分支到本地 https://blog.csdn.net/carfge/article/details/79691360
[5] git刪除本地分支 https://blog.csdn.net/github_27263697/article/details/79373997
[6] git上傳本地分支到github項目分支 https://blog.csdn.net/qq_27437967/article/details/71189571
此外,還有如下網址也給了一些指導,故在此記錄:
git常用命令以及如何與fork別人的倉庫保持同步 https://www.cnblogs.com/-walker/p/7278951.html
git fetch 更新遠程代碼到本地倉庫 https://www.cnblogs.com/chenlogin/p/6592228.html
創建時間:2019年02月16日 21:48:12 筆記:Github中git的基礎指令
修改時間:2019年3月21日16:04:55 [github] 克隆、刪除分支,覆蓋本地倉庫等操作
場景: 我去年上傳了一個github項目,但是當時還是小白,直接從本地各個地方拖拽文件夾上傳,,,所以本地的文件不完整。
這次想用git clone
命令下載遠程倉庫代碼,發現太大了。
但是呢,我本地的又不完整,所以我先參考的 [7],執行的是:git fetch origin master
,有一點用:
$ git fetch origin master
remote: Enumerating objects: 64, done.
remote: Counting objects: 100% (64/64), done.
remote: Total 2508 (delta 64), reused 64 (delta 64), pack-reused 2444
Receiving objects: 100% (2508/2508), 139.08 MiB | 162.00 KiB/s, done.
Resolving deltas: 100% (614/614), completed with 11 local objects.
From https://github.com/DehengYang/sfa-rfa
* branch master -> FETCH_HEAD
37a4543…4542980 master -> origin/master
可以看到reuse 64,我感覺是本地已有的文件沒有重複下載。
但是呢,有一個不完整的文件夾沒有覆蓋掉,我現在得強行覆蓋。
方案:
1)輸入:git fetch --all && git reset --hard origin/master && git pull
(也可以分開輸入,這樣心裡有底。) [8]
2)理論上這樣就可以了,可以用git status
看一下當前的狀況,如果還不行就輸入:git pull origin master
,感覺這個命令非常過硬,強制執行。
$ git pull origin master
From https://github.com/DehengYang/sfa-rfa
* branch master -> FETCH_HEAD
Already up-to-date.
此外,在這樣的場景下,我一開始輸入git status
的時候顯示很多 untracked files
,參考[9],主要是兩個命令:git add <these-untracked-files>
可以添加這些沒有被追蹤的文件,使用git rm <-r> --cached <file-or-dir>
就可以移除。
正如上文所說,Git在未進行commit操作之前,存在三種狀態:Untracked files,Changes not staged for commit及Changes to be committed,每種狀態之間可以隨意進行互相轉換。了解這三種狀態各自所對應的不同情況,能夠幫助你方便有效的使用Git來管理項目。
場景: 我的一個圖片名字叫A and B.png
我想在readme裡面展示這張圖,我寫的是:[A and B](A and B.png)
(大概這樣吧,好像也要加上圖片對應的網頁地址)
但是顯示不出這張圖片。
解決:
參考[10],改成:[A and B](A%20and%20B.png)
即可。
這個問題我還沒研究透,不想多寫了。大概列出文獻。
Is there a way to edit a commit message on GitHub? https://superuser.com/questions/751699/is-there-a-way-to-edit-a-commit-message-on-github
git 查看某個文件下的某個commit的修改記錄 https://blog.csdn.net/m_review/article/details/79315556
如何修改Git commit的信息 https://www.cnblogs.com/shenh062326/p/git.html
場景:在本地倉庫中,我刪除了一個文件夾,然後git status就會發現:有很多deleted files。但是這種刪除怎麼同步到github倉庫(origin master)呢?
解決方案:
git add -ugit commit -m "<your comments>"git push origin master
參考:
How to remove multiple deleted files in Git repository https://stackoverflow.com/questions/6004453/how-to-remove-multiple-deleted-files-in-git-repository
git add -A 和 git add . 的區別 https://www.cnblogs.com/skura23/p/5859243.html
git fetch origin mastergit log -p master.. origin/mastergit merge origin/master
參考:git fetch 更新遠程代碼到本地倉庫 http://www.cnblogs.com/chenlogin/p/6592228.html
[7] git fetch 更新遠程代碼到本地倉庫 https://www.cnblogs.com/chenlogin/p/6592228.html
[8] git強制覆蓋本地代碼和強制推送本地到遠程倉庫 https://blog.csdn.net/sheep8521/article/details/81383865
關於pull、fetch還參考了:
git pull命令 https://www.yiibai.com/git/git_pull.html
詳解git pull和git fetch的區別: https://blog.csdn.net/weixin_41975655/article/details/82887273
[9] Git中三種文件狀態及其轉換 https://phplaber.iteye.com/blog/1699926
[10] URLs with spaces do not render a links in markdown cells https://github.com/nteract/nteract/issues/914
在運行:git push origin master 的時候,出錯:
fatal: unable to access 『https://github.com/DehengYang/FL-APR-research.git/』: Failed to connect to github.com port 443: Timed out
參考:
+ git clone fatal: unable to access 『https://github.com/carlon/demo.git/』: Failed to connect to github.com port 443: Timed out https://www.cnblogs.com/wy1935/p/7210114.html
先後在命令行(我的是windows git bash)中輸入:
git config --global http.proxygit config --global --unset http.proxy
當然,也可能是網絡不好,多試幾次就行。
GihubMarkdown中的複選框按鈕的實現 https://blog.csdn.net/Erice_s/article/details/80202536
How to add color to Github’s README.md file https://stackoverflow.com/questions/11509830/how-to-add-color-to-githubs-readme-md-file
如何向Github README.md中添加圖片 https://blog.csdn.net/qq_33207292/article/details/80068154