Git多分支平行發展(一個倉庫包含多個不一樣的項目)

背景

最近在用laravel開發微信小程序的接口,由於服務器PHP版本的問題,分別用了laravel 5.6(php 7.1,開發環境)laravel 5.4 (php 5.6,服務器環境),開發完成後,兩個項目絕大部分的代碼都差很少,不想再建一個倉庫放php 5.6版的代碼,便試着在原有倉庫新建分支來存儲php 5.6的項目,搗鼓的一上午終於搞出來了,如今記錄下。php

主要步驟:laravel

  1. 在遠程和本地倉庫中新建一個分支(我建的是laravel54);
  2. 刪除遠程laravel54分支上的全部文件;
  3. 將新項目的文件推送到laravel54上。

流程

新建一個文件夾(個人是laravel5.4)

建立git本地倉庫並關聯遠程倉庫

沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4
$ git init
Initialized empty Git repository in D:/PHP/xampp/htdocs/apple/laravel5.4/.git/

沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (master)
$ git remote add origin git@xxxxxxxxxxxxxxxxxxxxx.git

創建遠程倉庫分支 laravel54

注意,創建遠程分支後,暫時還不能在本地創建其餘分支,由於剛建立的git倉庫默認的master分支要在第一次commit以後纔會真正創建,這時建立其餘分支會報錯,解決辦法在後面說明。git

沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (master)
$ git branch laravel54
fatal: Not a valid object name: 'master'.

隨便新建一個文件,裏面能夠不用寫任何東西,而後commit

沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (master)
$ touch clearTheBranch

沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (master)
$ git add clearTheBranch

沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (master)
$ git commit -m'clean this branch'
[master (root-commit) c7c5349] clean this branch
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 clearTheBranch

推送以前先更新(下面的代碼是最核心的代碼)

新建的分支默認與master分支同樣,因此要在推送以前先把分支上的內容拉下來。小程序

沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (master)
$ git pull origin laravel54
warning: no common commits
remote: Enumerating objects: 2453, done.
remote: Counting objects: 100% (2453/2453), done.
remote: Compressing objects: 100% (2320/2320), done.
Receiviremote: Total 2453 (delta 1547), reused 229 (delta 89)
Receiving objects: 100% (2453/2453), 1.40 MiB | 1.38 MiB/s, done.
Resolving deltas: 100% (1547/1547), done.
From xxxxxxxxxxxxxxxxxxxxx
 * branch            laravel54 -> FETCH_HEAD
 * [new branch]      laravel54 -> origin/laravel54
fatal: refusing to merge unrelated histories

這個時候會報錯,是正常現象,,緣由是本地倉庫和遠程倉庫不同,有相似二者有徹底不相同的commit歷史,只須要在在pull的同時加上一行代碼便可,代碼以下:微信小程序

沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (master)
$  git pull origin laravel54 --allow-unrelated-histories
From xxxxxxxxxxxxxxxxxxxxx
 * branch            laravel54 -> FETCH_HEAD
Merge made by the 'recursive' strategy.
......

這步操做的時候,會有merge的commit說明,簡單寫下,這個只是merge的說明,並不會真正和mater合併。
通過這一步操做,就已經把遠程倉庫laravel54分支裏面的文件都拉下來了,接下來創建與遠程倉庫對應的本地分支。服務器

創建並切換到本地分支 laravel54

沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (master)
$ git branch laravel54

沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (master)
$ git checkout laravel54
Switched to branch 'laravel54'

沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (laravel54)
$

// 也能夠直接用 git checkout -b laravel54

刪除本地倉庫裏的全部文件(除了.git的文件夾),而後推送

沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (laravel54)
$ git add .

沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (laravel54)
$ git commit -m'delete all files'
[laravel54 d8bf12d] delete all files
215 files changed, 32019 deletions(-)
......

沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (laravel54)
$ git push origin laravel54
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 683 bytes | 341.00 KiB/s, done.
Total 6 (delta 1), reused 1 (delta 0)
remote: Powered by Gitee.com
To xxxxxxxxxxxxxxxxxxxxx.git
   c810298..d8bf12d  laravel54 -> laravel54

這個時候,遠程倉庫的laravel54分支便和本地倉庫的laravel54分支同樣都是空白的,這樣就能夠爲所欲爲的推送了。微信

新項目推送

這時就能夠把以前的項目拷進這個文件夾裏面,而後按照正常的推送流程進行推送。app

沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (laravel54)
$ git add .
warning: LF will be replaced by CRLF in .env.example.
The file will have its original line endings in your working directory.
.......

沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (laravel54)
$ git commit -m'laravel54 push'
[laravel54 eba0954] laravel54 push
 113 files changed, 11583 insertions(+)
 create mode 100644 .env.example
 create mode 100644 .gitattributes
 .......

沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (laravel54)
$ git push origin laravel54
Enumerating objects: 152, done.
Counting objects: 100% (152/152), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (126/126), done.
Writing objects: 100% (151/151), 211.43 KiB | 2.94 MiB/s, done.
Total 151 (delta 14), reused 42 (delta 4)
remote: Resolving deltas: 100% (14/14), done.
remote: Powered by Gitee.com
To xxxxxxxxxxxxxxxxxxxxx.git
   d8bf12d..eba0954  laravel54 -> laravel54

小總結

不用擔憂推送錯分支致使分支合併

通過如上操做,就能夠實現master分支和laravel54是兩個徹底不一樣的項目,能夠保持兩個項目平行發展,由於兩個分支的提交歷史不同,因此即使是誤操做,提交錯了分支,git會給出相應的報錯,不會把兩個分支直接合並,下面是代碼演示:fetch

  1. 本地laravel54向遠程master分支推送this

    沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (laravel54)
    $ touch a.txt
    
    沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (laravel54)
    $ git add a.txt
    
    沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (laravel54)
    $ git commit -m'test file a.txt'
    [laravel54 e246705] test file a.txt
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 a.txt
    
    沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (laravel54)
    $ git push origin master
    To xxxxxxxxxxxxxxxxxxxxx.git
     ! [rejected]        master -> master (fetch first)
    error: failed to push some refs to 'git@xxxxxxxxxxxxxxxxxxxxx.git'
    hint: Updates were rejected because the remote contains work that you do
    hint: not have locally. This is usually caused by another repository pushing
    hint: to the same ref. You may want to first integrate the remote changes
    hint: (e.g., 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    
    //本地版本回退
    
    沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (laravel54)
    $ git log
    commit e246705e9894a658d3fb2ea8f51b1ee0dd8ff834 (HEAD -> laravel54)
    Author: muyan <xiao7shuang@qq.com>
    Date:   Fri Jun 29 14:50:35 2018 +0800
    
        test file a.txt
    
    commit eba09541b60e496127f6280c3db9611f72504744 (origin/laravel54)
    Author: muyan <xiao7shuang@qq.com>
    Date:   Fri Jun 29 14:36:48 2018 +0800
    
        laravel54 push
    
    commit d8bf12d9df1337f9f7e0de723608796a490f66dc
    Author: muyan <xiao7shuang@qq.com>
    Date:   Fri Jun 29 14:24:02 2018 +0800
    
        delete all files
    
    commit dcfb2b00f4f8ad0f1f672cde9b9e9ddc79d85239 (master)
    Merge: c7c5349 c810298
    Author: muyan <xiao7shuang@qq.com>
    Date:   Fri Jun 29 13:56:24 2018 +0800
    
        Merge branch 'laravel54' of xxxxxxxxxxxxxxxxxxxxx
    
        for claering the branch
    
    commit c7c5349779f868fbc1cf18d742d634c9d3267c32
    Author: muyan <xiao7shuang@qq.com>
    Date:   Fri Jun 29 13:55:01 2018 +0800
    
        clean this branch
    
    沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (laravel54)
    $ git reset eba09541b60e496127f6280c3db9611f72504744    //回退到上一次提交以前的版本
    
    沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (laravel54)
    $ git status
    On branch laravel54
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
            a.txt
    
    nothing added to commit but untracked files present (use "git add" to track)
  2. 本地master分支向遠程master分支推送

    沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (master)
    $ git add a.txt
    
    沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (master)
    $ git commit -m 'test file a'
    [master d3e8bba] test file a
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 a.txt
    
    沐@ MINGW64 /d/PHP/xampp/htdocs/apple/laravel5.4 (master)
    $ git push origin master
    To xxxxxxxxxxxxxxxxxxxxx.git
     ! [rejected]        master -> master (fetch first)
    error: failed to push some refs to 'git@xxxxxxxxxxxxxxxxxxxxx.git'
    hint: Updates were rejected because the remote contains work that you do
    hint: not have locally. This is usually caused by another repository pushing
    hint: to the same ref. You may want to first integrate the remote changes
    hint: (e.g., 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.

理論上能夠實現無限平行分支

文章其餘地址我的博客 簡書

相關文章
相關標籤/搜索