从远程仓库获取

知识来源GitHub入门与实践

把GitHub上新建的仓库设置成远程仓库,并向这个仓库push了feature-D分支。现在能够访问这个远程仓库的人都可以获取feature-D分支并加以修改。

现在,在另一个目录下新建一个本地仓库,学习从远程仓库获取内容的相关操作。相当于我们刚刚执行过的push操作的目标仓库又有人来共同开发。

git clone ——获取远程仓库

  • 获取远程仓库

    首先我们切换到其他目录下,将GitHub上的仓库clone到本地,注意不要与之前是同一个目录。

    $ git clone [email protected]:JiangCheng97/git-tutorial.git
    Cloning into 'git-tutorial'...
    Enter passphrase for key '/c/Users/msi/.ssh/id_rsa':
    remote: Enumerating objects: 21, done.
    remote: Counting objects: 100% (21/21), done.
    remote: Compressing objects: 100% (12/12), done.
    Receiving objects: 100% (21/21), done.
    Resolving deltas: 100% (2/2), done.
    remote: Total 21 (delta 2), reused 21 (delta 2), pack-reused 0
    

    执行git clone命令之后我们会默认处于master分支下,同时系统会将origin设置成该远程仓库的标识符。也就是说,当前本地仓库的master分支与GitHub端远程仓库(origin)的master分支在内容上是完全相同的。

    $ git branch -a
    * master
      remotes/origin/HEAD -> origin/master
      remotes/origin/feature-D
      remotes/origin/master
    

    用git branch -a命令查看当前分支的相关信息。添加-a参数可以同时显示本地仓库和远程仓库的信息。

    结果显示 remotes/origin/feature-D,证明我们的远程仓库中已经有了feature-D分支了。

  • 获取远程的feature-D分支

    试着将feature-D分支获取到本地仓库

    $ git checkout -b feature-D origin/feature-D
    Switched to a new branch 'feature-D'
    Branch 'feature-D' set up to track remote branch 'feature-D' from 'origin'.
    

    -b参数后面的是本地仓库中新建的分支名称。为了便于理解,我们仍将其命名为feature-D。

    新建分支的名称后面是获取来源的分支名称。例子中指定了origin/feature-D,就是说明以origin仓库(这里指的是GitHub端仓库)

  • 向本地的feature-D分支提交更改

    现在,假定我们是另一名开发者,要做一个新的提交。在ReadMe.md文件中添加一行,查看更改。

    $ git diff
    diff --git a/README.md b/README.md
    index f1081b3..2be0c8d 100644
    --- a/README.md
    +++ b/README.md
    @@ -3,7 +3,6 @@
    
    
     - feature-A
    -
     - fix-B
    -
    -- feature-C
    \ No newline at end of file
    +- feature-C
    +- feature-D
    \ No newline at end of file
    
    

    按照之前的方式提交

    $ git add README.md
    
    $ git commit -m "feature-D"
    [feature-D eb040a8] feature-D
     1 file changed, 2 insertions(+), 3 deletions(-)
    
    
  • 推送feature-D分支

    $ git push
    Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
    Enter passphrase for key '/c/Users/msi/.ssh/id_rsa':
    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), 308 bytes | 308.00 KiB/s, done.
    Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    To github.com:JiangCheng97/git-tutorial.git
       1ac237d..eb040a8  feature-D -> feature-D
    
    

    从远程仓库获取feature-D分支,在本地仓库提交修改,再将feature-D分支推送回远程仓库,通过这一系列,就可以与其他开发者相互合作,共同培育feature-D分支,实现功能。

git pull ——获取最新的远程仓库分支

现在,我们放下刚刚操作的目录,回到原先那个目录下。这边的本地仓库只创建了feature-D分支,并没有在feature-D分支提交,而远程仓库的feature-D分支中已经有我们刚刚推送的提交。这时候我们就可以使用git pull命令,将本地的feature-D分支更新到最新的状态。当前分支为feature-D。

$ git pull origin feature-D
Enter passphrase for key '/c/Users/msi/.ssh/id_rsa':
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 288 bytes | 2.00 KiB/s, done.
From github.com:JiangCheng97/git-tutorial
 * branch            feature-D  -> FETCH_HEAD
   1ac237d..eb040a8  feature-D  -> origin/feature-D
Updating 1ac237d..eb040a8
Fast-forward
 README.md | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

GitHub端远程仓库中feature-D分支是最新状态,所以本地仓库中的feature-D分支就得到了更新。今后只需要像平常一样在本地提交再pull给远程仓库,就可以与其他开发者在同一分支中进行操作。

你可能感兴趣的:(从远程仓库获取)