github相關(guān)概念
這里要區(qū)分git和github
git操作是屬于左半部分,github屬于上面部分
git是自己本地倉(cāng)庫(kù)和自己遠(yuǎn)程倉(cāng)庫(kù)的操作
github是自己倉(cāng)庫(kù)和別人倉(cāng)庫(kù)(fork和被fork倉(cāng)庫(kù))之間操作
https://github.com/812865052/learngit.git
是倉(cāng)庫(kù)的https地址
git@github.com:812865052/learngit.git
是倉(cāng)庫(kù)的ssh地址
Joe和其它貢獻(xiàn)者已經(jīng)對(duì)這個(gè)項(xiàng)目做了一些修改,而你將在他們的修改的基礎(chǔ)上,還要再做一些修改。在你開(kāi)始之前,你最好"同步你的fork",以確保在最新的復(fù)制版本里工作。下面是你要做的:
git安裝
Linux:sudo apt-get install git
其他系統(tǒng)安裝
本地git配置
首先要在某個(gè)文件夾(空的或者已經(jīng)有文件)內(nèi)執(zhí)行:
git init
創(chuàng)建了一個(gè)git倉(cāng)庫(kù)
然后設(shè)置git屬性
git config --global user.name "yefeimac" #可以不加--global
git config --global user.email "your email address" #可以不加--global
git config --global core.sshCommand "ssh -i /Users/yefei/.ssh/id_rsa" #這里設(shè)置git使用哪個(gè)密鑰,非常好用
git配置文件地址: macOS and Linux: /Users/<username>/.gitconfig or ~/.gitconfig
配置git的ssh
在用ssh之前,你需要
先生成公鑰私鑰,然后將公鑰加入到github的setting中,按照教程配置~/yefei/.ssh/config
也可以參考中文教程
它的大概過(guò)程:
1.設(shè)置本地的ssh key,打開(kāi)git bash,輸入命令:
ssh-keygen -t rsa -C "XXXXXX@XXXX.com" 其中雙引號(hào)中是你注冊(cè)github時(shí)用的郵箱。
一直回車,選擇默認(rèn)路徑,和空密碼。最后會(huì)在默認(rèn)路徑下生成.ssh文件夾,打開(kāi).ssh里面有兩個(gè)文件,打開(kāi)id_rsa.pub復(fù)制里面的密鑰。
2.打開(kāi)github,選擇settings
點(diǎn)擊ssh and gpg keys,選擇ssh keys 右邊的new ssh key。出現(xiàn)下面綠色框的內(nèi)容,填寫(xiě)標(biāo)題,并將自己剛才復(fù)制的密鑰粘貼到key中。最后點(diǎn)擊add ssh key.
titile隨便取名字,一般讓你自己明白是哪個(gè)電腦,比如yefeiMBP
3.查看是否成功。在git bash中輸入命令:(注意是git bash,不是win自帶的cmd中輸入命令)
ssh -T git@github.com
會(huì)提示,是否continue,輸入yes。后就會(huì)看到:
Warning:Permanently added 'github.com,207.97.227.239' (RSA) to the list of known hosts.
Hi zhangsiyao11! You've successfully authenticated, but GitHub does not provide shell access.
這樣就成功了,不用理會(huì)warning。
同理,你的這個(gè)ssh既可以加到github上,也可以加到公司的gitlab里
新建遠(yuǎn)程倉(cāng)庫(kù)流程
-
配置GitHub倉(cāng)庫(kù)
在github上新建一個(gè)倉(cāng)庫(kù)具體見(jiàn)下圖
image.png
本地代碼和遠(yuǎn)程倉(cāng)庫(kù)關(guān)聯(lián)
1、首先執(zhí)行類似與git remote add orgin https://github.com/812865052/newstock.git
2、執(zhí)行g(shù)it pull --rebase origin master (解決failed to push some refs to git的辦法)
3、git push origin master
已有g(shù)ithub倉(cāng)庫(kù)
- 已有g(shù)ithub倉(cāng)庫(kù),從遠(yuǎn)程倉(cāng)庫(kù)下載代碼
執(zhí)行前面的本地新倉(cāng)庫(kù)配置步驟 - 常規(guī)的本地下載、提交和push
從遠(yuǎn)程倉(cāng)庫(kù)下載新的倉(cāng)庫(kù),可以使用
https git clone:
git clone https://github.com/project/repo.git
ssh git clone:
git clone git@github.com:project/repo.git
可以直接clone特定分支的代碼
git clone 分支名 地址
本地倉(cāng)庫(kù)修改后,如何同步:
1、本地修改上傳:
git add 文件名
git commit -m "comment"
或者刪除文件
git rm 文件名/文件夾(前提是該文件或者文件夾在git所在目錄里面)
然后提交 git commit -m "comment"
git push origin(reposity name) master
2、本地同步遠(yuǎn)程倉(cāng)庫(kù)代碼:
git pull origin master(好像還可以用fetch,更安全,可以后面研究下)
倉(cāng)庫(kù)
查看遠(yuǎn)程倉(cāng)庫(kù)
git remote -v
新建倉(cāng)庫(kù)
git remote add gitname https://github.com/812865052/newstock.git
本地倉(cāng)庫(kù)改名
如果不想使用origin這個(gè)名字,可以用 git remote rename origin newname改名。
提交的時(shí)候,就變成了git push newname master
若大批量 增、刪、改文件,顯然一個(gè)個(gè)添加或刪除是不可取的,以下命令可快捷操作暫存區(qū)(建議練習(xí)使用,加深對(duì)以下幾個(gè)命令的理解):
git add -A 暫存區(qū)與工作區(qū)保持一致(stages All)
git add . 暫存區(qū)新建文件及更改文件(stages new and modified, without deleted)
git add -u 暫存區(qū)刪除文件及更改文件(stages modified and deleted, without new)
單個(gè)文件的修改記錄
- git log filename
可以看到fileName相關(guān)的commit記錄 - git log -p filenam
可以顯示每次提交的diff - 只看某次提交中的某個(gè)文件變化,可以直接加上fileName
git show c5e69804bbd9725b5dece57f8cbece4a96b9f80b filename
diff查看
diff各個(gè)版本區(qū)別
工作目錄 vs 暫存區(qū)
git diff filename
意義:查看文件在工作目錄與暫存區(qū)的差別。如果還沒(méi) add 進(jìn)暫存區(qū),則查看文件自身修改前后的差別。
也可查看和另一分支的區(qū)別。$ git diff branch
filename
暫存區(qū) vs Git倉(cāng)庫(kù)
git diff --cached filename
意義:表示查看已經(jīng) add 進(jìn)暫存區(qū)但是尚未 commit 的內(nèi)容同最新一次 commit 時(shí)的內(nèi)容的差異。
也可以指定倉(cāng)庫(kù)版本:git diff --cached commit
filename
工作目錄 vs Git倉(cāng)庫(kù)
git diff commit
filename
意義:查看工作目錄同Git倉(cāng)庫(kù)指定 commit 的內(nèi)容的差異。
commit
=HEAD 時(shí):查看工作目錄同最近一次 commit 的內(nèi)容的差異。
Git倉(cāng)庫(kù) vs Git倉(cāng)庫(kù)
git diff commit
commit
意義:Git倉(cāng)庫(kù)任意兩次 commit 之間的差別。
本地分支版本回退的方法
首先,使用 git log 命令查看提交歷史,找到你想要回退的版本的提交哈希值(commit hash)或者提交消息。
運(yùn)行以下命令回退到指定版本:
git reset --hard <commit>
將 <commit> 替換為你要回退到的版本的提交哈希值或者提交消息。
例如,如果要回退到提交哈希值為 abc123 的版本,命令將如下所示:
git reset --hard abc123
注意:--hard 參數(shù)將會(huì)刪除回退版本后的所有更改,包括未提交的更改,請(qǐng)確保在執(zhí)行此命令之前備份和保存你的工作。
執(zhí)行回退命令后,Git 將會(huì)將當(dāng)前分支指向指定的版本,并且丟棄指定版本后的所有提交。
自己的遠(yuǎn)程分支版本回退的方法
在回退到指定版本后,使用 git log 命令確認(rèn)你的本地倉(cāng)庫(kù)已經(jīng)回退到了正確的版本。
如果你之前已經(jīng)將本地倉(cāng)庫(kù)與遠(yuǎn)程倉(cāng)庫(kù)關(guān)聯(lián),可以直接使用以下命令將回退推送到遠(yuǎn)程倉(cāng)庫(kù):
git push -f origin <branch_name>
將 <branch_name> 替換為你要推送的分支的名稱。
注意:由于回退操作改變了 Git 倉(cāng)庫(kù)的歷史記錄,使用 -f 參數(shù)強(qiáng)制推送是必要的。請(qǐng)謹(jǐn)慎使用此命令,并確保你具有足夠的權(quán)限來(lái)推送更改。
注意:本地分支回滾后,版本將落后遠(yuǎn)程分支,必須使用強(qiáng)制推送覆蓋遠(yuǎn)程分支,否則無(wú)法推送到遠(yuǎn)程分支
如果提示GitLab: You are not allowed to force push code to a protected branch on this project.則需要在gitlab setting里面的repository里面找到protect branch,要打開(kāi)允許force操作
本地同步遠(yuǎn)程倉(cāng)庫(kù)修改
想保留本地修改,同時(shí)把遠(yuǎn)程代碼更新到本地的話,最好的命令是git pull --rebase,只用git pull會(huì)多出很多無(wú)用的commit信息。詳細(xì)信息參考git pull --rebase和這篇
git pull = git fetch + git merge
git pull --rebase = git fetch + git rebase
或者是用git fetch upstream 然后git merge upstream/master 參考git pull 和 git fetch的區(qū)別?
如何同步不同名字的分支
The upstream branch of your current branch does not match
the name of your current branch. To push to the upstream branch
on the remote, use
git push gerrit HEAD:jacoco
To push to the branch of the same name on the remote, use
git push gerrit v4.7.0
分支
查看本地和遠(yuǎn)程分支
git branch -a
查看本地分支
git branch
查看所有遠(yuǎn)程分支
git branch -r
創(chuàng)建分支
git branch test
切換分支
git checkout test
創(chuàng)建并且切換分支
git checkout -b test
git checkout -b 本地分支名x origin/遠(yuǎn)程分支名x
使用該方式會(huì)在本地新建分支x,并自動(dòng)切換到該本地分支x。
采用此種方法建立的本地分支會(huì)和遠(yuǎn)程分支建立映射關(guān)系。
然后直接git push test(新建的本地分支)
git fetch origin 遠(yuǎn)程分支名x:本地分支名x
使用該方式會(huì)在本地新建分支x,但是不會(huì)自動(dòng)切換到該本地分支x,需要手動(dòng)checkout。
采用此種方法建立的本地分支不會(huì)和遠(yuǎn)程分支建立映射關(guān)系。
重命名分支
在當(dāng)前分支執(zhí)行g(shù)it branch -m new-branch-name,則把當(dāng)前分支重新命名為new-branch-name
必須是相同名字的分支才能push,否則會(huì)報(bào)error: src refspec xxx does not match any / error: failed to push some refs to
把master代碼合到當(dāng)前分支
1.切換到主分支
git checkout master
2.使用git pull把master代碼拉到本地
git pull upstream master
3.切換到自己的分支——>(XXX)
git checkout XXX
4.使用merge把主分支的代碼合并到自己的分支
git merge master
注意:合并時(shí)有可能會(huì)有沖突,解決完沖突才能push
5.最后push,你分支的代碼就和主分支的一樣了
git push origin xxx
刪除本地分支
git branch -d xxxxx
查看本地分支與遠(yuǎn)程分支的映射關(guān)系
git branch -vv (注意是兩個(gè)v)
第一行,本地的jacoco分支,沒(méi)有和遠(yuǎn)程分支關(guān)聯(lián)
第二行,本地的master分支,和遠(yuǎn)程的origin/master分支關(guān)聯(lián)
第三行,本地的v4.7.0分支,和遠(yuǎn)程的gerrit/jacoco分支關(guān)聯(lián)
其中,遠(yuǎn)程名字和ahead和behind的意思:
Ahead is the number of commits on this branch that do not exist on the base branch. Behind is the number of commits on the base branch that do not exist on this branch.
當(dāng)前分支與取消和遠(yuǎn)程分支關(guān)聯(lián)
git branch --unset-upstream
當(dāng)前分支與和遠(yuǎn)程分支關(guān)聯(lián)
git branch -u origin/addFile
或者使用命令:
git branch --set-upstream-to origin/addFile
fork別人項(xiàng)目后,先同步更新別人的提交,然后把自己的代碼merge到原項(xiàng)目
git remote -v
git remote add upstream git@github.com:xxx/xxx.git(這里還可以填https的地址) 把原項(xiàng)目加入到upstream
如果填錯(cuò)地址,想刪除upstream,用git remote rm upstream
git fetch upstream 從原項(xiàng)目拉取最新代碼
git merge upstream/master 將源代碼最新代碼合到fork分支
或者直接拉推特定分支代碼到當(dāng)前分支
git pull upstream Financial-User
git pull origin Financial-User
git push origin Financial-User
如果上一步出錯(cuò),提示:
error: Your local changes to the following files would be overwritten by merge:
Please, commit your changes or stash them before you can merge.
有兩種解決方法,第一, 先commit本地修改,然后合并在push到遠(yuǎn)程自己的項(xiàng)目。
第二,先執(zhí)行g(shù)it stash,然后在執(zhí)行g(shù)it merge upstream/master,最后執(zhí)行g(shù)it stash pop,把自己的修改又展示出來(lái)。
git push 把代碼上傳到fork項(xiàng)目
最后就是在網(wǎng)頁(yè)發(fā)起pull request請(qǐng)求
另外一種方法就是github網(wǎng)頁(yè)操作,具體步驟,參考這篇文章
同步master代碼到feature分支
#1 創(chuàng)建功能分支
(master) git checkout -b feature
#2 功能迭代
(feature) git commit ...
#3 合并最新主干代碼
(feature) git checkout master
(master) git pull orgin master
(master) git checkout feature ####把feature合并到master
(feature) git merge master
gitlab常見(jiàn)操作
git pull upstream master 本地分支拉取最新master代碼
git branch -a
git checkout xxx
git remote -v
git pull upstream Financial-User
忽略文件設(shè)置
.gitignore
文件位置就在項(xiàng)目根目錄
exclude
git 還提供了另一種 exclude 的方式來(lái)完成同樣的忽略
不同的是 .gitignore 這個(gè)文件本身會(huì)push到庫(kù)中去。保存的是公共的需要排除的文件。
而exclude 是自己本地忽略的設(shè)置,不會(huì)影響到其他人,也不會(huì)提交到庫(kù)中去。
exclude 文件所在位置
項(xiàng)目根目錄/.git/info/exclude
場(chǎng)景1-切到新分支看開(kāi)發(fā)代碼
git fetch -這里會(huì)把所有新分支拉下來(lái) 否則在 git branch -r看不到開(kāi)發(fā)新建的分支
git check out 分支名 就自動(dòng)切到新分支了 代碼也是分支代碼
如果報(bào)錯(cuò)
error: The following untracked working tree files would be overwritten by checkout:
remit-manager.iml
Please move or remove them before you switch branches.
Aborting
就執(zhí)行g(shù)it clean -df remit-manager.iml
然后再執(zhí)行checkout
查看沖突文件列表
git diff --name-only --diff-filter=U
【Git】pull 分支報(bào)錯(cuò) fatal: Need to specify how to reconcile divergent branches