简体   繁体   English

如何从一个分支复制到另一个分支 - 文件要掌握?

[英]How to copy from one branch to another - Files going to master?

I am trying to add some files from another branch, but they are being put onto master.我正在尝试从另一个分支添加一些文件,但它们被放到 master 上。

How can I copy files from one branch to another?如何将文件从一个分支复制到另一个分支?

I tried a good ole' fashioned cp : switch to source branch, copy to /tmp, switch to the target branch, and copy.我尝试了一个很好的 ole' 风格的cp :切换到源分支,复制到 /tmp,切换到目标分支,然后复制。 Tried to add... but the copied files are on master ?!试图添加...但复制的文件在master文件上?!

cd cheese_repo
git branch cheddar
ls
    react
cp -r react /tmp

git branch swiss
cp -r /tmp/react .
ls
    react
git branch
    * swiss
cd react
git branch
    * master

Why are these files now in master?为什么这些文件现在在 master 中? I was in the swiss branch when I copied them.当我复制它们时,我在swiss分公司。

This got me into some warm water... I did a commit and push without realizing that I was on master .这让我陷入了一些温水……我做了一次commitpush却没有意识到我在master Committing directly to master is no-no where I work.在我工作的地方,直接提交给masterno-no

How can I get my react files into the swiss branch for commit and push?如何将我的react文件放入swiss分支以进行提交和推送? Tyvm Keith :^) Tyvm 基思 :^)

The way to bring over a file as is from another branch is with checkout:从另一个分支按原样带入文件的方法是结帐:

git checkout other-branch -- some-file

That will put the file as it in on the other branch on the working tree and the index.这会将文件放在工作树索引的另一个分支上。

git branch
    * swiss
cd react
git branch
    * master

It seems react/ is a submodule, ie it has its own directory .git/ , its own independent history (commits), its own branches.似乎react/是一个子模块,即它有自己的目录.git/ ,自己的独立历史(提交),自己的分支。

From your below output, its seems that react folder is a repository in itself.从您的以下输出中,似乎 react 文件夹本身就是一个存储库。

git branch
* swiss
cd react
    git branch
    * master

You can check it by doing git remote -v from both folders ie cheese_repo and react.您可以通过从两个文件夹中执行git remote -v来检查它,即 cheese_repo 并做出反应。 It will give you the origin repo links.它将为您提供原始回购链接。

From your question, you want to have your folder react in the swiss branch of your cheese_repo.从你的问题来看,你想让你的文件夹在你的 cheese_repo 的 swiss 分支中做出反应。 You can do add and push, once you are in swiss branch.一旦你在瑞士分公司,你就可以添加和推送。

git branch
* swiss
git add react
git commit -m '<your commit msg>'
git push origin swiss

So, for posterity, git thought that the directory was part of a submodule.因此,对于后代,git 认为该目录是子模块的一部分。

I found this by cd 'ing into the directory and doing a git add on a specific file.我通过cd进入目录并对特定文件执行git add发现了这一点。 It threw this error:它抛出了这个错误:

fatal: Path 'web/react/package.json is in submodule 'web/react'

The solution was this:解决方案是这样的:

  1. rm -rf .git folder in directory rm -rf .git目录.git文件夹
  2. run git rm --cached path_to_submodule (no trailing slash)运行git rm --cached path_to_submodule (没有尾部斜杠)

Then I was able to:然后我能够:

  1. git add --all react
  2. git status
  3. git commit -m "My awesome UI"
  4. git push

Voila!瞧! Files in Bitbucket on the correct branch. Bitbucket 中正确分支上的文件。

Reference:参考:

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

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