git分支commitId介绍
HEAD—>[当前分支最新的commitId]
git checkout -[b|branch] <branchname>
示例:
hero@~ /d/git_study (master)
$ git status
On branch master
nothing to commit, working tree clean
创建分支
hero@~ /d/git_study (master)
$ git checkout -b dev_git_study
Switched to a new branch 'dev_git_study'
创建成功后切换到当前新创建的分支
hero@~ /d/git_study (dev_git_study)
$
git branch
示例:
hero@~ /d/git_study (dev_git_study)
$ git branch
* dev_git_study
master
$ git checkout <branchname>
示例:
hero@~ /d/git_study (dev_git_study)
$ git checkout master
Switched to branch 'master'
hero@~ /d/git_study (master)
$ git branch
dev_git_study
* master
hero@~ /d/git_study (master)
git branch -[d|delete] <branchname>
hero@~ /d/git_study (master)
$ git checkout dev_git_study
Switched to branch 'dev_git_study'
hero@~ /d/git_study (dev_git_study)
$ git branch -d dev_git_study
error: Cannot delete branch 'dev_git_study' checked out at 'D:/git_study'
hero@~ /d/git_study (dev_git_study)
hero@~ /d/git_study (dev_git_study)
$ git checkout master
Switched to branch 'master'
hero@~ /d/git_study (master)
$ git branch -d dev_git_study
Deleted branch dev_git_study (was 3e2f563).
hero@~ /d/git_study (master)
切换其他分支,删除分支(分支已修改,但并未合并到master分支),如何删除呢?,
创建新分支
hero@~ /d/git_study (master)
$ git checkout -b dev_study
Switched to a new branch 'dev_study'
更新mylog.txt文件
hero@~ /d/git_study (dev_study)
$ git status
On branch dev_study
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: mylog.txt
no changes added to commit (use "git add" and/or "git commit -a")
将修改文件添加到暂存区
hero@~ /d/git_study (dev_study)
$ git add mylog.txt
将修改内容提交到当前分支
hero@~ /d/git_study (dev_study)
$ git commit -m "create branch dev_study and update mylog.txt "
[dev_study b5d3c35] create branch dev_study and update mylog.txt
1 file changed, 3 insertions(+), 1 deletion(-)
切换到主分支
hero@~ /d/git_study (dev_study)
$ git checkout master
Switched to branch 'master'
删除dev_study
hero@~ /d/git_study (master)
$ git branch -d dev_study
error: The branch 'dev_study' is not fully merged.
If you are sure you want to delete it, run 'git branch -D dev_study'.
使用-D强制删除分支
hero@~ /d/git_study (master)
$ git branch -D dev_study
Deleted branch dev_study (was b5d3c35).
hero@~ /d/git_study (master)
查看分支情况,发现dev_study分支已经删除
hero@~ /d/git_study (master)
$ git branch
* master
注:使用
-D
参数强制删除分支,强制删除会导致当前删除的分支数据丢失
hero@~ /d/git_study (master)
$ git branch
* master
hero@~ /d/git_study (master)
$ git checkout -b dev_study_1
Switched to a new branch 'dev_study_1'
hero@~ /d/git_study (dev_study_1)
$ git branch -d master
Deleted branch master (was 3e2f563).
hero@~ /d/git_study (dev_study_1)
$ git branch
* dev_study_1
hero@~ /d/git_study (master)
$ git add mylog.txt
hero@~ /d/git_study (master)
$ git commit -m "add sth"
[master f2e6f14] add sth
1 file changed, 3 insertions(+), 1 deletion(-)
输出当前分支git log
hero@~ /d/git_study (master)
$ git log --pretty=oneline
f2e6f140b1d001db4730f8670a09c1e0fc5ec5b5 (HEAD -> master) add sth
3e2f563556ec6f0a023c8a5ddd7c7e143d8a4295 (dev_study_1) add sth bbb
a8787fd979b5f809f8ad9218ce30a66b3e257db7 add sth aaa
745d82dd7419e9f916fd82b2d594a0b6602e9ff0 add mylog.txt
hero@~ /d/git_study (master)
$ git checkout dev_study_1
Switched to branch 'dev_study_1'
hero@~ /d/git_study (dev_study_1)
$ git add mylog.txt
hero@~ /d/git_study (dev_study_1)
$ git commit -m "add sth to dev_study_1"
[dev_study_1 9874739] add sth to dev_study_1
1 file changed, 3 insertions(+), 1 deletion(-)
输出当前分支git log
hero@~ /d/git_study (dev_study_1)
$ git log --pretty=oneline
9874739134e850db8a3dc4f35c0c71bd8699c858 (HEAD -> dev_study_1) add sth to dev_study_1
3e2f563556ec6f0a023c8a5ddd7c7e143d8a4295 add sth bbb
a8787fd979b5f809f8ad9218ce30a66b3e257db7 add sth aaa
745d82dd7419e9f916fd82b2d594a0b6602e9ff0 add mylog.txt
hero@~ /d/git_study (dev_study_1)
注:每个分支都有属于自己的git log
git merge <branchname>
切换到目标分支,然后将当前需要合并的分支进行合并。如,将
dev_study_1
合并到master
上,master
分支为目标分支,dev_study_1
为需要合并的分支
hero@~ /d/git_study (dev_study_2)
$ git checkout master
Switched to branch 'master'
hero@~ /d/git_study (master)
$ git checkout -b dev_study_3
Switched to a new branch 'dev_study_3'
hero@~ /d/git_study (dev_study_3)
$ git status
On branch dev_study_3
nothing to commit, working tree clean
hero@~ /d/git_study (dev_study_3)
$ git status
On branch dev_study_3
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: mylog.txt
no changes added to commit (use "git add" and/or "git commit -a")
hero@~ /d/git_study (dev_study_3)
$ git add mylog.txt
hero@~ /d/git_study (dev_study_3)
$ git commit -m "add sth on dev_study_3"
[dev_study_3 e93a1b8] add sth on dev_study_3
1 file changed, 6 insertions(+), 6 deletions(-)
hero@~ /d/git_study (dev_study_3)
$ git checkout master
Switched to branch 'master'
## 将dev_study_3合并到master分支
hero@~ /d/git_study (master)
$ git merge dev_study_3
Updating 5f63744..e93a1b8
Fast-forward
mylog.txt | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
## 查看合并后的状态
hero@~ /d/git_study (master)
$ git status
On branch master
nothing to commit, working tree clean
## 可以省略此步骤
hero@~ /d/git_study (master)
$ git add mylog.txt
hero@~ /d/git_study (master)
$ git commit -m "merge dev_study_3 to master no conflict"
On branch master
nothing to commit, working tree clean
hero@~ /d/git_study (master)
$ git log --pretty=oneline
e93a1b870c2145e67a3abd852785b00ddc471c84 (HEAD -> master, dev_study_3) add sth on dev_study_3
5f637442951dd8d809e6678ef59af559251b59b4 (dev_study_2) add sth on dev_study_2
98b103a074e3a57eb65a99c147f31d13ddaec2df merge dev_study_1
859c65896118c50dec60f86dc91a0ae3f234cc4a add sth on dev_study_2
2e8779fdc67f8de2899740fcd4112539762e07e8 (dev_study_1) add sth on dev_study_1
9874739134e850db8a3dc4f35c0c71bd8699c858 add sth to dev_study_1
f2e6f140b1d001db4730f8670a09c1e0fc5ec5b5 add sth
3e2f563556ec6f0a023c8a5ddd7c7e143d8a4295 add sth bbb
a8787fd979b5f809f8ad9218ce30a66b3e257db7 add sth aaa
745d82dd7419e9f916fd82b2d594a0b6602e9ff0 add mylog.txt
hero@~ /d/git_study (master)
示例:
### 查看分支信息
hero@~ /d/git_study (dev_study_1)
$ git branch
* dev_study_1
master
## 查看分支状态信息
hero@~ /d/git_study (dev_study_1)
$ git status
On branch dev_study_1
nothing to commit, working tree clean
## 查看分支git提交记录
hero@~ /d/git_study (dev_study_1)
$ git log --pretty=oneline
9874739134e850db8a3dc4f35c0c71bd8699c858 (HEAD -> dev_study_1) add sth to dev_study_1
3e2f563556ec6f0a023c8a5ddd7c7e143d8a4295 add sth bbb
a8787fd979b5f809f8ad9218ce30a66b3e257db7 add sth aaa
745d82dd7419e9f916fd82b2d594a0b6602e9ff0 add mylog.txt
## 创建分支dev_study_2
hero@~ /d/git_study (dev_study_1)
$ git checkout -b dev_study_2
Switched to a new branch 'dev_study_2'
M mylog.txt
## 查看分支
hero@~ /d/git_study (dev_study_2)
$ git branch
dev_study_1
* dev_study_2
master
## 切换分支dev_study_1
hero@~ /d/git_study (dev_study_2)
$ git checkout dev_study_1
Switched to branch 'dev_study_1'
M mylog.txt
## 在分支dev_study_1 上修改内容
hero@~ /d/git_study (dev_study_1)
$ git status
On branch dev_study_1
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: mylog.txt
no changes added to commit (use "git add" and/or "git commit -a")
## 将分支dev_study_1 上修改内容 保存到版本库中的暂存区
hero@~ /d/git_study (dev_study_1)
$ git add mylog.txt
## 将分支dev_study_1 上修改内容 提交
hero@~ /d/git_study (dev_study_1)
$ git commit -m "add sth on dev_study_1"
[dev_study_1 2e8779f] add sth on dev_study_1
1 file changed, 3 insertions(+), 1 deletion(-)
## 切换到主分支
hero@~ /d/git_study (dev_study_2)
$ git checkout master
Switched to branch 'master'
## 查看主分支状态
hero@~ /d/git_study (master)
$ git status
On branch master
nothing to commit, working tree clean
## 将dev_study_1合并到master分支上
hero@~ /d/git_study (master)
$ git merge dev_study_1
Auto-merging mylog.txt
CONFLICT (content): Merge conflict in mylog.txt
Automatic merge failed; fix conflicts and then commit the result.
上述合并过程中,发现了冲突,如何解决呢?1、取消分支合并,2、解决合并冲突
aaaaa
bbbb
<<<<<<< HEAD
ddddd
=======
gggg
dev_study_1 update
>>>>>>> dev_study_1
取消分支示例:
hero@~ /d/git_study (master|MERGING)
$ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: mylog.txt
no changes added to commit (use "git add" and/or "git commit -a")
hero@~ /d/git_study (master|MERGING)
$ git merge --abort
hero@~ /d/git_study (master)
$ git status
On branch master
nothing to commit, working tree clean
hero@~ /d/git_study (master)
$
取消合并后文件内容:
aaaaa
bbbb
ddddd
示例:
aaaaa
bbbb
<<<<<<< HEAD
ddddd
=======
gggg
dev_study_1 update
>>>>>>> dev_study_1
<<<<<<< HEAD
到=======
之间的内容为目标分支内容,=======
到 合并分支>>>>>>> dev_study_1
为即将合并的分支内容。选择需要的内容,去掉<<<<<<< HEAD
和=======
以及>>>>>>> dev_study_1
即可。如下所示:
如:
aaaaa
bbbb
ddddd
gggg
dev_study_1 update
git代码演示
## 直接提交合并后的内容
hero@~ /d/git_study (master|MERGING)
$ git commit -m "merge dev_study_1"
error: Committing is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
U mylog.txt
hero@~ /d/git_study (master|MERGING)
$ git add mylog.txt
hero@~ /d/git_study (master|MERGING)
$ git commit -m "merge dev_study_1"
[master 98b103a] merge dev_study_1
## 查看git提交记录
hero@~ /d/git_study (master)
$ git log --pretty=oneline
98b103a074e3a57eb65a99c147f31d13ddaec2df (HEAD -> master) merge dev_study_1
2e8779fdc67f8de2899740fcd4112539762e07e8 (dev_study_1) add sth on dev_study_1
9874739134e850db8a3dc4f35c0c71bd8699c858 add sth to dev_study_1
f2e6f140b1d001db4730f8670a09c1e0fc5ec5b5 add sth
3e2f563556ec6f0a023c8a5ddd7c7e143d8a4295 add sth bbb
a8787fd979b5f809f8ad9218ce30a66b3e257db7 add sth aaa
745d82dd7419e9f916fd82b2d594a0b6602e9ff0 add mylog.txt
hero@~ /d/git_study (master)
以上介绍分支的基本操作。