简体   繁体   English

Git:如何将仅跟踪的文件推到远程

[英]Git: How to push only tracked files to remote

I've seen many variants of this question on SO but none of them had what I looking for. 我在SO上已经看到了这个问题的许多变体,但没有一个具有我想要的东西。 Say I have a repo ~/MyRepo/ : 说我有一个~/MyRepo/

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

In MyRepo only the file code.r is being tracked, and junk.txt is not tracked. MyRepo仅跟踪文件code.r ,而不跟踪junk.txt And say I have a remote (eg on Dropbox) at ~/Dropbox/MyShare/ that a friend has access to (only for reading). 并说我在~/Dropbox/MyShare/上有一个远程(例如,在Dropbox上),朋友可以访问(仅用于读取)。 The intent is for this remote to only contain my latest committed version of code.r , and not contain junk.txt . 目的是使此远程服务器仅包含我最新提交code.r 版本而不包含junk.txt I'm trying to achieve the following: whenever I commit code.r I want to be able to update (automatically if possible) the remote ~/Dropbox/MyShare/code.r with the committed version of ~/MyRepo/code.r . 我正在尝试实现以下目标:每当我提交 code.r我都希望能够使用提交的~/MyRepo/code.r版本(可能的话自动)更新远程~/Dropbox/MyShare/code.r ~/MyRepo/code.r

I am aware of .gitignore but not interested in that route because I have several untracked files that I want to ignore, and I only want to "push" the files I'm tracking to the remote. 我知道.gitignore但对该路由不感兴趣,因为我有几个我想忽略的未跟踪文件,而我只想将要跟踪的文件“推送”到远程。 I also tried the approach of doing a cloning into ~/Dropbox/MyShare/ but cloning or pulling always seem to include tracked and untracked files, thus polluting my Remote. 我还尝试了将cloning~/Dropbox/MyShare/但是克隆或拉动似乎总是包含跟踪未跟踪的文件,从而污染了Remote。

My current solution is to have a post-commit hook under ~/MyRepo/.git/hooks/ that has explicit commands to individually copy all the "files I care about" to the remote. 我当前的解决方案是在~/MyRepo/.git/hooks/下有一个提交后的~/MyRepo/.git/hooks/ ,该~/MyRepo/.git/hooks/具有显式命令,可以将所有“我关心的文件”分别复制到远程。 I don't like this solution because the "files I care about" can change, and I don't have to go and update the post-commit hook. 我不喜欢这种解决方案,因为“我关心的文件”可以更改,并且我不必去更新提交后的钩子。

I am hoping there is a way to automatically make the last-committed versions available in ~/Dropbox/MyShare/ by some combination of git commands. 我希望有一种方法可以通过git命令的一些组合自动在~/Dropbox/MyShare/提供最后提交的版本。 I don't mind doing a single manual "push" command every time I commit in ~/MyRepo/ . 我不介意每次在~/MyRepo/提交时都执行单个手动“ push”命令。

Here is what I tried based on one of the answers below, but I am still stumped. 这是我根据以下答案之一尝试的方法,但仍然感到困惑。

# 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)                                                                                                                                                                                                                                            

Now when I try to push, I get this mean error message: 现在,当我尝试推送时,我得到以下平均错误消息:

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'           

Any ideas where I'm going wrong? 有什么想法我要去哪里吗?

If you do not add the file why would it be pushed to the remote - simple answer do not add files to you local repo you do not want pushed. 如果您不添加文件,为什么要将其推送到远程-简单的答案就是不要将文件添加到您不想推送的本地仓库中。

Either put the files in the repo or do not - the point of a DVCS is to have the same files everywhere. 要么将文件放到存储库中,要么不放-DVCS的重点是到处都有相同的文件。

I would look again at .gitignore it works - you can keep it locally and not include it in the repo if you do not want to share it - this is the sole purpose of this file and the fact you do not want to use it seems odd. 我会再看一次.gitignore它的工作原理-您可以将其保存在本地,如果您不想共享它,则可以不将其包含在回购中-这是该文件的唯一目的,并且您似乎不想使用它奇。 Bit like saying I want to compile C files but I do not want to use a compiler 有点像说我要编译C文件,但我不想使用编译器

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

Only fileA will get pushed 仅fileA将被推送

This doesn't directly answer your question but it's also a bit long for a comment and I wanted to share it anyway. 这并不能直接回答您的问题,但是要发表评论还很长,我还是想分享一下。 It may help some from going through the pain I felt. 这可能有助于减轻我的痛苦。

So my advice from experience is: 所以我的经验建议是:

Don't mix Dropbox and github repositories. 不要混合Dropbox和github仓库。

I got burned a couple of times with this with conflicts for nearly every file getting generated. 对于几乎每个生成的文件,我都因为冲突而被烧毁了两次。

I now always keep my github (ie rails) projects in a different folder. 现在,我始终将github(即rails)项目保留在其他文件夹中。

I create ~/Dropnot as the name for this different folder. 我创建~/Dropnot作为此不同文件夹的名称。
As always I create an alias in my .bash_aliases file, in this case alias not='cd ~/Dropnot' 和往常一样,我在.bash_aliases文件中创建一个别名,在这种情况下, alias not='cd ~/Dropnot'

When I am setting up a new machine however, I often use a Dropbox/xfer folder to quickly do a 1 time setup to transfer all my Dropnot files (I copy them there on another machine just for that). 但是,当我设置一台新计算机时,我经常使用Dropbox / xfer文件夹快速进行一次设置,以传输所有我的Dropnot文件(为此我将它们复制到另一台计算机上)。
Going forward I then use fetch/pull/push for changes to the various repos under that directory. 接下来,我将使用fetch / pull / push更改该目录下的各种存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM