比如我在Ubuntu系统下用git:
远程仓库origin包含两个分支master和dev,git clone之后默认文件夹显示的是master分支的代码,之后可以在当前目录下checkout切换到dev分支。我理解这样我当前命令行下是处于dev分支,那我如何具体查看dev分支的代码呢?可视化的代码仍然是master分支下的啊。
2条回答 默认 最新
- IT林跑跑 2019-04-17 11:50关注
不知道你是不是哪步操作错了,几个命令
git branch 查看本地仓库分支 git branch --remote 查看远程仓库分支 git branch branchname 创建名为branchname的分支 git checkout branchname 切换分支为branchname git push <remote> <branch> 推送分支到远程仓库
当前我这个testBrach是从刚创建的,然后拉取下来,是个空仓库
testBranch# ll total 12K drwxr-xr-x 3 root root 4.0K Apr 17 19:21 ./ drwxr-xr-x 4 root root 4.0K Apr 17 19:21 ../ drwxr-xr-x 7 root root 4.0K Apr 17 19:21 .git/
当前使用git branch没有输出任何分支
testBranch# git branch
在当前的仓库下创建一个文件叫master里面写一句话,然后提交到远程master分支
testBranch# vi master this is master testBranch# git add . testBranch# git commit -m "add master" [master (root-commit) 6dd57c5] add master 1 file changed, 1 insertion(+) create mode 100644 master testBranch# git push origin master Counting objects: 3, done. Writing objects: 100% (3/3), 220 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To /home/git/temp/testBranch/ * [new branch] master -> master
这下远程和本地都有master分支了,并且当前分支下有一个 master文件
testBranch# git branch * master testBranch# git branch --remote origin/master testBranch# ll total 16K drwxr-xr-x 3 root root 4.0K Apr 17 19:23 ./ drwxr-xr-x 4 root root 4.0K Apr 17 19:21 ../ drwxr-xr-x 8 root root 4.0K Apr 17 19:24 .git/ -rw-r--r-- 1 root root 15 Apr 17 19:23 master
然后我们创建一个dev分支,切换到dev分支。
testBranch# git branch dev testBranch# git checkout dev * dev master testBranch# ll total 16K drwxr-xr-x 3 root root 4.0K Apr 17 19:23 ./ drwxr-xr-x 4 root root 4.0K Apr 17 19:21 ../ drwxr-xr-x 8 root root 4.0K Apr 17 19:32 .git/ -rw-r--r-- 1 root root 15 Apr 17 19:23 master
这里会有master文件是因为dev分支是从master出来的,我们在T1时刻已经提交了master文件,所以T2时刻dev分支从master分支出来,会包含T2时刻master包含的所有文件。
为了这里演示我就把master删除,然后创建了一个dev文件,也写了一句话,然后提交dev分支
testBranch# git rm master rm 'master' testBranch# vi dev this is dev testBranch# git add . testBranch# git commit -m "add dev" [dev 155e4f5] add dev 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 dev delete mode 100644 master testBranch# git push origin dev Counting objects: 3, done. Writing objects: 100% (3/3), 240 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To /home/git/temp/testBranch/ 6dd57c5..155e4f5 dev -> dev
当前本地分支和远程分支都有master和dev了
root@iZbp1ccq9xttkzab5d3fc9Z:/home/linxingyang/workdir/git/testBranch# git branch * dev master root@iZbp1ccq9xttkzab5d3fc9Z:/home/linxingyang/workdir/git/testBranch# git branch --remote origin/dev origin/master
先查看当前分支(dev)下面的内容,然后切到master分支,在切回dev分支,可以看到工作目录对应的文件是有变化的。
/testBranch# ll total 16K drwxr-xr-x 3 root root 4.0K Apr 17 19:40 ./ drwxr-xr-x 4 root root 4.0K Apr 17 19:21 ../ -rw-r--r-- 1 root root 12 Apr 17 19:40 dev drwxr-xr-x 8 root root 4.0K Apr 17 19:44 .git/ testBranch# git checkout master Switched to branch 'master' Your branch is up-to-date with 'origin/master'. testBranch# ll total 16K drwxr-xr-x 3 root root 4.0K Apr 17 19:46 ./ drwxr-xr-x 4 root root 4.0K Apr 17 19:21 ../ drwxr-xr-x 8 root root 4.0K Apr 17 19:46 .git/ -rw-r--r-- 1 root root 15 Apr 17 19:46 master testBranch# git checkout dev Switched to branch 'dev' testBranch# ll total 16K drwxr-xr-x 3 root root 4.0K Apr 17 19:46 ./ drwxr-xr-x 4 root root 4.0K Apr 17 19:21 ../ -rw-r--r-- 1 root root 12 Apr 17 19:46 dev drwxr-xr-x 8 root root 4.0K Apr 17 19:46 .git/
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决评论 打赏 举报无用 1