Git 同步

git 的分布式设计, 使得每个开发者的 local 都有 remote repo 的一份 copy , 我们在这份 copy 上提交, 创建新 branch, 最终, 我们会把我们在 local 的改动, 同步到远程的 repo; 当别人更新了远程 repo, 我们还要从远程 repo 同步这些改动到自己的 local.

这一系列的操作, 我们可以统称为 git 的同步, 同步发生在 local reporemote repo 之间, 这篇文章就详细说说同步的事情.


remote URL and shortcuts

要想跟 remote repo,我们自然要知道 repo 的地址,git repo 的地址是一个 URL

这个 URL,可能是一个文件系统路径,可能是一个 http 协议的地址,可能是一个 ssh 协议的地址

git 所有跟远程 repo 交互的命令,都需要提供这个 URL,但是这些 URL 往往都比较长,且难以记忆,给他们起一个名字( 也可以叫 shortcut ),通过这个 shortcut 来跟远程 repo 交互是一个可以想到的比较好的方式,实际上 git 也是这么做的,我们在 git clone 的时候,输入这个URLgit默认就为我们创建了这么一个shortcut,叫origin,这个 shortcutURL 对,组成了一个 remote connection

我们可以使用 git remote 来查看 local repo 目前有哪些 remote shortcut,使用git remote -v来查看shortcut详细对应的URL

除了 git 默认给我们创建的 shortcut,我们实际上可以用 git remote 创建新的 remote shortcut,这个命令其实可以管理 remote connection,增删改查样样拿手。


local repo 与 remote repo 同步

有了 remote shortcut,我们跟 remote repo 打交道就变得简单多了,在说同步之前, 还得说说 branch.

如果我们有了 remote connection, 那么我们在本地, 其实就有了 remote 的所有 branch, 可以通过git branch -a看到 remotes/开头的 branch, 使用 git clone 之后, 因为会默认创建origin这个 remote shortchut, 所以你会看到有 remotes/origin/master 这种 branch.

git 的同步,首先是将我们本地的 remote branch 更新, 然后再去 upload 或者是将 remote branch 的更新 mergelocal branch.

同步有 DownloadUpload 两个动作.

  • Download

    download 有两个命令, git fetchgit pull

    git pull 其实是 git fetchgit merge 的组合, 它更新 localremote branch 的代码, 并将改动 merge 到我们当前的 branch

    git fetch 则仅仅是将 remote 的改动更新到 localremote branch, 它比git pull更加安全.

  • Upload

    git push 来将我们 local branch 的改动提交到 remote. 当然它首先是将 local branch 的改动更新到 local remote branch, 然后再去将改动上传给 remote.

无论是 download 还是 upload, 我们自然都要指定 remote 的地址是什么, 因为我们创建了 remote shortcut, 所以我们可以直接使用诸如 origin 来操作, 下面是常用的一些操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#Download
git fetch —all # download 所有的 remotes
git fetch origin # download origin 这个 remote 的所有 branch

git pull —rebase # 建议使用 rebase 的方式来 merge 远程的代码到我们的 local branch
git pull origin master # 将 origin 这个 remote 的master 分支更新, 并 merge 到我们当前的 branch, 这个其实是`git pull`这个命令的默认动作.

#Upload
git push origin master #将我们本地的改动更新到 origin 这个 remote 的 master 分支. origin 是 push 命令的默认 remote

#remote 的管理
git remote add other_repo <other_repo_url> # 添加一个新的 remote, 叫 other_repo, 我们可以在后续的 git fetch other_repo, git push other_repo master 里来跟 othe_repo 这个 remote 来同步代码.
git remote add -f other_repo <other_repo_url> # 添加新的 remote, 并直接将这个 remote 的 repo fetch 到本地.
git remote rename other_repo feature_repo
git remote rm feature_reop

git remote show origin # 查看某个 remote shortcut 详细信息, 比如本地的哪个 branch 跟踪的是 remote 的哪个 branch

文章转载自 比克王国 Git同步

原文作者: dgb8901,yinxing

原文链接: https://www.itwork.club/2020/01/06/git-remote/

版权声明: 转载请注明出处

为您推荐

体验小程序「简易记账」

关注公众号「特想学英语」

什么是 Javascript 里的语句结束值(Statement Completion Value)?