美文网首页Git简单应用
Git 学习笔记(远程仓库篇)

Git 学习笔记(远程仓库篇)

作者: TW安洋 | 来源:发表于2016-12-04 21:21 被阅读64次

    创建远程仓库

    登陆 GitHub,然后选择 Repositories 并点击 New,输入 Repository name, 点击 Create repository 即可创建一个新的远程仓库。

    create repository.png repositorydetails.png LearnGit.png

    Clone远程仓库到本地

    git clone git@github.com:anyangxaut/LearnGit.git

    效果图如下所示:

    $ git clone git@github.com:anyangxaut/LearnGit.git
    Cloning into 'LearnGit'...
    remote: Counting objects: 3, done.
    remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    Receiving objects: 100% (3/3), done.
    Checking connectivity... done.

    clonerepository.png

    将一个已有的本地仓库与 github 仓库进行关联

    git remote add origin git@github.com:anyangxaut/LearnGit.git

    远程仓库的默认名称是origin。

    把代码 push 到远程仓库

    git push -u origin master

    新建远程仓库并第一次推送 master 分支时,加上 -u 参数,Git 不但会把本地的 master 分支内容推送的远程新的 master 分支,还会把本地的 master 分支和远程的 master 分支关联起来,此后提交代码可以省去该参数,即git push origin master

    $ git push -u origin master
    Counting objects: 3, done.
    Delta compression using up to 8 threads.
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (3/3), 294 bytes | 0 bytes/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To git@github.com:anyangxaut/LearnGit.git
    b9df407..77391b3 master -> master
    Branch master set up to track remote branch master from origin.

    查看远程仓库信息

    git remote -v

    效果图如下所示:

    $ git remote -v
    origin git@github.com:anyangxaut/LearnGit.git (fetch)
    origin git@github.com:anyangxaut/LearnGit.git (push)

    把本地的某个 branch push 到远程仓库

    git push origin <branch name>

    效果如下:

    $ git branch newbranch
    $ git branch
    * master
    newbranch
    $ git push origin newbranch
    Total 0 (delta 0), reused 0 (delta 0)
    To git@github.com:anyangxaut/LearnGit.git
    * [new branch] newbranch -> newbranch

    pushbranch.png

    获取远程修改到本地

    git pull

    效果如下所示:

    $ git pull
    remote: Counting objects: 4, done.
    remote: Compressing objects: 100% (3/3), done.
    remote: Total 4 (delta 0), reused 3 (delta 0), pack-reused 0
    Unpacking objects: 100% (4/4), done.
    From github.com:anyangxaut/LearnGit
    77391b3..196661a master -> origin/master
    77391b3..de923a8 newbranch -> origin/newbranch
    Updating 77391b3..196661a
    Fast-forward
    README.md | 2 ++
    1 file changed, 2 insertions(+)

    本地创建并切换到远程分支对应的分支

    当我们在github中创建了一个新的分支originbranch后,本地创建并切换到远程分支对应的分支时我们会输入以下命令:

    git remote update
    git fetch
    git checkout -b branch-name origin/branch-name

    效果如下所示:

    $ git branch
    * master
    newbranch
    $ git remote show origin
    * remote origin
    Fetch URL: git@github.com:anyangxaut/LearnGit.git
    Push URL: git@github.com:anyangxaut/LearnGit.git
    HEAD branch: master
    Remote branches:
    master tracked
    newbranch tracked
    originbranch new (next fetch will store in remotes/origin)
    Local branch configured for 'git pull':
    master merges with remote master
    Local refs configured for 'git push':
    master pushes to master (up to date)
    newbranch pushes to newbranch (local out of date)
    $ git remote update
    Fetching origin
    From github.com:anyangxaut/LearnGit
    * [new branch] originbranch -> origin/originbranch
    $ git fetch
    $ git checkout -b originbranch origin/originbranch
    Branch originbranch set up to track remote branch originbranch from origin.
    Switched to a new branch 'originbranch'
    $ git branch
    master
    newbranch
    * originbranch

    相关资料:

    1. Git 官网
    2. Git 官方文档
    3. [廖雪峰的 Git 教程](http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8
      067c8c017b000)
    4. Git 常用命令查询文档
    5. Git 在线学习网址
    6. http://stackoverflow.com/questions/945654/git-checkout-updating-paths-is-incompatible-with-switching-branches

    相关文章

      网友评论

      • 6d96978eeefb:好长好用心,就是看的有点头晕。。。
        TW安洋: @TW李鹏 内容有点多哈,我再改进改进,缩短篇幅,尽力描述清楚。之前没太写过博客,大家可以多提提意见,谢谢!😃

      本文标题:Git 学习笔记(远程仓库篇)

      本文链接:https://www.haomeiwen.com/subject/tkfcmttx.html