Git笔记 (10)

可以看到最后出现了合并冲突,冲突出现在README文件中,我们需要打开文件看看冲突情况:

$ vi README print(1) print(2) print(3) <<<<<<< HEAD print(master) ======= print(newtesting) >>>>>>> newtesting

为了解决冲突,你必须选择使用由=======分割的两部分中的一个,或者你也可以自行合并这些内容。我们选择print(master),从<<<<<<<

$ git add README $ git status On branch master Your branch is ahead of 'origin/master' by 6 commits. (use "git push" to publish your local commits) All conflicts fixed but you are still merging. (use "git commit" to conclude merge) $ git commit -am "solve conflicts" [master dc6b322] solve conflicts $ git log --oneline --decorate --graph --all -8 * dc6b322 (HEAD -> master) solve conflicts |\ | * 1aaf545 (newtesting) Newtesting commit * | 3883017 master commit |/ * 972909a add print(3) into README * ce53a90 add MIT LICENSE * ebc9b45 add print(2) into README * 326dd0b add print(1) into README * b861d60 branch note begin 6.7 远程分支

还记得我们之前通过git push -u origin master命令推送我们地仓库吗?最后推送的地址是origin/master,这个叫做远程分支。其实在本地有一个origin/master的指针,这个叫做远程跟踪分支,用来跟踪远程分支(最后一次沟通)的状态,这个指针所指向的位置不会随着本地操作而发生改变,而当使用git fetch、git pull等命令会随着远程仓库的状态而改变。而本地的master指针是会默认追踪origin/master,这个追踪是从git clone或者git remote add那一刻起。

当你想要公开分享一个分支时,需要将其推送到有写入权限的远程仓库上。本地的分支并不会自动与远程仓库同步-你必须显式地推送想要分享的分支。这样,你就可以把不愿意分享的内容放到私人分支上,而将需要和别人协作的内容推送到公开分支。下面通过实例进行学习。

首先我们点开在GitHub创建的仓库——playground,然后如图所示,创建一个新分支testing,由于我已经创建,所以已经显示有testing分支。

image_1clisnd8c1hdqc6428ce4i10t7g8.png-18.7kB


我们在本地中使用git fetch命令,将刚刚创建的分支下载到本地。

$ git fetch From github.com:FangYang970206/playground * [new branch] testing -> origin/testing

可以看到多出新的分支testing(本地分支)跟踪origin/master(远程跟踪分支)。我们通过git checkout <branch_name>看到分支是否处于跟踪的状态。

$ git checkout master Switched to branch 'master' Your branch is up to date with 'origin/master'. $ git checkout testing Switched to a new branch 'testing' Branch 'testing' set up to track remote branch 'testing' from 'origin'.

现在分别对testing和master分支做一次提交并push

$ echo "print("testing")" >> README $ git commit -am README warning: LF will be replaced by CRLF in README. The file will have its original line endings in your working directory. [testing f2466c5] README 1 file changed, 1 insertion(+) $ git push origin testing Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 8 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 367 bytes | 183.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To github.com:FangYang970206/playground.git dc6b322..f2466c5 testing -> testing $ git checkout master Switched to branch 'master' Your branch is up to date with 'origin/master'. $ echo "print("master1")" >> README $ git commit -am README warning: LF will be replaced by CRLF in README. The file will have its original line endings in your working directory. [master b849b25] README 1 file changed, 1 insertion(+) $ git push origin master Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 8 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 360 bytes | 360.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To github.com:FangYang970206/playground.git dc6b322..b849b25 master -> master

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wppgpw.html