【问题标题】:Git: How to push only tracked files to remoteGit:如何仅将跟踪的文件推送到远程
【发布时间】:2012-09-14 10:33:41
【问题描述】:

我在 SO 上看到了这个问题的许多变体,但没有一个有我想要的。假设我有一个 repo ~/MyRepo/:

$ ls -a ~/MyRepo
code.r junk.txt .git

MyRepo 中,仅跟踪文件code.r,不跟踪junk.txt。 假设我在~/Dropbox/MyShare/ 有一个遥控器(例如在 Dropbox 上),朋友可以访问(仅用于阅读)。目的是让这个遥控器只包含我的最新提交版本code.r,并且包含junk.txt。我正在尝试实现以下目标:每当我提交 code.r 时,我希望能够使用~/MyRepo/code.r 的提交版本更新(如果可能,自动更新)远程~/Dropbox/MyShare/code.r

我知道.gitignore,但对这条路线不感兴趣,因为我有几个我想忽略的未跟踪文件,我只想将我正在跟踪的文件“推送”到远程。 我还尝试了将cloning 放入~/Dropbox/MyShare/ 的方法,但克隆或拉取似乎总是包含已跟踪未跟踪 文件,从而污染了我的遥控器。

我当前的解决方案是在~/MyRepo/.git/hooks/ 下有一个提交后挂钩,该挂钩具有明确的命令,可以将所有“我关心的文件”单独复制到远程。我不喜欢这个解决方案,因为“我关心的文件”可以改变,而且我不必去更新 post-commit 钩子。

我希望有一种方法可以通过 git 命令的某种组合自动使最后提交的版本在 ~/Dropbox/MyShare/ 中可用。我不介意每次在~/MyRepo/ 中提交时都执行一个手动“推送”命令。

这是我根据以下答案之一尝试过的,但我仍然很难过。

# create my repo
mkdir foo
cd foo
git init
touch fileA fileB fileC
git add fileA
git commit -m 'new'

# now create remote 
mkdir ../foo_remote
cd ../foo_remote
git init
cd ../foo
git remote add foo_remote ../foo_remote

bash-3.2$ git remote -v                                                                                                                                                                                                                                                         
foo_remote      ../foo_remote (fetch)                                                                                                                                                                                                                                           
foo_remote      ../foo_remote (push)                                                                                                                                                                                                                                            

现在,当我尝试推送时,我收到了这条卑鄙的错误消息:

bash-3.2$ git push foo_remote master                                                                                                                                                                                                                                            

Counting objects: 3, done.                                                                                                                                                                                                                                                      
Writing objects: 100% (3/3), 207 bytes, done.                                                                                                                                                                                                                                   
Total 3 (delta 0), reused 0 (delta 0)                                                                                                                                                                                                                                           
Unpacking objects: 100% (3/3), done.                                                                                                                                                                                                                                            
remote: error: refusing to update checked out branch: refs/heads/master                                                                                                                                                                                                         
remote: error: By default, updating the current branch in a non-bare repository                                                                                                                                                                                                 
remote: error: is denied, because it will make the index and work tree inconsistent                                                                                                                                                                                             
remote: error: with what you pushed, and will require 'git reset --hard' to match                                                                                                                                                                                               
remote: error: the work tree to HEAD.                                                                                                                                                                                                                                           
remote: error:                                                                                                                                                                                                                                                                  
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to                                                                                                                                                                                                
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into                                                                                                                                                                                                
remote: error: its current branch; however, this is not recommended unless you                                                                                                                                                                                                  
remote: error: arranged to update its work tree to match what you pushed in some                                                                                                                                                                                                
remote: error: other way.                                                                                                                                                                                                                                                       
remote: error:                                                                                                                                                                                                                                                                  
remote: error: To squelch this message and still keep the default behaviour, set                                                                                                                                                                                                
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.                                                                                                                                                                                                  
To ../foo_remote                                                                                                                                                                                                                                                                
 ! [remote rejected] master -> master (branch is currently checked out)                                                                                                                                                                                                         
error: failed to push some refs to '../foo_remote'           

任何想法我哪里出错了?

【问题讨论】:

    标签: git push versioning dropbox git-remote


    【解决方案1】:

    如果您不添加文件,为什么要将其推送到远程 - 简单的答案不要将文件添加到您不想推送的本地存储库。

    要么将文件放入存储库中,要么不放入 - DVCS 的重点是在任何地方都有相同的文件。

    我会再次查看 .gitignore 它的工作原理 - 如果您不想共享它,您可以将其保留在本地并且不将其包含在 repo 中 - 这是此文件的唯一目的以及您不想共享的事实使用它似乎很奇怪。有点像说我想编译 C 文件但我不想使用编译器

    mkdir foo
    cd foo
    git init
    touch fileA fileB fileC fileD
    git add fileA
    git commit -m "New file"
    git push
    

    只有文件 A 会被推送

    【讨论】:

    • 也许我遗漏了一些基本的东西——我想不出一种方法来创建一个只包含我的跟踪文件的远程克隆——我尝试过的每一种方法也复制了未跟踪的文件。如果有一种方法可以创建一个只有我跟踪的文件的遥控器,那我就完成了,因为就像你说的那样,推送只会发送提交的文件。
    • 对 - 不要这样做 git add . 因为这会添加所有未跟踪的文件 - 添加你想要的文件
    • 创建远程时使用git --bare init
    • 反过来做 - 从假定的遥控器 - 做一个 git pull 并让你的 repo 成为遥控器
    • 虽然严格来说没有“正确答案”,但我会接受你的,因为你“更努力地工作”:)
    【解决方案2】:

    这并不能直接回答您的问题,但评论也有点长,我还是想分享它。它可能会帮助一些人摆脱我所感受到的痛苦。

    所以我的经验建议是:

    不要混合使用 Dropbox 和 github 存储库。

    我为此被烧了几次,几乎每个生成的文件都有冲突。

    我现在总是将我的 github(即 rails)项目保存在不同的文件夹中。

    我创建~/Dropnot 作为这个不同文件夹的名称。
    与往常一样,我在 .bash_aliases 文件中创建了一个别名,在本例中为 alias not='cd ~/Dropnot'

    但是,当我设置一台新机器时,我经常使用 Dropbox/xfer 文件夹快速进行一次设置以传输我所有的 Dropnot 文件(为此,我将它们复制到另一台机器上)。
    接下来我会使用 fetch/pull/push 来更改该目录下的各种 repos。

    【讨论】:

    • 谢谢迈克尔——(请参阅下面我的最后一条评论)——不知何故,我的主仓库似乎进入了克隆它复制所有文件的状态,包括未跟踪的文件。我不知道这是否与它是否在 Dropbox 下有关,但是当我对 .git 目录进行核对并仅对我关心的文件执行“git add”,然后克隆它时,它现在仅正确复制跟踪的文件。
    • 当然。请记住,如果您通过拉取使用 github 进行了更改,然后转到另一台拥有您的保管箱的机器,它会将它们视为更改的文件并创建新文件。这对我来说不断导致冲突,但只有在您使用多台机器时才能看到。
    猜你喜欢
    • 2011-01-09
    • 2014-02-04
    • 2016-02-22
    • 2018-02-12
    • 1970-01-01
    • 2011-12-23
    • 2017-01-16
    • 2011-02-15
    相关资源
    最近更新 更多