git branch 分支和分支合并

一般一个项目有一个默认的分支 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里可以查到。

时间: 2024-11-01 20:56:12

git branch 分支和分支合并的相关文章

git branch分支开发

作者:zhanhailiang 日期:2015-01-06 查看当前branch列表 [[email protected]~/wade/git/billfeller.github.io]# git branch gh-pages * master 新建分支 [[email protected]~/wade/git/billfeller.github.io]# git branch dev 切换分支 [[email protected]~/wade/git/billfeller.github.io

GIT 分支管理:创建与合并分支、解决合并冲突

分支就是科幻电影里面的平行宇宙,当你正在电脑前努力学习Git的时候,另一个你正在另一个平行宇宙里努力学习SVN. 如果两个平行宇宙互不干扰,那对现在的你也没啥影响.不过,在某个时间点,两个平行宇宙合并了,结果,你既学会了Git又学会了SVN! 分支在实际中有什么用呢?假设你准备开发一个新功能,但是需要两周才能完成,第一周你写了50%的代码,如果立刻提交,由于代码还没写完,不完整的代码库会导致别人不能干活了.如果等代码全部写完再一次提交,又存在丢失每天进度的巨大风险. 现在有了分支,就不用怕了.你

Git:初始化项目、创建合并分支、回滚等常用方法总结

Git 初始化项目 创建新的Git仓库 echo "# git_project" >> README.md git init git add README.md git commit -m "first commit" git remote add origin https://github.com/cjunn/git_project.git git push -u origin master 提交记录 git add . 或者 git add test

[Git01]Pro Git 第三章 分支 读书笔记

[git]分支 Git 的分支模型称为“必杀技特性”,而正是因为它,将 Git 从版本控制系统家族里区分出来. Git 有何特别之处呢?Git 的分支可谓是难以置信的轻量级,它的新建操作几乎可以在瞬间完成,并且在不同分支间切换起来也差不多一样快. 和许多其他版本控制系统不同,Git 鼓励在工作流程中频繁使用分支与合并,哪怕一天之内进行许多次都没有关系. 理解分支的概念并熟练运用后,你才会意识到为什么 Git 是一个如此强大而独特的工具,并从此真正改变你的开发方式. Question1 :Git

git常用命令及分支简介

1.git基本命令 1)git add 将想要快照的内容写入缓存区 2)git status -s "AM" 状态的意思是,这个文件在我们将它添加到缓存之后又有改动 3)git commit -m '第一次版本提交' -m选项添加备注信息 4)git clone url 使用 git clone 拷贝一个 Git 仓库到本地 5)git diff 查看执行 git status 的结果的详细信息 尚未缓存的改动:git diff 查看已缓存的改动: git diff --cached

【Git】笔记4 分支管理1

1.创建与合并分支 一开始的时候,master分支是一条线,Git用master指向最新的提交,再用HEAD指向master,就能确定当前分支,以及当前分支的提交点: 每次提交,master分支都会向前移动一步,这样,随着你不断提交,master分支的线也越来越长: 当我们创建新的分支,例如dev时,Git新建了一个指针叫dev,指向master相同的提交,再把HEAD指向dev,就表示当前分支在dev上: 你看,Git创建一个分支很快,因为除了增加一个dev指针,改改HEAD的指向,工作区的文

git 入门教程之分支总览

分支就是一条独立的时间线,既有分支,必有主干,正如一棵树谈到树枝,必有树干一样的道理.我们先前对git 的全部操作默认都是在主干上进行的,这个主干也是一种特殊的分支,名为 master 分支. 无论是穿越历史还是撤销更改,我们都或多或少接触过时间线,git 管理的版本串在一起就组成了这个时间线,其中master 分支是当前分支,HEAD 指向master ,因此HEAD 相当于指向了最新的版本. 基于分支上的操作,每一次 commit 都会提交一个新版本,并且新的 commit 指向原来的 co

Git远程仓库及分支管理

问题一:配置完本地的git后,每次提交都需要我输入密码?  NND 解决办法: 再次用 ssh-keygen -t rsa -C "[email protected]"  生成密钥 其中,问你是否覆盖 输入y   ——   Overwrite (y/n)? y  覆盖之前的id-rsa 其余全部回车,千万不要在Enter passphrase (empty for no passphrase)输入密码了. 最后,在github的Settings的SSH and GPG keys中,点击

【git】idea /git bash命令 操作分支

1.需求 因为目前要对项目做一些改动,而项目又即将上线,这些新的改动又不需要一起上线,所以这个时候需要在原有的master分支上重新拉出一个分支进行开发. 2.分支操作 打开git bash工具→切换到本地项目所在目录(cd d:  / cd cxlwork  /  cd ..) 查看目前git状态: $ git status 查看所有分支 $ git branch -a 分支前带*号代表你目前所在的分支 查看本地分支 $ git branch 分支前带*号代表你目前所在的分支 查看远程分支 $