Git 以分支的方式同时管理多个项目
当然使用万能的Git分支完全可以满足你这么个性化要求( 以分支的形式管理不同项目 )
首页你需要分支是必须要 创建的吧 (对应不同的项目)
那么问题来了,后创建的分支默认是以当前分支为父分支的,像下面这样:"master-dev"的父分支是master
既然Git在创建是默认给我们的新分支指定了父亲,那么可不可以在创建是不需要呢?
强大的Git同样提供了解决方法(创建时提供 --orphan 参数即可 ):git checkout --orphan 分支名
注意:
尽管创建分支时没有了父分支,但创建成功后,原分支的文件会在创建时添加到当前暂存区的,所以需要移除( 不需要的情况下 )
然后再将原分支的文件从当前分支仓库中移除,这样你的分支里的文件对于其他分支来说就是独一无二的了(即使不移除原分支的文件,此文件也是新添加到当前分支的,所以跟其他分支没有任何关系)
而其他分支也完全不可能会影响你当前分支的工作( 不存在依赖关系的前提下 )
当切换到PC分支时,Git仓库里的文件也会发生变化,也就实现了我们的目的
不同的分支其实就是不同的目录和文件,跟其他分支没有关系的
提交记录应该是这样子的
可以看到下面pc分支的提交连接线(蓝色)跟其他的连接线(绿色)是不同的
master-dev是在master上创建的子分支( 存在依赖关系 )
以同样的方式创建分支mobile,同样没有父亲, git checkout --orphan mobile
然后在不同分支进行模拟提交,那么提交记录就有可能是这样的
应用场景不同,请谨慎使用,否则提交历史将惨不忍睹
最终的Git分支和对应的目录及文件是这样的,现在你可以在一个Git里愉快的玩耍了
技术有风险使用请谨慎
技术有风险使用请谨慎
技术有风险使用请谨慎
重要的事情说三遍
举个栗子:
F:\Demo\D11>tree /F 文件夹 PATH 列表 卷序列号为 A627-5B49 F:. └─master main.txt F:\Demo\D11>git status fatal: Not a git repository (or any of the parent directories): .git F:\Demo\D11>git init Initialized empty Git repository in F:/Demo/D11/.git/ F:\Demo\D11>git status On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) master/ nothing added to commit but untracked files present (use "git add" to track) F:\Demo\D11>git add --all F:\Demo\D11>git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: master/main.txt F:\Demo\D11>git commit -a -m "master branch init" [master (root-commit) 3ae67eb] master branch init 1 file changed, 1 insertion(+) create mode 100644 master/main.txt F:\Demo\D11>git commit -a -m "master edit" [master 60197ce] master edit 1 file changed, 3 insertions(+), 1 deletion(-) F:\Demo\D11>git checkout -b "master-dev" Switched to a new branch 'master-dev' F:\Demo\D11>git status On branch master-dev Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: master/main.txt no changes added to commit (use "git add" and/or "git commit -a") F:\Demo\D11>git commit -a -m "master-dev edit" [master-dev 67837dd] master-dev edit 1 file changed, 3 insertions(+), 1 deletion(-) F:\Demo\D11>git show-branch ! [master] master edit * [master-dev] master-dev edit -- * [master-dev] master-dev edit +* [master] master edit F:\Demo\D11>git tree --all WARNING: terminal is not fully functional * 67837dd (HEAD, master-dev) master-dev edit * 60197ce (master) master edit * 3ae67eb master branch init F:\Demo\D11>git citool F:\Demo\D11>git checkout --orphan pc Switched to a new branch 'pc' F:\Demo\D11>git status On branch pc Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: master/main.txt Untracked files: (use "git add <file>..." to include in what will be committed) pc/ F:\Demo\D11>git rm -r master/ rm 'master/main.txt' F:\Demo\D11>RD /S master/ f:\demo\d11\master, 是否确认(Y/N)? Y F:\Demo\D11>tree /F 文件夹 PATH 列表 卷序列号为 A627-5B49 F:. └─pc pc.txt F:\Demo\D11>git add --all F:\Demo\D11>git commit -a -m "pc/pc init" [pc (root-commit) 3e176c6] pc/pc init 1 file changed, 1 insertion(+) create mode 100644 pc/pc.txt F:\Demo\D11>git show-branch ! [master] master edit ! [master-dev] master-dev edit * [pc] pc/pc init --- * [pc] pc/pc init + [master-dev] master-dev edit ++ [master] master edit ++ [master^] master branch init F:\Demo\D11>git tree --all WARNING: terminal is not fully functional * 3e176c6 (HEAD, pc) pc/pc init * 67837dd (master-dev) master-dev edit * 60197ce (master) master edit * 3ae67eb master branch init F:\Demo\D11>git status On branch pc Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: pc/pc.txt no changes added to commit (use "git add" and/or "git commit -a") F:\Demo\D11>git commit -a -m "pc edit" [pc d1f98d3] pc edit 1 file changed, 3 insertions(+), 1 deletion(-) F:\Demo\D11>git commit -a -m "pc reedit" [pc 2fd85bf] pc reedit 1 file changed, 3 insertions(+), 1 deletion(-) F:\Demo\D11>git show-branch ! [master] master edit ! [master-dev] master-dev edit * [pc] pc reedit --- * [pc] pc reedit * [pc^] pc edit * [pc~2] pc/pc init + [master-dev] master-dev edit ++ [master] master edit ++ [master^] master branch init F:\Demo\D11>git tree --all WARNING: terminal is not fully functional * 2fd85bf (HEAD, pc) pc reedit * d1f98d3 pc edit * 3e176c6 pc/pc init * 67837dd (master-dev) master-dev edit * 60197ce (master) master edit * 3ae67eb master branch init F:\Demo\D11>git checkout "master-dev" Switched to branch 'master-dev' F:\Demo\D11>git diff WARNING: terminal is not fully functional diff --git a/master/main.txt b/master/main.txt index 75bf703..5a345f8 100644 --- a/master/main.txt +++ b/master/main.txt @@ -2,4 +2,6 @@ main/main init master edit -master-dev edit \ No newline at end of file +master-dev edit +master-dev reedit + F:\Demo\D11>git commit -a -m "master-dev reedit" [master-dev 67cec74] master-dev reedit 1 file changed, 3 insertions(+), 1 deletion(-) F:\Demo\D11>git show-branch ! [master] master edit * [master-dev] master-dev reedit ! [pc] pc reedit --- * [master-dev] master-dev reedit * [master-dev^] master-dev edit +* [master] master edit +* [master^] master branch init + [pc] pc reedit + [pc^] pc edit + [pc~2] pc/pc init F:\Demo\D11>git tree --all WARNING: terminal is not fully functional * 67cec74 (HEAD, master-dev) master-dev reedit * 67837dd master-dev edit * 60197ce (master) master edit * 3ae67eb master branch init * 2fd85bf (pc) pc reedit * d1f98d3 pc edit * 3e176c6 pc/pc init F:\Demo\D11>git checkout master Switched to branch 'master' F:\Demo\D11>git commit -a -m "master reedit" [master b79efa5] master reedit 1 file changed, 3 insertions(+), 1 deletion(-) F:\Demo\D11>git show-branch * [master] master reedit ! [master-dev] master-dev reedit ! [pc] pc reedit --- * [master] master reedit + [master-dev] master-dev reedit + [master-dev^] master-dev edit *+ [master^] master edit *+ [master~2] master branch init + [pc] pc reedit + [pc^] pc edit + [pc~2] pc/pc init F:\Demo\D11>git tree --all WARNING: terminal is not fully functional * b79efa5 (HEAD, master) master reedit | * 67cec74 (master-dev) master-dev reedit | * 67837dd master-dev edit |/ * 60197ce master edit * 3ae67eb master branch init * 2fd85bf (pc) pc reedit * d1f98d3 pc edit * 3e176c6 pc/pc init F:\Demo\D11>git citool F:\Demo\D11>git checkout pc Switched to branch 'pc' F:\Demo\D11>git commit -a -m "pc re-reedit" [pc 1d24c39] pc re-reedit 1 file changed, 3 insertions(+), 1 deletion(-) F:\Demo\D11>git show-branch ! [master] master reedit ! [master-dev] master-dev reedit * [pc] pc re-reedit --- * [pc] pc re-reedit * [pc^] pc reedit * [pc~2] pc edit * [pc~3] pc/pc init + [master] master reedit + [master-dev] master-dev reedit + [master-dev^] master-dev edit ++ [master^] master edit ++ [master~2] master branch init F:\Demo\D11>git tree --all WARNING: terminal is not fully functional * 1d24c39 (HEAD, pc) pc re-reedit * 2fd85bf pc reedit * d1f98d3 pc edit * 3e176c6 pc/pc init * b79efa5 (master) master reedit | * 67cec74 (master-dev) master-dev reedit | * 67837dd master-dev edit |/ * 60197ce master edit * 3ae67eb master branch init F:\Demo\D11>git checkout --orphan mobile Switched to a new branch 'mobile' F:\Demo\D11>git status On branch mobile Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: pc/pc.txt Untracked files: (use "git add <file>..." to include in what will be committed) mobile/ F:\Demo\D11>tree /F 文件夹 PATH 列表 卷序列号为 A627-5B49 F:. ├─mobile │ mobile.txt │ └─pc F:\Demo\D11>RD /S f:\demo\d11\pc f:\demo\d11\pc, 是否确认(Y/N)? Y F:\Demo\D11>tree /F 文件夹 PATH 列表 卷序列号为 A627-5B49 F:. └─mobile mobile.txt F:\Demo\D11>git status On branch mobile Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: pc/pc.txt Changes not staged for commit: (use "git add/rm <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) deleted: pc/pc.txt Untracked files: (use "git add <file>..." to include in what will be committed) mobile/ F:\Demo\D11>git rm -r pc/ rm 'pc/pc.txt' F:\Demo\D11>git status On branch mobile Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) mobile/ nothing added to commit but untracked files present (use "git add" to track) F:\Demo\D11>git add --all F:\Demo\D11>git commit -a -m "pc/pc master init" [mobile (root-commit) a94d110] pc/pc master init 1 file changed, 1 insertion(+) create mode 100644 mobile/mobile.txt F:\Demo\D11>git show-branch ! [master] master reedit ! [master-dev] master-dev reedit * [mobile] pc/pc master init ! [pc] pc re-reedit ---- * [mobile] pc/pc master init + [pc] pc re-reedit + [pc^] pc reedit + [pc~2] pc edit + [pc~3] pc/pc init + [master] master reedit + [master-dev] master-dev reedit + [master-dev^] master-dev edit ++ [master^] master edit ++ [master~2] master branch init F:\Demo\D11>git tree --all WARNING: terminal is not fully functional * a94d110 (HEAD, mobile) pc/pc master init * 1d24c39 (pc) pc re-reedit * 2fd85bf pc reedit * d1f98d3 pc edit * 3e176c6 pc/pc init * b79efa5 (master) master reedit | * 67cec74 (master-dev) master-dev reedit | * 67837dd master-dev edit |/ * 60197ce master edit * 3ae67eb master branch init F:\Demo\D11>git show-branch ! [master] master reedit ! [master-dev] master-dev reedit * [mobile] pc/pc master init ! [pc] pc re-reedit ---- * [mobile] pc/pc master init + [pc] pc re-reedit + [pc^] pc reedit + [pc~2] pc edit + [pc~3] pc/pc init + [master] master reedit + [master-dev] master-dev reedit + [master-dev^] master-dev edit ++ [master^] master edit ++ [master~2] master branch init F:\Demo\D11>git commit -a -m "mobile edit" [mobile de5452e] mobile edit 1 file changed, 3 insertions(+), 1 deletion(-) F:\Demo\D11>git commit -a -m "mobile re-edit" [mobile 70dc006] mobile re-edit 1 file changed, 3 insertions(+), 1 deletion(-) F:\Demo\D11>git show-branch ! [master] master reedit ! [master-dev] master-dev reedit * [mobile] mobile re-edit ! [pc] pc re-reedit ---- * [mobile] mobile re-edit * [mobile^] mobile edit * [mobile~2] pc/pc master init + [pc] pc re-reedit + [pc^] pc reedit + [pc~2] pc edit + [pc~3] pc/pc init + [master] master reedit + [master-dev] master-dev reedit + [master-dev^] master-dev edit ++ [master^] master edit ++ [master~2] master branch init F:\Demo\D11> F:\Demo\D11>git tree --all WARNING: terminal is not fully functional * 70dc006 (HEAD, mobile) mobile re-edit * de5452e mobile edit * a94d110 pc/pc master init * 1d24c39 (pc) pc re-reedit * 2fd85bf pc reedit * d1f98d3 pc edit * 3e176c6 pc/pc init * b79efa5 (master) master reedit | * 67cec74 (master-dev) master-dev reedit | * 67837dd master-dev edit |/ * 60197ce master edit * 3ae67eb master branch init F:\Demo\D11>git checkout "master-dev" Switched to branch 'master-dev' F:\Demo\D11>git commit -a -m "master-dev re-reedit" [master-dev 1c57e77] master-dev re-reedit 1 file changed, 2 insertions(+) F:\Demo\D11>git show-branch ! [master] master reedit * [master-dev] master-dev re-reedit ! [mobile] mobile re-edit ! [pc] pc re-reedit ---- * [master-dev] master-dev re-reedit * [master-dev^] master-dev reedit * [master-dev~2] master-dev edit + [mobile] mobile re-edit + [mobile^] mobile edit + [mobile~2] pc/pc master init + [pc] pc re-reedit + [pc^] pc reedit + [pc~2] pc edit + [pc~3] pc/pc init + [master] master reedit +* [master-dev~3] master edit +* [master-dev~4] master branch init F:\Demo\D11>git tree --all WARNING: terminal is not fully functional * 1c57e77 (HEAD, master-dev) master-dev re-reedit * 67cec74 master-dev reedit * 67837dd master-dev edit | * 70dc006 (mobile) mobile re-edit | * de5452e mobile edit | * a94d110 pc/pc master init | * 1d24c39 (pc) pc re-reedit | * 2fd85bf pc reedit | * d1f98d3 pc edit | * 3e176c6 pc/pc init | * b79efa5 (master) master reedit |/ * 60197ce master edit * 3ae67eb master branch init F:\Demo\D11>git checkout master Switched to branch 'master' F:\Demo\D11>git commit -a -m "master re-reedit" [master a0a0f1a] master re-reedit 1 file changed, 3 insertions(+), 1 deletion(-) F:\Demo\D11>git show-branch * [master] master re-reedit ! [master-dev] master-dev re-reedit ! [mobile] mobile re-edit ! [pc] pc re-reedit ---- * [master] master re-reedit * [master^] master reedit + [master-dev] master-dev re-reedit + [master-dev^] master-dev reedit + [master-dev~2] master-dev edit *+ [master~2] master edit *+ [master~3] master branch init + [mobile] mobile re-edit + [mobile^] mobile edit + [mobile~2] pc/pc master init + [pc] pc re-reedit + [pc^] pc reedit + [pc~2] pc edit + [pc~3] pc/pc init F:\Demo\D11>git tree --all WARNING: terminal is not fully functional * a0a0f1a (HEAD, master) master re-reedit * b79efa5 master reedit | * 1c57e77 (master-dev) master-dev re-reedit | * 67cec74 master-dev reedit | * 67837dd master-dev edit |/ * 60197ce master edit * 3ae67eb master branch init * 70dc006 (mobile) mobile re-edit * de5452e mobile edit * a94d110 pc/pc master init * 1d24c39 (pc) pc re-reedit * 2fd85bf pc reedit * d1f98d3 pc edit * 3e176c6 pc/pc init F:\Demo\D11>git checkout pc Deletion of directory 'master' failed. Should I try again? (y/n) n Switched to branch 'pc' F:\Demo\D11>git status On branch pc nothing to commit, working directory clean F:\Demo\D11>git commit -a -m "pc re-re-reedit" [pc 0f657c0] pc re-re-reedit 1 file changed, 3 insertions(+), 1 deletion(-) F:\Demo\D11>git show-branch ! [master] master re-reedit ! [master-dev] master-dev re-reedit ! [mobile] mobile re-edit * [pc] pc re-re-reedit ---- * [pc] pc re-re-reedit * [pc^] pc re-reedit * [pc~2] pc reedit * [pc~3] pc edit * [pc~4] pc/pc init + [master] master re-reedit + [master^] master reedit + [master-dev] master-dev re-reedit + [master-dev^] master-dev reedit + [master-dev~2] master-dev edit ++ [master~2] master edit ++ [master~3] master branch init + [mobile] mobile re-edit + [mobile^] mobile edit + [mobile~2] pc/pc master init F:\Demo\D11>git tree --all WARNING: terminal is not fully functional * 0f657c0 (HEAD, pc) pc re-re-reedit * 1d24c39 pc re-reedit * 2fd85bf pc reedit * d1f98d3 pc edit * 3e176c6 pc/pc init * a0a0f1a (master) master re-reedit * b79efa5 master reedit | * 1c57e77 (master-dev) master-dev re-reedit | * 67cec74 master-dev reedit | * 67837dd master-dev edit |/ * 60197ce master edit * 3ae67eb master branch init * 70dc006 (mobile) mobile re-edit * de5452e mobile edit * a94d110 pc/pc master init F:\Demo\D11>git checkout "master-dev" Deletion of directory 'pc' failed. Should I try again? (y/n) n Switched to branch 'master-dev' F:\Demo\D11>git commit -a -m "master-dev re-re-reedit" [master-dev a6be472] master-dev re-re-reedit 1 file changed, 1 insertion(+) F:\Demo\D11>git show-branch ! [master] master re-reedit * [master-dev] master-dev re-re-reedit ! [mobile] mobile re-edit ! [pc] pc re-re-reedit ---- * [master-dev] master-dev re-re-reedit * [master-dev^] master-dev re-reedit * [master-dev~2] master-dev reedit * [master-dev~3] master-dev edit + [pc] pc re-re-reedit + [pc^] pc re-reedit + [pc~2] pc reedit + [pc~3] pc edit + [pc~4] pc/pc init + [master] master re-reedit + [master^] master reedit +* [master-dev~4] master edit +* [master-dev~5] master branch init + [mobile] mobile re-edit + [mobile^] mobile edit + [mobile~2] pc/pc master init F:\Demo\D11>git tree --all WARNING: terminal is not fully functional * a6be472 (HEAD, master-dev) master-dev re-re-reedit * 1c57e77 master-dev re-reedit * 67cec74 master-dev reedit * 67837dd master-dev edit | * 0f657c0 (pc) pc re-re-reedit | * 1d24c39 pc re-reedit | * 2fd85bf pc reedit | * d1f98d3 pc edit | * 3e176c6 pc/pc init | * a0a0f1a (master) master re-reedit | * b79efa5 master reedit |/ * 60197ce master edit * 3ae67eb master branch init * 70dc006 (mobile) mobile re-edit * de5452e mobile edit * a94d110 pc/pc master init F:\Demo\D11>git checkout pc Already on 'pc' F:\Demo\D11>git commit -a -m "pc re-re-re-reedit" [pc f906c94] pc re-re-re-reedit 1 file changed, 3 insertions(+), 1 deletion(-) F:\Demo\D11>