保留git存储库的多个只读副本

Joh*_*nie 4 git

我在github.com上有一个存储库,我git下载到我的工作站,编辑,提交然后git将更改推回到github.一切正常.

但我经常使用例如克隆来自github.com的只读副本:

git clone git://github.com/jhsrennie/Test.git Test-ro
Run Code Online (Sandbox Code Playgroud)

通常这是因为我有临时测试安装,我不会开发,但我需要检查代码构建和运行.我的github存储库的副本可以是只读的,因为没有任何更改需要推回到github.麻烦的是,当我使用以下命令从github下载更改到我的只读副本时:

git pull origin master
Run Code Online (Sandbox Code Playgroud)

我现在发现git status显示如下:

$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#   (use "git push" to publish your local commits)
Run Code Online (Sandbox Code Playgroud)

我无法推动更改,我不想反正,但是未完成的提交会导致我出现问题,例如我无法查看新的远程分支.我最终不得不删除我的只读存储库,并在每次需要更新它时重新克隆它.

如何从github.com更新我的github存储库的只读副本,而不会让他们认为我需要推送提交?

或者,我是以愚蠢的方式做到这一点,是否有更好的方法来实现我的目标?

回应Cupcake的回答

这对我来说似乎很奇怪,而且显然我缺少了一些东西.这是如何重现问题:

  • 使用以下命令克隆存储库: git clone git@github.com:jhsrennie/Test.git Test-rw
  • 克隆只读副本: git clone git://github.com/jhsrennie/Test.git Test-ro
  • 进行更改Test-rw,然后git addgit commit

git status 现在显示(正如您所期望的):

renniej@RATHAUS /d/Dev/GIT/Test-rw (master)
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
Run Code Online (Sandbox Code Playgroud)

最后我将更改推送到github git push origin master

现在切换到只读副本并检查git status显示:

renniej@RATHAUS /d/Dev/GIT/Test-ro (master)
$ git status
# On branch master
nothing to commit, working directory clean
Run Code Online (Sandbox Code Playgroud)

使用git pull origin master我更新它(这是对单个文件README.md的更改):

renniej@RATHAUS /d/Dev/GIT/Test-ro (master)
$ git pull origin master
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0)
Unpacking objects: 100% (3/3), done.
From git://github.com/jhsrennie/Test
 * branch            master     -> FETCH_HEAD
Updating 76b02d1..5b03266
Fast-forward
 README.md | 2 ++
 1 file changed, 2 insertions(+)
Run Code Online (Sandbox Code Playgroud)

git status现在给:

renniej@RATHAUS /d/Dev/GIT/Test-ro (master)
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
Run Code Online (Sandbox Code Playgroud)

我能想到的唯一解释是,只读副本无法识别更新来自最初克隆的相同存储库,但至少可以说这似乎很奇怪.

Abe*_*ker 5

发生的事情是远程跟踪分支origin/master没有更新.如果您阅读git-pull手册页,则会记录git pull使用参数调用when时的行为:

A parameter <ref> without a colon is equivalent to <ref>: when pulling/fetching, so it merges <ref> into the current branch without storing the remote branch anywhere locally

简单的解决方法是确保master设置跟踪origin/master,然后调用git pull不带任何参数,其更新远程跟踪分支为您服务.

如果您已经运行git pull origin master并需要更新origin/masterref,请使用git fetchgit remote update.