创建远程仓库
登陆 GitHub,然后选择 Repositories 并点击 New,输入 Repository name, 点击 Create repository 即可创建一个新的远程仓库。
create repository.png repositorydetails.png LearnGit.pngClone远程仓库到本地
git clone git@github.com:anyangxaut/LearnGit.git
效果图如下所示:
$ git clone git@github.com:anyangxaut/LearnGit.git
clonerepository.png
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.
将一个已有的本地仓库与 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>
效果如下:
pushbranch.png$ 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
获取远程修改到本地
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
网友评论