Git远程仓library

Git远程仓library is store in network on Git仓library, 用于团队协作 and codebackup. through远程仓library, many 个Development者可以共享 and 协作Development同一个project.

1. 常用 Git远程仓libraryservice

目 before has 许 many providingGit远程仓libraryservice 平台, 以 under is 最常用 几个:

1.1 GitHub

GitHub is 全球最 big Git远程仓library托管平台, 拥 has big 量open-sourceproject and 活跃 Development者community. 它providing了code托管, issues跟踪, CI/CDetc.丰富 functions.

1.2 GitLab

GitLab is a open-source Git仓librarymanagement平台, providing了code托管, CI/CD, containers仓libraryetc.functions. It supports自托管deployment, 适合企业 in 部using.

1.3 Gitee (码云)

Gitee is 国 in Git仓library托管平台, 访问速度 fast , 适合国 in Development者using. 它providing了code托管, code审查, CI/CDetc.functions.

1.4 Bitbucket

Bitbucket is Atlassian公司推出 Git仓library托管平台, and Jira, Confluenceetc.Atlassian产品集成良 good , 适合usingAtlassiantool链 团队.

2. clone远程仓library

using git clone commands可以将远程仓libraryclone to 本地, creation一个本地replica.

# clone远程仓library (HTTPS方式) git clone https://github.com/username/repository.git # clone远程仓library (SSH方式) git clone git@github.com:username/repository.git # clone to 指定Table of Contents git clone https://github.com/username/repository.git my-project

HTTPS vs SSH

  • HTTPS: usinguser名 and password or 访问tokenforauthentication, 适合公共仓library or 临时using.
  • SSH: usingSSHkeyforauthentication, 更security, 更方便, 适合 long 期using.

3. 关联远程仓library

such as果已经 has 了本地Git仓library, 可以using git remote add commands将其关联 to 远程仓library.

# 关联远程仓library git remote add origin https://github.com/username/repository.git # 查看已关联 远程仓library git remote -v

执行 git remote -v commands after , 你会看 to class似以 under 输出:

origin https://github.com/username/repository.git (fetch) origin https://github.com/username/repository.git (push)

4. push本地branch to 远程仓library

using git push commands可以将本地branch submittingpush to 远程仓library.

# push当 before branch to 远程仓library (首次push) git push -u origin master # push当 before branch to 远程仓library ( after 续push) git push # push指定branch to 远程仓library git push origin feature-branch # push所 has branch to 远程仓library git push --all origin # pushtag to 远程仓library git push --tags origin

关于 -u parameter

-u parameter用于将本地branch and 远程branch关联起来, 这样 after 续push时就可以直接using git push commands, 无需指定远程仓library and branch.

5. from 远程仓librarypullcode

using git pull commands可以 from 远程仓librarypull最 new code并merge to 本地branch.

# from 远程仓librarypull并merge to 当 before branch git pull # from 指定远程仓library and branchpull git pull origin master

5.1 git pull vs git fetch

git pull commands相当于 git fetch + git merge, 它会先 from 远程仓library获取最 new code, 然 after 自动merge to 本地branch.

git fetch commands只会 from 远程仓library获取最 new code, 但不会自动merge, 需要手动执行 git merge commandsformerge.

# from 远程仓library获取最 new code git fetch origin # 查看远程branch git branch -r # 查看所 has branch (本地+远程) git branch -a # 将远程branchmerge to 本地branch git merge origin/master

6. 查看远程仓libraryinformation

using git remote commands可以查看 and management远程仓library.

# 查看已关联 远程仓library git remote # 查看远程仓library 详细information git remote -v # 查看指定远程仓library information git remote show origin

7. modify远程仓libraryURL

such as果远程仓library URL发生变化, 可以using git remote set-url commandsmodify本地关联 远程仓libraryURL.

# modify远程仓libraryURL git remote set-url origin https://new-url.com/repository.git # verificationmodify is 否成功 git remote -v

8. delete远程仓library关联

using git remote remove commands可以delete本地 and 远程仓library 关联.

# delete远程仓library关联 git remote remove origin

9. push本地tag to 远程仓library

using git push commands可以将本地tagpush to 远程仓library.

# push指定tag to 远程仓library git push origin v1.0.0 # push所 has tag to 远程仓library git push --tags origin # push所 has tag and branch to 远程仓library git push --all --tags origin

10. delete远程branch or tag

using git push commands可以delete远程branch or tag.

# delete远程branch git push origin --delete feature-branch # 另一种delete远程branch 方式 git push origin :feature-branch # delete远程tag git push origin --delete v1.0.0 # 另一种delete远程tag 方式 git push origin :refs/tags/v1.0.0

实践case: usingGitHubmanagementproject

  1. in GitHub on creation一个 new 仓library
  2. clone远程仓library to 本地: git clone https://github.com/username/repository.git
  3. 进入本地仓libraryTable of Contents: cd repository
  4. creation一个README.mdfile: echo "# My Project" > README.md
  5. submitting更改: git add README.md && git submitting -m "Initial submitting"
  6. push更改 to 远程仓library: git push
  7. in GitHub on 查看仓library, 确认更改已push成功
  8. in GitHub on modifyREADME.mdfile
  9. in 本地pull最 new 更改: git pull
  10. 查看本地file, 确认已pull to GitHub on modify

互动练习

请completion以 under 练习, 巩固Git远程仓libraryoperation:

  1. in GitHub/GitLab/Gitee on creation一个 new 远程仓library
  2. 将本地已 has Git仓library关联 to new creation 远程仓library
  3. push本地所 has branch and tag to 远程仓library
  4. using git remote -v 查看远程仓libraryinformation
  5. clone刚刚creation 远程仓library to 另一个本地Table of Contents
  6. in clone 仓libraryinmodify一个file并submitting
  7. pushmodify to 远程仓library
  8. in 原本地仓libraryinpull最 new 更改
  9. creation一个 new 本地branch, submitting一些更改, 然 after push to 远程仓library
  10. delete远程仓libraryin testbranch

completion练习 after , 你应该able to熟练MasterGit远程仓library basicoperation!