Github(4)-远程操作
使用github 托管代碼簡(jiǎn)單使用教程--遠(yuǎn)程操作
- 3.連接本地倉(cāng)庫(kù)與遠(yuǎn)程倉(cāng)庫(kù)
- 3.1 git clone 克隆實(shí)現(xiàn)鏈接
- 3.2 git remote add origin
- 4.遠(yuǎn)程操作
- 4.1 git checkout -b 切換分支1
- 4.2 git branch 查看分支
- 4.3 git merge 合并分支
- 4.3.1 step1:切換成主分支
- 4.3.2 step2:合并分支
- 4.3.3 step3:刪除分支
- 4.3.4 git merge --no-ff 帶參數(shù)合并分支
- 4.3.5 -D 強(qiáng)制刪除沒有合并的分支
- 4.4 git switch 切換分支2
- 4.5 解決沖突
- 4.6 分支管理策略
- 4.7 創(chuàng)建bug分支
- 4.7.1 git stash
- 4.7.2 bug 修復(fù)工作流程
- 4.7.3 git stash apply + git stash drop
- 4.7.4 git stash pop
- 4.7.5 git stash list
- 4.7.6 git cheery-pick 一鍵修復(fù)多分支bug
- 4.8 創(chuàng)建 feature 分支--新功能開發(fā)
- 4.9 多人協(xié)作
- 4.9.1 git remote
- 4.9.2 git push 推送特定分支
- 4.9.3 git pull
- 4.10 rebase
- 5.[git push](https://blog.csdn.net/u010312474/article/details/107915694)
廖雪峰老師博文學(xué)習(xí)筆記:https://www.liaoxuefeng.com/wiki/896043488029600/897884457270432
3.連接本地倉(cāng)庫(kù)與遠(yuǎn)程倉(cāng)庫(kù)
3.1 git clone 克隆實(shí)現(xiàn)鏈接
克隆遠(yuǎn)程倉(cāng)庫(kù)到本地,克隆下來的倉(cāng)庫(kù)自然的形成和遠(yuǎn)程倉(cāng)庫(kù)的鏈接。
step 1.在github 平臺(tái)上新建倉(cāng)庫(kù)
step 2.克隆改倉(cāng)庫(kù)到本地
git clone git@github.com:cyy-hub/Deecamp.git
step 3.在本地倉(cāng)庫(kù)上進(jìn)行文件管理
step 4.將需要上推到遠(yuǎn)程倉(cāng)庫(kù)的文件進(jìn)行Push操作
注意:
1.clone倉(cāng)庫(kù),git會(huì)將本地的master 分支與遠(yuǎn)程的master分支對(duì)應(yīng)起來。遠(yuǎn)程倉(cāng)庫(kù)默認(rèn)名稱是origin.
2.克隆下來時(shí),只能看到master分支, 從遠(yuǎn)程創(chuàng)建其他分支到本地
git checkout -b dev origin/dev
3.2 git remote add origin
step1. 在本地新建一個(gè)倉(cāng)庫(kù)(新建一個(gè)文件夾之后 git init)
$ git init
Initialized empty Git repository in /Users/michael/learngit/.git/
step 2.在github 平臺(tái)上新建倉(cāng)庫(kù)(兩個(gè)倉(cāng)庫(kù)的名字暫時(shí)設(shè)為一致)
step 3. 創(chuàng)建鏈接
git remote add origin git@github.com:cyy-hub/Deecamp.git
step 4.在本地倉(cāng)庫(kù)上進(jìn)行文件管理
step 5.將需要上推到遠(yuǎn)程倉(cāng)庫(kù)的文件進(jìn)行Push操作
第一次上推的時(shí)候用git push -u origin master,本地的master分支內(nèi)容推送的遠(yuǎn)程新的master分支,同時(shí)將本地的master分支和遠(yuǎn)程的master分支關(guān)聯(lián)起來
git push -u origin master
參考博文:https://www.cnblogs.com/longshiyVip/p/5642360.html
4.遠(yuǎn)程操作
每次提交,git 將每個(gè)版本串成一條時(shí)間線,一條時(shí)間線就是一個(gè)分支.
Head 指向當(dāng)前作用的分支,如果沒有新建分支,即為指向master分支。
4.1 git checkout -b 切換分支1
$ git checkout -b dev
Switched to a new branch ‘dev’
git checkout命令加上-b參數(shù)表示創(chuàng)建并切換到新的分支上,相當(dāng)于以下兩條命令:
$ git branch dev
$ git checkout dev
Switched to branch ‘dev’
4.2 git branch 查看分支
$ git branch
- dev
master
4.3 git merge 合并分支
4.3.1 step1:切換成主分支
git checkout master
4.3.2 step2:合并分支
(將指定分支(dev)合并到當(dāng)前分支(master))
git merge dev
4.3.3 step3:刪除分支
合并完可以選擇刪除過多的分支(dev)
git branch -d dev
4.3.4 git merge --no-ff 帶參數(shù)合并分支
一般情況下,Git會(huì)用Fast forward模式合并。但這種模式下,刪除分支后,會(huì)丟掉分支信息。如果要禁用Fast forward模式,Git就會(huì)在merge時(shí)生成一個(gè)新的commit,這樣,從分支歷史上就可以看出分支信息。
git merge --no-ff -m “merge with no-ff” dev
因?yàn)楹喜⑶覄?chuàng)建一個(gè)新的commit,所以加上-m參數(shù),把commit描述寫進(jìn)去。
4.3.5 -D 強(qiáng)制刪除沒有合并的分支
git branch -D feature-vulcan
4.4 git switch 切換分支2
創(chuàng)建(-c參數(shù))并切換到新的分支上
git switch -c dev
切換到已有分支
git switch master
4.5 解決沖突
兩個(gè)分支上對(duì)同一個(gè)文件進(jìn)行了修改,修改內(nèi)容不一致,無法直接merge,需要手動(dòng)解決沖突后再提交。
merge 的時(shí)候會(huì)顯示沖突文件在哪里
CONFLICT (content): Merge conflict in readme.txt
打開對(duì)應(yīng)的文件,會(huì)提示沖突內(nèi)容,保留想留下的,去掉><=,再add,commit
<<<<<<< HEAD
Creating a new branch is quick & simple.
=======
Creating a new branch is quick AND simple.
>>>>>>> feature1
帶參數(shù)的git log 可以查看分支合并情況
git log --graph --pretty=oneline --abbrev-commit
4.6 分支管理策略
1.master 分支需要保持穩(wěn)定性,僅用來發(fā)布新版本,不能在上面干活;
2.每個(gè)人在各自的分支上干活,穩(wěn)定時(shí)的往主分支中合并即可
4.7 創(chuàng)建bug分支
4.7.1 git stash
把當(dāng)前工作現(xiàn)場(chǎng)存儲(chǔ)起來,等之后恢復(fù)現(xiàn)場(chǎng)繼續(xù)工作。運(yùn)行完git stash 之后用git status來查看工作區(qū)就是干凈的。
git stash
4.7.2 bug 修復(fù)工作流程
每個(gè)bug 都可以通過一個(gè)新的臨時(shí)分支來修復(fù),修復(fù)后合并分支,然后刪除臨時(shí)分支。
step1: 確定在哪個(gè)分支上修復(fù)bug, 就在該分支上創(chuàng)建臨時(shí)分支
step2: 修復(fù)big文件, 提交文件add, commit
step3:切換回原分支,合并分支merge
step4: 刪除bug 分支
4.7.3 git stash apply + git stash drop
step1: 恢復(fù)內(nèi)容
git stash apply
step2: 刪除stash里的內(nèi)容
git stash drop
多個(gè)現(xiàn)場(chǎng)保護(hù)在stash 中,apply可以指定想要恢復(fù)的工作現(xiàn)場(chǎng)
$ git stash list
stash@{0}: WIP on dev: f52c633 add merge
$ git stash apply stash@{0}
4.7.4 git stash pop
同時(shí)完成恢復(fù)和刪除
git stash pop
4.7.5 git stash list
git stash list可以查看被保護(hù)起來的現(xiàn)場(chǎng)的情況,git stash pop 保存
4.7.6 git cheery-pick 一鍵修復(fù)多分支bug
修復(fù)了一個(gè)分支上的一個(gè)bug之后,從這個(gè)分支創(chuàng)建出來的分支同樣存在這樣的bug 。修復(fù)同樣的分支,不需要再操作一遍。只需要把剛剛在 4.7.2 step 2 中的commit 所做的修改復(fù)制到copy 分支中。
step1: 切換到需要修復(fù)的copy 分支
step2: 復(fù)制修復(fù)修改
git cherry-pick 版本號(hào)
此處會(huì)創(chuàng)建一個(gè)新的的commit.
注:整個(gè)修復(fù)過程可以選擇任意一個(gè)待修復(fù)的分支做修復(fù),然后在其他分支上cherry-pick 該修復(fù)
4.8 創(chuàng)建 feature 分支–新功能開發(fā)
在軟件開發(fā)中,總需要開發(fā)新功能,需要跑一些實(shí)驗(yàn)性的代碼。所以開發(fā)一個(gè)新功能的時(shí)候,應(yīng)該創(chuàng)建一個(gè)feature 分支,在上面進(jìn)行開發(fā),合并,刪除。
4.9 多人協(xié)作
4.9.1 git remote
查看遠(yuǎn)程分支,-v 可以查看抓取和推送權(quán)限
$ git remote
origin
$ git remote -v
origin git@github.com:michaelliao/learngit.git (fetch)
origin git@github.com:michaelliao/learngit.git (push) # 可以看到說明有推送權(quán)限
4.9.2 git push 推送特定分支
在工程開發(fā)中需要確定哪些分支需要推送,哪些分支不需要、
git push origin master # 推送master分支
git push origin dev # 推送dev 分支
4.9.3 git pull
將遠(yuǎn)程代碼庫(kù)的最新內(nèi)容拉下來后直接合并,即:git pull = git fetch + git merge,這樣可能會(huì)產(chǎn)生沖突,需要手動(dòng)解決。
協(xié)同開發(fā)時(shí),git push 時(shí)(其他小伙伴已經(jīng)push 操作過),報(bào)錯(cuò):
$ git push origin dev
To github.com:michaelliao/learngit.git
! [rejected] dev -> dev (non-fast-forward)
error: failed to push some refs to ‘git@github.com:michaelliao/learngit.git’
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: ‘git pull …’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push --help’ for details.
解決:先用git pull 將遠(yuǎn)程倉(cāng)庫(kù)中的 dev 分支來回來,回自動(dòng)在本地合并。很大的可能需要手動(dòng)解決沖突,再push。
$ git pull
Auto-merging env.txt
CONFLICT (add/add): Merge conflict in env.txt
Automatic merge failed; fix conflicts and then commit the result.
4.10 rebase
(使git 提交歷史變成一條干凈的時(shí)間線)
git push:error: failed to push some refs to
git pull+ git status:Your branch is ahead of ‘origin/master’ by 3 commits.
git rebase
4.4 git fetch
將遠(yuǎn)程代碼庫(kù)的最新內(nèi)容拉到本地,在檢查拉到本地的內(nèi)容以后決定是否合并到工作區(qū)的分支中。 (git merge需要自己操作么)
廖雪峰老師博文學(xué)習(xí)筆記:https://www.liaoxuefeng.com/wiki/896043488029600/897884457270432
5.git push
git push origin HEAD:refs/for/master
總結(jié)
以上是生活随笔為你收集整理的Github(4)-远程操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Shiro学习笔记
- 下一篇: 机器学习知识总结系列-机器学习中的优化算