This course is for anyone who wants to learn Git. No prior experience is required.
创建并切换当前分支为bugfix-signup-form分支之后将该文件的内容改为:
1 2 3
WHO THIS COURSE IS FOR ====================== This course is for anyone who wants to learn Git.
并提交更改:
1 2 3 4 5 6 7
(bugfix-signup-form)$ code audience.txt
(bugfix-signup-form)$ git add .
(bugfix-signup-form)$ git commit -m "fix the bug that prevented the users from signing up" [bugfix-signup-form 7a5cdb3] fix the bug that prevented the users from signing up 1 file changed, 3 insertions(+), 4 deletions(-)
查看一下,HEAD指向当前分支:
1 2 3
(bugfix-signup-form)$ git log --oneline 7a5cdb3 (HEAD -> bugfix-signup-form) fix the bug that prevented the users from signing up 82ca774 (master) add audience.txt
(master)$ git log --oneline --all 7a5cdb3 (bugfix-signup-form) fix the bug that prevented the users from signing up 82ca774 (HEAD -> master) add audience.txt
(master)$ git branch -d bugfix-signup-form error: The branch 'bugfix-signup-form' is not fully merged. If you are sure you want to delete it, run 'git branch -D bugfix-signup-form'.
fix the bug that prevented the users from signing up
查看具体改了啥内容(由于当前分支就是master, master..也可以省略不写):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
(master)$ git diff master..bugfix-signup-form diff --git a/audience.txt b/audience.txt index 01fd2e1..ecbc229 100644 --- a/audience.txt +++ b/audience.txt @@ -1,4 +1,3 @@ -AUDIENCE - -This course is for anyone who wants to learn Git. -No prior experience is required. \ No newline at end of file +WHO THIS COURSE IS FOR +====================== +This course is for anyone who wants to learn Git. \ No newline at end of file
看简略的修改状态信息:
1 2
(master)$ git diff --name-status bugfix-signup-form M audience.txt
(master)$ git switch bugfix-signup-form error: Your local changes to the following files would be overwritten by checkout: audience.txt Please commit your changes or stash them before you switch branches. Aborting
(bugfix-login-form)$ git log --oneline --all --graph * 1ad00a7 (HEAD -> bugfix-login-form) update file1.js * 7a5cdb3 (master, bugfix-signup-form) fix the bug that prevented the users from signing up * 82ca774 add audience.txt
此时master和bugfix-signup-form指向同一commit,bugfix-login-form 指向本 modify commit。然后用 No fast-forward 的方式来合并两分支,可以看到 git merge 命令的返回结果中不再有“Fast-forward”出现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
(bugfix-login-form)$ git switch master Switched to branch 'master'
(master)$ git merge --no-ff bugfix-login-form Merge made by the 'recursive' strategy. file1.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
(master)$ git log --oneline --all --graph * 9612ab4 (HEAD -> master) Merge branch 'bugfix-login-form' |\ | * 1ad00a7 (bugfix-login-form) update file1.js |/ * 7a5cdb3 (bugfix-signup-form) fix the bug that prevented the users from signing up * 82ca774 add audience.txt
1 2 3 4
/**使用No fast-forward merges的优缺点(两派观点)**/ 👉 CONS:pollutes the history (those who prefer linear history) 👉 PROS:1) True reflection of history 2) Allow reverting a feature (easier for us to undo a feature)
(master)$ git merge bugfix-change-password merge: bugfix-change-password - not something we can merge
(master)$ git merge bugfix-change-password.txt Auto-merging change-password.txt CONFLICT (content): Merge conflict in change-password.txt Automatic merge failed; fix conflicts and then commit the result.
(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: //---------merge失败的原因所在------------------ (use "git add <file>..." to mark resolution) both modified: change-password.txt
no changes added to commit (use "git add" and/or "git commit -a")
看看文件:
VSCode提供了几种(可以直接点击的):Accept Current Change等三种方法消除conflict
也可以手动改,但此时不能增加新的内容,只能选择两个分支的修改内容。最后改为:
1 2 3 4
hello
change in the master branch. change in the bugfix branch.
这回就可以成功提交:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
(master|MERGING)$ git add change-password.txt
(master|MERGING)$ git status On branch master All conflicts fixed but you are still merging. (use "git commit" to conclude merge)
Changes to be committed: modified: change-password.txt (master|MERGING)$ git commit [master 74c5e1c] Merge branch 'bugfix-change-password.txt'
(master)$ git commit -m "Fix the bug on the photo upload page" [master 3a99538] Fix the bug on the photo upload page 2 files changed, 2 insertions(+), 2 deletions(-)
(feature-shopping-cart)$ git rebase master error: could not apply 06ee042... add mountain to toc.txt Resolve all conflicts manually, mark them as resolved with "git add/rm <conflicted_files>", then run "git rebase --continue". You can instead skip this commit: run "git rebase --skip". To abort and get back to the state before "git rebase", run "git rebase --abort". Could not apply 06ee042... add mountain to toc.txt Auto-merging toc.txt CONFLICT (content): Merge conflict in toc.txt
Normal merge conflict for'toc.txt': {local}: modified file {remote}: modified file
(feature-shopping-cart|REBASE 1/1)$ git rebase --abort
toc.txt.orig 是解决冲突的时候产生的额外文件,删掉就好了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
(feature-shopping-cart)$ git status On branch feature-shopping-cart Untracked files: (use "git add <file>..." to include in what will be committed) toc.txt.orig
nothing added to commit but untracked files present (use "git add" to track)
(feature-shopping-cart)$ cat toc.txt.orig <<<<<<< HEAD ocean ======= mountain >>>>>>> 06ee042 (add mountain to toc.txt)
(master)$ git cherry-pick 06ee042 Auto-merging toc.txt CONFLICT (content): Merge conflict in toc.txt error: could not apply 06ee042... add mountain to toc.txt hint: after resolving the conflicts, mark the corrected paths hint: with 'git add <paths>' or 'git rm <paths>' hint: and commit the result with 'git commit'