赞
踩
同一个仓库多个分支进行同时修改,最后合并到同一个仓库中
各个分支互不影响。相当于多个独立开发的环境。
#创建分支branch1并切换到分支branch1中 git branch / git checkout
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ git branch branch1
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ git branch
branch1
* master
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ git checkout branch1
Switched to branch 'branch1'
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (branch1)
#在分支branch1中创建文件1.txt
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (branch1)
$ touch 1.txt
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (branch1)
$ git add 1.txt
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (branch1)
$ git commit -m "add 1.txt"
[branch1 2514902] add 1.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 1.txt
#分支可以看到所在路径根节点下的文件
例如branch1是master的分支,test.txt在master下,
即使我切换到branch1中也能看到。
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (branch1)
$ ll
total 1
-rw-r--r-- 1 MY-PC 197609 0 6月 7 10:30 1.txt
-rw-r--r-- 1 MY-PC 197609 13 6月 7 10:12 test.txt
合并创建分支和切换分支两个命令 git checkout -b
将git branch和git checkout合并成git checkout -b (branch)
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (branch1)
$ git checkout -b branch2
Switched to a new branch 'branch2'
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (branch2)
#删除分支 git branch -d
在branch2下创建分支branch3,切换到branch3中delete branch2,
如果在branch2中delete branch2 会报错。
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (branch2)
$ git checkout -b branch3
Switched to a new branch 'branch3'
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (branch3)
$ git branch -d branch2
Deleted branch branch2 (was 2514902).
在branch3中创建2.txt并在master中尝试删除branch3中的2.txt
报错,因为此时branch3中已经发生变动
而新增的2.txt还没有合并到master中。
可以选择用大写-D参数强制删除
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master) $ git checkout branch3 Switched to branch 'branch3' MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (branch3) $ touch 2.txt MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (branch3) $ vim 2.txt MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (branch3) $ cat 2.txt hello MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (branch3) $ git add 2.txt warning: LF will be replaced by CRLF in 2.txt. The file will have its original line endings in your working directory MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (branch3) $ git commit -m "add 2.txt" [branch3 628ce7f] add 2.txt 1 file changed, 1 insertion(+) create mode 100644 2.txt MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (branch3) $ git checkout master Switched to branch 'master' Your branch is up to date with 'origin/master'. MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master) $ git branch -d branch3 error: The branch 'branch3' is not fully merged. If you are sure you want to delete it, run 'git branch -D branch3'.
#将branch3中的内容合并到master中,最后提交的一般是master git merge
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master) $ git merge branch3 Updating c0b5233..628ce7f Fast-forward 1.txt | 0 2.txt | 1 + 2 files changed, 1 insertion(+) create mode 100644 1.txt create mode 100644 2.txt MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master) $ git push 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), 527 bytes | 105.00 KiB/s, done. Total 6 (delta 0), reused 0 (delta 0) To github.com:Jacobtsang/git-tutorial.git c0b5233..628ce7f master -> master
虽然现在本地仓master主干下有三个分支,
而远端仓看到的只有master
#将分支push到远端 git push origin
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ git push origin branch3
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'branch3' on GitHub by visiting:
remote: https://github.com/Jacobtsang/git-tutorial/pull/new/branch3
remote:
To github.com:Jacobtsang/git-tutorial.git
* [new branch] branch3 -> branch3
#在本地删除远端分支 git push origin :
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ git push origin :branch3
To github.com:Jacobtsang/git-tutorial.git
- [deleted] branch3
#push上去的分支想更改别的名字
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ git push origin branch3:b3
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'b3' on GitHub by visiting:
remote: https://github.com/Jacobtsang/git-tutorial/pull/new/b3
remote:
To github.com:Jacobtsang/git-tutorial.git
* [new branch] branch3 -> b3
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。