如何在 Git 中将某些文件推送到 origin/master?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25689214/
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-09 02:25:59  来源:igfitidea点击:

How can I push certain files to origin/master in Git?

gitpush

提问by Mike.R

I am little bit new to Git. I would like to push some files to my Git origin (remote).

我对 Git 有点陌生。我想将一些文件推送到我的 Git 源(远程)。

What I did:

我做了什么:

I had my master, and I created a branch to do some job. After that I merged my branch to my master. During my work, a lot of binary files and project files were changed/added locally. I would like onlyto add .javafiles which changed to remote server.

我有我的主人,我创建了一个分支来做一些工作。之后,我将我的分支合并到我的主人。在我的工作中,很多二进制文件和项目文件在本地被更改/添加。我只想添加更改为远程服务器的.java文件。

(I believe that I experimented with commitswhen I worked on my branch, just to check how it work.)

(我相信我在我的分支上工作时尝试了提交,只是为了检查它是如何工作的。)

My master is up to datewith my origin (that is what I get when I do git pull. Also I did git fetch origin. I always received (when I ran git status):

我的主人知道我的出身是最新的(这就是我做的时候得到的git pull。我也这样做了git fetch origin。我总是收到(当我跑时git status):

On branch master Your branch is ahead of origin/master by 12 commits.
  (use "git push" to publish your local commits)

nothing to commit, working directory clean`

I tried to add, committhose files, but running git statuswasn't changed. I tried to do add, commiton the new branch:

我试图添加、提交这些文件,但运行git status没有改变。我尝试在新分支上添加、提交

On branch NewBranch nothing to commit, working directory clean

I tried to reset Head. I didn't find a solution for my problem in the Git tutorial or on Stack Overflow.

我试图重置 Head。我没有在 Git 教程或 Stack Overflow 上找到解决我的问题的方法。

Of course I can push all files to remote origin, but I don't think it's a good solution.

当然我可以将所有文件推送到远程源,但我认为这不是一个好的解决方案。

Some duplicate questions that I found: How to push a single file, how to push changes made to only certain files?, and How to commit only some files?.

我发现了一些重复的问题: 如何推送单个文件如何仅推送对某些文件所做的更改?, 以及如何只提交一些文件?.

采纳答案by Mike.R

I solved my problem: (I was confused with git status response because it wasn't changed when I tried to add/commit files which were already there).Thanks to linuxdan and DaveZych and Temich.

我解决了我的问题:(我对 git status 响应感到困惑,因为当我尝试添加/提交已经存在的文件时它没有改变)。感谢 linuxdan、DaveZych 和 Temich。

git checkout -b NewBranch

After that I deleted all unnecessary files.

之后我删除了所有不必要的文件。

git rm --cache build/web/WEB-INF/classes/doggizz/utils/\*.class

Move back to master

回到主人

git checkout master

(only disadvantage that those files were deleted when I moved back to master so I copied manually project file , I tried to stash before checkoutbut it didn't helped)

(唯一的缺点是当我移回 master 时这些文件被删除,所以我手动复制了项目文件,我尝试在结帐隐藏,但没有帮助)

git merge NewBranch
git push

回答by linuxdan

  1. Create a new branch from master
    1. git checkout master
    2. git checkout -b new_branch
  2. Checkout just the file you want from your old branch
    1. git checkout old_branch path/to/some/file
    2. repeat as necessary for additional files
  3. Commit the files to your new branch
    1. git commit -a
  4. Push new branch to your origin
    1. git push origin master
  1. 从 master 创建一个新分支
    1. git checkout master
    2. git checkout -b new_branch
  2. 只从旧分支签出您想要的文件
    1. git checkout old_branch path/to/some/file
    2. 根据需要对其他文件重复
  3. 将文件提交到您的新分支
    1. git commit -a
  4. 将新分支推送到您的原点
    1. git push origin master