git 如何将子目录中的单个文件推送到 Github(不是 master)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13479763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:56:49  来源:igfitidea点击:

How to push a single file in a subdirectory to Github (not master)

gitgithubgit-branchgit-push

提问by moon star

I have changed a single file in a subdirectory of my repository and I want to push just that file to Github.

我更改了存储库子目录中的单个文件,我只想将该文件推送到 Github。

I've made a small change to one file, and I don't want to re-upload the entire repository.

我对一个文件做了一个小改动,我不想重新上传整个存储库。

It seems like all of the instructions that I've seen so far require me to merge locally with the master and then push my local master to the remote origin.

到目前为止,我看到的所有指令似乎都要求我在本地与 master 合并,然后将我的本地 master 推送到远程源。

How can I push just that one file?

我怎样才能只推送那个文件?

回答by krilovich

When you do a push, git only takes the changes that you have committed.

当您执行推送时,git 只会接受您已提交的更改。

Remember when you do a git statusit shows you the files you changed since the last push?

还记得当你做一个时,git status它会向你显示自上次推送以来你更改的文件吗?

Once you commit those changes and do a push they are the only files that get pushed so you don't have to worry about thinking that the entire master gets pushed because in reality it does not.

一旦您提交了这些更改并进行了推送,它们就是唯一被推送的文件,因此您不必担心认为整个 master 都会被推送,因为实际上并没有。

How to push a single file:

如何推送单个文件:

git commit yourfile.js
git status
git push origin master

回答by Salman Srabon

Very simple. Just follow these procedure:
1. git status
2. git add {File_Name} //the file name you haven been changed
3. git status
4. git commit -m '{your_message}'
5. git push origin master

很简单。只需按照以下步骤操作:
1. git status
2. git add {File_Name} //您更改过的文件名
3. git status
4. git commit -m '{your_message}'
5. git push origin master

回答by Adam Adamaszek

Let me start by saying that the way git works is you are not pushing/fetching files; well, at least not directly.

首先让我说 git 的工作方式是你不是推送/获取文件;好吧,至少不是直接的。

You are pushing/fetching refs, that point to commits. Then a commit in git is a reference to a tree of objects (where files are represented as objects, among other objects).

您正在推送/获取 refs,指向提交。然后 git 中的提交是对对象树的引用(其中文件表示为对象,以及其他对象)。

So, when you are pushing a commit, what git does it pushes a set of references like in this picture:

因此,当您推送提交时,git 执行的操作会推送一组引用,如下图所示:

git commits

git 提交

If you didn't push your master branch yet, the whole history of the branch will get pushed.

如果你还没有推送你的主分支,分支的整个历史将被推送。

So, in your example, when you commit and push your file, the whole master branch will be pushed, if it was not pushed before.

因此,在您的示例中,当您提交并推送文件时,如果之前没有推送过,则整个主分支都将被推送。

To do what you asked for, you need to create a clean branch with no history, like in this answer.

要执行您的要求,您需要创建一个没有历史记录的干净分支,就像在这个答案中一样

回答by Michael Krelin - hacker

If you commit one file and push your revision, it will not transfer the whole repository, it will push changes.

如果您提交一个文件并推送您的修订,它不会传输整个存储库,而是推送更改。

回答by Andrew T Finnell

It will only push the new commits. It won't push the whole "master" branch. That is part of the benefit of working with a Distributed Version Control System. Git figures out what is actually needed and only pushes those pieces. If the branch you are on has been changed and pushed by someone else you'll need to pull first. Then push your commits.

它只会推送新的提交。它不会推动整个“主”分支。这是使用分布式版本控制系统的好处的一部分。Git 弄清楚实际需要什么,然后只推送这些部分。如果您所在的分支已被其他人更改并推送,则您需要先拉取。然后推送你的提交。

回答by itro

Push only single file

只推送单个文件

git commit -m "Message goes here" filename

Push only two files.

只推送两个文件。

git commit -m "Message goes here" file1 file2