git 分支创建、切换、合并 以及合并冲突的解决

发布时间 2023-08-01 08:26:22作者: huidaoqingdao
shj@MSI MINGW64 /d/code_lib/git-demo
$ git init
Initialized empty Git repository in D:/code_lib/git-demo/.git/

shj@MSI MINGW64 /d/code_lib/git-demo (master)
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        hello.txt

nothing added to commit but untracked files present (use "git add" to track)

shj@MSI MINGW64 /d/code_lib/git-demo (master)
$ git add hello.txt

shj@MSI MINGW64 /d/code_lib/git-demo (master)
$ git commit -m "first commit" hello.txt
[master (root-commit) 50a91ab] first commit
 1 file changed, 10 insertions(+)
 create mode 100644 hello.txt

shj@MSI MINGW64 /d/code_lib/git-demo (master)
$ git reflog
50a91ab (HEAD -> master) HEAD@{0}: commit (initial): first commit

shj@MSI MINGW64 /d/code_lib/git-demo (master)
$ git branch hot-fix

shj@MSI MINGW64 /d/code_lib/git-demo (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'

shj@MSI MINGW64 /d/code_lib/git-demo (hot-fix)
$ git add hello.txt

shj@MSI MINGW64 /d/code_lib/git-demo (hot-fix)
$ git commit -m "hot-fix first commit" hello.txt
[hot-fix 8596051] hot-fix first commit
 1 file changed, 1 insertion(+), 1 deletion(-)

shj@MSI MINGW64 /d/code_lib/git-demo (hot-fix)
$ git checkout master
Switched to branch 'master'

shj@MSI MINGW64 /d/code_lib/git-demo (master)
$ git add hello.txt

shj@MSI MINGW64 /d/code_lib/git-demo (master)
$ git commit -m "master second commit" hello.txt
[master 3cffece] master second commit
 1 file changed, 1 insertion(+), 1 deletion(-)

shj@MSI MINGW64 /d/code_lib/git-demo (master)
$ git status
On branch master
nothing to commit, working tree clean

shj@MSI MINGW64 /d/code_lib/git-demo (master)
$ git merge hot-fix
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.

shj@MSI MINGW64 /d/code_lib/git-demo (master|MERGING)
$

建立分支hot-fix后分别对master和hot-fix两个分支中的hello.txt文件做出修改,导致合并冲突如下,此时需要手动修改

 手动修改完成之后,需要git add 和 git commit (注意不要带hello.txt文件名)

shj@MSI MINGW64 /d/code_lib/git-demo (master|MERGING)
$ git add hello.txt

shj@MSI MINGW64 /d/code_lib/git-demo (master|MERGING)
$ git commit -m "commit after merge" hello.txt
fatal: cannot do a partial commit during a merge.

shj@MSI MINGW64 /d/code_lib/git-demo (master|MERGING)
$ git commit -m "commit after merge"
[master 67fe1cf] commit after merge

shj@MSI MINGW64 /d/code_lib/git-demo (master)