Git笔记 (11)

还可以更改其他本地指针来跟踪远程跟踪分支,下面通过实例来学习

$ git checkout -b foo origin/master Switched to a new branch 'foo' Branch 'foo' set up to track remote branch 'master' from 'origin'. $ echo "# if" >> test.rb $ git commit -am "commit test.rb" warning: LF will be replaced by CRLF in test.rb. The file will have its original line endings in your working directory. [foo d3f4109] commit test.rb 1 file changed, 1 insertion(+) $ git push origin HEAD:master Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 8 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 260 bytes | 86.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), completed with 1 local object. To github.com:FangYang970206/playground.git b849b25..d3f4109 HEAD -> master $ git checkout testing Switched to branch 'testing' Your branch is up to date with 'origin/testing'. $ git checkout -b testing1 origin/testing Switched to a new branch 'testing1' Branch 'testing1' set up to track remote branch 'testing' from 'origin'. $ echo "print("testing1")" >> README $ git commit -am "print testing1" warning: LF will be replaced by CRLF in README. The file will have its original line endings in your working directory. [testing1 2600339] print testing1 1 file changed, 1 insertion(+) $ git push origin HEAD: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), 373 bytes | 124.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To github.com:FangYang970206/playground.git f2466c5..2600339 HEAD -> testing $ git log --oneline --decorate --graph --all -9 * d3f4109 (origin/master, origin/HEAD, foo) commit test.rb * b849b25 (master) README | * 2600339 (HEAD -> testing1, origin/testing) print testing1 | * f2466c5 (testing) README |/ * dc6b322 solve conflicts |\ | * 1aaf545 Newtesting commit * | 3883017 master commit |/ * 972909a (newtesting) add print(3) into README * ce53a90 add MIT LICENSE

可以看到之前跟踪远程跟踪分支的master和testing指针是没有移动,新建的foo和testing1取代了它们。这样要注意一点,使用不同于远程分支名的分支进行push,一定要指定当前分支,不然默认还是通过branch_name推送到origin/branch_name,出现Everything up-to-date,指定可以通过以上的HEAD(source):testing(target)
这里的取代方式可以有两种:一种是直接新建分支取代,也就是上面的git checkout -b testing1(newbranch) origin/testing(Remote tracking branch),还有一种是通过现在分支进行取代,可以通过git branch -u <Remote tracking branch> <existed branch>命令,注意一点,不要出现远程跟踪分支和跟踪的指针出现分叉,这样会导致Your branch and 'origin/xxx' have diverged这个问题,详细可参考这个链接。

6.8 分支练习(强烈推荐)

强烈建议去https://learngitbranching.js.org/,这个网站有很多分支的练习,还有一部分是远程控制的练习,配合动画,非常适合正在学习git的朋友,相信可以让你的git本领上一个台阶。

7. pull request的使用

如果你发现你感兴趣的仓库的bug或者你想添加某个新功能到你感兴趣的仓库中(一般是先在Issue中提出想法或问题,沟通好后可以在对应编号上创建pull re,这时候你就可以pull request这个沟通利器了(一般是先在Issue中提出想法或问题,沟通好后可以在对应编号上创建pull request)。

这里介绍一下一个仓库,可供你进行pull request练习,仓库地址是https://github.com/Data4Democracy/github-playground,当然,也欢迎你对本文的仓库https://github.com/FangYang970206/playground进行pull request,由于对自己的仓库进行pull request,本质上就是本地合并再提交,所以没有必要,完全可以在本地进行,pull request是以协作开发为目的,为了方便,对https://github.com/Data4Democracy/github-playground进行pull request,看本篇文章的朋友就可以随意选择两者之一。

pull request流程:

先fork你感兴趣的仓库到自己的仓库中(副本)

将副本仓库克隆到本地

从master分支中创建一个新分支

在分支中进行修改,以此改进项目

将分支推送到github仓库

创建一个pull request

讨论,根据实际情况继续修改

项目的拥有者合并或关闭你的合并请求

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

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