一般一个项目有一个默认的分支 master 主分支,然后可以有许多个分支,在别的分支上的操作不会影响到主分支。使用git branch查看当前多多少分支以及当前处于哪个分支上;执行git branch 分支名称 创建分支;执行 git checkout 分支名称 切换当前分支。
使用 git branch -d 分支名称 删除分支 使用git branch -m 当前分支名称 新分支名称 修改分支名称
[email protected] MINGW64 /c/laoni/PycharmProjects/github_test (master) $ git branch * master [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master) $ git branch slave [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master) $ git branch * master slave [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master) $ git checkout slave Switched to branch ‘slave‘ [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ git branch master * slave [email protected]-TPP
在分支上提交做的修改,不会影响到master分支,可以这么做,把原有master分支的源码下到本地,然后创建新的分支,把自己修改后的代码commit到新分支上。
eg:在slave分支commit info.py文件,然后再切换到master分支
切换到master分支后查看提交的记录没有add info.py 文件内容也消失了。(如果是新建的文件,那文件也会消失。)
git merge 分支名称 合并分支,最理想的状态是被合并的分支(slave)里的文件,是当前分支(master)没有的,这样就完全没冲突。
$ git checkout slave Switched to branch ‘slave‘ [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ git branch master * slave [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ ls 456 bb.css dd.css index.html info.py [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ cat info.py asdfsadfsfasfasdf [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ git checkout master Switched to branch ‘master‘ [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master) $ ls 456 bb.css dd.css index.html [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master) $ git merge slave Updating c5b475a..a068c80 Fast-forward info.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 info.py [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master) $ cat info.py asdfsadfsfasfasdf
git merge 分支名称
使用git diff可以查看当前分支做了哪些修改,使用git diff master..slave 可以对比两个分支的不同。
PS:修改文件后需要重新add和commit
使用git diff
$ ls 456 bb.css dd.css index.html info.py [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ vim info.py [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ git diff warning: LF will be replaced by CRLF in info.py. The file will have its original line endings in your working directory. diff --git a/info.py b/info.py index 5c895f9..7e024db 100644 --- a/info.py +++ b/info.py @@ -1 +1,2 @@ -asdfsadfsfasfasdf \ No newline at end of file +asdfsadfsfasfasdf +modify 66666
继续上面的例子执行git diff master..slave
[email protected] MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ git status On branch slave 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: info.py Untracked files: (use "git add <file>..." to include in what will be committed) dd.css no changes added to commit (use "git add" and/or "git commit -a") [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ git add info.py warning: LF will be replaced by CRLF in info.py. The file will have its original line endings in your working directory. [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ git status On branch slave Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: info.py Untracked files: (use "git add <file>..." to include in what will be committed) dd.css [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ git commit -m "modify info.py" [slave 6abf028] modify info.py 1 file changed, 2 insertions(+), 1 deletion(-) [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ git status On branch slave Untracked files: (use "git add <file>..." to include in what will be committed) dd.css nothing added to commit but untracked files present (use "git add" to track) [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ git diff master..slave diff --git a/info.py b/info.py index 5c895f9..7e024db 100644 --- a/info.py +++ b/info.py @@ -1 +1,2 @@ -asdfsadfsfasfasdf \ No newline at end of file +asdfsadfsfasfasdf +modify 66666
[email protected] MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git branch
master
* slave
[email protected] MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git checkout master
Switched to branch ‘master‘
[email protected] MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git merge slave
Updating a068c80..6abf028
Fast-forward
info.py | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
[email protected] MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ cat info.py
asdfsadfsfasfasdf
modify 66666
关于合并冲突代码的做法:
下面我分别给master和slave创建文件new,其中master的内容是master,slave的new的内容是slave。
[email protected] MINGW64 /c/laoni/PycharmProjects/github_test (master) $ vim new [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master) $ cat new master [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master) $ git add new warning: LF will be replaced by CRLF in new. The file will have its original line endings in your working directory. [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master) $ git commit -m "add new" [master 8b5d2fd] add new 1 file changed, 1 insertion(+) create mode 100644 new [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master) $ git checkout slave Switched to branch ‘slave‘ [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ vim new [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ git add new warning: LF will be replaced by CRLF in new. The file will have its original line endings in your working directory. [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ git commit -m "add new" [slave 775f3ab] add new 1 file changed, 1 insertion(+) create mode 100644 new [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $
master slave
接下来要合并冲突代码:
PS:git返回信息提示有内容冲突文件new,让我们修改了new后再commit
[email protected] MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ git checkout master Switched to branch ‘master‘ [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master) $ git merge slave Auto-merging new CONFLICT (add/add): Merge conflict in new Automatic merge failed; fix conflicts and then commit the result.
等号上面是当前分支(master)的,等号下面是要被合并的分支(slave),我们修改这个文件,在冲突内容中选择一个,删除另外的,再commit就可以了。
$ git add new [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master|MERGING) $ git commit -m "modify new" [master 112da02] modify new [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master) $ git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) dd.css nothing added to commit but untracked files present (use "git add" to track) [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master) $ git log --oneline 112da02 modify new 775f3ab add new 8b5d2fd add new 6abf028 modify info.py a068c80 add info.py c5b475a Revert "add 123" cfcbd5c add 456 13f5bcb add 123 8110523 Revert "add adc" 74f7cb6 add bb.css 577fab6 Revert "revert abc" e1f2701 add adc 358cdac 添加UI.js 04c94a8 添加一个文件index.html
接下来有第三种合并方式,会保留合并分支的记录
$ git branch * master slave [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master) $ git checkout slave Switched to branch ‘slave‘ [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ ls 456 bb.css dd.css index.html info.py new [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ vim 998 [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ git add 998 warning: LF will be replaced by CRLF in 998. The file will have its original line endings in your working directory. [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ git commit -m "add 998" [slave 6957dae] add 998 1 file changed, 1 insertion(+) create mode 100644 998 [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ git checkout master Switched to branch ‘master‘ [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master) $ git merge slave --no-ff -m "merge slave --no--ff" Merge made by the ‘recursive‘ strategy. 998 | 1 + 1 file changed, 1 insertion(+) create mode 100644 998 [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master) $ git log --oneline bdba943 merge slave --no--ff 6957dae add 998 112da02 modify new 775f3ab add new 8b5d2fd add new 6abf028 modify info.py a068c80 add info.py c5b475a Revert "add 123" cfcbd5c add 456 13f5bcb add 123 8110523 Revert "add adc" 74f7cb6 add bb.css 577fab6 Revert "revert abc" e1f2701 add adc 358cdac 添加UI.js 04c94a8 添加一个文件index.html
总结:分支合并有三种:
1、faster快进的合并分支,这种最理想没文件内容冲突,但git log里查看不到合并记录。
2、冲突合并,修改冲突的文件再add和commit。
3、禁用faster,这种方式会记录合并分支记录,在git log里可以查到。